Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 4529aef

Browse files
committed
refactor(utils): re-org based on usage for string
1 parent c59255b commit 4529aef

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-
import Ecto.Query, warn: false
6-
import Helper.ErrorHandler
7-
import Helper.ErrorCode
8-
9-
import Helper.Validator.Guards, only: [g_none_empty_str: 1]
10-
11-
alias Helper.Cache
12-
135
def map_key_stringify(%{__struct__: _} = map) when is_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+
defmodule Helper.Utils.String do
2+
@moduledoc """
3+
string utils
4+
"""
5+
6+
def stringfy(v) when is_binary(v), do: v
7+
def stringfy(v) when is_integer(v), do: to_string(v)
8+
def stringfy(v) when is_atom(v), do: to_string(v)
9+
def stringfy(v), do: v
10+
11+
# see https://stackoverflow.com/a/49558074/4050784
12+
@spec str_occurence(String.t(), String.t()) :: Integer.t()
13+
def str_occurence(string, substr) when is_binary(string) and is_binary(substr) do
14+
len = string |> String.split(substr) |> length()
15+
len - 1
16+
end
17+
18+
def str_occurence(_, _), do: "must be strings"
19+
20+
@doc """
21+
["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
22+
"""
23+
def count_words(words) when is_list(words) do
24+
Enum.reduce(words, %{}, &update_word_count/2)
25+
end
26+
27+
defp update_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
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

Comments
 (0)