bban parse function
This commit is contained in:
parent
ae17d13731
commit
4abf01b752
|
@ -71,7 +71,7 @@ The package can be installed by adding `iban_ex` to your list of dependencies in
|
|||
```elixir
|
||||
def deps do
|
||||
[
|
||||
{:iban_ex, "~> 0.1.3"}
|
||||
{:iban_ex, "~> 0.1.5"}
|
||||
]
|
||||
end
|
||||
```
|
||||
|
|
|
@ -14,5 +14,9 @@ defmodule IbanEx.Commons do
|
|||
string
|
||||
|> normalize()
|
||||
|> String.slice(range)
|
||||
# |> case do
|
||||
# "" -> nil
|
||||
# result -> result
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,10 +4,18 @@ defmodule IbanEx.Country.BG do
|
|||
|
||||
## Examples
|
||||
|
||||
iex> %IbanEx.Iban{country_code: "BG", check_digits: "80", bank_code: "BNBG", branch_code: "9661", national_check: nil, account_number: "1020345678"}
|
||||
iex> |> IbanEx.Country.BG.to_string()
|
||||
"BG 80 BNBG 9661 1020345678"
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "BG",
|
||||
...> check_digits: "80",
|
||||
...> bank_code: "BNBG",
|
||||
...> branch_code: "9661",
|
||||
...> national_check: nil,
|
||||
...> account_number: "1020345678"
|
||||
...> }
|
||||
...> |> IbanEx.Country.BG.to_string()
|
||||
"BG 80 BNBG 9661 1020345678"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 22
|
||||
|
|
|
@ -20,13 +20,10 @@ defmodule IbanEx.Parser do
|
|||
check_digits: check_digits(valid_iban)
|
||||
}
|
||||
|
||||
regex = Country.country_module(iban_map.country_code).rule()
|
||||
bban = bban(iban_string)
|
||||
|
||||
bban_map =
|
||||
for {key, val} <- Regex.named_captures(regex, bban),
|
||||
into: %{},
|
||||
do: {String.to_atom(key), val}
|
||||
iban_string
|
||||
|> bban()
|
||||
|> parse_bban(iban_map.country_code)
|
||||
|
||||
{:ok, struct(Iban, Map.merge(iban_map, bban_map))}
|
||||
|
||||
|
@ -35,6 +32,14 @@ defmodule IbanEx.Parser do
|
|||
end
|
||||
end
|
||||
|
||||
@spec parse_bban(binary(), <<_::16>>) :: map()
|
||||
def parse_bban(bban_string, country_code) do
|
||||
regex = Country.country_module(country_code).rule()
|
||||
for {key, val} <- Regex.named_captures(regex, bban_string),
|
||||
into: %{},
|
||||
do: {String.to_atom(key), val}
|
||||
end
|
||||
|
||||
@spec country_code(iban_string()) :: country_code_string()
|
||||
def country_code(iban_string), do: normalize_and_slice(iban_string, 0..1)
|
||||
|
||||
|
|
Loading…
Reference in New Issue