Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitf303cb5

Browse files
authored
refactor(utils): re-org based on usage (#310)
* refactor(utils): re-org based on usage* refactor(utils): re-org based on usage for string
1 parent416d2ed commitf303cb5

File tree

3 files changed

+187
-165
lines changed

3 files changed

+187
-165
lines changed

‎lib/helper/utils/map.ex‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
defmoduleHelper.Utils.Mapdo
2+
@moduledoc"""
3+
unitil functions
4+
"""
5+
defmap_key_stringify(%{__struct__:_}=map)whenis_map(map)do
6+
map=Map.from_struct(map)
7+
map|>Enum.reduce(%{},fn{key,val},acc->Map.put(acc,to_string(key),val)end)
8+
end
9+
10+
defmap_key_stringify(map)whenis_map(map)do
11+
map|>Enum.reduce(%{},fn{key,val},acc->Map.put(acc,to_string(key),val)end)
12+
end
13+
14+
@doc"""
15+
see https://stackoverflow.com/a/61559842/4050784
16+
adjust it for map keys from atom to string
17+
"""
18+
defkeys_to_atoms(json)whenis_map(json)do
19+
Map.new(json,&reduce_keys_to_atoms/1)
20+
end
21+
22+
defkeys_to_atoms(string)whenis_binary(string),do:string
23+
24+
defpreduce_keys_to_atoms({key,val})whenis_map(val),
25+
# do: {String.to_existing_atom(key), keys_to_atoms(val)}
26+
do:{String.to_atom(key),keys_to_atoms(val)}
27+
28+
defpreduce_keys_to_atoms({key,val})whenis_list(val),
29+
do:{String.to_atom(key),Enum.map(val,&keys_to_atoms(&1))}
30+
31+
defpreduce_keys_to_atoms({key,val}),do:{String.to_atom(key),val}
32+
33+
@doc"""
34+
see https://stackoverflow.com/a/61559842/4050784
35+
adjust it for map keys from atom to string
36+
"""
37+
@speckeys_to_strings(map)::map
38+
defkeys_to_strings(json)whenis_map(json)do
39+
Map.new(json,&reduce_keys_to_strings/1)
40+
end
41+
42+
defpreduce_keys_to_strings({key,val})whenis_map(val),
43+
do:{Atom.to_string(key),keys_to_strings(val)}
44+
45+
defpreduce_keys_to_strings({key,val})whenis_list(val),
46+
do:{Atom.to_string(key),Enum.map(val,&keys_to_strings(&1))}
47+
48+
defpreduce_keys_to_strings({key,val}),do:{Atom.to_string(key),val}
49+
50+
@doc"""
51+
Recursivly camelize the map keys
52+
usage: convert factory attrs to used for simu Graphql parmas
53+
"""
54+
defcamelize_map_key(map,v_trans\\:ignore)do
55+
map_list=
56+
Enum.map(map,fn{k,v}->
57+
v=
58+
conddo
59+
is_datetime?(v)->
60+
DateTime.to_iso8601(v)
61+
62+
is_map(v)->
63+
camelize_map_key(safe_map(v))
64+
65+
is_binary(v)->
66+
handle_camelize_value_trans(v,v_trans)
67+
68+
true->
69+
v
70+
end
71+
72+
map_to_camel({k,v})
73+
end)
74+
75+
Enum.into(map_list,%{})
76+
end
77+
78+
defphandle_camelize_value_trans(v,:ignore),do:v
79+
defphandle_camelize_value_trans(v,:downcase),do:String.downcase(v)
80+
defphandle_camelize_value_trans(v,:upcase),do:String.upcase(v)
81+
82+
defpsafe_map(%{__struct__:_}=map),do:Map.from_struct(map)
83+
defpsafe_map(map),do:map
84+
85+
defpmap_to_camel({k,v}),do:{Recase.to_camel(to_string(k)),v}
86+
87+
@specsnake_map_key(map)::map
88+
defsnake_map_key(map)do
89+
map_list=
90+
Enum.map(map,fn{k,v}->
91+
v=
92+
conddo
93+
is_datetime?(v)->
94+
DateTime.to_iso8601(v)
95+
96+
is_map(v)->
97+
snake_map_key(safe_map(v))
98+
99+
true->
100+
v
101+
end
102+
103+
{Recase.to_snake(to_string(k)),v}
104+
end)
105+
106+
Enum.into(map_list,%{})
107+
end
108+
109+
defpis_datetime?(%DateTime{}),do:true
110+
defpis_datetime?(_),do:false
111+
112+
defmap_atom_value(attrs,:string)do
113+
results=
114+
Enum.map(attrs,fn{k,v}->
115+
conddo
116+
v==trueorv==false->
117+
{k,v}
118+
119+
is_atom(v)->
120+
{k,v|>to_string()|>String.downcase()}
121+
122+
true->
123+
{k,v}
124+
end
125+
end)
126+
127+
results|>Enum.into(%{})
128+
end
129+
130+
defdeep_merge(left,right),do:Map.merge(left,right,&deep_resolve/3)
131+
132+
# Key exists in both maps, and both values are maps as well.
133+
# These can be merged recursively.
134+
# defp deep_resolve(_key, left = %{},right = %{}) do
135+
defpdeep_resolve(_key,%{}=left,%{}=right),do:deep_merge(left,right)
136+
137+
# Key exists in both maps, but at least one of the values is
138+
# NOT a map. We fall back to standard merge behavior, preferring
139+
# the value on the right.
140+
defpdeep_resolve(_key,_left,right),do:right
141+
end

‎lib/helper/utils/string.ex‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmoduleHelper.Utils.Stringdo
2+
@moduledoc"""
3+
string utils
4+
"""
5+
6+
defstringfy(v)whenis_binary(v),do:v
7+
defstringfy(v)whenis_integer(v),do:to_string(v)
8+
defstringfy(v)whenis_atom(v),do:to_string(v)
9+
defstringfy(v),do:v
10+
11+
# see https://stackoverflow.com/a/49558074/4050784
12+
@specstr_occurence(String.t(),String.t())::Integer.t()
13+
defstr_occurence(string,substr)whenis_binary(string)andis_binary(substr)do
14+
len=string|>String.split(substr)|>length()
15+
len-1
16+
end
17+
18+
defstr_occurence(_,_),do:"must be strings"
19+
20+
@doc"""
21+
["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
22+
"""
23+
defcount_words(words)whenis_list(words)do
24+
Enum.reduce(words,%{},&update_word_count/2)
25+
end
26+
27+
defpupdate_word_count(word,acc)do
28+
Map.update(acc,to_string(word),1,&(&1+1))
29+
end
30+
end

‎lib/helper/utils.ex‎renamed to ‎lib/helper/utils/utils.ex‎

Lines changed: 16 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ defmodule Helper.Utils do
88

99
importHelper.Validator.Guards,only:[g_none_empty_str:1]
1010

11-
aliasHelper.Cache
11+
aliasHelper.{Cache,Utils}
12+
13+
# Map utils
14+
defdelegatemap_key_stringify(map),to:Utils.Map
15+
defdelegatekeys_to_atoms(map),to:Utils.Map
16+
defdelegatekeys_to_strings(map),to:Utils.Map
17+
defdelegatecamelize_map_key(map),to:Utils.Map
18+
defdelegatecamelize_map_key(map,opt),to:Utils.Map
19+
defdelegatesnake_map_key(map),to:Utils.Map
20+
defdelegatedeep_merge(left,right),to:Utils.Map
21+
defdelegatemap_atom_value(attrs,opt),to:Utils.Map
22+
23+
# String Utils
24+
defdelegatestringfy(str),to:Utils.String
25+
defdelegatecount_words(str),to:Utils.String
26+
defdelegatestr_occurence(string,substr),to:Utils.String
1227

1328
defget_config(section,key,app\\:groupher_server)
1429

@@ -87,125 +102,9 @@ defmodule Helper.Utils do
87102
|>Absinthe.Resolution.put_result({:error,message:err_msg,code:ecode()})
88103
end
89104

90-
defmap_key_stringify(%{__struct__:_}=map)whenis_map(map)do
91-
map=Map.from_struct(map)
92-
map|>Enum.reduce(%{},fn{key,val},acc->Map.put(acc,to_string(key),val)end)
93-
end
94-
95-
defmap_key_stringify(map)whenis_map(map)do
96-
map|>Enum.reduce(%{},fn{key,val},acc->Map.put(acc,to_string(key),val)end)
97-
end
98-
99-
@doc"""
100-
see https://stackoverflow.com/a/61559842/4050784
101-
adjust it for map keys from atom to string
102-
"""
103-
defkeys_to_atoms(json)whenis_map(json)do
104-
Map.new(json,&reduce_keys_to_atoms/1)
105-
end
106-
107-
defkeys_to_atoms(string)whenis_binary(string),do:string
108-
109-
defreduce_keys_to_atoms({key,val})whenis_map(val),
110-
# do: {String.to_existing_atom(key), keys_to_atoms(val)}
111-
do:{String.to_atom(key),keys_to_atoms(val)}
112-
113-
defreduce_keys_to_atoms({key,val})whenis_list(val),
114-
do:{String.to_atom(key),Enum.map(val,&keys_to_atoms(&1))}
115-
116-
defreduce_keys_to_atoms({key,val}),do:{String.to_atom(key),val}
117-
118-
@doc"""
119-
see https://stackoverflow.com/a/61559842/4050784
120-
adjust it for map keys from atom to string
121-
"""
122-
@speckeys_to_strings(map)::map
123-
defkeys_to_strings(json)whenis_map(json)do
124-
Map.new(json,&reduce_keys_to_strings/1)
125-
end
126-
127-
defpreduce_keys_to_strings({key,val})whenis_map(val),
128-
do:{Atom.to_string(key),keys_to_strings(val)}
129-
130-
defpreduce_keys_to_strings({key,val})whenis_list(val),
131-
do:{Atom.to_string(key),Enum.map(val,&keys_to_strings(&1))}
132-
133-
defpreduce_keys_to_strings({key,val}),do:{Atom.to_string(key),val}
134-
135-
@doc"""
136-
Recursivly camelize the map keys
137-
usage: convert factory attrs to used for simu Graphql parmas
138-
"""
139-
defcamelize_map_key(map,v_trans\\:ignore)do
140-
map_list=
141-
Enum.map(map,fn{k,v}->
142-
v=
143-
conddo
144-
is_datetime?(v)->
145-
DateTime.to_iso8601(v)
146-
147-
is_map(v)->
148-
camelize_map_key(safe_map(v))
149-
150-
is_binary(v)->
151-
handle_camelize_value_trans(v,v_trans)
152-
153-
true->
154-
v
155-
end
156-
157-
map_to_camel({k,v})
158-
end)
159-
160-
Enum.into(map_list,%{})
161-
end
162-
163-
defphandle_camelize_value_trans(v,:ignore),do:v
164-
defphandle_camelize_value_trans(v,:downcase),do:String.downcase(v)
165-
defphandle_camelize_value_trans(v,:upcase),do:String.upcase(v)
166-
167-
defpsafe_map(%{__struct__:_}=map),do:Map.from_struct(map)
168-
defpsafe_map(map),do:map
169-
170-
defpmap_to_camel({k,v}),do:{Recase.to_camel(to_string(k)),v}
171-
172-
@specsnake_map_key(map)::map
173-
defsnake_map_key(map)do
174-
map_list=
175-
Enum.map(map,fn{k,v}->
176-
v=
177-
conddo
178-
is_datetime?(v)->
179-
DateTime.to_iso8601(v)
180-
181-
is_map(v)->
182-
snake_map_key(safe_map(v))
183-
184-
true->
185-
v
186-
end
187-
188-
{Recase.to_snake(to_string(k)),v}
189-
end)
190-
191-
Enum.into(map_list,%{})
192-
end
193-
194-
defis_datetime?(%DateTime{}),do:true
195-
defis_datetime?(_),do:false
196-
197-
defdeep_merge(left,right)do
198-
Map.merge(left,right,&deep_resolve/3)
199-
end
200-
201105
defintegerfy(id)whenis_binary(id),do:String.to_integer(id)
202106
defintegerfy(id),do:id
203107

204-
defstringfy(v)whenis_binary(v),do:v
205-
defstringfy(v)whenis_integer(v),do:to_string(v)
206-
defstringfy(v)whenis_atom(v),do:to_string(v)
207-
defstringfy(v),do:v
208-
209108
# TODO: enhance, doc
210109
defrepeat(times,[x])whenis_integer(x),do:to_string(for_<-1..times,do:x)
211110
defrepeat(times,x),do:for(_<-1..times,do:x)
@@ -220,58 +119,10 @@ defmodule Helper.Utils do
220119
end)
221120
end
222121

223-
defmap_atom_value(attrs,:string)do
224-
results=
225-
Enum.map(attrs,fn{k,v}->
226-
conddo
227-
v==trueorv==false->
228-
{k,v}
229-
230-
is_atom(v)->
231-
{k,v|>to_string()|>String.downcase()}
232-
233-
true->
234-
{k,v}
235-
end
236-
end)
237-
238-
results|>Enum.into(%{})
239-
end
240-
241122
defempty_pagi_datado
242123
%{entries:[],total_count:0,page_size:0,total_pages:1,page_number:1}
243124
end
244125

245-
# Key exists in both maps, and both values are maps as well.
246-
# These can be merged recursively.
247-
# defp deep_resolve(_key, left = %{},right = %{}) do
248-
defpdeep_resolve(_key,%{}=left,%{}=right),do:deep_merge(left,right)
249-
250-
# Key exists in both maps, but at least one of the values is
251-
# NOT a map. We fall back to standard merge behavior, preferring
252-
# the value on the right.
253-
defpdeep_resolve(_key,_left,right),do:right
254-
255-
@doc"""
256-
["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
257-
"""
258-
defcount_words(words)whenis_list(words)do
259-
Enum.reduce(words,%{},&update_word_count/2)
260-
end
261-
262-
defpupdate_word_count(word,acc)do
263-
Map.update(acc,to_string(word),1,&(&1+1))
264-
end
265-
266-
# see https://stackoverflow.com/a/49558074/4050784
267-
@specstr_occurence(String.t(),String.t())::Integer.t()
268-
defstr_occurence(string,substr)whenis_binary(string)andis_binary(substr)do
269-
len=string|>String.split(substr)|>length()
270-
len-1
271-
end
272-
273-
defstr_occurence(_,_),do:"must be strings"
274-
275126
@speclarge_than(String.t()|Integer.t(),Integer.t())::true|false
276127
deflarge_than(value,target)whenis_binary(value)andis_integer(target)do
277128
String.length(value)>=target

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp