Current section
Files
Jump to
Current section
Files
lib/util/util.ex
defmodule Util do
@moduledoc false
def uuid() do
UUID.uuid4(:hex)
end
def random_string(length) do
:crypto.strong_rand_bytes(length)
|> Base.url_encode64()
|> binary_part(0, length)
|> String.upcase()
end
def random_num_string(length) do
{a, _} = :crypto.rand_plugin_next(0)
a
|> :erlang.integer_to_binary
|> binary_part(0, length)
end
def map_drop_key(map, keys) do
Enum.reduce(
keys,
map,
fn (h, acc) ->
Map.delete(acc, h)
end
)
end
def iif(condition, true_result, false_result) do
if condition do
true_result
else
false_result
end
end
def del_price(price) when is_binary(price)do
case Integer.parse(price) do
{p, _} ->
p
:error ->
:error
end
end
def del_price(price) do
to_integer(price)
end
def to_atom(str) when is_binary(str) do
String.to_existing_atom(str)
end
def to_atom(str) when is_atom(str) do
str
end
def to_atom(_) do
:error
end
def binary_to_list(str) when is_binary(str) do
case Jason.decode(str) do
{:ok, r} ->
r
_ ->
:error
end
end
def binary_to_list(str) do
str
end
# array[integer/string,integer/string] -> string
def list_to_binary(label)do
{:ok, r} = Jason.encode(label)
r
end
def list_ele_to_integer(str) when is_binary(str) do
case Jason.decode(str) do
{:ok, list} ->
list_ele_to_integer(list)
_ ->
[]
end
end
def list_ele_to_integer(list) when is_list(list) do
Enum.reduce(
list,
[],
fn (h, acc) ->
case Enum.member?(acc, h) do
true ->
acc
_ ->
[to_integer(h) | acc]
end
end
)
end
def concat_cypher([h | []], acc) do
"c.#{h} AS #{h}" <> acc
end
def concat_cypher([h | rest], acc) do
concat_cypher(rest, ", c.#{h} AS #{h}" <> acc)
end
def to_integer(value) when is_integer(value) do
value
end
def to_integer(value) when is_binary(value) do
case Integer.parse(value) do
{i, _} ->
i
err ->
err
end
end
def to_integer(value) when is_float(value) do
to_integer(Float.to_string(value))
end
def to_integer(_) do
-1
end
def to_string(str) when is_integer(str) do
Integer.to_string(str)
end
def to_string(str) when is_float(str) do
Float.to_string(str)
end
def to_string(str) do
str
end
def to_float(i) when is_binary(i) do
{f, _} = Float.parse(i)
f
end
def to_float(int) do
{f, _} = Float.parse(Util.to_string(int))
f
end
def demand_time_out(1) do
ConfigServer.key_value(:demand_time_out1)
end
def demand_time_out(2) do
ConfigServer.key_value(:demand_time_out2)
end
def demand_time_out(3) do
ConfigServer.key_value(:demand_time_out3)
end
def demand_time_out(_) do
:error
end
def one_day_sec() do
86400
end
def one_hour() do
60 * 60
end
end