Compare commits

...

10 Commits
0.1.5 ... main

81 changed files with 2182 additions and 275 deletions

View File

@ -29,7 +29,7 @@ In just a few letters and numbers, the IBAN captures all of the country, bank, a
#### To check IBAN's country is supported
```elixir
iex> {:error, unsupported_country_code} = IbanEx.Parser.parse("AZ21NABZ00000000137010001944")
iex> {:error, unsupported_country_code} = IbanEx.Parser.parse("ZU21NABZ00000000137010001944")
{:error, :unsupported_country_code}
iex> IbanEx.Error.message(unsupported_country_code)
"Unsupported country code"
@ -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.5"}
{:iban_ex, "~> 0.1.8"}
]
end
```

56
TODO.md Normal file
View File

@ -0,0 +1,56 @@
TODO Check Regexes and add unsupported now countries
```elixir
"AA" => %{length: 16, rule: ~r/^[0-9A-Z]{12}$/i},
"AO" => %{length: 25, rule: ~r/^[0-9]{21}$/i},
"AX" => %{length: 18, rule: ~r/^[0-9]{14}$/i},
"BF" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"BI" => %{length: 16, rule: ~r/^[0-9]{12}$/i},
"BJ" => %{length: 28, rule: ~r/^[A-Z]{1}[0-9]{23}$/i},
"BL" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"BY" => %{length: 28, rule: ~r/^[0-9A-Z]{4}[0-9]{4}[0-9A-Z]{16}$/i},
"CF" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"CG" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"CI" => %{length: 28, rule: ~r/^[A-Z]{1}[0-9]{23}$/i},
"CM" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"CV" => %{length: 25, rule: ~r/^[0-9]{21}$/i},
"DJ" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"DZ" => %{length: 24, rule: ~r/^[0-9]{20}$/i},
"GA" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"GF" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"GP" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"GQ" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"GW" => %{length: 25, rule: ~r/^[0-9A-Z]{2}[0-9]{19}$/i},
"HN" => %{length: 28, rule: ~r/^[A-Z]{4}[0-9]{20}$/i},
"IE" => %{length: 22, rule: ~r/^[A-Z]{4}[0-9]{14}$/i},
"IQ" => %{length: 23, rule: ~r/^[0-9A-Z]{4}[0-9]{15}$/i},
"IR" => %{length: 26, rule: ~r/^[0-9]{22}$/i},
"IS" => %{length: 26, rule: ~r/^[0-9]{22}$/i},
"KM" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"LC" => %{length: 32, rule: ~r/^[A-Z]{4}[0-9A-Z]{24}$/i},
"MA" => %{length: 28, rule: ~r/^[0-9]{24}$/i},
"MF" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"MG" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"MK" => %{length: 19, rule: ~r/^[0-9]{3}[0-9A-Z]{10}[0-9]{2}$/i},
"ML" => %{length: 28, rule: ~r/^[A-Z]{1}[0-9]{23}$/i},
"MQ" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"MU" => %{length: 30, rule: ~r/^[A-Z]{4}[0-9]{19}[A-Z]{3}$/i},
"MZ" => %{length: 25, rule: ~r/^[0-9]{21}$/i},
"NC" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"NE" => %{length: 28, rule: ~r/^[A-Z]{2}[0-9]{22}$/i},
"NI" => %{length: 32, rule: ~r/^[A-Z]{4}[0-9]{24}$/i},
"PF" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"PM" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"PS" => %{length: 29, rule: ~r/^[A-Z]{4}[0-9A-Z]{21}$/i},
"QA" => %{length: 29, rule: ~r/^[A-Z]{4}[0-9]{4}[0-9A-Z]{17}$/i},
"RE" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"SC" => %{length: 31, rule: ~r/^[A-Z]{4}[0-9]{20}[A-Z]{3}$/i},
"SN" => %{length: 28, rule: ~r/^[A-Z]{1}[0-9]{23}$/i},
"ST" => %{length: 25, rule: ~r/^[0-9]{8}[0-9]{13}$/i},
"TD" => %{length: 27, rule: ~r/^[0-9]{23}$/i},
"TF" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"TG" => %{length: 28, rule: ~r/^[A-Z]{2}[0-9]{22}$/i},
"TN" => %{length: 24, rule: ~r/^[0-9]{20}$/i},
"WF" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i},
"YT" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i}
```

View File

@ -1,6 +1,11 @@
defmodule IbanEx.Commons do
@moduledoc false
@spec blank(nil | binary()) :: nil | binary()
def blank(nil), do: nil
def blank(""), do: nil
def blank(string) when is_binary(string), do: string
@spec normalize(binary()) :: binary()
def normalize(string) do
string

View File

@ -7,42 +7,75 @@ defmodule IbanEx.Country do
@type error_tuple() :: {:error, atom()}
@supported_countries %{
"AD" => IbanEx.Country.AD,
"AE" => IbanEx.Country.AE,
"AL" => IbanEx.Country.AL,
"AT" => IbanEx.Country.AT,
"AZ" => IbanEx.Country.AZ,
"BA" => IbanEx.Country.BA,
"BE" => IbanEx.Country.BE,
"BG" => IbanEx.Country.BG,
"BH" => IbanEx.Country.BH,
"BR" => IbanEx.Country.BR,
"CH" => IbanEx.Country.CH,
"CR" => IbanEx.Country.CR,
"CY" => IbanEx.Country.CY,
"CZ" => IbanEx.Country.CZ,
"DE" => IbanEx.Country.DE,
"DO" => IbanEx.Country.DO,
"DK" => IbanEx.Country.DK,
"EE" => IbanEx.Country.EE,
"ES" => IbanEx.Country.ES,
"EG" => IbanEx.Country.EG,
"FI" => IbanEx.Country.FI,
"FR" => IbanEx.Country.FR,
"FO" => IbanEx.Country.FO,
"GB" => IbanEx.Country.GB,
"GE" => IbanEx.Country.GE,
"GI" => IbanEx.Country.GI,
"GL" => IbanEx.Country.GL,
"GR" => IbanEx.Country.GR,
"GT" => IbanEx.Country.GT,
"HR" => IbanEx.Country.HR,
"HU" => IbanEx.Country.HU,
"IE" => IbanEx.Country.IE,
"IL" => IbanEx.Country.IL,
"IT" => IbanEx.Country.IT,
"IS" => IbanEx.Country.IS,
"JO" => IbanEx.Country.JO,
"KZ" => IbanEx.Country.KZ,
"KW" => IbanEx.Country.KW,
"LB" => IbanEx.Country.LB,
"LI" => IbanEx.Country.LI,
"LT" => IbanEx.Country.LT,
"LU" => IbanEx.Country.LU,
"LV" => IbanEx.Country.LV,
"MC" => IbanEx.Country.MC,
"MD" => IbanEx.Country.MD,
"ME" => IbanEx.Country.ME,
"MK" => IbanEx.Country.MK,
"MR" => IbanEx.Country.MR,
"MT" => IbanEx.Country.MT,
"NL" => IbanEx.Country.NL,
"NO" => IbanEx.Country.NO,
"PL" => IbanEx.Country.PL,
"PT" => IbanEx.Country.PT,
"PK" => IbanEx.Country.PK,
"QA" => IbanEx.Country.QA,
"RO" => IbanEx.Country.RO,
"RS" => IbanEx.Country.RS,
"SA" => IbanEx.Country.SA,
"SE" => IbanEx.Country.SE,
"SM" => IbanEx.Country.SM,
"SI" => IbanEx.Country.SI,
"SK" => IbanEx.Country.SK,
"SM" => IbanEx.Country.SM,
"SV" => IbanEx.Country.SV,
"TL" => IbanEx.Country.TL,
"TR" => IbanEx.Country.TR,
"UA" => IbanEx.Country.UA,
"VA" => IbanEx.Country.VA
"VA" => IbanEx.Country.VA,
"VG" => IbanEx.Country.VG,
"XK" => IbanEx.Country.XK
}
@supported_country_codes Map.keys(@supported_countries)
@ -57,7 +90,7 @@ defmodule IbanEx.Country do
@spec supported_country_modules() :: [module()] | []
def supported_country_modules(), do: @supported_country_modules
@spec country_module(country_code) :: Module.t() | error_tuple()
@spec country_module(country_code) :: module() | error_tuple()
def country_module(country_code) when is_binary(country_code) or is_atom(country_code) do
normalized_country_code = normalize(country_code)
case is_country_code_supported?(normalized_country_code) do

42
lib/iban_ex/country/ad.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.AD do
@moduledoc """
Andorra IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "AD",
...> check_digits: "12",
...> bank_code: "0001",
...> branch_code: "2030",
...> national_check: nil,
...> account_number: "200359100100"
...> }
...> |> IbanEx.Country.AD.to_string()
"AD 12 0001 2030 200359100100"
```
"""
@size 24
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9A-Z]{12})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number]
|> Enum.join(joiner)
end
end

25
lib/iban_ex/country/ae.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.AE do
@moduledoc """
United Arab Emirates IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "AE",
...> check_digits: "07",
...> bank_code: "033",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "1234567890123456"
...> }
...> |> IbanEx.Country.AE.to_string()
"AE 07 033 1234567890123456"
```
"""
@size 23
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
end

42
lib/iban_ex/country/al.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.AL do
@moduledoc """
Albania IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "AL",
...> check_digits: "47",
...> bank_code: "212",
...> branch_code: "1100",
...> national_check: "9",
...> account_number: "0000000235698741"
...> }
...> |> IbanEx.Country.AL.to_string()
"AL 47 212 1100 9 0000000235698741"
```
"""
@size 28
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{4})(?<national_check>[0-9]{1})(?<account_number>[0-9A-Z]{16})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, national_check, account_number]
|> Enum.join(joiner)
end
end

25
lib/iban_ex/country/az.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.AZ do
@moduledoc """
Azerbaijan IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "AZ",
...> check_digits: "21",
...> bank_code: "NABZ",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00000000137010001944"
...> }
...> |> IbanEx.Country.AZ.to_string()
"AZ 21 NABZ 00000000137010001944"
```
"""
@size 28
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9A-Z]{20})$/i
use IbanEx.Country.Template
end

42
lib/iban_ex/country/ba.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.BA do
@moduledoc """
Bosnia and Herzegovina IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "BA",
...> check_digits: "39",
...> bank_code: "129",
...> branch_code: "007",
...> national_check: "94",
...> account_number: "94010284"
...> }
...> |> IbanEx.Country.BA.to_string()
"BA 39 129 007 94010284 94"
```
"""
@size 20
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{3})(?<account_number>[0-9]{8})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|> Enum.join(joiner)
end
end

View File

@ -1,4 +1,6 @@
defmodule IbanEx.Country.BG do
# TODO Bulgaria IBAN contains account type (first 2 digits of account number)
@moduledoc """
Bulgaria IBAN parsing rules

25
lib/iban_ex/country/bh.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.BH do
@moduledoc """
Bahrain IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "BH",
...> check_digits: "67",
...> bank_code: "BMAG",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00001299123456"
...> }
...> |> IbanEx.Country.BH.to_string()
"BH 67 BMAG 00001299123456"
```
"""
@size 22
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{14})$/i
use IbanEx.Country.Template
end

42
lib/iban_ex/country/br.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.BR do
@moduledoc """
Brazil IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "BR",
...> check_digits: "18",
...> bank_code: "00360305",
...> branch_code: "00001",
...> account_number: "0009795493",
...> national_check: "C1"
...> }
...> |> IbanEx.Country.BR.to_string()
"BR 18 00360305 00001 0009795493 C1"
```
"""
@size 29
@rule ~r/^(?<bank_code>[0-9]{8})(?<branch_code>[0-9]{5})(?<account_number>[0-9]{10})(?<national_check>[A-Z]{1}[0-9A-Z]{1})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
account_number: account_number,
national_check: national_check
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.CH do
## Examples
iex> %IbanEx.Iban{country_code: "CH", check_digits: "93", bank_code: "00762", branch_code: nil, national_check: nil, account_number: "011623852957"}
iex> |> IbanEx.Country.CH.to_string()
"CH 93 00762 011623852957"
```elixir
iex> %IbanEx.Iban{
...> country_code: "CH",
...> check_digits: "93",
...> bank_code: "00762",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "011623852957"
...> }
...> |> IbanEx.Country.CH.to_string()
"CH 93 00762 011623852957"
```
"""
@size 21

25
lib/iban_ex/country/cr.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.CR do
@moduledoc """
Costa Rica IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "CR",
...> check_digits: "05",
...> bank_code: "0152",
...> branch_code: nil,
...> account_number: "02001026284066",
...> national_check: nil
...> }
...> |> IbanEx.Country.CR.to_string()
"CR 05 0152 02001026284066"
```
"""
@size 22
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{14})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.CY do
## Examples
iex> %IbanEx.Iban{country_code: "CY", check_digits: "17", bank_code: "002", branch_code: "00128", national_check: nil, account_number: "0000001200527600"}
iex> |> IbanEx.Country.CY.to_string()
"CY 17 002 00128 0000001200527600"
```elixir
iex> %IbanEx.Iban{
...> country_code: "CY",
...> check_digits: "17",
...> bank_code: "002",
...> branch_code: "00128",
...> national_check: nil,
...> account_number: "0000001200527600"
...> }
...> |> IbanEx.Country.CY.to_string()
"CY 17 002 00128 0000001200527600"
```
"""
@size 28

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.CZ do
## Examples
iex> %IbanEx.Iban{country_code: "CZ", check_digits: "65", bank_code: "0800", branch_code: nil, national_check: nil, account_number: "0000192000145399"}
iex> |> IbanEx.Country.CZ.to_string()
"CZ 65 0800 0000192000145399"
```elixir
iex> %IbanEx.Iban{
...> country_code: "CZ",
...> check_digits: "65",
...> bank_code: "0800",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000192000145399"
...> }
...> |> IbanEx.Country.CZ.to_string()
"CZ 65 0800 0000192000145399"
```
"""
@size 24

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.DE do
## Examples
iex> %IbanEx.Iban{country_code: "DE", check_digits: "89", bank_code: "37040044", branch_code: nil, national_check: nil, account_number: "0532013000"}
iex> |> IbanEx.Country.DE.to_string()
"DE 89 37040044 0532013000"
```elixir
iex> %IbanEx.Iban{
...> country_code: "DE",
...> check_digits: "89",
...> bank_code: "37040044",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0532013000"
...> }
...> |> IbanEx.Country.DE.to_string()
"DE 89 37040044 0532013000"
```
"""
@size 22

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.DK do
## Examples
iex> %IbanEx.Iban{country_code: "DK", check_digits: "50", bank_code: "0040", branch_code: nil, national_check: nil, account_number: "0440116243"}
iex> |> IbanEx.Country.DK.to_string()
"DK 50 0040 0440116243"
```elixir
iex> %IbanEx.Iban{
...> country_code: "DK",
...> check_digits: "50",
...> bank_code: "0040",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0440116243"
...> }
...> |> IbanEx.Country.DK.to_string()
"DK 50 0040 0440116243"
```
"""
@size 18
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{10})$/i

25
lib/iban_ex/country/do.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.DO do
@moduledoc """
Dominican Republic IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "DO",
...> check_digits: "28",
...> bank_code: "BAGR",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00000001212453611324"
...> }
...> |> IbanEx.Country.DO.to_string()
"DO 28 BAGR 00000001212453611324"
```
"""
@size 28
@rule ~r/^(?<bank_code>[0-9A-Z]{4})(?<account_number>[0-9]{20})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.EE do
## Examples
iex> %IbanEx.Iban{country_code: "EE", check_digits: "38", bank_code: "22", branch_code: "00", national_check: "5", account_number: "22102014568"}
iex> |> IbanEx.Country.EE.to_string()
"EE 38 22 00 22102014568 5"
```elixir
iex> %IbanEx.Iban{
...> country_code: "EE",
...> check_digits: "38",
...> bank_code: "22",
...> branch_code: "00",
...> national_check: "5",
...> account_number: "22102014568"
...> }
...> |> IbanEx.Country.EE.to_string()
"EE 38 22 00 22102014568 5"
```
"""
@size 20

42
lib/iban_ex/country/eg.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.EG do
@moduledoc """
Egypt IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "EG",
...> check_digits: "38",
...> bank_code: "0019",
...> branch_code: "0005",
...> national_check: nil,
...> account_number: "00000000263180002"
...> }
...> |> IbanEx.Country.EG.to_string()
"EG 38 0019 0005 00000000263180002"
```
"""
@size 29
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9]{17})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.ES do
## Examples
iex> %IbanEx.Iban{country_code: "ES", check_digits: "91", bank_code: "2100", branch_code: "0418", national_check: "45", account_number: "0200051332"}
iex> |> IbanEx.Country.ES.to_string()
"ES 91 2100 0418 45 0200051332"
```elixir
iex> %IbanEx.Iban{
...> country_code: "ES",
...> check_digits: "91",
...> bank_code: "2100",
...> branch_code: "0418",
...> national_check: "45",
...> account_number: "0200051332"
...> }
...> |> IbanEx.Country.ES.to_string()
"ES 91 2100 0418 45 0200051332"
```
"""
@size 24

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.FI do
## Examples
iex> %IbanEx.Iban{country_code: "FI", check_digits: "21", bank_code: "123456", branch_code: nil, national_check: "5", account_number: "0000078"}
iex> |> IbanEx.Country.FI.to_string()
"FI 21 123456 0000078 5"
```elixir
iex> %IbanEx.Iban{
...> country_code: "FI",
...> check_digits: "21",
...> bank_code: "123456",
...> branch_code: nil,
...> national_check: "5",
...> account_number: "0000078"
...> }
...> |> IbanEx.Country.FI.to_string()
"FI 21 123456 0000078 5"
```
"""
@size 18

43
lib/iban_ex/country/fo.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.FO do
@moduledoc """
Faroe Islands IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "FO",
...> check_digits: "62",
...> bank_code: "6460",
...> branch_code: nil,
...> national_check: "4",
...> account_number: "000163163"
...> }
...> |> IbanEx.Country.FO.to_string()
"FO 62 6460 000163163 4"
```
"""
@size 18
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{9})(?<national_check>[0-9]{1})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number, national_check]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.FR do
## Examples
iex> %IbanEx.Iban{country_code: "FR", check_digits: "14", bank_code: "20041", branch_code: "01005", national_check: "06", account_number: "0500013M026"}
iex> |> IbanEx.Country.FR.to_string()
"FR 14 20041 01005 0500013M026 06"
```elixir
iex> %IbanEx.Iban{
...> country_code: "FR",
...> check_digits: "14",
...> bank_code: "20041",
...> branch_code: "01005",
...> national_check: "06",
...> account_number: "0500013M026"
...> }
...> |> IbanEx.Country.FR.to_string()
"FR 14 20041 01005 0500013M026 06"
```
"""
@size 27

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.GB do
## Examples
iex> %IbanEx.Iban{country_code: "GB", check_digits: "29", bank_code: "NWBK", branch_code: "601613", national_check: "06", account_number: "31926819"}
iex> |> IbanEx.Country.GB.to_string()
"GB 29 NWBK 601613 31926819"
```elixir
iex> %IbanEx.Iban{
...> country_code: "GB",
...> check_digits: "29",
...> bank_code: "NWBK",
...> branch_code: "601613",
...> national_check: "06",
...> account_number: "31926819"
...> }
...> |> IbanEx.Country.GB.to_string()
"GB 29 NWBK 601613 31926819"
```
"""
@size 22

25
lib/iban_ex/country/ge.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.GE do
@moduledoc """
Georgia IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "GE",
...> check_digits: "29",
...> bank_code: "NB",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000000101904917"
...> }
...> |> IbanEx.Country.GE.to_string()
"GE 29 NB 0000000101904917"
```
"""
@size 22
@rule ~r/^(?<bank_code>[A-Z]{2})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.GI do
## Examples
iex> %IbanEx.Iban{country_code: "GI", check_digits: "75", bank_code: "NWBK", branch_code: nil, national_check: nil, account_number: "000000007099453"}
iex> |> IbanEx.Country.GI.to_string()
"GI 75 NWBK 000000007099453"
```elixir
iex> %IbanEx.Iban{
...> country_code: "GI",
...> check_digits: "75",
...> bank_code: "NWBK",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "000000007099453"
...> }
...> |> IbanEx.Country.GI.to_string()
"GI 75 NWBK 000000007099453"
```
"""
@size 23

25
lib/iban_ex/country/gl.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.GL do
@moduledoc """
Greenland IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "GL",
...> check_digits: "89",
...> bank_code: "6471",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0001000206"
...> }
...> |> IbanEx.Country.GL.to_string()
"GL 89 6471 0001000206"
```
"""
@size 18
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{10})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.GR do
## Examples
iex> %IbanEx.Iban{country_code: "GR", check_digits: "16", bank_code: "011", branch_code: "0125", national_check: nil, account_number: "0000000012300695"}
iex> |> IbanEx.Country.GR.to_string()
"GR 16 011 0125 0000000012300695"
```elixir
iex> %IbanEx.Iban{
...> country_code: "GR",
...> check_digits: "16",
...> bank_code: "011",
...> branch_code: "0125",
...> national_check: nil,
...> account_number: "0000000012300695"
...> }
...> |> IbanEx.Country.GR.to_string()
"GR 16 011 0125 0000000012300695"
```
"""
@size 27

25
lib/iban_ex/country/gt.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.GT do
@moduledoc """
Guatemala IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "GT",
...> check_digits: "82",
...> bank_code: "TRAJ",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "01020000001210029690"
...> }
...> |> IbanEx.Country.GT.to_string()
"GT 82 TRAJ 01020000001210029690"
```
"""
@size 28
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{20})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.HR do
## Examples
iex> %IbanEx.Iban{country_code: "HR", check_digits: "12", bank_code: "1001005", branch_code: nil, national_check: nil, account_number: "1863000160"}
iex> |> IbanEx.Country.HR.to_string()
"HR 12 1001005 1863000160"
```elixir
iex> %IbanEx.Iban{
...> country_code: "HR",
...> check_digits: "12",
...> bank_code: "1001005",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "1863000160"
...> }
...> |> IbanEx.Country.HR.to_string()
"HR 12 1001005 1863000160"
```
"""
@size 21

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.HU do
## Examples
iex> %IbanEx.Iban{country_code: "HU", check_digits: "42", bank_code: "117", branch_code: "7301", national_check: "0", account_number: "6111110180000000"}
iex> |> IbanEx.Country.HU.to_string()
"HU 42 117 7301 6111110180000000 0"
```elixir
iex> %IbanEx.Iban{
...> country_code: "HU",
...> check_digits: "42",
...> bank_code: "117",
...> branch_code: "7301",
...> national_check: "0",
...> account_number: "6111110180000000"
...> }
...> |> IbanEx.Country.HU.to_string()
"HU 42 117 7301 6111110180000000 0"
```
"""
@size 28

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.IE do
## Examples
iex> %IbanEx.Iban{country_code: "IE", check_digits: "29", bank_code: "AIBK", branch_code: "931152", national_check: nil, account_number: "12345678"}
iex> |> IbanEx.Country.IE.to_string()
"IE 29 AIBK 931152 12345678"
```elixir
iex> %IbanEx.Iban{
...> country_code: "IE",
...> check_digits: "29",
...> bank_code: "AIBK",
...> branch_code: "931152",
...> national_check: nil,
...> account_number: "12345678"
...> }
...> |> IbanEx.Country.IE.to_string()
"IE 29 AIBK 931152 12345678"
```
"""
@size 22

42
lib/iban_ex/country/il.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.IL do
@moduledoc """
Israel IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "IL",
...> check_digits: "62",
...> bank_code: "010",
...> branch_code: "800",
...> national_check: nil,
...> account_number: "0000099999999"
...> }
...> |> IbanEx.Country.IL.to_string()
"IL 62 010 800 0000099999999"
```
"""
@size 23
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{3})(?<account_number>[0-9]{13})$/i
use IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number]
|> Enum.join(joiner)
end
end

45
lib/iban_ex/country/is.ex Normal file
View File

@ -0,0 +1,45 @@
defmodule IbanEx.Country.IS do
# TODO Iceland IBAN contains identification number (last 10 digits of account number)
@moduledoc """
Island IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "IS",
...> check_digits: "14",
...> bank_code: "0159",
...> branch_code: "26",
...> national_check: nil,
...> account_number: "0076545510730339"
...> }
...> |> IbanEx.Country.IS.to_string()
"IS 14 0159 26 0076545510730339"
```
"""
@size 26
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.IT do
## Examples
iex> %IbanEx.Iban{country_code: "IT", check_digits: "60", bank_code: "05428", branch_code: "11101", national_check: "X", account_number: "000000123456"}
iex> |> IbanEx.Country.IT.to_string()
"IT 60 X 05428 11101 000000123456"
```elixir
iex> %IbanEx.Iban{
...> country_code: "IT",
...> check_digits: "60",
...> bank_code: "05428",
...> branch_code: "11101",
...> national_check: "X",
...> account_number: "000000123456"
...> }
...> |> IbanEx.Country.IT.to_string()
"IT 60 X 05428 11101 000000123456"
```
"""
@size 27

42
lib/iban_ex/country/jo.ex Normal file
View File

@ -0,0 +1,42 @@
defmodule IbanEx.Country.JO do
@moduledoc """
Jordan IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "JO",
...> check_digits: "94",
...> bank_code: "CBJO",
...> branch_code: "0010",
...> national_check: nil,
...> account_number: "000000000131000302"
...> }
...> |> IbanEx.Country.JO.to_string()
"JO 94 CBJO 0010 000000000131000302"
```
"""
@size 30
@rule ~r/^(?<bank_code>[A-Z0-9]{4})(?<branch_code>[0-9]{4})(?<account_number>[A-Z0-9]{18})$/i
use IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number]
|> Enum.join(joiner)
end
end

25
lib/iban_ex/country/kw.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.KW do
@moduledoc """
Kuwait IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "KW",
...> check_digits: "81",
...> bank_code: "CBKU",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000000000001234560101"
...> }
...> |> IbanEx.Country.KW.to_string()
"KW 81 CBKU 0000000000001234560101"
```
"""
@size 30
@rule ~r/^(?<bank_code>[A-Z0-9]{4})(?<account_number>[0-9]{22})$/i
use IbanEx.Country.Template
end

25
lib/iban_ex/country/kz.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.KZ do
@moduledoc """
Kazakhstan IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "KZ",
...> check_digits: "86",
...> bank_code: "125",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "KZT5004100100"
...> }
...> |> IbanEx.Country.KZ.to_string()
"KZ 86 125 KZT5004100100"
```
"""
@size 20
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[A-Z0-9]{13})$/i
use IbanEx.Country.Template
end

25
lib/iban_ex/country/lb.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.LB do
@moduledoc """
Lebanon IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "LB",
...> check_digits: "62",
...> bank_code: "0999",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00000001001901229114"
...> }
...> |> IbanEx.Country.LB.to_string()
"LB 62 0999 00000001001901229114"
```
"""
@size 28
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{20})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.LI do
## Examples
iex> %IbanEx.Iban{country_code: "LI", check_digits: "21", bank_code: "08810", branch_code: nil, national_check: nil, account_number: "0002324013AA"}
iex> |> IbanEx.Country.LI.to_string()
"LI 21 08810 0002324013AA"
```elixir
iex> %IbanEx.Iban{
...> country_code: "LI",
...> check_digits: "21",
...> bank_code: "08810",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0002324013AA"
...> }
...> |> IbanEx.Country.LI.to_string()
"LI 21 08810 0002324013AA"
```
"""
@size 21

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.LT do
## Examples
iex> %IbanEx.Iban{country_code: "LT", check_digits: "12", bank_code: "10000", branch_code: nil, national_check: nil, account_number: "11101001000"}
iex> |> IbanEx.Country.LT.to_string()
"LT 12 10000 11101001000"
```elixir
iex> %IbanEx.Iban{
...> country_code: "LT",
...> check_digits: "12",
...> bank_code: "10000",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "11101001000"
...> }
...> |> IbanEx.Country.LT.to_string()
"LT 12 10000 11101001000"
```
"""
@size 20

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.LU do
## Examples
iex> %IbanEx.Iban{country_code: "LU", check_digits: "28", bank_code: "001", branch_code: nil, national_check: nil, account_number: "9400644750000"}
iex> |> IbanEx.Country.LU.to_string()
"LU 28 001 9400644750000"
```elixir
iex> %IbanEx.Iban{
...> country_code: "LU",
...> check_digits: "28",
...> bank_code: "001",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "9400644750000"
...> }
...> |> IbanEx.Country.LU.to_string()
"LU 28 001 9400644750000"
```
"""
@size 20

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.LV do
## Examples
iex> %IbanEx.Iban{country_code: "LV", check_digits: "80", bank_code: "BANK", branch_code: nil, national_check: nil, account_number: "0000435195001"}
iex> |> IbanEx.Country.LV.to_string()
"LV 80 BANK 0000435195001"
```elixir
iex> %IbanEx.Iban{
...> country_code: "LV",
...> check_digits: "80",
...> bank_code: "BANK",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000435195001"
...> }
...> |> IbanEx.Country.LV.to_string()
"LV 80 BANK 0000435195001"
```
"""
@size 21

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.MC do
## Examples
iex> %IbanEx.Iban{country_code: "MC", check_digits: "58", bank_code: "11222", branch_code: "00001", national_check: "30", account_number: "01234567890"}
iex> |> IbanEx.Country.MC.to_string()
"MC 58 11222 00001 01234567890 30"
```elixir
iex> %IbanEx.Iban{
...> country_code: "MC",
...> check_digits: "58",
...> bank_code: "11222",
...> branch_code: "00001",
...> national_check: "30",
...> account_number: "01234567890"
...> }
...> |> IbanEx.Country.MC.to_string()
"MC 58 11222 00001 01234567890 30"
```
"""
@size 27

25
lib/iban_ex/country/md.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.MD do
@moduledoc """
Republic of Moldova IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "MD",
...> check_digits: "24",
...> bank_code: "AG",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "000225100013104168"
...> }
...> |> IbanEx.Country.MD.to_string()
"MD 24 AG 000225100013104168"
```
"""
@size 24
@rule ~r/^(?<bank_code>[A-Z]{2})(?<account_number>[0-9]{18})$/i
use IbanEx.Country.Template
end

43
lib/iban_ex/country/me.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.ME do
@moduledoc """
Montenegro IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "ME",
...> check_digits: "25",
...> bank_code: "505",
...> branch_code: nil,
...> national_check: "51",
...> account_number: "0000123456789"
...> }
...> |> IbanEx.Country.ME.to_string()
"ME 25 505 0000123456789 51"
```
"""
@size 22
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{13})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number, national_check]
|> Enum.join(joiner)
end
end

43
lib/iban_ex/country/mk.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.MK do
@moduledoc """
Macedonia IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "MK",
...> check_digits: "07",
...> bank_code: "250",
...> branch_code: nil,
...> national_check: "84",
...> account_number: "1200000589"
...> }
...> |> IbanEx.Country.MK.to_string()
"MK 07 250 1200000589 84"
```
"""
@size 19
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{10})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number, national_check]
|> Enum.join(joiner)
end
end

43
lib/iban_ex/country/mr.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.MR do
@moduledoc """
Mauritania IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "MR",
...> check_digits: "13",
...> bank_code: "00020",
...> branch_code: "00101",
...> national_check: "53",
...> account_number: "00001234567"
...> }
...> |> IbanEx.Country.MR.to_string()
"MR 13 00020 00101 00001234567 53"
```
"""
@size 27
@rule ~r/^(?<bank_code>[0-9]{5})(?<branch_code>[0-9]{5})(?<account_number>[0-9]{11})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.MT do
## Examples
iex> %IbanEx.Iban{country_code: "MT", check_digits: "84", bank_code: "MALT", branch_code: "01100", national_check: nil, account_number: "0012345MTLCAST001S"}
iex> |> IbanEx.Country.MT.to_string()
"MT 84 MALT 01100 0012345MTLCAST001S"
```elixir
iex> %IbanEx.Iban{
...> country_code: "MT",
...> check_digits: "84",
...> bank_code: "MALT",
...> branch_code: "01100",
...> national_check: nil,
...> account_number: "0012345MTLCAST001S"
...> }
...> |> IbanEx.Country.MT.to_string()
"MT 84 MALT 01100 0012345MTLCAST001S"
```
"""
@size 31

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.NL do
## Examples
iex> %IbanEx.Iban{country_code: "NL", check_digits: "91", bank_code: "ABNA", branch_code: nil, national_check: nil, account_number: "0417164300"}
iex> |> IbanEx.Country.NL.to_string()
"NL 91 ABNA 0417164300"
```elixir
iex> %IbanEx.Iban{
...> country_code: "NL",
...> check_digits: "91",
...> bank_code: "ABNA",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0417164300"
...> }
...> |> IbanEx.Country.NL.to_string()
"NL 91 ABNA 0417164300"
```
"""
@size 18

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.NO do
## Examples
iex> %IbanEx.Iban{country_code: "NO", check_digits: "93", bank_code: "8601", branch_code: nil, national_check: "7", account_number: "111794"}
iex> |> IbanEx.Country.NO.to_string()
"NO 93 8601 111794 7"
```elixir
iex> %IbanEx.Iban{
...> country_code: "NO",
...> check_digits: "93",
...> bank_code: "8601",
...> branch_code: nil,
...> national_check: "7",
...> account_number: "111794"
...> }
...> |> IbanEx.Country.NO.to_string()
"NO 93 8601 111794 7"
```
"""
@size 15

25
lib/iban_ex/country/pk.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.PK do
@moduledoc """
Pakistan IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "PK",
...> check_digits: "36",
...> bank_code: "SCBL",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000001123456702"
...> }
...> |> IbanEx.Country.PK.to_string()
"PK 36 SCBL 0000001123456702"
```
"""
@size 24
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.PL do
## Examples
iex> %IbanEx.Iban{country_code: "PL", check_digits: "61", bank_code: "109", branch_code: "0101", national_check: "4", account_number: "0000071219812874"}
iex> |> IbanEx.Country.PL.to_string()
"PL 61 109 0101 4 0000071219812874"
```elixir
iex> %IbanEx.Iban{
...> country_code: "PL",
...> check_digits: "61",
...> bank_code: "109",
...> branch_code: "0101",
...> national_check: "4",
...> account_number: "0000071219812874"
...> }
...> |> IbanEx.Country.PL.to_string()
"PL 61 109 0101 4 0000071219812874"
```
"""
@size 28

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.PT do
## Examples
iex> %IbanEx.Iban{country_code: "PT", check_digits: "50", bank_code: "0002", branch_code: "0123", national_check: "54", account_number: "12345678901"}
iex> |> IbanEx.Country.PT.to_string()
"PT 50 0002 0123 12345678901 54"
```elixir
iex> %IbanEx.Iban{
...> country_code: "PT",
...> check_digits: "50",
...> bank_code: "0002",
...> branch_code: "0123",
...> national_check: "54",
...> account_number: "12345678901"
...> }
...> |> IbanEx.Country.PT.to_string()
"PT 50 0002 0123 12345678901 54"
```
"""
@size 25

25
lib/iban_ex/country/qa.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.QA do
@moduledoc """
Qatar IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "QA",
...> check_digits: "58",
...> bank_code: "DOHB",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00001234567890ABCDEFG"
...> }
...> |> IbanEx.Country.QA.to_string()
"QA 58 DOHB 00001234567890ABCDEFG"
```
"""
@size 29
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[A-Z0-9]{21})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.RO do
## Examples
iex> %IbanEx.Iban{country_code: "RO", check_digits: "49", bank_code: "AAAA", branch_code: nil, national_check: nil, account_number: "1B31007593840000"}
iex> |> IbanEx.Country.RO.to_string()
"RO 49 AAAA 1B31007593840000"
```elixir
iex> %IbanEx.Iban{
...> country_code: "RO",
...> check_digits: "49",
...> bank_code: "AAAA",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "1B31007593840000"
...> }
...> |> IbanEx.Country.RO.to_string()
"RO 49 AAAA 1B31007593840000"
```
"""
@size 24

43
lib/iban_ex/country/rs.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.RS do
@moduledoc """
Serbia IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "RS",
...> check_digits: "35",
...> bank_code: "260",
...> branch_code: nil,
...> national_check: "79",
...> account_number: "0056010016113"
...> }
...> |> IbanEx.Country.RS.to_string()
"RS 35 260 0056010016113 79"
```
"""
@size 22
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{13})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number, national_check]
|> Enum.join(joiner)
end
end

25
lib/iban_ex/country/sa.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.SA do
@moduledoc """
Saudi Arabia IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "SA",
...> check_digits: "03",
...> bank_code: "80",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "000000608010167519"
...> }
...> |> IbanEx.Country.SA.to_string()
"SA 03 80 000000608010167519"
```
"""
@size 24
@rule ~r/^(?<bank_code>[0-9]{2})(?<account_number>[0-9]{18})$/i
use IbanEx.Country.Template
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.SE do
## Examples
iex> %IbanEx.Iban{country_code: "SE", check_digits: "45", bank_code: "500", branch_code: nil, national_check: nil, account_number: "00000058398257466"}
iex> |> IbanEx.Country.SE.to_string()
"SE 45 500 00000058398257466"
```elixir
iex> %IbanEx.Iban{
...> country_code: "SE",
...> check_digits: "45",
...> bank_code: "500",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00000058398257466"
...> }
...> |> IbanEx.Country.SE.to_string()
"SE 45 500 00000058398257466"
```
"""
@size 24

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.SI do
## Examples
iex> %IbanEx.Iban{country_code: "SI", check_digits: "56", bank_code: "26", branch_code: "330", national_check: "86", account_number: "00120390"}
iex> |> IbanEx.Country.SI.to_string()
"SI 56 26 330 00120390 86"
```elixir
iex> %IbanEx.Iban{
...> country_code: "SI",
...> check_digits: "56",
...> bank_code: "26",
...> branch_code: "330",
...> national_check: "86",
...> account_number: "00120390"
...> }
...> |> IbanEx.Country.SI.to_string()
"SI 56 26 330 00120390 86"
```
"""
@size 19

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.SK do
## Examples
iex> %IbanEx.Iban{country_code: "SK", check_digits: "31", bank_code: "1200", branch_code: nil, national_check: nil, account_number: "0000198742637541"}
iex> |> IbanEx.Country.SK.to_string()
"SK 31 1200 0000198742637541"
```elixir
iex> %IbanEx.Iban{
...> country_code: "SK",
...> check_digits: "31",
...> bank_code: "1200",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000198742637541"
...> }
...> |> IbanEx.Country.SK.to_string()
"SK 31 1200 0000198742637541"
```
"""
@size 24

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.SM do
## Examples
iex> %IbanEx.Iban{country_code: "SM", check_digits: "86", bank_code: "03225", branch_code: "09800", national_check: "U", account_number: "000000270100"}
iex> |> IbanEx.Country.SM.to_string()
"SM 86 U 03225 09800 000000270100"
```elixir
iex> %IbanEx.Iban{
...> country_code: "SM",
...> check_digits: "86",
...> bank_code: "03225",
...> branch_code: "09800",
...> national_check: "U",
...> account_number: "000000270100"
...> }
...> |> IbanEx.Country.SM.to_string()
"SM 86 U 03225 09800 000000270100"
```
"""
@size 27

25
lib/iban_ex/country/sv.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.SV do
@moduledoc """
El Salvador IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "SV",
...> check_digits: "62",
...> bank_code: "CENR",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "00000000000000700025"
...> }
...> |> IbanEx.Country.SV.to_string()
"SV 62 CENR 00000000000000700025"
```
"""
@size 28
@rule ~r/^(?<bank_code>[A-Z0-9]{4})(?<account_number>[0-9]{20})$/i
use IbanEx.Country.Template
end

View File

@ -9,6 +9,10 @@ defmodule IbanEx.Country.Template do
@callback size() :: size()
@callback rule() :: rule()
@callback rules() :: []
@callback rules_map() :: %{}
@callback bban_fields() :: [atom()]
@callback bban_size() :: non_neg_integer()
@callback to_string(Iban.t(), joiner()) :: String.t()
@callback to_string(Iban.t()) :: String.t()
@ -36,13 +40,59 @@ defmodule IbanEx.Country.Template do
end
@impl IbanEx.Country.Template
@spec size() :: Integer.t()
@spec size() :: integer()
def size(), do: @size
@doc """
Return Regex for parsing complete BBAN (part of IBAN string)
"""
@impl IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@impl IbanEx.Country.Template
@spec bban_size() :: integer()
def bban_size() do
{_rules, bban_size} = calculate_rules()
bban_size
end
@impl IbanEx.Country.Template
@spec bban_fields() :: []
def bban_fields(), do: rules_map() |> Map.keys()
@impl IbanEx.Country.Template
@spec rules_map() :: %{}
def rules_map(), do: rules() |> Map.new()
@impl IbanEx.Country.Template
@spec rules() :: []
def rules() do
{rules, _bban_size} = calculate_rules()
rules
end
defp calculate_rules() do
scanner = ~r/\(\?\<([\w_]+)\>(([^{]+)\{(\d+)\})\)/i
source =
@rule
|> Regex.source()
{list, bban_length} =
Regex.scan(scanner, source)
|> Enum.reduce({[], 0}, fn [_part, k, r, _syms, l], {list, position} = acc ->
key = String.to_atom(k)
{:ok, regex} = Regex.compile(r, "i")
length = String.to_integer(l)
left = position
right = left + length - 1
{[{key, %{regex: regex, range: left..right}} | list], right + 1}
end)
{Enum.reverse(list), bban_length}
end
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
end
end

43
lib/iban_ex/country/tl.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.TL do
@moduledoc """
Timor-Leste IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "TL",
...> check_digits: "38",
...> bank_code: "008",
...> branch_code: nil,
...> national_check: "57",
...> account_number: "00123456789101"
...> }
...> |> IbanEx.Country.TL.to_string()
"TL 38 008 00123456789101 57"
```
"""
@size 23
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{14})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number, national_check]
|> Enum.join(joiner)
end
end

43
lib/iban_ex/country/tr.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.TR do
@moduledoc """
Turkey IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "TR",
...> check_digits: "33",
...> bank_code: "00061",
...> branch_code: nil,
...> national_check: "0",
...> account_number: "0519786457841326"
...> }
...> |> IbanEx.Country.TR.to_string()
"TR 33 00061 0 0519786457841326"
```
"""
@size 26
@rule ~r/^(?<bank_code>[A-Z0-9]{5})(?<national_check>[0-9]{1})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, national_check, account_number]
|> Enum.join(joiner)
end
end

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.UA do
## Examples
iex> %IbanEx.Iban{country_code: "UA", check_digits: "21", bank_code: "322313", branch_code: nil, national_check: nil, account_number: "0000026007233566001"}
iex> |> IbanEx.Country.UA.to_string()
"UA 21 322313 0000026007233566001"
```elixir
iex> %IbanEx.Iban{
...> country_code: "UA",
...> check_digits: "21",
...> bank_code: "322313",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000026007233566001"
...> }
...> |> IbanEx.Country.UA.to_string()
"UA 21 322313 0000026007233566001"
```
"""
@size 29

View File

@ -4,10 +4,18 @@ defmodule IbanEx.Country.VA do
## Examples
iex> %IbanEx.Iban{country_code: "VA", check_digits: "59", bank_code: "001", branch_code: nil, national_check: nil, account_number: "123000012345678"}
iex> |> IbanEx.Country.VA.to_string()
"VA 59 001 123000012345678"
```elixir
iex> %IbanEx.Iban{
...> country_code: "VA",
...> check_digits: "59",
...> bank_code: "001",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "123000012345678"
...> }
...> |> IbanEx.Country.VA.to_string()
"VA 59 001 123000012345678"
```
"""
@size 22

25
lib/iban_ex/country/vg.ex Normal file
View File

@ -0,0 +1,25 @@
defmodule IbanEx.Country.VG do
@moduledoc """
British Virgin Islands IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "VG",
...> check_digits: "96",
...> bank_code: "VPVG",
...> branch_code: nil,
...> national_check: nil,
...> account_number: "0000012345678901"
...> }
...> |> IbanEx.Country.VG.to_string()
"VG 96 VPVG 0000012345678901"
```
"""
@size 24
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{16})$/i
use IbanEx.Country.Template
end

43
lib/iban_ex/country/xk.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule IbanEx.Country.XK do
@moduledoc """
Kosovo IBAN parsing rules
## Examples
```elixir
iex> %IbanEx.Iban{
...> country_code: "XK",
...> check_digits: "05",
...> bank_code: "12",
...> branch_code: "12",
...> national_check: "06",
...> account_number: "0123456789"
...> }
...> |> IbanEx.Country.XK.to_string()
"XK 05 12 12 0123456789 06"
```
"""
@size 20
@rule ~r/^(?<bank_code>[0-9]{2})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{10})(?<national_check>[0-9]{2})$/i
use IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: branch_code,
national_check: national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|> Enum.join(joiner)
end
end

View File

@ -1,14 +1,27 @@
defprotocol IbanEx.Deserialize do
@type iban_or_error() :: IbanEx.Iban.t() | {:error, :can_not_parse_map | atom()}
@type iban() :: IbanEx.Iban.t()
@type iban_or_error() ::
iban()
| {:invalid_checksum, binary()}
| {:invalid_format, binary()}
| {:invalid_length, binary()}
| {:can_not_parse_map, binary()}
| {:unsupported_country_code, binary()}
@spec to_iban(t()) :: iban_or_error()
def to_iban(value)
end
defimpl IbanEx.Deserialize, for: [BitString, String] do
alias IbanEx.{Parser, Error}
@type iban_or_error() :: IbanEx.Iban.t() | {:error, atom()}
@spec to_iban(binary()) :: iban_or_error()
@spec to_iban(String.t()) :: iban_or_error()
@type iban() :: IbanEx.Iban.t()
@type iban_or_error() ::
iban()
| {:invalid_checksum, binary()}
| {:invalid_format, binary()}
| {:invalid_length, binary()}
| {:can_not_parse_map, binary()}
| {:unsupported_country_code, binary()}
def to_iban(string) do
case Parser.parse(string) do
{:ok, iban} -> iban
@ -18,10 +31,17 @@ defimpl IbanEx.Deserialize, for: [BitString, String] do
end
defimpl IbanEx.Deserialize, for: Map do
alias IbanEx.Iban
@type iban_or_error() :: IbanEx.Iban.t() | {:error, :can_not_parse_map}
alias IbanEx.{Iban, Error}
@type iban() :: IbanEx.Iban.t()
@type iban_or_error() ::
iban()
| {:invalid_checksum, binary()}
| {:invalid_format, binary()}
| {:invalid_length, binary()}
| {:can_not_parse_map, binary()}
| {:unsupported_country_code, binary()}
@spec to_iban(map()) :: iban_or_error()
def to_iban(
%{
country_code: _country_code,
@ -49,13 +69,10 @@ defimpl IbanEx.Deserialize, for: Map do
to_iban(atomized_map)
end
def to_iban(map) when is_map(map), do: {:error, :can_not_parse_map}
def to_iban(map) when is_map(map), do: {:can_not_parse_map, Error.message(:can_not_parse_map)}
end
defimpl IbanEx.Deserialize, for: List do
alias IbanEx.Iban
@type iban_or_error() :: IbanEx.Iban.t() | {:error, :can_not_parse_map}
@spec to_iban(list()) :: iban_or_error()
def to_iban(list), do: struct(Iban, Map.new(list))
end

View File

@ -9,6 +9,10 @@ defmodule IbanEx.Error do
| :can_not_parse_map
| :length_to_long
| :length_to_short
| :invalid_bank_code
| :invalid_account_number
| :invalid_branch_code
| :invalid_national_check
| atom()
@type errors() :: [error()]
@errors [
@ -18,7 +22,11 @@ defmodule IbanEx.Error do
:invalid_checksum,
:can_not_parse_map,
:length_to_long,
:length_to_short
:length_to_short,
:invalid_bank_code,
:invalid_account_number,
:invalid_branch_code,
:invalid_national_check
]
@messages [
@ -28,8 +36,12 @@ defmodule IbanEx.Error do
invalid_checksum: "IBAN's checksum is invalid",
can_not_parse_map: "Can't parse map to IBAN struct",
length_to_long: "IBAN longer then required length",
length_to_short: "IBAN shorter then required length"
]
length_to_short: "IBAN shorter then required length",
invalid_bank_code: "Bank code violates required format",
invalid_account_number: "Account number violates required format",
invalid_branch_code: "Branch code violates required format",
invalid_national_check: "National check symbols violates required format",
]
@spec message(error()) :: String.t()
def message(error) when error in @errors, do: @messages[error]

View File

@ -7,12 +7,22 @@ defmodule IbanEx.Parser do
@type iban_string() :: String.t()
@type country_code_string() :: <<_::16>>
@type check_digits_string() :: <<_::16>>
@type iban_or_error() :: IbanEx.Iban.t() | {:error, atom()}
@spec parse({:ok, String.t()} | String.t()) :: iban_or_error()
@type iban() :: IbanEx.Iban.t()
@type iban_or_error() ::
{:ok, iban()}
| {:invalid_checksum, binary()}
| {:invalid_format, binary()}
| {:invalid_length, binary()}
| {:can_not_parse_map, binary()}
| {:unsupported_country_code, binary()}
@spec parse({:ok, binary()}) :: iban_or_error()
def parse({:ok, iban_string}), do: parse(iban_string)
def parse(iban_string) do
def parse(iban_string, options \\ [incomplete: false])
def parse(iban_string, incomplete: false) do
case Validator.validate(iban_string) do
{:ok, valid_iban} ->
iban_map = %{
@ -32,12 +42,69 @@ defmodule IbanEx.Parser do
end
end
def parse(iban_string, incomplete: true) do
iban_map = %{
country_code: country_code(iban_string),
check_digits: check_digits(iban_string)
}
bban = bban(iban_string)
case Country.is_country_code_supported?(iban_map.country_code) do
true ->
result =
parse_bban(bban, iban_map.country_code, incomplete: true)
|> Map.merge(iban_map)
{:ok, struct(Iban, result)}
false ->
{:error, :unsupported_country_code}
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),
def parse_bban(bban_string, country_code, options \\ [incomplete: false])
def parse_bban(bban_string, country_code, incomplete: true) do
case Country.is_country_code_supported?(country_code) do
true ->
country_code
|> Country.country_module()
|> parse_bban_by_country_rules(bban_string)
false ->
%{}
end
end
def parse_bban(bban_string, country_code, incomplete: false) do
case Country.is_country_code_supported?(country_code) do
true ->
Country.country_module(country_code).rule()
|> parse_bban_by_regex(bban_string)
false ->
%{}
end
end
defp parse_bban_by_country_rules(country_module, bban_string) do
for {field, rule} <- country_module.rules,
into: %{},
do: {String.to_atom(key), val}
do: {field, normalize_and_slice(bban_string, rule.range)}
end
defp parse_bban_by_regex(_regex, nil), do: %{}
defp parse_bban_by_regex(regex, bban_string) do
case Regex.named_captures(regex, bban_string) do
map when is_map(map) ->
for {key, val} <- map,
into: %{},
do: {String.to_atom(key), val}
nil ->
%{}
end
end
@spec country_code(iban_string()) :: country_code_string()

View File

@ -6,6 +6,6 @@ defmodule IbanEx.Serialize do
@spec to_string(Iban.t()) :: String.t()
def to_string(iban), do: Formatter.format(iban)
@spec to_map(Iban.t()) :: Map.t()
@spec to_map(Iban.t()) :: map()
def to_map(iban), do: Map.from_struct(iban)
end

View File

@ -3,10 +3,11 @@ defmodule IbanEx.Validator do
alias IbanEx.{Country, Parser}
alias IbanEx.Validator.Replacements
import IbanEx.Commons, only: [normalize: 1]
import IbanEx.Commons, only: [normalize: 1, normalize_and_slice: 2]
defp error_accumulator(acc, error_message)
defp error_accumulator(acc, {:error, error}), do: [error | acc]
# defp error_accumulator(acc, list) when is_list(list), do: list ++ acc
defp error_accumulator(acc, _), do: acc
defp violation_functions(),
@ -14,13 +15,26 @@ defmodule IbanEx.Validator do
{&__MODULE__.iban_violates_format?/1, {:error, :invalid_format}},
{&__MODULE__.iban_unsupported_country?/1, {:error, :unsupported_country_code}},
{&__MODULE__.iban_violates_length?/1, {:error, :invalid_length}},
{&__MODULE__.iban_violates_country_rule?/1, {:error, :invalid_format}},
{&__MODULE__.iban_violates_checksum?/1, {:error, :invalid_checksum}}
{&__MODULE__.iban_violates_country_rule?/1, {:error, :invalid_format_for_country}},
{&__MODULE__.iban_violates_bank_code_format?/1, {:error, :invalid_bank_code}},
{&__MODULE__.iban_violates_account_number_format?/1, {:error, :invalid_account_number}},
{&__MODULE__.iban_violates_branch_code_format?/1, {:error, :invalid_branch_code}},
{&__MODULE__.iban_violates_national_check_format?/1, {:error, :invalid_national_check}},
{&__MODULE__.iban_violates_checksum?/1, {:error, :invalid_checksum}},
]
@doc """
Accumulate check results in the list of errors
Check iban_violates_format?, iban_unsupported_country?, iban_violates_length?, iban_violates_country_rule?, iban_violates_checksum?
Check
iban_violates_format?,
iban_unsupported_country?,
iban_violates_length?,
iban_violates_country_rule?,
iban_violates_bank_code_format?,
iban_violates_account_number_format?
iban_violates_branch_code_format?,
iban_violates_national_check_format?,
iban_violates_checksum?,
"""
@spec violations(String.t()) :: [] | [atom()]
def violations(iban) do
@ -36,16 +50,35 @@ defmodule IbanEx.Validator do
iban_unsupported_country?,
iban_violates_length?,
iban_violates_country_rule?,
iban_violates_checksum?
iban_violates_bank_code_format?,
iban_violates_account_number_format?,
iban_violates_branch_code_format?,
iban_violates_national_check_format?,
iban_violates_checksum?,
"""
@spec validate(String.t()) :: {:ok, String.t()} | {:error, Atom.t()}
@type iban() :: binary()
@type iban_or_error() ::
{:ok, iban()}
| {:invalid_checksum, binary()}
| {:invalid_format, binary()}
| {:invalid_length, binary()}
| {:unsupported_country_code, binary()}
| {:invalid_bank_code, binary()}
| {:invalid_account_number, binary()}
| {:invalid_branch_code, binary()}
| {:invalid_national_check, binary()}
@spec validate(String.t()) :: {:ok, String.t()} | {:error, atom()}
def validate(iban) do
cond do
iban_violates_format?(iban) -> {:error, :invalid_format}
iban_unsupported_country?(iban) -> {:error, :unsupported_country_code}
iban_violates_length?(iban) -> {:error, :invalid_length}
iban_violates_country_rule?(iban) -> {:error, :invalid_format}
iban_violates_country_rule?(iban) -> {:error, :invalid_format_for_country}
iban_violates_bank_code_format?(iban) -> {:error, :invalid_bank_code}
iban_violates_account_number_format?(iban) -> {:error, :invalid_account_number}
iban_violates_branch_code_format?(iban) -> {:error, :invalid_branch_code}
iban_violates_national_check_format?(iban) -> {:error, :invalid_national_check}
iban_violates_checksum?(iban) -> {:error, :invalid_checksum}
true -> {:ok, normalize(iban)}
end
@ -63,6 +96,34 @@ defmodule IbanEx.Validator do
def iban_violates_format?(iban),
do: Regex.match?(~r/[^A-Z0-9]/i, normalize(iban))
# - Check whether a given IBAN violates the required format in bank_code.
@spec iban_violates_bank_code_format?(binary()) :: boolean
def iban_violates_bank_code_format?(iban), do: iban_violates_bban_part_format?(iban, :bank_code)
# - Check whether a given IBAN violates the required format in branch_code.
@spec iban_violates_branch_code_format?(binary()) :: boolean
def iban_violates_branch_code_format?(iban), do: iban_violates_bban_part_format?(iban, :branch_code)
# - Check whether a given IBAN violates the required format in account_number.
@spec iban_violates_account_number_format?(binary()) :: boolean
def iban_violates_account_number_format?(iban), do: iban_violates_bban_part_format?(iban, :account_number)
# - Check whether a given IBAN violates the required format in national_check.
@spec iban_violates_national_check_format?(binary()) :: boolean
def iban_violates_national_check_format?(iban), do: iban_violates_bban_part_format?(iban, :national_check)
defp iban_violates_bban_part_format?(iban, part) do
with country <- Parser.country_code(iban),
bban <- Parser.bban(iban),
true <- Country.is_country_code_supported?(country),
country_module <- Country.country_module(country),
{:ok, rule} <- Map.fetch(country_module.rules_map(), part) do
!Regex.match?(rule.regex, normalize_and_slice(bban, rule.range))
else
_ -> false
end
end
# - Check whether a given IBAN violates the supported countries.
@spec iban_unsupported_country?(String.t()) :: boolean
def iban_unsupported_country?(iban) do
@ -86,21 +147,23 @@ defmodule IbanEx.Validator do
end
@doc "Check length of IBAN"
@spec check_iban_length(String.t()) :: {:error, :length_to_short | :length_to_long } | :ok
@spec check_iban_length(String.t()) :: {:error, :length_to_short | :length_to_long} | :ok
def check_iban_length(iban) do
unless iban_unsupported_country?(iban) do
country_module =
iban
|> Parser.country_code()
|> Country.country_module()
case iban_unsupported_country?(iban) do
true ->
{:error, :unsupported_country_code}
case country_module.size() - size(iban) do
diff when diff > 0 -> {:error, :length_to_short}
diff when diff < 0 -> {:error, :length_to_long}
0 -> :ok
end
else
{:error, :unsupported_country_code}
false ->
country_module =
iban
|> Parser.country_code()
|> Country.country_module()
case country_module.size() - size(iban) do
diff when diff > 0 -> {:error, :length_to_short}
diff when diff < 0 -> {:error, :length_to_long}
0 -> :ok
end
end
end
@ -113,7 +176,7 @@ defmodule IbanEx.Validator do
rule <- country_module.rule() do
!Regex.match?(rule, bban)
else
{:error, _error} -> true
_ -> true
end
end

View File

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

View File

@ -0,0 +1,172 @@
defmodule IbanExParserTest do
alias IbanEx.{Country, Iban, Parser}
use ExUnit.Case, async: true
@ibans [
"AL47212110090000000235698741",
"AD1200012030200359100100",
"AT611904300234573201",
"AZ21NABZ00000000137010001944",
"BH67BMAG00001299123456",
"BE68539007547034",
"BA391290079401028494",
"BR1800360305000010009795493C1",
"BG80BNBG96611020345678",
"CR05015202001026284066",
"HR1210010051863000160",
"CY17002001280000001200527600",
"CZ6508000000192000145399",
"DK5000400440116243",
"DO28BAGR00000001212453611324",
"EG380019000500000000263180002",
"SV62CENR00000000000000700025",
"EE382200221020145685",
"FO6264600001631634",
"FI2112345600000785",
"FR1420041010050500013M02606",
"GE29NB0000000101904917",
"DE89370400440532013000",
"GI75NWBK000000007099453",
"GR1601101250000000012300695",
"GL8964710001000206",
"GT82TRAJ01020000001210029690",
"HU42117730161111101800000000",
"IS140159260076545510730339",
"IE29AIBK93115212345678",
"IL620108000000099999999",
"IT60X0542811101000000123456",
"JO94CBJO0010000000000131000302",
"KZ86125KZT5004100100",
"XK051212012345678906",
"KW81CBKU0000000000001234560101",
"LV80BANK0000435195001",
"LB62099900000001001901229114",
"LI21088100002324013AA",
"LT121000011101001000",
"LU280019400644750000",
"MK07250120000058984",
"MT84MALT011000012345MTLCAST001S",
"MR1300020001010000123456753",
"MC5811222000010123456789030",
"ME25505000012345678951",
"NL91ABNA0417164300",
"NO9386011117947",
"PK36SCBL0000001123456702",
"PL61109010140000071219812874",
"PT50000201231234567890154",
"QA58DOHB00001234567890ABCDEFG",
"MD24AG000225100013104168",
"RO49AAAA1B31007593840000",
"SM86U0322509800000000270100",
"SA0380000000608010167519",
"RS35260005601001611379",
"SK3112000000198742637541",
"SI56263300012039086",
"ES9121000418450200051332",
"SE4550000000058398257466",
"CH9300762011623852957",
"TL380080012345678910157",
"TR330006100519786457841326",
"UA213223130000026007233566001",
"AE070331234567890123456",
"GB29NWBK60161331926819",
"VA59001123000012345678",
"VG96VPVG0000012345678901"
]
test "parsing valid IBANs from available countries returns {:ok, %IbanEx.Iban{}}" do
Enum.all?(@ibans, fn iban ->
iban_country =
iban
|> String.upcase()
|> String.slice(0..1)
result =
case {Country.is_country_code_supported?(iban_country), Parser.parse(iban)} do
{true, {:ok, %Iban{}}} ->
true
_ ->
false
end
assert(result, iban)
end)
end
test "parsing invalid IBANs from unavailable countries returns {:error, :unsupported_country_code}" do
invalid_ibans =
[
# Fake country codes
"SD3112000000198742637541",
"SU56263300012039086",
"ZZ9121000418450200051332",
"FU4550000000058398257466",
"GF9300762011623852957",
"FX380080012345678910157",
"RT330006100519786457841326",
"UL213223130000026007233566001",
"AP070331234567890123456",
"FF29NWBK60161331926819",
"VV59001123000012345678",
"GV96VPVG0000012345678901",
# Unsupported now by library
"AA0096VPVG0000012345",
"AO213223130000026",
"AX00213223130000026007",
"BF3112000000198742637541375",
"BI31120000001987",
"BJ31120000001987426375413750",
"BL3112000000198742637541375",
"BY31120000001987426375413754",
"CF3112000000198742637541375",
"CG3112000000198742637541375",
"CI31120000001987426375413750",
"CM3112000000198742637541375",
"CV31120000001987426375413",
"DJ3112000000198742637541375",
"DZ3112000000198742637541",
"GA3112000000198742637541375",
"GF3112000000198742637541375",
"GP3112000000198742637541375",
"GQ3112000000198742637541375",
"GW31120000001987426375413",
"HN31120000001987426375413759",
"IQ311200000019874263754",
"IR311200000019874263754137",
"KM3112000000198742637541375",
"LC311200000019874263754",
"MA31120000001987426375413750",
"MF3112000000198742637541375",
"MG3112000000198742637541375",
"ML31120000001987426375413750",
"MQ3112000000198742637541375",
"MU3112000000198742637541375000",
"MZ31120000001987426375413",
"NC3112000000198742637541375",
"NE31120000001987426375413750",
"NI311200000019874263754137500000",
"PF3112000000198742637541375",
"PM3112000000198742637541375",
"PS311200000019874263754137500",
"RE3112000000198742637541375",
"SC311200000019874263754137500000",
"SN31120000001987426375413750",
"ST31120000001987426375413",
"TD3112000000198742637541375",
"TF3112000000198742637541375",
"TG31120000001987426375413750",
"TN3112000000198742637541",
"WF3112000000198742637541375",
"YT3112000000198742637541375"
]
Enum.all?(
invalid_ibans,
&assert(
match?({:error, :unsupported_country_code}, Parser.parse(&1)),
"expected #{&1} to match {:error, :unsupported_country_code}"
)
)
end
end

View File

@ -1,130 +1,74 @@
defmodule IbanExTest do
alias IbanEx.{Country, Iban, Parser}
use ExUnit.Case, async: true
doctest_file "README.md"
doctest IbanEx.Country.AD
doctest IbanEx.Country.AE
doctest IbanEx.Country.AL
doctest IbanEx.Country.AT
doctest IbanEx.Country.AZ
doctest IbanEx.Country.BA
doctest IbanEx.Country.BE
doctest IbanEx.Country.BG
doctest IbanEx.Country.BH
doctest IbanEx.Country.BR
doctest IbanEx.Country.CH
doctest IbanEx.Country.CR
doctest IbanEx.Country.CY
doctest IbanEx.Country.CZ
doctest IbanEx.Country.DE
doctest IbanEx.Country.DK
doctest IbanEx.Country.DO
doctest IbanEx.Country.EE
doctest IbanEx.Country.EG
doctest IbanEx.Country.ES
doctest IbanEx.Country.FI
doctest IbanEx.Country.FO
doctest IbanEx.Country.FR
doctest IbanEx.Country.GB
doctest IbanEx.Country.GE
doctest IbanEx.Country.GI
doctest IbanEx.Country.GL
doctest IbanEx.Country.GR
doctest IbanEx.Country.GT
doctest IbanEx.Country.JO
doctest IbanEx.Country.HR
doctest IbanEx.Country.HU
doctest IbanEx.Country.IE
doctest IbanEx.Country.IL
doctest IbanEx.Country.IT
doctest IbanEx.Country.IS
doctest IbanEx.Country.KZ
doctest IbanEx.Country.KW
doctest IbanEx.Country.LB
doctest IbanEx.Country.LI
doctest IbanEx.Country.LT
doctest IbanEx.Country.LU
doctest IbanEx.Country.LV
doctest IbanEx.Country.MC
doctest IbanEx.Country.MD
doctest IbanEx.Country.ME
doctest IbanEx.Country.MK
doctest IbanEx.Country.MR
doctest IbanEx.Country.MT
doctest IbanEx.Country.NL
doctest IbanEx.Country.NO
doctest IbanEx.Country.PK
doctest IbanEx.Country.PL
doctest IbanEx.Country.PT
doctest IbanEx.Country.QA
doctest IbanEx.Country.RO
doctest IbanEx.Country.RS
doctest IbanEx.Country.SA
doctest IbanEx.Country.SE
doctest IbanEx.Country.SM
doctest IbanEx.Country.SI
doctest IbanEx.Country.SK
doctest IbanEx.Country.SM
doctest IbanEx.Country.SV
doctest IbanEx.Country.TL
doctest IbanEx.Country.TR
doctest IbanEx.Country.UA
doctest IbanEx.Country.VA
@ibans [
"AL47212110090000000235698741",
"AD1200012030200359100100",
"AT611904300234573201",
"AZ21NABZ00000000137010001944",
"BH67BMAG00001299123456",
"BE68539007547034",
"BA391290079401028494",
"BR1800360305000010009795493C1",
"BG80BNBG96611020345678",
"CR05015202001026284066",
"HR1210010051863000160",
"CY17002001280000001200527600",
"CZ6508000000192000145399",
"DK5000400440116243",
"DO28BAGR00000001212453611324",
"EG380019000500000000263180002",
"SV62CENR00000000000000700025",
"EE382200221020145685",
"FO6264600001631634",
"FI2112345600000785",
"FR1420041010050500013M02606",
"GE29NB0000000101904917",
"DE89370400440532013000",
"GI75NWBK000000007099453",
"GR1601101250000000012300695",
"GL8964710001000206",
"GT82TRAJ01020000001210029690",
"HU42117730161111101800000000",
"IS140159260076545510730339",
"IE29AIBK93115212345678",
"IL620108000000099999999",
"IT60X0542811101000000123456",
"JO94CBJO0010000000000131000302",
"KZ86125KZT5004100100",
"XK051212012345678906",
"KW81CBKU0000000000001234560101",
"LV80BANK0000435195001",
"LB62099900000001001901229114",
"LI21088100002324013AA",
"LT121000011101001000",
"LU280019400644750000",
"MK07250120000058984",
"MT84MALT011000012345MTLCAST001S",
"MR1300020001010000123456753",
"MC5811222000010123456789030",
"ME25505000012345678951",
"NL91ABNA0417164300",
"NO9386011117947",
"PK36SCBL0000001123456702",
"PL61109010140000071219812874",
"PT50000201231234567890154",
"QA58DOHB00001234567890ABCDEFG",
"MD24AG000225100013104168",
"RO49AAAA1B31007593840000",
"SM86U0322509800000000270100",
"SA0380000000608010167519",
"RS35260005601001611379",
"SK3112000000198742637541",
"SI56263300012039086",
"ES9121000418450200051332",
"SE4550000000058398257466",
"CH9300762011623852957",
"TL380080012345678910157",
"TR330006100519786457841326",
"UA213223130000026007233566001",
"AE070331234567890123456",
"GB29NWBK60161331926819",
"VA59001123000012345678",
"VG96VPVG0000012345678901"
]
test "parsing valid IBANs from available countries returns {:ok, %IbanEx.Iban{}}" do
assert Enum.all?(@ibans, fn iban ->
iban_country = iban |> String.upcase() |> String.slice(0..1)
case {Country.is_country_code_supported?(iban_country), Parser.parse(iban)} do
{true, {:ok, %Iban{}}} ->
true
{false, {:error, :unsupported_country_code}} ->
true
_ ->
false
end
end)
end
doctest IbanEx.Country.VG
doctest IbanEx.Country.XK
end

View File

@ -2,7 +2,121 @@ defmodule IbanExValidatorTest do
alias IbanEx.{Validator}
use ExUnit.Case, async: true
test "check IBANs length" do
@ibans [
"AL47212110090000000235698741",
"AD1200012030200359100100",
"AT611904300234573201",
"AZ21NABZ00000000137010001944",
"BH67BMAG00001299123456",
"BE68539007547034",
"BA391290079401028494",
"BR1800360305000010009795493C1",
"BG80BNBG96611020345678",
"CR05015202001026284066",
"HR1210010051863000160",
"CY17002001280000001200527600",
"CZ6508000000192000145399",
"DK5000400440116243",
"DO28BAGR00000001212453611324",
"EG380019000500000000263180002",
"SV62CENR00000000000000700025",
"EE382200221020145685",
"FO6264600001631634",
"FI2112345600000785",
"FR1420041010050500013M02606",
"GE29NB0000000101904917",
"DE89370400440532013000",
"GI75NWBK000000007099453",
"GR1601101250000000012300695",
"GL8964710001000206",
"GT82TRAJ01020000001210029690",
"HU42117730161111101800000000",
"IS140159260076545510730339",
"IE29AIBK93115212345678",
"IL620108000000099999999",
"IT60X0542811101000000123456",
"JO94CBJO0010000000000131000302",
"KZ86125KZT5004100100",
"XK051212012345678906",
"KW81CBKU0000000000001234560101",
"LV80BANK0000435195001",
"LB62099900000001001901229114",
"LI21088100002324013AA",
"LT121000011101001000",
"LU280019400644750000",
"MK07250120000058984",
"MT84MALT011000012345MTLCAST001S",
"MR1300020001010000123456753",
"MC5811222000010123456789030",
"ME25505000012345678951",
"NL91ABNA0417164300",
"NO9386011117947",
"PK36SCBL0000001123456702",
"PL61109010140000071219812874",
"PT50000201231234567890154",
"QA58DOHB00001234567890ABCDEFG",
"MD24AG000225100013104168",
"RO49AAAA1B31007593840000",
"SM86U0322509800000000270100",
"SA0380000000608010167519",
"RS35260005601001611379",
"SK3112000000198742637541",
"SI56263300012039086",
"ES9121000418450200051332",
"SE4550000000058398257466",
"CH9300762011623852957",
"TL380080012345678910157",
"TR330006100519786457841326",
"UA213223130000026007233566001",
"AE070331234567890123456",
"GB29NWBK60161331926819",
"VA59001123000012345678",
"VG96VPVG0000012345678901"
]
test "Check Account number format positive cases" do
Enum.all?(@ibans, &assert(!Validator.iban_violates_account_number_format?(&1), &1))
end
test "Check National check format positive cases" do
Enum.all?(@ibans, &assert(!Validator.iban_violates_national_check_format?(&1), &1))
end
test "Check Branch code format positive cases" do
Enum.all?(@ibans, &assert(!Validator.iban_violates_branch_code_format?(&1), &1))
end
test "Check Bank code format positive cases" do
Enum.all?(@ibans, &assert(!Validator.iban_violates_bank_code_format?(&1), &1))
end
test "Check Account number format negative cases" do
cases = [
# shorter then need
{"AL4721211009000000023568741", true},
{"AD120001203020035900100", true},
{"AZ21NABZ0000000013701000944", true},
# invalid characters (leters) in number
{"AT6119043002A4573201", true},
{"BH67BMAG000012991A3456", true},
{"BE685390075X7034", true},
{"BA391290079401S28494", true},
{"BR180036030500001000979549CC1", true},
{"HR12100100518630001", true},
# shorter then need and has
# invalid characters (leters) in number
{"BR18003603050000100097CC1", true},
{"CR050152020010262806Ї", true},
# FIXME it is invalid IBAN for Bulgaria — need to change a rules function in Country Template module
# {"BG80BNBG9661102034567Ї", true},
]
Enum.all?(cases, fn {iban, result} ->
assert(Validator.iban_violates_account_number_format?(iban) == result, iban)
end)
end
test "Check IBANs length" do
cases = [
{"FG2112345CC6000007", {:error, :unsupported_country_code}},
{"UK2112345CC6000007", {:error, :unsupported_country_code}},