Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_strings_in_sweetxml #86

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/ex_phone_number/metadata/normalize.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule ExPhoneNumber.Metadata.Normalize do
def pattern(nil), do: nil
def pattern([]), do: nil
def pattern(""), do: nil

def pattern(pattern) when is_binary(pattern) do
pattern
|> String.replace(["\n", " "], "")
|> Regex.compile!()
end

def string(""), do: nil
def string(string) when is_binary(string), do: string

def boolean("true"), do: true
def boolean(_), do: false

def rule(nil), do: nil

def rule(char_list) when is_list(char_list),
do: char_list |> List.to_string() |> rule()

def rule(string) when is_binary(string) do
string
|> String.replace(~r/\$(\d)/, "\\\\g{\\g{1}}")
end

def range(""), do: nil

def range(string) when is_binary(string) do
string
|> String.replace(["\n", " "], "")
|> String.split(",")
|> Enum.flat_map(&range_to_list/1)
|> Enum.uniq()
|> Enum.sort()
end

defp range_to_list("[" <> range) do
range
|> String.trim_trailing("]")
|> String.split("-", parts: 2)
|> then(fn [from, to] -> Range.new(String.to_integer(from), String.to_integer(to)) end)

Check failure on line 43 in lib/ex_phone_number/metadata/normalize.ex

View workflow job for this annotation

GitHub Actions / Test on Elixir 1.11.4 (OTP 22.3.4.26)

** (CompileError) lib/ex_phone_number/metadata/normalize.ex:43: undefined function then/2
|> Enum.to_list()
end

defp range_to_list(number) do
[String.to_integer(number)]
end
end
49 changes: 8 additions & 41 deletions lib/ex_phone_number/metadata/number_format.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ExPhoneNumber.Metadata.NumberFormat do
alias ExPhoneNumber.Metadata.Normalize
@moduledoc false

# string
Expand All @@ -25,52 +26,18 @@ defmodule ExPhoneNumber.Metadata.NumberFormat do
kwlist =
xpath_node
|> xmap(
pattern: ~x"./@pattern"s |> transform_by(&normalize_pattern/1),
format: ~x"./format/text()"s |> transform_by(&normalize_rule/1),
pattern: ~x"./@pattern"s |> transform_by(&Normalize.pattern/1),
format: ~x"./format/text()"s |> transform_by(&Normalize.rule/1),
leading_digits_pattern: [
~x"./leadingDigits"el,
pattern: ~x"./text()"s |> transform_by(&normalize_pattern/1)
pattern: ~x"./text()"s |> transform_by(&Normalize.pattern/1)
],
national_prefix_formatting_rule: ~x"./@nationalPrefixFormattingRule"o |> transform_by(&normalize_string/1),
national_prefix_optional_when_formatting: ~x"./@nationalPrefixOptionalWhenFormatting"o |> transform_by(&normalize_boolean/1),
domestic_carrier_code_formatting_rule: ~x"./@carrierCodeFormattingRule"o |> transform_by(&normalize_string/1),
intl_format: ~x"./intlFormat/text()"o |> transform_by(&normalize_rule/1)
national_prefix_formatting_rule: ~x"./@nationalPrefixFormattingRule"so |> transform_by(&Normalize.string/1),
national_prefix_optional_when_formatting: ~x"./@nationalPrefixOptionalWhenFormatting"so |> transform_by(&Normalize.boolean/1),
domestic_carrier_code_formatting_rule: ~x"./@carrierCodeFormattingRule"so |> transform_by(&Normalize.string/1),
intl_format: ~x"./intlFormat/text()"o |> transform_by(&Normalize.rule/1)
)

struct(%NumberFormat{}, kwlist)
end

defp normalize_boolean(nil), do: nil

defp normalize_boolean(true_char_list)
when is_list(true_char_list) and length(true_char_list) == 4,
do: true

defp normalize_pattern(nil), do: nil
defp normalize_pattern(""), do: nil
defp normalize_pattern([]), do: nil

defp normalize_pattern(string) when is_binary(string) do
string
|> String.split(["\n", " "], trim: true)
|> List.to_string()
|> Regex.compile!()
end

defp normalize_string(nil), do: nil

defp normalize_string(char_list) when is_list(char_list) do
char_list
|> List.to_string()
end

defp normalize_rule(nil), do: nil

defp normalize_rule(char_list) when is_list(char_list),
do: char_list |> List.to_string() |> normalize_rule()

defp normalize_rule(string) when is_binary(string) do
string
|> String.replace(~r/\$(\d)/, "\\\\g{\\g{1}}")
end
end
Loading
Loading