Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Jul 4, 2021
1 parent 357f321 commit 29448e6
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -10,8 +10,8 @@ def deps do
end
```
-------------------------------------
Ran 249 tests at 2021-07-03
85% successfully
214 success
35 failed
Ran 249 tests at 2021-07-04
88% successfully
221 success
28 failed
-------------------------------------
87 changes: 77 additions & 10 deletions lib/address_formatting.ex
Expand Up @@ -4,18 +4,16 @@ defmodule AddressFormatting do
"""
alias AddressFormatting.FileHelpers

@state_codes FileHelpers.load_yaml("state_codes")
@county_codes FileHelpers.load_yaml("county_codes")
@country_codes FileHelpers.load_yaml("country_codes")
@state_codes FileHelpers.load_yaml_reverse("state_codes")
@county_codes FileHelpers.load_yaml_reverse("county_codes")
@country2lang FileHelpers.load_yaml("country2lang")
@components FileHelpers.load_components()
@components FileHelpers.load_components() |> Map.drop(["suburb"])
@worldwide FileHelpers.load_yaml("worldwide", directory: "countries")
@all_components FileHelpers.load_abbreviations()

@standard_keys ["island"] ++ Map.keys(@components)
def state_codes(), do: @state_codes
def county_codes(), do: @county_codes
def country_codes(), do: @country_codes
@standard_keys ["island", "suburb"] ++ Map.keys(@components)
def get_codes_dict("state"), do: @state_codes
def get_codes_dict("county"), do: @county_codes
def country2lang(), do: @country2lang
def load_components(), do: @components
def load_worldwide(), do: @worldwide
Expand Down Expand Up @@ -114,7 +112,10 @@ defmodule AddressFormatting do
with {:ok, variables} <- check_country_case(variables, country_data),
{:ok, variables} <- convert_component_aliases(variables, country_data),
{:ok, variables} <- convert_keys_to_attention(variables, country_data),
{:ok, variables} <- convert_to_code(variables, country_data),
{:ok, variables} <- convert_constants(variables, country_data),
{:ok, variables} <- convert_country_numeric(variables, country_data),
{:ok, variables} <- add_codes(variables, country_data, "county"),
{:ok, variables} <- add_codes(variables, country_data, "state"),
{:ok, variables} <- add_postformat(variables, country_data),
{:ok, variables} <- run_replace(variables, country_data),
{:ok, variables} <- check_use_country(variables, country_data),
Expand All @@ -132,6 +133,7 @@ defmodule AddressFormatting do
[~r/^[,\s]+/, ""],
# linestarting with dash due to a parameter missing
[~r/^- /, ""],
[~r/\n\h-/, "\n"],
# multiple commas to one
[~r/,\s*,/, ", "],
# one horiz whitespace behind comma
Expand Down Expand Up @@ -198,10 +200,68 @@ defmodule AddressFormatting do
{:ok, variables}
end

def convert_to_code(variables, _country_data) do
def convert_constants(variables, _country_data) do
variables =
case Map.get(variables, "country_code") do
"UK" ->
Map.put(variables, "country_code", "GB")

"NL" ->
state = Map.get(variables, "state")

cond do
state == "Curaçao" ->
%{variables | "country" => "Curaçao", "country_code" => "CW"}

String.downcase(state) =~ "sint maarten" ->
%{variables | "country" => "Sint Maarten", "country_code" => "SX"}

String.downcase(state) =~ "aruba" ->
%{variables | "country" => "Aruba", "country_code" => "AW"}

true ->
variables
end

_other ->
variables
end

{:ok, variables}
end

def convert_country_numeric(variables, _country_data) do
country = Map.get(variables, "country")

variables =
if is_integer?(country) do
{state, variables} = Map.pop(variables, "state")
Map.put(variables, "country", state)
else
variables
end

{:ok, variables}
end

def add_codes(variables, _country_data, key) do
country_code = Map.get(variables, "country_code")

updated_variables =
case Map.get(variables, key) do
nil ->
variables

state ->
case get_in(get_codes_dict(key), [country_code, state]) do
nil -> variables
code -> Map.put(variables, key <> "_code", code)
end
end

{:ok, updated_variables}
end

def convert_keys_to_attention(variables, _country_data) do
attention =
variables
Expand Down Expand Up @@ -239,4 +299,11 @@ defmodule AddressFormatting do

def upcase(nil), do: nil
def upcase(string), do: String.upcase(string)
def is_integer?(val) when is_integer(val), do: true

def is_integer?(val) when is_bitstring(val) do
Regex.match?(~r{\A\d*\z}, val)
end

def is_integer?(_), do: false
end
17 changes: 10 additions & 7 deletions lib/address_test_helper.ex
Expand Up @@ -12,16 +12,19 @@ defmodule AddressHelper do
list
|> Enum.map(&str_or_tuple/1)
|> Enum.join()
end
end

def log_to_readme(data) do
case System.get_env(@env_var) do
nil -> data
nil ->
data

_ ->
File.open(@filename, [:append])
|> elem(1)
|> IO.binwrite(data)
data
File.open(@filename, [:append])
|> elem(1)
|> IO.binwrite(data)

data
end
end

Expand All @@ -36,7 +39,6 @@ defmodule AddressHelper do

{template, variables} = data = AddressFormatting.get_template(components)
rendered = AddressFormatting.render(data)


case ExUnit.Diff.compute(expected, rendered, :==) do
{%ExUnit.Diff{equivalent?: true}, _} ->
Expand All @@ -55,6 +57,7 @@ defmodule AddressHelper do
|> Enum.each(fn {a, b} -> IO.puts("#{a}: #{b}") end)

IO.puts("---")

variables
|> Map.delete("postformat_replace")
|> Enum.each(fn {a, b} -> IO.puts("#{a}: #{b}") end)
Expand Down
9 changes: 9 additions & 0 deletions lib/file_helpers.ex
Expand Up @@ -22,6 +22,15 @@ defmodule AddressFormatting.FileHelpers do
|> Map.new()
end

def load_yaml_reverse(name) do
name
|> load_yaml()
|> Enum.map(fn {cc, map} ->
{cc, Map.new(Enum.map(map, fn {k, v} -> {v, k} end))}
end)
|> Map.new()
end

def load_yaml(name, opts \\ []) do
filename = Enum.join([name, "yaml"], ".")
directory = Keyword.get(opts, :directory, "")
Expand Down
6 changes: 3 additions & 3 deletions test/address_formatting_test.exs
Expand Up @@ -10,7 +10,7 @@ defmodule AddressFormattingTest do
end

{success, failed} =
for {_, data} = input_tuple <-
for {_, _data} = input_tuple <-
AddressFormatting.FileHelpers.load_testcases_countries(),
reduce: {0, 0} do
{success, fail} ->
Expand All @@ -29,7 +29,7 @@ defmodule AddressFormattingTest do
#{failed} failed
-------------------------------------
"""
|> AddressHelper.log_to_readme
|> IO.puts
|> AddressHelper.log_to_readme()
|> IO.puts()
end
end

0 comments on commit 29448e6

Please sign in to comment.