Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
webdeb committed Oct 26, 2017
0 parents commit 692c245
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
miniban-*.tar

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Miniban

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `miniban` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:miniban, "~> 0.1.0"}
]
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/miniban](https://hexdocs.pm/miniban).

7 changes: 7 additions & 0 deletions benchmarks/validate_iban.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
iterations = 100_000
iban = "MD75EX0900002374642125EU"

Benchee.run(%{
"Miniban" => fn -> for _ <- 0..iterations, do: Miniban.is_valid_iban?(iban) end,
"Bankster" => fn -> for _ <- 0..iterations, do: Bankster.iban_valid?(iban) end,
})
30 changes: 30 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :miniban, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:miniban, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
31 changes: 31 additions & 0 deletions lib/miniban.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Miniban do
@moduledoc """
Documentation for Miniban.
"""

@country_code_len %{
"AD" => 24, "AE" => 23, "AT" => 20, "AZ" => 28, "BA" => 20, "BE" => 16, "BG" => 22, "BH" => 22, "BR" => 29,
"CH" => 21, "CR" => 21, "CY" => 28, "CZ" => 24, "DE" => 22, "DK" => 18, "DO" => 28, "EE" => 20, "ES" => 24,
"FI" => 18, "FO" => 18, "FR" => 27, "GB" => 22, "GI" => 23, "GL" => 18, "GR" => 27, "GT" => 28, "HR" => 21,
"HU" => 28, "IE" => 22, "IL" => 23, "IS" => 26, "IT" => 27, "JO" => 30, "KW" => 30, "KZ" => 20, "LB" => 28,
"LI" => 21, "LT" => 20, "LU" => 20, "LV" => 21, "MC" => 27, "MD" => 24, "ME" => 22, "MK" => 19, "MR" => 27,
"MT" => 31, "MU" => 30, "NL" => 18, "NO" => 15, "PK" => 24, "PL" => 28, "PS" => 29, "PT" => 25, "QA" => 29,
"RO" => 24, "RS" => 22, "SA" => 24, "SE" => 24, "SI" => 19, "SK" => 24, "SM" => 27, "TN" => 24, "TR" => 26,
}

@doc """
Validate an IBAN
"""
def is_valid_iban?(input) do
{ prefix, rest } = String.replace(input, ~r/ +/, "")
|> String.upcase()
|> String.split_at(4)

iban = rest <> prefix

@country_code_len[String.slice(prefix, 0..1)] == String.length(iban) &&
1 == Regex.replace(~r/[A-Z]/, iban, fn << c :: 8 >> -> to_string(c - 55) end)
|> String.to_integer()
|> rem(97)
end
end
29 changes: 29 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Miniban.MixProject do
use Mix.Project

def project do
[
app: :miniban,
version: "0.1.0",
elixir: "~> 1.6-dev",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# for benchmarks comparison
{:benchee, "~> 0.10", only: :dev},
{:bankster, "~> 0.2.2", only: :dev}
]
end
end
5 changes: 5 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%{
"bankster": {:hex, :bankster, "0.2.2", "28015dc2a05e44fd6bf8f2920532d8df60c687430b1e0a9927e8bfc805395faa", [], [], "hexpm"},
"benchee": {:hex, :benchee, "0.10.0", "7227e83ab81e4753610125d251bbf78c99167fbe7f931a8cbfbc3eda8526259c", [], [{:deep_merge, "~> 0.1", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm"},
"deep_merge": {:hex, :deep_merge, "0.1.1", "c27866a7524a337b6a039eeb8dd4f17d458fd40fbbcb8c54661b71a22fffe846", [], [], "hexpm"},
}
22 changes: 22 additions & 0 deletions test/miniban_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule MinibanTest do
use ExUnit.Case
doctest Miniban

@valid [
"MD75EX0900002374642125EU",
"BE68539007547034",
"NL86INGB0002445588",
]

@invalid [
"ZZ68539007547034",
"BE68539007547035",
]

test "valid iban" do
assert Enum.reduce(@valid, true, fn iban, ok -> ok && Miniban.is_valid_iban?(iban) end)
end
test "invalid iban" do
refute Enum.reduce(@invalid, true, fn iban, ok -> ok && Miniban.is_valid_iban?(iban) end)
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 692c245

Please sign in to comment.