|
|
|
@@ -2,7 +2,7 @@ defmodule IbanEx.Parser do
|
|
|
|
@moduledoc false
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
|
|
|
|
alias IbanEx.{Country, Iban, Validator}
|
|
|
|
alias IbanEx.{Country, Iban, Validator}
|
|
|
|
import IbanEx.Commons, only: [normalize_and_slice: 2, blank: 1]
|
|
|
|
import IbanEx.Commons, only: [normalize_and_slice: 2]
|
|
|
|
|
|
|
|
|
|
|
|
@type iban_string() :: String.t()
|
|
|
|
@type iban_string() :: String.t()
|
|
|
|
@type country_code_string() :: <<_::16>>
|
|
|
|
@type country_code_string() :: <<_::16>>
|
|
|
|
@@ -45,21 +45,32 @@ def parse(iban_string) do
|
|
|
|
def parse_bban(bban_string, country_code, options \\ [incomplete: false])
|
|
|
|
def parse_bban(bban_string, country_code, options \\ [incomplete: false])
|
|
|
|
|
|
|
|
|
|
|
|
def parse_bban(bban_string, country_code, incomplete: true) do
|
|
|
|
def parse_bban(bban_string, country_code, incomplete: true) do
|
|
|
|
Country.country_module(country_code).incomplete_rule()
|
|
|
|
case Country.is_country_code_supported?(country_code) do
|
|
|
|
|> parse_bban_by_regex(bban_string)
|
|
|
|
true ->
|
|
|
|
|
|
|
|
Country.country_module(country_code).incomplete_rule()
|
|
|
|
|
|
|
|
|> parse_bban_by_regex(bban_string)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
|
|
|
|
%{}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def parse_bban(bban_string, country_code, _options) do
|
|
|
|
def parse_bban(bban_string, country_code, incomplete: false) do
|
|
|
|
Country.country_module(country_code).rule()
|
|
|
|
case Country.is_country_code_supported?(country_code) do
|
|
|
|
|> parse_bban_by_regex(bban_string)
|
|
|
|
true ->
|
|
|
|
|
|
|
|
Country.country_module(country_code).rule()
|
|
|
|
|
|
|
|
|> parse_bban_by_regex(bban_string)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
|
|
|
|
%{}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp parse_bban_by_regex(_regex, nil), do: %{}
|
|
|
|
defp parse_bban_by_regex(regex, bban_string) do
|
|
|
|
defp parse_bban_by_regex(regex, bban_string) do
|
|
|
|
case Regex.named_captures(regex, bban_string) do
|
|
|
|
case Regex.named_captures(regex, bban_string) do
|
|
|
|
map when is_map(map) ->
|
|
|
|
map when is_map(map) ->
|
|
|
|
for {key, val} <- map,
|
|
|
|
for {key, val} <- map,
|
|
|
|
into: %{},
|
|
|
|
into: %{},
|
|
|
|
do: {String.to_atom(key), blank(val)}
|
|
|
|
do: {String.to_atom(key), val}
|
|
|
|
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
nil ->
|
|
|
|
%{}
|
|
|
|
%{}
|
|
|
|
@@ -67,11 +78,11 @@ defp parse_bban_by_regex(regex, bban_string) do
|
|
|
|
end
|
|
|
|
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) |> blank()
|
|
|
|
def country_code(iban_string), do: normalize_and_slice(iban_string, 0..1)
|
|
|
|
|
|
|
|
|
|
|
|
@spec check_digits(binary()) :: check_digits_string()
|
|
|
|
@spec check_digits(binary()) :: check_digits_string()
|
|
|
|
def check_digits(iban_string), do: normalize_and_slice(iban_string, 2..3) |> blank()
|
|
|
|
def check_digits(iban_string), do: normalize_and_slice(iban_string, 2..3)
|
|
|
|
|
|
|
|
|
|
|
|
@spec bban(binary()) :: binary()
|
|
|
|
@spec bban(binary()) :: binary()
|
|
|
|
def bban(iban_string), do: normalize_and_slice(iban_string, 4..-1//1) |> blank()
|
|
|
|
def bban(iban_string), do: normalize_and_slice(iban_string, 4..-1//1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|