Compare commits
No commits in common. "6b5f3676c8e411cf3bd8b3b1d09a2f4fc48bf6b4" and "368225f3317935e1178b79d580e45d2ac357dd84" have entirely different histories.
6b5f3676c8
...
368225f331
47
README.md
47
README.md
|
@ -12,32 +12,30 @@ In just a few letters and numbers, the IBAN captures all of the country, bank, a
|
|||
|
||||
### Successfull case to parse IBAN
|
||||
|
||||
#### Parse string with valid formatted IBAN from supported country
|
||||
Parse string with valid formatted IBAN from supported country
|
||||
|
||||
```elixir
|
||||
```elixir
|
||||
{:ok, iban} = "FI2112345600000785" |> IbanEx.Parser.parse()
|
||||
IO.inspect(iban)
|
||||
IbanEx.Iban.pretty(iban)
|
||||
```
|
||||
|
||||
#### Success case responses
|
||||
#### Response
|
||||
|
||||
```elixir
|
||||
%IbanEx.Iban{
|
||||
country_code: "FI",
|
||||
check_digits: "21",
|
||||
bank_code: "123456",
|
||||
branch_code: nil,
|
||||
national_check: "5",
|
||||
account_number: "0000078"
|
||||
}
|
||||
|
||||
"FI 21 123456 0000078 5"
|
||||
```
|
||||
%IbanEx.Iban{
|
||||
country_code: "FI",
|
||||
check_digits: "21",
|
||||
bank_code: "123456",
|
||||
branch_code: nil,
|
||||
national_check: "5",
|
||||
account_number: "0000078"
|
||||
}
|
||||
|
||||
"FI 21 123456 0000078 5"
|
||||
|
||||
### Errors cases of IBAN parsing
|
||||
|
||||
#### Parse strings with invalid formatted IBANs from unsupported and supported countries
|
||||
Parse strings with invalid formatted IBANs from unsupported and supported countries
|
||||
|
||||
```elixir
|
||||
{:error, unsupported_country_code} = "AZ21NABZ00000000137010001944" |> IbanEx.Parser.parse()
|
||||
|
@ -50,22 +48,22 @@ IO.inspect(IbanEx.Error.message(invalid_length_code), label: invalid_length_code
|
|||
IO.inspect(IbanEx.Error.message(invalid_checksum), label: invalid_checksum)
|
||||
```
|
||||
|
||||
#### Error cases response
|
||||
#### Response
|
||||
|
||||
unsupported_country_code: "Unsupported country code"
|
||||
invalid_length: "IBAN violates the required length"
|
||||
invalid_checksum: "IBAN's checksum is invalid"
|
||||
|
||||
```elixir
|
||||
unsupported_country_code: "Unsupported country code"
|
||||
invalid_length: "IBAN violates the required length"
|
||||
invalid_checksum: "IBAN's checksum is invalid"
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
The package can be installed by adding `iban_ex` to your list of dependencies in `mix.exs`:
|
||||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||
by adding `iban_ex` to your list of dependencies in `mix.exs`:
|
||||
|
||||
```elixir
|
||||
def deps do
|
||||
[
|
||||
{:iban_ex, "~> 0.1.1"}
|
||||
{:iban_ex, "~> 0.1.0"}
|
||||
]
|
||||
end
|
||||
```
|
||||
|
@ -73,3 +71,4 @@ end
|
|||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||
be found at <https://hexdocs.pm/iban_ex>.
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Commons do
|
||||
@moduledoc false
|
||||
|
||||
@spec normalize(binary()) :: binary()
|
||||
def normalize(string) do
|
||||
string
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Country do
|
||||
@moduledoc false
|
||||
|
||||
import IbanEx.Commons, only: [normalize: 1]
|
||||
|
||||
@type country_code() :: <<_::16>> | atom()
|
||||
|
|
|
@ -3,8 +3,32 @@ defmodule IbanEx.Country.AT do
|
|||
Austria IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 20
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 20
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.BE do
|
|||
Belgium IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 16
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{7})(?<national_check>[0-9]{2})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 16
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,17 +3,23 @@ defmodule IbanEx.Country.BG do
|
|||
Bulgaria IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 22
|
||||
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9]{2}[0-9A-Z]{8})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
|
||||
@spec size() :: 22
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,8 +3,32 @@ defmodule IbanEx.Country.CH do
|
|||
Switzerland IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 21
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9A-Z]{12})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 21
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.CY do
|
|||
Cyprus IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 28
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{16})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 28
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.CZ do
|
|||
Czech Republic IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 24
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{16})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 24
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.DE do
|
|||
Germany IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 22
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{8})(?<account_number>[0-9]{10})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 22
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,35 @@ defmodule IbanEx.Country.DK do
|
|||
@moduledoc """
|
||||
Denmark IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 18
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{10})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 18
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.EE do
|
|||
Estonian IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 20
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{2})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{11})(?<national_check>[0-9]{1})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 20
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.ES do
|
|||
Spain IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 24
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<national_check>[0-9]{2})(?<account_number>[0-9]{10})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 24
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.FI do
|
|||
Finland IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 18
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9]{7})(?<national_check>[0-9]{1})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 18
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.FR do
|
|||
France IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 27
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{5})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{11})(?<national_check>[0-9]{2})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 27
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.GB do
|
|||
United Kingdom IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 22
|
||||
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{6})(?<account_number>[0-9]{8})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 22
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.HR do
|
|||
Croatia IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 21
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{7})(?<account_number>[0-9]{10})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 21
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.LT do
|
|||
Lithuanian IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 20
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 20
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.LU do
|
|||
Luxembourg IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 20
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9A-Z]{13})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 20
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.LV do
|
|||
Latvian IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 21
|
||||
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9A-Z]{13})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 21
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.MT do
|
|||
Malta IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 31
|
||||
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{18})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 31
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.NL do
|
|||
Netherlands IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 18
|
||||
|
||||
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{10})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 18
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.PL do
|
|||
Poland IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 28
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{4})(?<national_check>[0-9]{1})(?<account_number>[0-9]{16})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 28
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -3,15 +3,23 @@ defmodule IbanEx.Country.PT do
|
|||
Portugal IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@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
|
||||
@spec size() :: 25
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec to_string(Iban.t()) :: binary()
|
||||
@spec to_string(Iban.t(), binary()) :: binary()
|
||||
def to_string(
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%Iban{
|
||||
country_code: country_code,
|
||||
check_digits: check_digits,
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Country.Template do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.Iban
|
||||
@type size() :: non_neg_integer()
|
||||
@type rule() :: Regex.t()
|
||||
|
@ -9,41 +7,6 @@ defmodule IbanEx.Country.Template do
|
|||
|
||||
@callback size() :: size()
|
||||
@callback rule() :: rule()
|
||||
@callback to_string(Iban.t(), joiner()) :: String.t()
|
||||
@callback to_string(Iban.t()) :: String.t()
|
||||
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
alias IbanEx.Iban
|
||||
@behaviour 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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec size() :: Integer.t()
|
||||
def size(), do: @size
|
||||
|
||||
@impl IbanEx.Country.Template
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
|
||||
end
|
||||
end
|
||||
@callback to_s(Iban.t(), joiner()) :: String.t()
|
||||
@callback to_s(Iban.t()) :: String.t()
|
||||
end
|
||||
|
|
|
@ -3,8 +3,34 @@ defmodule IbanEx.Country.UA do
|
|||
Ukrainian IBAN parsing rules
|
||||
"""
|
||||
|
||||
alias IbanEx.Iban
|
||||
|
||||
@behaviour IbanEx.Country.Template
|
||||
|
||||
@size 29
|
||||
|
||||
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9A-Z]{19})$/i
|
||||
|
||||
use IbanEx.Country.Template
|
||||
@spec size() :: 29
|
||||
def size(), do: @size
|
||||
|
||||
@spec rule() :: Regex.t()
|
||||
def rule(), do: @rule
|
||||
|
||||
@spec to_s(Iban.t()) :: binary()
|
||||
@spec to_s(Iban.t(), binary()) :: binary()
|
||||
def to_s(
|
||||
%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]
|
||||
|> Enum.join(joiner)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,8 +7,8 @@ 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()
|
||||
@spec to_iban(binary()) :: IbanEx.Iban.t()
|
||||
def to_iban(string) do
|
||||
case Parser.parse(string) do
|
||||
{:ok, iban} -> iban
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
defmodule IbanEx.Error do
|
||||
@moduledoc false
|
||||
@moduledoc """
|
||||
|
||||
"""
|
||||
|
||||
@type error() ::
|
||||
:unsupported_country_code
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Formatter do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.Country
|
||||
import IbanEx.Commons, only: [normalize: 1]
|
||||
|
||||
|
@ -30,7 +28,7 @@ defmodule IbanEx.Formatter do
|
|||
|
||||
def format(iban, :pretty) do
|
||||
country_module = Country.country_module(iban.country_code)
|
||||
country_module.to_string(iban)
|
||||
country_module.to_s(iban)
|
||||
end
|
||||
|
||||
def format(iban, :splitted) do
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Iban do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.Formatter
|
||||
alias IbanEx.{Serialize}
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Parser do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.{Country, Iban, Validator}
|
||||
import IbanEx.Commons, only: [normalize_and_slice: 2]
|
||||
|
||||
|
@ -11,27 +9,20 @@ defmodule IbanEx.Parser do
|
|||
|
||||
@spec parse({:ok, String.t()} | String.t()) :: iban_or_error()
|
||||
def parse({:ok, iban_string}), do: parse(iban_string)
|
||||
|
||||
def parse(iban_string) do
|
||||
case Validator.validate(iban_string) do
|
||||
{:ok, valid_iban} ->
|
||||
iban_map = %{
|
||||
country_code: country_code(valid_iban),
|
||||
check_digits: check_digits(valid_iban)
|
||||
}
|
||||
with {:ok, valid_iban} <- Validator.validate(iban_string) do
|
||||
iban_map = %{
|
||||
country_code: country_code(valid_iban),
|
||||
check_digits: check_digits(valid_iban),
|
||||
}
|
||||
|
||||
regex = Country.country_module(iban_map.country_code).rule()
|
||||
bban = bban(iban_string)
|
||||
regex = Country.country_module(iban_map.country_code).rule()
|
||||
bban = bban(iban_string)
|
||||
bban_map = for {key, val} <- Regex.named_captures(regex, bban), into: %{}, do: {String.to_atom(key), val}
|
||||
|
||||
bban_map =
|
||||
for {key, val} <- Regex.named_captures(regex, bban),
|
||||
into: %{},
|
||||
do: {String.to_atom(key), val}
|
||||
|
||||
{:ok, struct(Iban, Map.merge(iban_map, bban_map))}
|
||||
|
||||
{:error, error_type} ->
|
||||
{:error, error_type}
|
||||
{:ok, struct(Iban, Map.merge(iban_map, bban_map))}
|
||||
else
|
||||
{:error, error_type} -> {:error, error_type}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
defmodule IbanEx.Serialize do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.{Iban, Formatter}
|
||||
|
||||
@spec to_string(Iban.t()) :: String.t()
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
defmodule IbanEx.Validator do
|
||||
@moduledoc false
|
||||
|
||||
alias IbanEx.{Country, Parser}
|
||||
alias IbanEx.Validator.Replacements
|
||||
import IbanEx.Commons, only: [normalize: 1]
|
||||
|
||||
@spec validate(String.t()) :: {:ok, String.t()} | {:error, Atom.t()}
|
||||
@spec validate(String.t()) :: {:ok, String.t()} | {:error}
|
||||
def validate(iban) do
|
||||
cond do
|
||||
iban_violates_format?(iban) ->
|
||||
|
|
3
mix.exs
3
mix.exs
|
@ -2,7 +2,7 @@ defmodule IbanEx.MixProject do
|
|||
use Mix.Project
|
||||
|
||||
@source_url "https://g.tulz.dev/opensource/iban-ex"
|
||||
@version "0.1.1"
|
||||
@version "0.1.0"
|
||||
|
||||
def project do
|
||||
[
|
||||
|
@ -68,6 +68,7 @@ defmodule IbanEx.MixProject do
|
|||
{:ex_doc, ">= 0.0.0", only: ~w(dev test)a, runtime: false},
|
||||
{:sobelow, ">= 0.0.0", only: ~w(dev test)a, runtime: false},
|
||||
{:mix_audit, ">= 0.0.0", only: ~w(dev test)a, runtime: false},
|
||||
{:esbuild, "~> 0.7.0", runtime: Mix.env() == :dev},
|
||||
{:observer_cli, "~> 1.7.4", only: :dev, runtime: false},
|
||||
{:elixir_sense, github: "elixir-lsp/elixir_sense", only: ~w(dev)a}
|
||||
|
||||
|
|
Loading…
Reference in New Issue