36 lines
1.6 KiB
Elixir
36 lines
1.6 KiB
Elixir
defmodule Localizator.Commons do
|
|
@html_regex ~r/<(br|basefont|hr|input|source|frame|param|area|meta|!--|col|link|option|base|img|wbr|!DOCTYPE).*?>|<(a|abbr|acronym|address|applet|article|aside|audio|b|bdi|bdo|big|blockquote|body|button|canvas|caption|center|cite|code|colgroup|command|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frameset|head|header|hgroup|h1|h2|h3|h4|h5|h6|html|i|iframe|ins|kbd|keygen|label|legend|li|map|mark|menu|meter|nav|noframes|noscript|object|ol|optgroup|output|p|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video).*?<\/\2>/i
|
|
|
|
def is_html?(string) when is_binary(string) do
|
|
String.match?(string, @html_regex)
|
|
end
|
|
|
|
def first_non_empty_longest_string(map) when is_map(map) do
|
|
map
|
|
|> Map.values()
|
|
|> Enum.reject(&is_nil(&1))
|
|
|> Enum.filter(&is_binary(&1))
|
|
|> Enum.map(&String.trim(&1))
|
|
|> Enum.reject(&(&1 == ""))
|
|
|> Enum.sort(&(String.length(&1) >= String.length(&2)))
|
|
|> List.first()
|
|
end
|
|
|
|
def struct_from_map(a_map, as: a_struct) do
|
|
# Find the keys within the map
|
|
keys =
|
|
Map.keys(a_struct)
|
|
|> Enum.filter(fn x -> x != :__struct__ end)
|
|
|
|
# Process map, checking for both string / atom keys
|
|
processed_map =
|
|
for key <- keys, into: %{} do
|
|
value = Map.get(a_map, key) || Map.get(a_map, to_string(key))
|
|
{key, value}
|
|
end
|
|
|
|
a_struct = Map.merge(a_struct, processed_map)
|
|
a_struct
|
|
end
|
|
end
|