Skip to content

Commit b011aaf

Browse files
author
Matthias Kalb
committed
Refactor typespecs and add comments
1 parent bf4ffa6 commit b011aaf

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

lib/bankster.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ defmodule Bankster do
33
The main API for Bankster.
44
"""
55

6+
## -- Functions
67
@doc """
78
Validates a given string whether it's a valid SWIFT BIC
89
## Example
910
iex> Bankster.bic_valid?("INVALIDBIC")
1011
false
1112
"""
12-
@spec bic_valid?(String.t()) :: boolean
13+
@spec bic_valid?(binary()) :: boolean()
1314
defdelegate bic_valid?(iban), to: Bankster.Bic, as: :valid?
1415

1516
@doc """
@@ -18,7 +19,7 @@ defmodule Bankster do
1819
iex> Bankster.iban_valid?("INVALIDIBAN")
1920
false
2021
"""
21-
@spec iban_valid?(String.t()) :: boolean
22+
@spec iban_valid?(binary()) :: boolean()
2223
defdelegate iban_valid?(iban), to: Bankster.Iban, as: :valid?
2324

2425
@doc """
@@ -33,6 +34,6 @@ defmodule Bankster do
3334
iex> Bankster.iban_validate("DK8387188644726815")
3435
{:ok, "DK8387188644726815"}
3536
"""
36-
@spec iban_validate(String.t()) :: {:ok, String.t()} | {:error, Atom.t()}
37+
@spec iban_validate(binary()) :: {:ok, binary()} | {:error, atom()}
3738
defdelegate iban_validate(iban), to: Bankster.Iban, as: :validate
3839
end

lib/bankster/bic.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
defmodule Bankster.Bic do
22
@moduledoc """
3-
Provides some SWIFT BIC related functions.
3+
Module provides some SWIFT BIC related functions.
44
"""
55

6+
## -- Module attributes
67
@bic_validation_regex ~r/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/
78

9+
## -- Functions
810
@doc """
911
Validates a given string whether it's a valid SWIFT BIC
10-
## Example
11-
iex> Bankster.Bic.valid?("INVALIDBIC")
12-
false
13-
"""
14-
@spec valid?(String.t()) :: boolean
15-
def valid?(bic), do: is_valid?(bic)
1612
17-
defp is_valid?(bic) when is_binary(bic),
13+
## Examples
14+
iex> Bankster.Bic.valid?("INVALIDBIC")
15+
false
16+
"""
17+
@spec valid?(binary()) :: boolean()
18+
def valid?(bic) when is_binary(bic),
1819
do: Regex.match?(@bic_validation_regex, String.replace(bic, ~r/\s*/, ""))
1920

20-
defp is_valid?(_), do: false
21+
def valid?(_),
22+
do: false
2123
end

lib/bankster/iban.ex

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
defmodule Bankster.Iban do
22
@moduledoc """
3-
Provides some IBAN related functions.
3+
Module provides some IBAN related functions.
44
"""
55

6+
## -- Module constants
7+
# - IBAN Rules
68
@iban_rules %{
79
"AA" => %{length: 16, rule: ~r/^[0-9A-Z]{12}$/i},
810
"AD" => %{length: 24, rule: ~r/^[0-9]{8}[0-9A-Z]{12}$/i},
@@ -122,6 +124,7 @@ defmodule Bankster.Iban do
122124
"YT" => %{length: 27, rule: ~r/^[0-9]{10}[0-9A-Z]{11}[0-9]{2}$/i}
123125
}
124126

127+
# - IBAN replacements
125128
@replacements %{
126129
"A" => "10",
127130
"B" => "11",
@@ -151,14 +154,15 @@ defmodule Bankster.Iban do
151154
"Z" => "35"
152155
}
153156

157+
## -- Functions
154158
@doc """
155159
Formats and returns a given IBAN in compact format.
156160
157161
## Examples
158162
iex> Bankster.Iban.format_compact("DK8 38 7188 64472 6815 ")
159163
"DK8387188644726815"
160164
"""
161-
@spec format_compact(String.t()) :: String.t()
165+
@spec format_compact(binary()) :: binary()
162166
def format_compact(iban), do: format_default(iban)
163167

164168
@doc """
@@ -168,7 +172,7 @@ defmodule Bankster.Iban do
168172
iex> Bankster.Iban.format_pretty("DK8387188644726815")
169173
"DK838 7188 64472 6815"
170174
"""
171-
@spec format_pretty(String.t()) :: String.t()
175+
@spec format_pretty(binary()) :: binary()
172176
def format_pretty(iban) do
173177
~r/.{1,4}/
174178
|> Regex.scan(format_default(iban))
@@ -183,7 +187,7 @@ defmodule Bankster.Iban do
183187
iex> Bankster.Iban.country_code("DK8387188644726815")
184188
"DK"
185189
"""
186-
@spec country_code(String.t()) :: String.t()
190+
@spec country_code(binary()) :: binary()
187191
def country_code(iban) do
188192
iban
189193
|> format_default()
@@ -197,7 +201,7 @@ defmodule Bankster.Iban do
197201
iex> Bankster.Iban.checksum("DK8387188644726815")
198202
83
199203
"""
200-
@spec checksum(String.t()) :: String.t()
204+
@spec checksum(binary()) :: binary()
201205
def checksum(iban) do
202206
iban
203207
|> format_default()
@@ -211,7 +215,7 @@ defmodule Bankster.Iban do
211215
iex> Bankster.Iban.supported_countries()
212216
["SM", "KZ", "SN", "BA", "GA", "KW", "MU", ...]
213217
"""
214-
@spec supported_countries() :: list(String.t())
218+
@spec supported_countries :: list(binary())
215219
def supported_countries, do: Map.keys(@iban_rules)
216220

217221
@doc """
@@ -224,13 +228,12 @@ defmodule Bankster.Iban do
224228
iex> Bankster.Iban.supported_country?("XYZ")
225229
false
226230
"""
227-
@spec supported_country?(String.t()) :: boolean
228-
def supported_country?(country_code)
229-
231+
@spec supported_country?(binary()) :: boolean()
230232
def supported_country?(country_code) when is_binary(country_code),
231233
do: Map.has_key?(@iban_rules, format_default(country_code))
232234

233-
def supported_country?(_invalid), do: false
235+
def supported_country?(_country_code),
236+
do: false
234237

235238
@doc """
236239
Returns the BBAN of the given IBAN.
@@ -239,7 +242,7 @@ defmodule Bankster.Iban do
239242
iex> Bankster.Iban.bban("DK8387188644726815")
240243
"87188644726815"
241244
"""
242-
@spec bban(String.t()) :: String.t()
245+
@spec bban(binary()) :: binary()
243246
def bban(iban) do
244247
iban
245248
|> format_default()
@@ -253,7 +256,7 @@ defmodule Bankster.Iban do
253256
iex> Bankster.Iban.size("DK 8387 188644 726815")
254257
18
255258
"""
256-
@spec size(String.t()) :: Integer.t()
259+
@spec size(binary()) :: integer()
257260
def size(iban) do
258261
iban
259262
|> format_default()
@@ -273,7 +276,7 @@ defmodule Bankster.Iban do
273276
iex> Bankster.Iban.validate("DK8387188644726815")
274277
{:ok, "DK8387188644726815"}
275278
"""
276-
@spec validate(String.t()) :: {:ok, String.t()} | {:error, Atom.t()}
279+
@spec validate(binary()) :: {:ok, binary()} | {:error, atom()}
277280
def validate(iban) do
278281
cond do
279282
iban_violates_format?(iban) ->
@@ -306,31 +309,36 @@ defmodule Bankster.Iban do
306309
iex> Bankster.Iban.valid?("DK8387188644726815")
307310
true
308311
"""
309-
@spec valid?(String.t()) :: boolean
310-
def valid?(iban), do: match?({:ok, _}, validate(iban))
311-
312-
##################################################
313-
## HELPERS
312+
@spec valid?(binary()) :: boolean()
313+
def valid?(iban), do: match?({:ok, _iban}, validate(iban))
314314

315-
@spec format_default(String.t()) :: String.t()
315+
## -- Helper functions
316+
# - Format a given IBAN in a default format.
317+
@spec format_default(binary()) :: binary()
316318
defp format_default(iban) do
317319
iban
318320
|> to_string()
319321
|> String.replace(~r/\s*/i, "")
320322
|> String.upcase()
321323
end
322324

323-
@spec iban_violates_format?(String.t()) :: boolean
324-
defp iban_violates_format?(iban), do: Regex.match?(~r/[^A-Z0-9]/i, format_default(iban))
325+
# - Check whether a given IBAN violates the required format.
326+
@spec iban_violates_format?(binary()) :: boolean
327+
defp iban_violates_format?(iban),
328+
do: Regex.match?(~r/[^A-Z0-9]/i, format_default(iban))
325329

326-
@spec iban_violates_country?(String.t()) :: boolean
327-
defp iban_violates_country?(iban), do: !Map.has_key?(@iban_rules, country_code(iban))
330+
# - Check whether a given IBAN violates the supported countries.
331+
@spec iban_violates_country?(binary()) :: boolean
332+
defp iban_violates_country?(iban),
333+
do: !Map.has_key?(@iban_rules, country_code(iban))
328334

329-
@spec iban_violates_length?(String.t()) :: boolean
335+
# - Check whether a given IBAN violates the required length.
336+
@spec iban_violates_length?(binary()) :: boolean
330337
defp iban_violates_length?(iban),
331338
do: size(iban) != get_in(@iban_rules, [country_code(iban), :length])
332339

333-
@spec iban_violates_country_rule?(String.t()) :: boolean
340+
# - Check whether a given IBAN violates the country rules.
341+
@spec iban_violates_country_rule?(binary()) :: boolean
334342
defp iban_violates_country_rule?(iban) do
335343
if iban_rule = get_in(@iban_rules, [country_code(iban), :rule]) do
336344
!Regex.match?(iban_rule, String.slice(format_default(iban), 4..-1//1))
@@ -339,7 +347,8 @@ defmodule Bankster.Iban do
339347
end
340348
end
341349

342-
@spec iban_violates_checksum?(String.t()) :: boolean
350+
# - Check whether a given IBAN violates the required checksum.
351+
@spec iban_violates_checksum?(binary()) :: boolean
343352
defp iban_violates_checksum?(iban) do
344353
remainder =
345354
for(<<c <- bban(iban) <> country_code(iban) <> "00">>,

0 commit comments

Comments
 (0)