bban parse function

This commit is contained in:
Danil Negrienko 2024-05-11 11:52:34 -04:00
parent ae17d13731
commit 4abf01b752
5 changed files with 29 additions and 12 deletions

View File

@ -71,7 +71,7 @@ The package can be installed by adding `iban_ex` to your list of dependencies in
```elixir ```elixir
def deps do def deps do
[ [
{:iban_ex, "~> 0.1.3"} {:iban_ex, "~> 0.1.5"}
] ]
end end
``` ```

View File

@ -14,5 +14,9 @@ defmodule IbanEx.Commons do
string string
|> normalize() |> normalize()
|> String.slice(range) |> String.slice(range)
# |> case do
# "" -> nil
# result -> result
# end
end end
end end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.BG do
## Examples ## Examples
iex> %IbanEx.Iban{country_code: "BG", check_digits: "80", bank_code: "BNBG", branch_code: "9661", national_check: nil, account_number: "1020345678"} ```elixir
iex> |> IbanEx.Country.BG.to_string() 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" "BG 80 BNBG 9661 1020345678"
```
""" """
@size 22 @size 22

View File

@ -20,13 +20,10 @@ defmodule IbanEx.Parser do
check_digits: check_digits(valid_iban) check_digits: check_digits(valid_iban)
} }
regex = Country.country_module(iban_map.country_code).rule()
bban = bban(iban_string)
bban_map = bban_map =
for {key, val} <- Regex.named_captures(regex, bban), iban_string
into: %{}, |> bban()
do: {String.to_atom(key), val} |> parse_bban(iban_map.country_code)
{:ok, struct(Iban, Map.merge(iban_map, bban_map))} {:ok, struct(Iban, Map.merge(iban_map, bban_map))}
@ -35,6 +32,14 @@ defmodule IbanEx.Parser do
end end
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() @spec country_code(iban_string()) :: country_code_string()
def country_code(iban_string), do: normalize_and_slice(iban_string, 0..1) def country_code(iban_string), do: normalize_and_slice(iban_string, 0..1)

View File

@ -2,7 +2,7 @@ defmodule IbanEx.MixProject do
use Mix.Project use Mix.Project
@source_url "https://g.tulz.dev/opensource/iban-ex" @source_url "https://g.tulz.dev/opensource/iban-ex"
@version "0.1.3" @version "0.1.5"
def project do def project do
[ [