From 4abf01b7520c9b2afff5d868a89377c08f6af267 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 11 May 2024 11:52:34 -0400 Subject: [PATCH] bban parse function --- README.md | 2 +- lib/iban_ex/commons/commons.ex | 4 ++++ lib/iban_ex/country/bg.ex | 16 ++++++++++++---- lib/iban_ex/parser.ex | 17 +++++++++++------ mix.exs | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 415ef48..f060494 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/iban_ex/commons/commons.ex b/lib/iban_ex/commons/commons.ex index 243346f..a7c33c4 100644 --- a/lib/iban_ex/commons/commons.ex +++ b/lib/iban_ex/commons/commons.ex @@ -14,5 +14,9 @@ defmodule IbanEx.Commons do string |> normalize() |> String.slice(range) + # |> case do + # "" -> nil + # result -> result + # end end end diff --git a/lib/iban_ex/country/bg.ex b/lib/iban_ex/country/bg.ex index 6148105..fc123aa 100644 --- a/lib/iban_ex/country/bg.ex +++ b/lib/iban_ex/country/bg.ex @@ -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 diff --git a/lib/iban_ex/parser.ex b/lib/iban_ex/parser.ex index cc89647..0f365ad 100644 --- a/lib/iban_ex/parser.ex +++ b/lib/iban_ex/parser.ex @@ -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) diff --git a/mix.exs b/mix.exs index ffaad25..2a36cd4 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule IbanEx.MixProject do use Mix.Project @source_url "https://g.tulz.dev/opensource/iban-ex" - @version "0.1.3" + @version "0.1.5" def project do [