@@ -10,6 +10,7 @@ defmodule Helper.Utils do
10
10
11
11
alias Helper . { Cache , Utils }
12
12
13
+ # Map utils
13
14
defdelegate map_key_stringify ( map ) , to: Utils.Map
14
15
defdelegate keys_to_atoms ( map ) , to: Utils.Map
15
16
defdelegate keys_to_strings ( map ) , to: Utils.Map
@@ -19,6 +20,11 @@ defmodule Helper.Utils do
19
20
defdelegate deep_merge ( left , right ) , to: Utils.Map
20
21
defdelegate map_atom_value ( attrs , opt ) , to: Utils.Map
21
22
23
+ # String Utils
24
+ defdelegate stringfy ( str ) , to: Utils.String
25
+ defdelegate count_words ( str ) , to: Utils.String
26
+ defdelegate str_occurence ( string , substr ) , to: Utils.String
27
+
22
28
def get_config ( section , key , app \\ :groupher_server )
23
29
24
30
def get_config ( section , :all , app ) do
@@ -99,11 +105,6 @@ defmodule Helper.Utils do
99
105
def integerfy ( id ) when is_binary ( id ) , do: String . to_integer ( id )
100
106
def integerfy ( id ) , do: id
101
107
102
- def stringfy ( v ) when is_binary ( v ) , do: v
103
- def stringfy ( v ) when is_integer ( v ) , do: to_string ( v )
104
- def stringfy ( v ) when is_atom ( v ) , do: to_string ( v )
105
- def stringfy ( v ) , do: v
106
-
107
108
# TODO: enhance, doc
108
109
def repeat ( times , [ x ] ) when is_integer ( x ) , do: to_string ( for _ <- 1 .. times , do: x )
109
110
def repeat ( times , x ) , do: for ( _ <- 1 .. times , do: x )
@@ -122,26 +123,6 @@ defmodule Helper.Utils do
122
123
% { entries: [ ] , total_count: 0 , page_size: 0 , total_pages: 1 , page_number: 1 }
123
124
end
124
125
125
- @ doc """
126
- ["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
127
- """
128
- def count_words ( words ) when is_list ( words ) do
129
- Enum . reduce ( words , % { } , & update_word_count / 2 )
130
- end
131
-
132
- defp update_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
- @ spec str_occurence ( String . t ( ) , String . t ( ) ) :: Integer . t ( )
138
- def str_occurence ( string , substr ) when is_binary ( string ) and is_binary ( substr ) do
139
- len = string |> String . split ( substr ) |> length ( )
140
- len - 1
141
- end
142
-
143
- def str_occurence ( _ , _ ) , do: "must be strings"
144
-
145
126
@ spec large_than ( String . t ( ) | Integer . t ( ) , Integer . t ( ) ) :: true | false
146
127
def large_than ( value , target ) when is_binary ( value ) and is_integer ( target ) do
147
128
String . length ( value ) >= target