Compare commits
2 Commits
858439713a
...
763e1dba0c
| Author | SHA1 | Date | |
|---|---|---|---|
| 763e1dba0c | |||
| d197a86454 |
@@ -15,6 +15,7 @@ In just a few letters and numbers, the IBAN captures all of the country, bank, a
|
||||
```elixir
|
||||
iex> "FI2112345600000785" |> IbanEx.Parser.parse()
|
||||
{:ok, %IbanEx.Iban{
|
||||
iban: "FI2112345600000785",
|
||||
country_code: "FI",
|
||||
check_digits: "21",
|
||||
bank_code: "123456",
|
||||
|
||||
@@ -19,6 +19,7 @@ def normalize_and_slice(string, range) do
|
||||
string
|
||||
|> normalize()
|
||||
|> String.slice(range)
|
||||
|
||||
# |> case do
|
||||
# "" -> nil
|
||||
# result -> result
|
||||
|
||||
@@ -11,71 +11,107 @@ defmodule IbanEx.Country do
|
||||
"AE" => IbanEx.Country.AE,
|
||||
"AL" => IbanEx.Country.AL,
|
||||
"AT" => IbanEx.Country.AT,
|
||||
"AX" => IbanEx.Country.FI,
|
||||
"AZ" => IbanEx.Country.AZ,
|
||||
"BA" => IbanEx.Country.BA,
|
||||
"BE" => IbanEx.Country.BE,
|
||||
"BG" => IbanEx.Country.BG,
|
||||
"BH" => IbanEx.Country.BH,
|
||||
"BI" => IbanEx.Country.BI,
|
||||
"BL" => IbanEx.Country.FR,
|
||||
"BR" => IbanEx.Country.BR,
|
||||
"BY" => IbanEx.Country.BY,
|
||||
"CH" => IbanEx.Country.CH,
|
||||
"CR" => IbanEx.Country.CR,
|
||||
"CY" => IbanEx.Country.CY,
|
||||
"CZ" => IbanEx.Country.CZ,
|
||||
"DE" => IbanEx.Country.DE,
|
||||
"DO" => IbanEx.Country.DO,
|
||||
"DJ" => IbanEx.Country.DJ,
|
||||
"DK" => IbanEx.Country.DK,
|
||||
"DO" => IbanEx.Country.DO,
|
||||
"EE" => IbanEx.Country.EE,
|
||||
"ES" => IbanEx.Country.ES,
|
||||
"EG" => IbanEx.Country.EG,
|
||||
"ES" => IbanEx.Country.ES,
|
||||
"FI" => IbanEx.Country.FI,
|
||||
"FR" => IbanEx.Country.FR,
|
||||
"FK" => IbanEx.Country.FK,
|
||||
"FO" => IbanEx.Country.FO,
|
||||
"FR" => IbanEx.Country.FR,
|
||||
"GB" => IbanEx.Country.GB,
|
||||
"GE" => IbanEx.Country.GE,
|
||||
"GF" => IbanEx.Country.FR,
|
||||
"GG" => IbanEx.Country.GB,
|
||||
"GI" => IbanEx.Country.GI,
|
||||
"GL" => IbanEx.Country.GL,
|
||||
"GP" => IbanEx.Country.FR,
|
||||
"GR" => IbanEx.Country.GR,
|
||||
"GT" => IbanEx.Country.GT,
|
||||
"HN" => IbanEx.Country.HN,
|
||||
"HR" => IbanEx.Country.HR,
|
||||
"HU" => IbanEx.Country.HU,
|
||||
"IE" => IbanEx.Country.IE,
|
||||
"IL" => IbanEx.Country.IL,
|
||||
"IT" => IbanEx.Country.IT,
|
||||
"IM" => IbanEx.Country.GB,
|
||||
"IQ" => IbanEx.Country.IQ,
|
||||
"IS" => IbanEx.Country.IS,
|
||||
"IT" => IbanEx.Country.IT,
|
||||
"JE" => IbanEx.Country.GB,
|
||||
"JO" => IbanEx.Country.JO,
|
||||
"KZ" => IbanEx.Country.KZ,
|
||||
"KW" => IbanEx.Country.KW,
|
||||
"KZ" => IbanEx.Country.KZ,
|
||||
"LB" => IbanEx.Country.LB,
|
||||
"LC" => IbanEx.Country.LC,
|
||||
"LI" => IbanEx.Country.LI,
|
||||
"LT" => IbanEx.Country.LT,
|
||||
"LU" => IbanEx.Country.LU,
|
||||
"LV" => IbanEx.Country.LV,
|
||||
"LY" => IbanEx.Country.LY,
|
||||
"MC" => IbanEx.Country.MC,
|
||||
"MD" => IbanEx.Country.MD,
|
||||
"ME" => IbanEx.Country.ME,
|
||||
"MF" => IbanEx.Country.FR,
|
||||
"MK" => IbanEx.Country.MK,
|
||||
"MN" => IbanEx.Country.MN,
|
||||
"MQ" => IbanEx.Country.FR,
|
||||
"MR" => IbanEx.Country.MR,
|
||||
"MT" => IbanEx.Country.MT,
|
||||
"MU" => IbanEx.Country.MU,
|
||||
"NC" => IbanEx.Country.FR,
|
||||
"NI" => IbanEx.Country.NI,
|
||||
"NL" => IbanEx.Country.NL,
|
||||
"NO" => IbanEx.Country.NO,
|
||||
"PL" => IbanEx.Country.PL,
|
||||
"PT" => IbanEx.Country.PT,
|
||||
"OM" => IbanEx.Country.OM,
|
||||
"PF" => IbanEx.Country.FR,
|
||||
"PK" => IbanEx.Country.PK,
|
||||
"PL" => IbanEx.Country.PL,
|
||||
"PM" => IbanEx.Country.FR,
|
||||
"PS" => IbanEx.Country.PS,
|
||||
"PT" => IbanEx.Country.PT,
|
||||
"QA" => IbanEx.Country.QA,
|
||||
"RE" => IbanEx.Country.FR,
|
||||
"RO" => IbanEx.Country.RO,
|
||||
"RS" => IbanEx.Country.RS,
|
||||
"RU" => IbanEx.Country.RU,
|
||||
"SA" => IbanEx.Country.SA,
|
||||
"SC" => IbanEx.Country.SC,
|
||||
"SD" => IbanEx.Country.SD,
|
||||
"SE" => IbanEx.Country.SE,
|
||||
"SI" => IbanEx.Country.SI,
|
||||
"SK" => IbanEx.Country.SK,
|
||||
"SM" => IbanEx.Country.SM,
|
||||
"SO" => IbanEx.Country.SO,
|
||||
"ST" => IbanEx.Country.ST,
|
||||
"SV" => IbanEx.Country.SV,
|
||||
"TF" => IbanEx.Country.FR,
|
||||
"TL" => IbanEx.Country.TL,
|
||||
"TN" => IbanEx.Country.TN,
|
||||
"TR" => IbanEx.Country.TR,
|
||||
"UA" => IbanEx.Country.UA,
|
||||
"VA" => IbanEx.Country.VA,
|
||||
"VG" => IbanEx.Country.VG,
|
||||
"XK" => IbanEx.Country.XK
|
||||
"WF" => IbanEx.Country.FR,
|
||||
"XK" => IbanEx.Country.XK,
|
||||
"YE" => IbanEx.Country.YE,
|
||||
"YT" => IbanEx.Country.FR
|
||||
}
|
||||
|
||||
@supported_country_codes Map.keys(@supported_countries)
|
||||
@@ -93,6 +129,7 @@ def supported_country_modules(), do: @supported_country_modules
|
||||
@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
|
||||
true -> supported_countries()[normalized_country_code]
|
||||
_ -> {:error, :unsupported_country_code}
|
||||
@@ -100,6 +137,7 @@ def country_module(country_code) when is_binary(country_code) or is_atom(country
|
||||
end
|
||||
|
||||
@spec is_country_code_supported?(country_code()) :: boolean()
|
||||
def is_country_code_supported?(country_code) when is_binary(country_code) or is_atom(country_code),
|
||||
do: Enum.member?(@supported_country_codes, normalize(country_code))
|
||||
def is_country_code_supported?(country_code)
|
||||
when is_binary(country_code) or is_atom(country_code),
|
||||
do: Enum.member?(@supported_country_codes, normalize(country_code))
|
||||
end
|
||||
|
||||
43
lib/iban_ex/country/bi.ex
Normal file
43
lib/iban_ex/country/bi.ex
Normal file
@@ -0,0 +1,43 @@
|
||||
defmodule IbanEx.Country.BI do
|
||||
@moduledoc """
|
||||
Burundi IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "BI",
|
||||
...> check_digits: "42",
|
||||
...> bank_code: "10000",
|
||||
...> branch_code: "10001",
|
||||
...> account_number: "00003320451",
|
||||
...> national_check: "81"
|
||||
...> }
|
||||
...> |> IbanEx.Country.BI.to_string()
|
||||
"BI 42 10000 10001 00003320451 81"
|
||||
```
|
||||
"""
|
||||
|
||||
@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,
|
||||
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
|
||||
@@ -2,6 +2,17 @@ defmodule IbanEx.Country.BR do
|
||||
@moduledoc """
|
||||
Brazil IBAN parsing rules
|
||||
|
||||
According to SWIFT registry, Brazil BBAN structure is:
|
||||
- Bank code: 8 digits
|
||||
- Branch code: 5 digits
|
||||
- Account code: 12 characters (10n + 1a + 1c) - account number + account type + owner type
|
||||
|
||||
BBAN spec: 8!n5!n10!n1!a1!c (total 25 chars)
|
||||
Example: BR1800360305000010009795493C1
|
||||
- Bank: 00360305
|
||||
- Branch: 00001
|
||||
- Account: 0009795493C1 (includes account number + type + owner)
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
@@ -10,18 +21,27 @@ defmodule IbanEx.Country.BR do
|
||||
...> check_digits: "18",
|
||||
...> bank_code: "00360305",
|
||||
...> branch_code: "00001",
|
||||
...> account_number: "0009795493",
|
||||
...> national_check: "C1"
|
||||
...> account_number: "0009795493C1",
|
||||
...> national_check: nil
|
||||
...> }
|
||||
...> |> IbanEx.Country.BR.to_string()
|
||||
"BR 18 00360305 00001 0009795493 C1"
|
||||
"BR 18 00360305 00001 0009795493C1"
|
||||
```
|
||||
"""
|
||||
|
||||
@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
|
||||
@rule ~r/^(?<bank_code>[0-9]{8})(?<branch_code>[0-9]{5})(?<account_number>[0-9]{10}[A-Z]{1}[0-9A-Z]{1})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
def rules() do
|
||||
[
|
||||
bank_code: %{regex: ~r/[0-9]{8}/i, range: 0..7},
|
||||
branch_code: %{regex: ~r/[0-9]{5}/i, range: 8..12},
|
||||
account_number: %{regex: ~r/[0-9]{10}[A-Z]{1}[0-9A-Z]{1}/i, range: 13..24}
|
||||
]
|
||||
end
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
@@ -31,12 +51,12 @@ def to_string(
|
||||
check_digits: check_digits,
|
||||
bank_code: bank_code,
|
||||
branch_code: branch_code,
|
||||
account_number: account_number,
|
||||
national_check: national_check
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
||||
39
lib/iban_ex/country/by.ex
Normal file
39
lib/iban_ex/country/by.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.BY do
|
||||
@moduledoc """
|
||||
Belarus IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "BY",
|
||||
...> check_digits: "13",
|
||||
...> bank_code: "NBRB",
|
||||
...> account_number: "3600900000002Z00AB00"
|
||||
...> }
|
||||
...> |> IbanEx.Country.BY.to_string()
|
||||
"BY 13 NBRB 3600900000002Z00AB00"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 28
|
||||
@rule ~r/^(?<bank_code>[A-Z0-9]{4})(?<account_number>[0-9]{4}[A-Z0-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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
43
lib/iban_ex/country/dj.ex
Normal file
43
lib/iban_ex/country/dj.ex
Normal file
@@ -0,0 +1,43 @@
|
||||
defmodule IbanEx.Country.DJ do
|
||||
@moduledoc """
|
||||
Djibouti IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "DJ",
|
||||
...> check_digits: "21",
|
||||
...> bank_code: "00010",
|
||||
...> branch_code: "00000",
|
||||
...> account_number: "01540001001",
|
||||
...> national_check: "86"
|
||||
...> }
|
||||
...> |> IbanEx.Country.DJ.to_string()
|
||||
"DJ 21 00010 00000 01540001001 86"
|
||||
```
|
||||
"""
|
||||
|
||||
@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,
|
||||
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
|
||||
39
lib/iban_ex/country/fk.ex
Normal file
39
lib/iban_ex/country/fk.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.FK do
|
||||
@moduledoc """
|
||||
Falkland Islands (Malvinas) IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "FK",
|
||||
...> check_digits: "88",
|
||||
...> bank_code: "SC",
|
||||
...> account_number: "123456789012"
|
||||
...> }
|
||||
...> |> IbanEx.Country.FK.to_string()
|
||||
"FK 88 SC 123456789012"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 18
|
||||
@rule ~r/^(?<bank_code>[A-Z]{2})(?<account_number>[0-9]{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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
@@ -2,6 +2,19 @@ defmodule IbanEx.Country.FR do
|
||||
@moduledoc """
|
||||
France IBAN parsing rules
|
||||
|
||||
According to SWIFT registry and Wise validation, France BBAN structure is:
|
||||
- Bank code: 5 digits
|
||||
- Branch code: 5 digits
|
||||
- Account number: 11 alphanumeric characters
|
||||
- National check: 2 digits
|
||||
|
||||
BBAN spec: 5!n5!n11!c2!n (total 23 chars)
|
||||
Example: FR1420041010050500013M02606
|
||||
- Bank: 20041
|
||||
- Branch: 01005
|
||||
- Account: 0500013M026
|
||||
- National check: 06
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
@@ -10,8 +23,8 @@ defmodule IbanEx.Country.FR do
|
||||
...> check_digits: "14",
|
||||
...> bank_code: "20041",
|
||||
...> branch_code: "01005",
|
||||
...> national_check: "06",
|
||||
...> account_number: "0500013M026"
|
||||
...> account_number: "0500013M026",
|
||||
...> national_check: "06"
|
||||
...> }
|
||||
...> |> IbanEx.Country.FR.to_string()
|
||||
"FR 14 20041 01005 0500013M026 06"
|
||||
@@ -23,6 +36,8 @@ defmodule IbanEx.Country.FR do
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
@@ -32,12 +47,13 @@ def to_string(
|
||||
check_digits: check_digits,
|
||||
bank_code: bank_code,
|
||||
branch_code: branch_code,
|
||||
national_check: national_check,
|
||||
account_number: account_number
|
||||
account_number: account_number,
|
||||
national_check: national_check
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
||||
39
lib/iban_ex/country/hn.ex
Normal file
39
lib/iban_ex/country/hn.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.HN do
|
||||
@moduledoc """
|
||||
Honduras IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "HN",
|
||||
...> check_digits: "88",
|
||||
...> bank_code: "CABF",
|
||||
...> account_number: "00000000000250005469"
|
||||
...> }
|
||||
...> |> IbanEx.Country.HN.to_string()
|
||||
"HN 88 CABF 00000000000250005469"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 28
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{20})$/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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
41
lib/iban_ex/country/iq.ex
Normal file
41
lib/iban_ex/country/iq.ex
Normal file
@@ -0,0 +1,41 @@
|
||||
defmodule IbanEx.Country.IQ do
|
||||
@moduledoc """
|
||||
Iraq IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "IQ",
|
||||
...> check_digits: "98",
|
||||
...> bank_code: "NBIQ",
|
||||
...> branch_code: "850",
|
||||
...> account_number: "123456789012"
|
||||
...> }
|
||||
...> |> IbanEx.Country.IQ.to_string()
|
||||
"IQ 98 NBIQ 850 123456789012"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 23
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{3})(?<account_number>[0-9]{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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
39
lib/iban_ex/country/lc.ex
Normal file
39
lib/iban_ex/country/lc.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.LC do
|
||||
@moduledoc """
|
||||
Saint Lucia IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "LC",
|
||||
...> check_digits: "55",
|
||||
...> bank_code: "HEMM",
|
||||
...> account_number: "000100010012001200023015"
|
||||
...> }
|
||||
...> |> IbanEx.Country.LC.to_string()
|
||||
"LC 55 HEMM 000100010012001200023015"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 32
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[A-Z0-9]{24})$/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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
41
lib/iban_ex/country/ly.ex
Normal file
41
lib/iban_ex/country/ly.ex
Normal file
@@ -0,0 +1,41 @@
|
||||
defmodule IbanEx.Country.LY do
|
||||
@moduledoc """
|
||||
Libya IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "LY",
|
||||
...> check_digits: "83",
|
||||
...> bank_code: "002",
|
||||
...> branch_code: "048",
|
||||
...> account_number: "000020100120361"
|
||||
...> }
|
||||
...> |> IbanEx.Country.LY.to_string()
|
||||
"LY 83 002 048 000020100120361"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 25
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{3})(?<account_number>[0-9]{15})$/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
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
39
lib/iban_ex/country/mn.ex
Normal file
39
lib/iban_ex/country/mn.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.MN do
|
||||
@moduledoc """
|
||||
Mongolia IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "MN",
|
||||
...> check_digits: "12",
|
||||
...> bank_code: "1234",
|
||||
...> account_number: "123456789123"
|
||||
...> }
|
||||
...> |> IbanEx.Country.MN.to_string()
|
||||
"MN 12 1234 123456789123"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 20
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
49
lib/iban_ex/country/mu.ex
Normal file
49
lib/iban_ex/country/mu.ex
Normal file
@@ -0,0 +1,49 @@
|
||||
defmodule IbanEx.Country.MU do
|
||||
@moduledoc """
|
||||
Mauritius IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "MU",
|
||||
...> check_digits: "17",
|
||||
...> bank_code: "BOMM01",
|
||||
...> branch_code: "01",
|
||||
...> account_number: "101030300200000MUR"
|
||||
...> }
|
||||
...> |> IbanEx.Country.MU.to_string()
|
||||
"MU 17 BOMM01 01 101030300200000MUR"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 30
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4}[0-9]{2})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{12}[0-9]{3}[A-Z]{3})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
def rules() do
|
||||
[
|
||||
bank_code: %{regex: ~r/[A-Z0-9]{6}/i, range: 0..5},
|
||||
branch_code: %{regex: ~r/[0-9]{2}/i, range: 6..7},
|
||||
account_number: %{regex: ~r/[0-9A-Z]{18}/i, range: 8..25}
|
||||
]
|
||||
end
|
||||
|
||||
@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
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
39
lib/iban_ex/country/ni.ex
Normal file
39
lib/iban_ex/country/ni.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.NI do
|
||||
@moduledoc """
|
||||
Nicaragua IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "NI",
|
||||
...> check_digits: "45",
|
||||
...> bank_code: "BAPR",
|
||||
...> account_number: "00000013000003558124"
|
||||
...> }
|
||||
...> |> IbanEx.Country.NI.to_string()
|
||||
"NI 45 BAPR 00000013000003558124"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 28
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{20})$/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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
39
lib/iban_ex/country/om.ex
Normal file
39
lib/iban_ex/country/om.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.OM do
|
||||
@moduledoc """
|
||||
Oman IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "OM",
|
||||
...> check_digits: "81",
|
||||
...> bank_code: "018",
|
||||
...> account_number: "0000001299123456"
|
||||
...> }
|
||||
...> |> IbanEx.Country.OM.to_string()
|
||||
"OM 81 018 0000001299123456"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 23
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[A-Z0-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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
25
lib/iban_ex/country/ps.ex
Normal file
25
lib/iban_ex/country/ps.ex
Normal file
@@ -0,0 +1,25 @@
|
||||
defmodule IbanEx.Country.PS do
|
||||
@moduledoc """
|
||||
Palestine IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "PS",
|
||||
...> check_digits: "92",
|
||||
...> bank_code: "PALS",
|
||||
...> branch_code: nil,
|
||||
...> national_check: nil,
|
||||
...> account_number: "000000000400123456702"
|
||||
...> }
|
||||
...> |> IbanEx.Country.PS.to_string()
|
||||
"PS 92 PALS 000000000400123456702"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 29
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[A-Z0-9]{21})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
end
|
||||
42
lib/iban_ex/country/ru.ex
Normal file
42
lib/iban_ex/country/ru.ex
Normal file
@@ -0,0 +1,42 @@
|
||||
defmodule IbanEx.Country.RU do
|
||||
@moduledoc """
|
||||
Russia IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "RU",
|
||||
...> check_digits: "03",
|
||||
...> bank_code: "044525225",
|
||||
...> branch_code: "40817",
|
||||
...> national_check: nil,
|
||||
...> account_number: "810538091310419"
|
||||
...> }
|
||||
...> |> IbanEx.Country.RU.to_string()
|
||||
"RU 03 044525225 40817 810538091310419"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 33
|
||||
@rule ~r/^(?<bank_code>[0-9]{9})(?<branch_code>[0-9]{5})(?<account_number>[A-Z0-9]{15})$/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
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
49
lib/iban_ex/country/sc.ex
Normal file
49
lib/iban_ex/country/sc.ex
Normal file
@@ -0,0 +1,49 @@
|
||||
defmodule IbanEx.Country.SC do
|
||||
@moduledoc """
|
||||
Seychelles IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "SC",
|
||||
...> check_digits: "18",
|
||||
...> bank_code: "SSCB11",
|
||||
...> branch_code: "01",
|
||||
...> account_number: "0000000000001497USD"
|
||||
...> }
|
||||
...> |> IbanEx.Country.SC.to_string()
|
||||
"SC 18 SSCB11 01 0000000000001497USD"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 31
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4}[0-9]{2})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{16}[A-Z]{3})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
def rules() do
|
||||
[
|
||||
bank_code: %{regex: ~r/[A-Z0-9]{6}/i, range: 0..5},
|
||||
branch_code: %{regex: ~r/[0-9]{2}/i, range: 6..7},
|
||||
account_number: %{regex: ~r/[0-9A-Z]{19}/i, range: 8..26}
|
||||
]
|
||||
end
|
||||
|
||||
@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
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
39
lib/iban_ex/country/sd.ex
Normal file
39
lib/iban_ex/country/sd.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule IbanEx.Country.SD do
|
||||
@moduledoc """
|
||||
Sudan IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "SD",
|
||||
...> check_digits: "21",
|
||||
...> bank_code: "29",
|
||||
...> account_number: "010501234001"
|
||||
...> }
|
||||
...> |> IbanEx.Country.SD.to_string()
|
||||
"SD 21 29 010501234001"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 18
|
||||
@rule ~r/^(?<bank_code>[0-9]{2})(?<account_number>[0-9]{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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
@@ -23,7 +23,6 @@ defmodule IbanEx.Country.SI do
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
@@ -41,5 +40,4 @@ def to_string(
|
||||
[country_code, check_digits, bank_code, branch_code, account_number, national_check]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
42
lib/iban_ex/country/so.ex
Normal file
42
lib/iban_ex/country/so.ex
Normal file
@@ -0,0 +1,42 @@
|
||||
defmodule IbanEx.Country.SO do
|
||||
@moduledoc """
|
||||
Somalia IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "SO",
|
||||
...> check_digits: "21",
|
||||
...> bank_code: "1000",
|
||||
...> branch_code: "001",
|
||||
...> national_check: nil,
|
||||
...> account_number: "001000100141"
|
||||
...> }
|
||||
...> |> IbanEx.Country.SO.to_string()
|
||||
"SO 21 1000 001 001000100141"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 23
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{3})(?<account_number>[0-9]{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,
|
||||
account_number: account_number
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
43
lib/iban_ex/country/st.ex
Normal file
43
lib/iban_ex/country/st.ex
Normal file
@@ -0,0 +1,43 @@
|
||||
defmodule IbanEx.Country.ST do
|
||||
@moduledoc """
|
||||
Sao Tome and Principe IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "ST",
|
||||
...> check_digits: "23",
|
||||
...> bank_code: "0001",
|
||||
...> branch_code: "0001",
|
||||
...> account_number: "00518453101",
|
||||
...> national_check: "46"
|
||||
...> }
|
||||
...> |> IbanEx.Country.ST.to_string()
|
||||
"ST 23 0001 0001 00518453101 46"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 25
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<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,
|
||||
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
|
||||
@@ -93,7 +93,7 @@ defp calculate_rules() do
|
||||
{Enum.reverse(list), bban_length}
|
||||
end
|
||||
|
||||
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
|
||||
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0, rules: 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
43
lib/iban_ex/country/tn.ex
Normal file
43
lib/iban_ex/country/tn.ex
Normal file
@@ -0,0 +1,43 @@
|
||||
defmodule IbanEx.Country.TN do
|
||||
@moduledoc """
|
||||
Tunisia IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "TN",
|
||||
...> check_digits: "59",
|
||||
...> bank_code: "10",
|
||||
...> branch_code: "006",
|
||||
...> account_number: "0351835984788",
|
||||
...> national_check: "31"
|
||||
...> }
|
||||
...> |> IbanEx.Country.TN.to_string()
|
||||
"TN 59 10 006 0351835984788 31"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 24
|
||||
@rule ~r/^(?<bank_code>[0-9]{2})(?<branch_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,
|
||||
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
|
||||
41
lib/iban_ex/country/ye.ex
Normal file
41
lib/iban_ex/country/ye.ex
Normal file
@@ -0,0 +1,41 @@
|
||||
defmodule IbanEx.Country.YE do
|
||||
@moduledoc """
|
||||
Yemen IBAN parsing rules
|
||||
|
||||
## Examples
|
||||
|
||||
```elixir
|
||||
iex> %IbanEx.Iban{
|
||||
...> country_code: "YE",
|
||||
...> check_digits: "15",
|
||||
...> bank_code: "CBYE",
|
||||
...> branch_code: "0001",
|
||||
...> account_number: "018861234567891234"
|
||||
...> }
|
||||
...> |> IbanEx.Country.YE.to_string()
|
||||
"YE 15 CBYE 0001 018861234567891234"
|
||||
```
|
||||
"""
|
||||
|
||||
@size 30
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{4})(?<account_number>[A-Z0-9]{18})$/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
|
||||
} = _iban,
|
||||
joiner \\ " "
|
||||
) do
|
||||
[country_code, check_digits, bank_code, branch_code, account_number]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
@@ -27,7 +27,7 @@ defmodule IbanEx.Error do
|
||||
:invalid_account_number,
|
||||
:invalid_branch_code,
|
||||
:invalid_national_check
|
||||
]
|
||||
]
|
||||
|
||||
@messages [
|
||||
unsupported_country_code: "Unsupported country code",
|
||||
@@ -40,8 +40,8 @@ defmodule IbanEx.Error do
|
||||
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",
|
||||
]
|
||||
invalid_national_check: "National check symbols violates required format"
|
||||
]
|
||||
|
||||
@spec message(error()) :: String.t()
|
||||
def message(error) when error in @errors, do: @messages[error]
|
||||
|
||||
@@ -8,7 +8,7 @@ defmodule IbanEx.Formatter do
|
||||
|
||||
@type iban() :: IbanEx.Iban.t()
|
||||
@type available_format() :: :compact | :pretty | :splitted
|
||||
@type available_formats_list() :: [:compact | :pretty | :splitted ]
|
||||
@type available_formats_list() :: [:compact | :pretty | :splitted]
|
||||
|
||||
@spec available_formats() :: available_formats_list()
|
||||
def available_formats(), do: @available_formats
|
||||
@@ -25,6 +25,7 @@ def splitted(iban), do: format(iban, :splitted)
|
||||
@spec format(iban()) :: String.t()
|
||||
@spec format(iban(), available_format()) :: String.t()
|
||||
def format(iban, format \\ :compact)
|
||||
|
||||
def format(iban, :compact),
|
||||
do: format(iban, :pretty) |> normalize()
|
||||
|
||||
|
||||
@@ -5,14 +5,21 @@ defmodule IbanEx.Iban do
|
||||
alias IbanEx.{Serialize}
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
country_code: <<_::16>>,
|
||||
check_digits: String.t(),
|
||||
bank_code: String.t(),
|
||||
branch_code: String.t() | nil,
|
||||
national_check: String.t() | nil,
|
||||
account_number: String.t()
|
||||
}
|
||||
defstruct country_code: "UA", check_digits: nil, bank_code: nil, branch_code: nil, national_check: nil, account_number: nil
|
||||
iban: String.t(),
|
||||
country_code: <<_::16>>,
|
||||
check_digits: String.t(),
|
||||
bank_code: String.t(),
|
||||
branch_code: String.t() | nil,
|
||||
national_check: String.t() | nil,
|
||||
account_number: String.t()
|
||||
}
|
||||
defstruct iban: nil,
|
||||
country_code: "UA",
|
||||
check_digits: nil,
|
||||
bank_code: nil,
|
||||
branch_code: nil,
|
||||
national_check: nil,
|
||||
account_number: nil
|
||||
|
||||
@spec to_map(IbanEx.Iban.t()) :: map()
|
||||
defdelegate to_map(iban), to: Serialize
|
||||
|
||||
@@ -25,7 +25,10 @@ def parse(iban_string, options \\ [incomplete: false])
|
||||
def parse(iban_string, incomplete: false) do
|
||||
case Validator.validate(iban_string) do
|
||||
{:ok, valid_iban} ->
|
||||
normalized = normalize_and_slice(valid_iban, 0..-1//1)
|
||||
|
||||
iban_map = %{
|
||||
iban: normalized,
|
||||
country_code: country_code(valid_iban),
|
||||
check_digits: check_digits(valid_iban)
|
||||
}
|
||||
@@ -43,7 +46,10 @@ def parse(iban_string, incomplete: false) do
|
||||
end
|
||||
|
||||
def parse(iban_string, incomplete: true) do
|
||||
normalized = normalize_and_slice(iban_string, 0..-1//1)
|
||||
|
||||
iban_map = %{
|
||||
iban: normalized,
|
||||
country_code: country_code(iban_string),
|
||||
check_digits: check_digits(iban_string)
|
||||
}
|
||||
@@ -72,6 +78,7 @@ def parse_bban(bban_string, country_code, incomplete: true) do
|
||||
country_code
|
||||
|> Country.country_module()
|
||||
|> parse_bban_by_country_rules(bban_string)
|
||||
|
||||
false ->
|
||||
%{}
|
||||
end
|
||||
@@ -82,6 +89,7 @@ def parse_bban(bban_string, country_code, incomplete: false) do
|
||||
true ->
|
||||
Country.country_module(country_code).rule()
|
||||
|> parse_bban_by_regex(bban_string)
|
||||
|
||||
false ->
|
||||
%{}
|
||||
end
|
||||
|
||||
@@ -20,7 +20,7 @@ defp violation_functions(),
|
||||
{&__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}},
|
||||
{&__MODULE__.iban_violates_checksum?/1, {:error, :invalid_checksum}}
|
||||
]
|
||||
|
||||
@doc """
|
||||
@@ -39,7 +39,17 @@ defp violation_functions(),
|
||||
@spec violations(String.t()) :: [] | [atom()]
|
||||
def violations(iban) do
|
||||
violation_functions()
|
||||
|> Enum.reduce([], fn {fun, value}, acc -> error_accumulator(acc, !fun.(iban) or value) end)
|
||||
|> Enum.reduce([], fn {fun, value}, acc ->
|
||||
# Special handling for length check to get specific :length_to_short or :length_to_long
|
||||
if fun == (&__MODULE__.iban_violates_length?/1) do
|
||||
case check_iban_length(iban) do
|
||||
{:error, atom} when atom in [:length_to_short, :length_to_long] -> [atom | acc]
|
||||
_ -> acc
|
||||
end
|
||||
else
|
||||
error_accumulator(acc, !fun.(iban) or value)
|
||||
end
|
||||
end)
|
||||
|> Enum.reverse()
|
||||
end
|
||||
|
||||
@@ -92,9 +102,22 @@ defp size(iban) do
|
||||
end
|
||||
|
||||
# - Check whether a given IBAN violates the required format.
|
||||
@spec iban_violates_format?(String.t()) :: boolean
|
||||
def iban_violates_format?(iban),
|
||||
do: Regex.match?(~r/[^A-Z0-9]/i, normalize(iban))
|
||||
@spec iban_violates_format?(String.t() | nil) :: boolean
|
||||
def iban_violates_format?(nil), do: true
|
||||
|
||||
def iban_violates_format?(iban) when is_binary(iban) do
|
||||
# Remove spaces first but don't uppercase yet
|
||||
cleaned = String.replace(iban, ~r/\s/, "")
|
||||
# Check that country code (first 2 chars) are uppercase only
|
||||
country_code = String.slice(cleaned, 0..1)
|
||||
country_code_lowercase = country_code != String.upcase(country_code)
|
||||
|
||||
# Check for invalid characters (after normalization)
|
||||
normalized = normalize(iban)
|
||||
has_invalid_chars = Regex.match?(~r/[^A-Z0-9]/, normalized)
|
||||
|
||||
has_invalid_chars or country_code_lowercase
|
||||
end
|
||||
|
||||
# - Check whether a given IBAN violates the required format in bank_code.
|
||||
@spec iban_violates_bank_code_format?(binary()) :: boolean
|
||||
@@ -102,22 +125,25 @@ def iban_violates_bank_code_format?(iban), do: iban_violates_bban_part_format?(i
|
||||
|
||||
# - 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)
|
||||
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)
|
||||
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)
|
||||
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
|
||||
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
|
||||
|
||||
@@ -187,11 +187,12 @@ test "correctly calculates positions for Germany (simple structure)" do
|
||||
test "correctly calculates positions for France (complex structure)" do
|
||||
{:ok, iban} = Parser.parse("FR1420041010050500013M02606")
|
||||
|
||||
# BBAN: 20041010050500013M02606
|
||||
# Per SWIFT registry and Wise validation, France structure is:
|
||||
# BBAN: 20041010050500013M02606 (23 chars)
|
||||
# Bank (5n): 20041
|
||||
# Branch (5n): 01005
|
||||
# Account (11c): 0500013M026
|
||||
# Check (2n): 06
|
||||
# National check (2n): 06
|
||||
assert iban.bank_code == "20041"
|
||||
assert iban.branch_code == "01005"
|
||||
assert iban.account_number == "0500013M026"
|
||||
@@ -276,7 +277,7 @@ test "handles IBANs from all length categories" do
|
||||
29,
|
||||
# Longest
|
||||
33
|
||||
]
|
||||
]
|
||||
|
||||
Enum.each(length_samples, fn target_length ->
|
||||
ibans = TestData.ibans_with(length: target_length)
|
||||
@@ -361,7 +362,9 @@ test "parses all 53 SEPA country IBANs" do
|
||||
end
|
||||
|
||||
test "parses French territories using FR rules" do
|
||||
# French territories: GF, GP, MQ, RE, etc.
|
||||
# French territories use FR as country code in IBAN, but are listed separately in registry
|
||||
# Real IBANs for French territories start with "FR", not their territory code
|
||||
# See: docs/international_wide_ibans/README.md - SEPA Countries Include Territories
|
||||
french_territories = [
|
||||
"GF",
|
||||
"GP",
|
||||
@@ -375,7 +378,7 @@ test "parses French territories using FR rules" do
|
||||
"MF",
|
||||
"PM",
|
||||
"WF"
|
||||
]
|
||||
]
|
||||
|
||||
Enum.each(french_territories, fn territory ->
|
||||
ibans = TestData.valid_ibans(country: territory)
|
||||
@@ -383,8 +386,9 @@ test "parses French territories using FR rules" do
|
||||
if length(ibans) > 0 do
|
||||
iban = List.first(ibans)
|
||||
assert {:ok, parsed} = Parser.parse(iban)
|
||||
assert parsed.country_code == territory
|
||||
# Should follow FR structure
|
||||
# Territory IBANs use "FR" as the country code in the actual IBAN
|
||||
assert parsed.country_code == "FR"
|
||||
# Should follow FR structure (27 chars)
|
||||
assert String.length(parsed.iban) == 27
|
||||
end
|
||||
end)
|
||||
@@ -403,7 +407,12 @@ test "parsed IBANs match registry specifications" do
|
||||
assert String.length(parsed.iban) == spec["iban_length"],
|
||||
"Length mismatch for #{country_code}"
|
||||
|
||||
assert parsed.country_code == country_code
|
||||
# Extract actual country code from iban_spec (e.g., "FI2!n..." -> "FI")
|
||||
# Territories like AX use parent country code (FI) in actual IBANs
|
||||
expected_country_code = String.slice(spec["iban_spec"], 0..1)
|
||||
|
||||
assert parsed.country_code == expected_country_code,
|
||||
"Country code mismatch for #{country_code}: expected #{expected_country_code}, got #{parsed.country_code}"
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -67,9 +67,9 @@ test "returns false for unsupported country code" do
|
||||
|
||||
test "returns false for invalid characters in BBAN" do
|
||||
# Cyrillic character
|
||||
refute TestData.valid?("DE89370400440532013Ї00")
|
||||
# CInvalcdchara in shorerst
|
||||
refute TestData.valid?("NO938601111794Ї")
|
||||
refute TestData.valid?("DE89370400440532013Ї00")
|
||||
# CInvalcdchara in shorerst
|
||||
refute TestData.valid?("NO938601111794Ї")
|
||||
end
|
||||
|
||||
test "returns false for lowercase country code" do
|
||||
|
||||
@@ -97,12 +97,10 @@ test "parsing valid IBANs from available countries returns {:ok, %IbanEx.Iban{}}
|
||||
test "parsing invalid IBANs from unavailable countries returns {:error, :unsupported_country_code}" do
|
||||
invalid_ibans =
|
||||
[
|
||||
# Fake country codes
|
||||
"SD3112000000198742637541",
|
||||
# Fake country codes (removed SD, GF, AX, BY, DJ, HN, IQ - now supported)
|
||||
"SU56263300012039086",
|
||||
"ZZ9121000418450200051332",
|
||||
"FU4550000000058398257466",
|
||||
"GF9300762011623852957",
|
||||
"FX380080012345678910157",
|
||||
"RT330006100519786457841326",
|
||||
"UL213223130000026007233566001",
|
||||
@@ -110,47 +108,29 @@ test "parsing invalid IBANs from unavailable countries returns {:error, :unsuppo
|
||||
"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",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
defmodule IbanExTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
doctest_file "README.md"
|
||||
doctest_file("README.md")
|
||||
doctest IbanEx.Country.AD
|
||||
doctest IbanEx.Country.AE
|
||||
doctest IbanEx.Country.AL
|
||||
|
||||
@@ -106,7 +106,7 @@ test "Check Account number format negative cases" do
|
||||
# shorter then need and has
|
||||
# invalid characters (leters) in number
|
||||
{"BR18003603050000100097CC1", true},
|
||||
{"CR050152020010262806Ї", true},
|
||||
{"CR050152020010262806Ї", true}
|
||||
# FIXME it is invalid IBAN for Bulgaria — need to change a rules function in Country Template module
|
||||
# {"BG80BNBG9661102034567Ї", true},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user