@@ -10,6 +10,7 @@ defmodule Helper.Utils do
1010
1111 alias Helper . { Cache , Utils }
1212
13+ # Map utils
1314 defdelegate map_key_stringify ( map ) , to: Utils.Map
1415 defdelegate keys_to_atoms ( map ) , to: Utils.Map
1516 defdelegate keys_to_strings ( map ) , to: Utils.Map
@@ -19,6 +20,11 @@ defmodule Helper.Utils do
1920 defdelegate deep_merge ( left , right ) , to: Utils.Map
2021 defdelegate map_atom_value ( attrs , opt ) , to: Utils.Map
2122
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+
2228 def get_config ( section , key , app \\ :groupher_server )
2329
2430 def get_config ( section , :all , app ) do
@@ -99,11 +105,6 @@ defmodule Helper.Utils do
99105 def integerfy ( id ) when is_binary ( id ) , do: String . to_integer ( id )
100106 def integerfy ( id ) , do: id
101107
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-
107108 # TODO: enhance, doc
108109 def repeat ( times , [ x ] ) when is_integer ( x ) , do: to_string ( for _ <- 1 .. times , do: x )
109110 def repeat ( 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- 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-
145126 @ spec large_than ( String . t ( ) | Integer . t ( ) , Integer . t ( ) ) :: true | false
146127 def large_than ( value , target ) when is_binary ( value ) and is_integer ( target ) do
147128 String . length ( value ) >= target
0 commit comments