Compare commits

..

No commits in common. "6b5f3676c8e411cf3bd8b3b1d09a2f4fc48bf6b4" and "368225f3317935e1178b79d580e45d2ac357dd84" have entirely different histories.

34 changed files with 480 additions and 167 deletions

View File

@ -12,7 +12,7 @@ In just a few letters and numbers, the IBAN captures all of the country, bank, a
### Successfull case to parse IBAN ### 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() {:ok, iban} = "FI2112345600000785" |> IbanEx.Parser.parse()
@ -20,24 +20,22 @@ IO.inspect(iban)
IbanEx.Iban.pretty(iban) IbanEx.Iban.pretty(iban)
``` ```
#### Success case responses #### Response
```elixir %IbanEx.Iban{
%IbanEx.Iban{ country_code: "FI",
country_code: "FI", check_digits: "21",
check_digits: "21", bank_code: "123456",
bank_code: "123456", branch_code: nil,
branch_code: nil, national_check: "5",
national_check: "5", account_number: "0000078"
account_number: "0000078" }
}
"FI 21 123456 0000078 5" "FI 21 123456 0000078 5"
```
### Errors cases of IBAN parsing ### 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 ```elixir
{:error, unsupported_country_code} = "AZ21NABZ00000000137010001944" |> IbanEx.Parser.parse() {: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) 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 ## 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 ```elixir
def deps do def deps do
[ [
{:iban_ex, "~> 0.1.1"} {:iban_ex, "~> 0.1.0"}
] ]
end end
``` ```
@ -73,3 +71,4 @@ end
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) 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 and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/iban_ex>. be found at <https://hexdocs.pm/iban_ex>.

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Commons do defmodule IbanEx.Commons do
@moduledoc false
@spec normalize(binary()) :: binary() @spec normalize(binary()) :: binary()
def normalize(string) do def normalize(string) do
string string

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Country do defmodule IbanEx.Country do
@moduledoc false
import IbanEx.Commons, only: [normalize: 1] import IbanEx.Commons, only: [normalize: 1]
@type country_code() :: <<_::16>> | atom() @type country_code() :: <<_::16>> | atom()

View File

@ -3,8 +3,32 @@ defmodule IbanEx.Country.AT do
Austria IBAN parsing rules Austria IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20 @size 20
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i @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 end

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.BE do
Belgium IBAN parsing rules Belgium IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 16 @size 16
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{7})(?<national_check>[0-9]{2})$/i @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,17 +3,23 @@ defmodule IbanEx.Country.BG do
Bulgaria IBAN parsing rules Bulgaria IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22 @size 22
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9]{2}[0-9A-Z]{8})$/i @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 @spec size() :: 22
def size(), do: @size
@impl IbanEx.Country.Template @spec rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,8 +3,32 @@ defmodule IbanEx.Country.CH do
Switzerland IBAN parsing rules Switzerland IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21 @size 21
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9A-Z]{12})$/i @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 end

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.CY do
Cyprus IBAN parsing rules Cyprus IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 28 @size 28
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{16})$/i @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.CZ do
Czech Republic IBAN parsing rules Czech Republic IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 24 @size 24
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{16})$/i @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 end

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.DE do
Germany IBAN parsing rules Germany IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22 @size 22
@rule ~r/^(?<bank_code>[0-9]{8})(?<account_number>[0-9]{10})$/i @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 end

View File

@ -2,8 +2,35 @@ defmodule IbanEx.Country.DK do
@moduledoc """ @moduledoc """
Denmark IBAN parsing rules Denmark IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18 @size 18
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{10})$/i @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 end

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.EE do
Estonian IBAN parsing rules Estonian IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20 @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 @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.ES do
Spain IBAN parsing rules Spain IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 24 @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 @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.FI do
Finland IBAN parsing rules Finland IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18 @size 18
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9]{7})(?<national_check>[0-9]{1})$/i @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.FR do
France IBAN parsing rules France IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 27 @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 @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.GB do
United Kingdom IBAN parsing rules United Kingdom IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22 @size 22
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{6})(?<account_number>[0-9]{8})$/i @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.HR do
Croatia IBAN parsing rules Croatia IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21 @size 21
@rule ~r/^(?<bank_code>[0-9]{7})(?<account_number>[0-9]{10})$/i @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 end

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.LT do
Lithuanian IBAN parsing rules Lithuanian IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20 @size 20
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i @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 end

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.LU do
Luxembourg IBAN parsing rules Luxembourg IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20 @size 20
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9A-Z]{13})$/i @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 end

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.LV do
Latvian IBAN parsing rules Latvian IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21 @size 21
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9A-Z]{13})$/i @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 end

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.MT do
Malta IBAN parsing rules Malta IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 31 @size 31
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{18})$/i @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.NL do
Netherlands IBAN parsing rules Netherlands IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18 @size 18
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{10})$/i @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 end

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.PL do
Poland IBAN parsing rules Poland IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 28 @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 @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -3,15 +3,23 @@ defmodule IbanEx.Country.PT do
Portugal IBAN parsing rules Portugal IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 25 @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 @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 rule() :: Regex.t()
@spec to_string(Iban.t()) :: binary() def rule(), do: @rule
@spec to_string(Iban.t(), binary()) :: binary()
def to_string( @spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{ %Iban{
country_code: country_code, country_code: country_code,
check_digits: check_digits, check_digits: check_digits,

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Country.Template do defmodule IbanEx.Country.Template do
@moduledoc false
alias IbanEx.Iban alias IbanEx.Iban
@type size() :: non_neg_integer() @type size() :: non_neg_integer()
@type rule() :: Regex.t() @type rule() :: Regex.t()
@ -9,41 +7,6 @@ defmodule IbanEx.Country.Template do
@callback size() :: size() @callback size() :: size()
@callback rule() :: rule() @callback rule() :: rule()
@callback to_string(Iban.t(), joiner()) :: String.t() @callback to_s(Iban.t(), joiner()) :: String.t()
@callback to_string(Iban.t()) :: String.t() @callback to_s(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
end end

View File

@ -3,8 +3,34 @@ defmodule IbanEx.Country.UA do
Ukrainian IBAN parsing rules Ukrainian IBAN parsing rules
""" """
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 29 @size 29
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9A-Z]{19})$/i @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 end

View File

@ -7,8 +7,8 @@ end
defimpl IbanEx.Deserialize, for: [BitString, String] do defimpl IbanEx.Deserialize, for: [BitString, String] do
alias IbanEx.{Parser, Error} alias IbanEx.{Parser, Error}
@type iban_or_error() :: IbanEx.Iban.t() | {:error, atom()} @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(String.t()) :: iban_or_error()
@spec to_iban(binary()) :: IbanEx.Iban.t()
def to_iban(string) do def to_iban(string) do
case Parser.parse(string) do case Parser.parse(string) do
{:ok, iban} -> iban {:ok, iban} -> iban

View File

@ -1,5 +1,7 @@
defmodule IbanEx.Error do defmodule IbanEx.Error do
@moduledoc false @moduledoc """
"""
@type error() :: @type error() ::
:unsupported_country_code :unsupported_country_code

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Formatter do defmodule IbanEx.Formatter do
@moduledoc false
alias IbanEx.Country alias IbanEx.Country
import IbanEx.Commons, only: [normalize: 1] import IbanEx.Commons, only: [normalize: 1]
@ -30,7 +28,7 @@ defmodule IbanEx.Formatter do
def format(iban, :pretty) do def format(iban, :pretty) do
country_module = Country.country_module(iban.country_code) country_module = Country.country_module(iban.country_code)
country_module.to_string(iban) country_module.to_s(iban)
end end
def format(iban, :splitted) do def format(iban, :splitted) do

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Iban do defmodule IbanEx.Iban do
@moduledoc false
alias IbanEx.Formatter alias IbanEx.Formatter
alias IbanEx.{Serialize} alias IbanEx.{Serialize}

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Parser do defmodule IbanEx.Parser do
@moduledoc false
alias IbanEx.{Country, Iban, Validator} alias IbanEx.{Country, Iban, Validator}
import IbanEx.Commons, only: [normalize_and_slice: 2] 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() @spec parse({:ok, String.t()} | String.t()) :: iban_or_error()
def parse({:ok, iban_string}), do: parse(iban_string) def parse({:ok, iban_string}), do: parse(iban_string)
def parse(iban_string) do def parse(iban_string) do
case Validator.validate(iban_string) do with {:ok, valid_iban} <- Validator.validate(iban_string) do
{:ok, valid_iban} -> iban_map = %{
iban_map = %{ country_code: country_code(valid_iban),
country_code: country_code(valid_iban), check_digits: check_digits(valid_iban),
check_digits: check_digits(valid_iban) }
}
regex = Country.country_module(iban_map.country_code).rule() regex = Country.country_module(iban_map.country_code).rule()
bban = bban(iban_string) bban = bban(iban_string)
bban_map = for {key, val} <- Regex.named_captures(regex, bban), into: %{}, do: {String.to_atom(key), val}
bban_map = {:ok, struct(Iban, Map.merge(iban_map, bban_map))}
for {key, val} <- Regex.named_captures(regex, bban), else
into: %{}, {:error, error_type} -> {:error, error_type}
do: {String.to_atom(key), val}
{:ok, struct(Iban, Map.merge(iban_map, bban_map))}
{:error, error_type} ->
{:error, error_type}
end end
end end

View File

@ -1,6 +1,4 @@
defmodule IbanEx.Serialize do defmodule IbanEx.Serialize do
@moduledoc false
alias IbanEx.{Iban, Formatter} alias IbanEx.{Iban, Formatter}
@spec to_string(Iban.t()) :: String.t() @spec to_string(Iban.t()) :: String.t()

View File

@ -1,11 +1,9 @@
defmodule IbanEx.Validator do defmodule IbanEx.Validator do
@moduledoc false
alias IbanEx.{Country, Parser} alias IbanEx.{Country, Parser}
alias IbanEx.Validator.Replacements alias IbanEx.Validator.Replacements
import IbanEx.Commons, only: [normalize: 1] 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 def validate(iban) do
cond do cond do
iban_violates_format?(iban) -> iban_violates_format?(iban) ->

View File

@ -2,7 +2,7 @@ defmodule IbanEx.MixProject do
use Mix.Project use Mix.Project
@source_url "https://g.tulz.dev/opensource/iban-ex" @source_url "https://g.tulz.dev/opensource/iban-ex"
@version "0.1.1" @version "0.1.0"
def project do def project do
[ [
@ -68,6 +68,7 @@ defmodule IbanEx.MixProject do
{:ex_doc, ">= 0.0.0", only: ~w(dev test)a, runtime: false}, {:ex_doc, ">= 0.0.0", only: ~w(dev test)a, runtime: false},
{:sobelow, ">= 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}, {: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}, {:observer_cli, "~> 1.7.4", only: :dev, runtime: false},
{:elixir_sense, github: "elixir-lsp/elixir_sense", only: ~w(dev)a} {:elixir_sense, github: "elixir-lsp/elixir_sense", only: ~w(dev)a}