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.

Commit4529aef

Browse files
committed
refactor(utils): re-org based on usage for string
1 parentc59255b commit4529aef

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

‎lib/helper/utils/map.ex‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ defmodule Helper.Utils.Map do
22
@moduledoc"""
33
unitil functions
44
"""
5-
importEcto.Query,warn:false
6-
importHelper.ErrorHandler
7-
importHelper.ErrorCode
8-
9-
importHelper.Validator.Guards,only:[g_none_empty_str:1]
10-
11-
aliasHelper.Cache
12-
135
defmap_key_stringify(%{__struct__:_}=map)whenis_map(map)do
146
map=Map.from_struct(map)
157
map|>Enum.reduce(%{},fn{key,val},acc->Map.put(acc,to_string(key),val)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/utils.ex‎

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Helper.Utils do
1010

1111
aliasHelper.{Cache,Utils}
1212

13+
# Map utils
1314
defdelegatemap_key_stringify(map),to:Utils.Map
1415
defdelegatekeys_to_atoms(map),to:Utils.Map
1516
defdelegatekeys_to_strings(map),to:Utils.Map
@@ -19,6 +20,11 @@ defmodule Helper.Utils do
1920
defdelegatedeep_merge(left,right),to:Utils.Map
2021
defdelegatemap_atom_value(attrs,opt),to:Utils.Map
2122

23+
# String Utils
24+
defdelegatestringfy(str),to:Utils.String
25+
defdelegatecount_words(str),to:Utils.String
26+
defdelegatestr_occurence(string,substr),to:Utils.String
27+
2228
defget_config(section,key,app\\:groupher_server)
2329

2430
defget_config(section,:all,app)do
@@ -99,11 +105,6 @@ defmodule Helper.Utils do
99105
defintegerfy(id)whenis_binary(id),do:String.to_integer(id)
100106
defintegerfy(id),do:id
101107

102-
defstringfy(v)whenis_binary(v),do:v
103-
defstringfy(v)whenis_integer(v),do:to_string(v)
104-
defstringfy(v)whenis_atom(v),do:to_string(v)
105-
defstringfy(v),do:v
106-
107108
# TODO: enhance, doc
108109
defrepeat(times,[x])whenis_integer(x),do:to_string(for_<-1..times,do:x)
109110
defrepeat(times,x),do:for(_<-1..times,do:x)
@@ -122,26 +123,6 @@ defmodule Helper.Utils do
122123
%{entries:[],total_count:0,page_size:0,total_pages:1,page_number:1}
123124
end
124125

125-
@doc"""
126-
["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
127-
"""
128-
defcount_words(words)whenis_list(words)do
129-
Enum.reduce(words,%{},&update_word_count/2)
130-
end
131-
132-
defpupdate_word_count(word,acc)do
133-
Map.update(acc,to_string(word),1,&(&1+1))
134-
end
135-
136-
# see https://stackoverflow.com/a/49558074/4050784
137-
@specstr_occurence(String.t(),String.t())::Integer.t()
138-
defstr_occurence(string,substr)whenis_binary(string)andis_binary(substr)do
139-
len=string|>String.split(substr)|>length()
140-
len-1
141-
end
142-
143-
defstr_occurence(_,_),do:"must be strings"
144-
145126
@speclarge_than(String.t()|Integer.t(),Integer.t())::true|false
146127
deflarge_than(value,target)whenis_binary(value)andis_integer(target)do
147128
String.length(value)>=target

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp