From a47e331b254cd9af1823c1a3ac7cb41c12913c5b Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Fri, 13 Dec 2024 00:34:25 -0500 Subject: [PATCH] Bugfixes. Bump to 0.1.1 --- README.md | 2 +- lib/ukraine_taxid_ex/base_parser.ex | 9 ++++++- lib/ukraine_taxid_ex/base_validator.ex | 33 ++++++++++++-------------- lib/ukraine_taxid_ex/edrpou/parser.ex | 4 +--- lib/ukraine_taxid_ex/itin/error.ex | 12 +++++----- lib/ukraine_taxid_ex/itin/parser.ex | 2 +- mix.exs | 2 +- 7 files changed, 33 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 58735a5..295b6e2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The package can be installed by adding `ukraine_tax_id` to your list of dependen ```elixir def deps do [ - {:ukraine_tax_id, "~> 0.1.0"} + {:ukraine_tax_id, "~> 0.1.1"} ] end ``` diff --git a/lib/ukraine_taxid_ex/base_parser.ex b/lib/ukraine_taxid_ex/base_parser.ex index 92cad60..812e6ef 100644 --- a/lib/ukraine_taxid_ex/base_parser.ex +++ b/lib/ukraine_taxid_ex/base_parser.ex @@ -1,6 +1,12 @@ defmodule UkraineTaxidEx.BaseParser do + @typedoc """ + Options for parsing: + - `:normalize?` - if true, pad the string to the right length (8 for EDRPOU, 10 for ITIN) + - `:clean?` - if true, remove non-digit characters + """ @type options :: [normalize?: boolean, clean?: boolean] - @callback parse(string :: String.t(), options :: options()) :: {:ok, term} | {:error, atom} + + @callback parse(code :: String.t(), options :: options()) :: {:ok, term} | {:error, atom} defmacro __using__(_) do quote do @@ -34,6 +40,7 @@ defmodule UkraineTaxidEx.BaseParser do end defp generate({:error, error}), do: {:error, error} + defp generate({:ok, string}), do: generate(string) end end end diff --git a/lib/ukraine_taxid_ex/base_validator.ex b/lib/ukraine_taxid_ex/base_validator.ex index 93fe047..36d6af8 100644 --- a/lib/ukraine_taxid_ex/base_validator.ex +++ b/lib/ukraine_taxid_ex/base_validator.ex @@ -1,12 +1,12 @@ defmodule UkraineTaxidEx.BaseValidator do - @callback validate(String.t()) :: - {:ok, String.t()} + @callback validate(code :: String.t()) :: + {:ok, code :: String.t()} | {:error, :length_too_short | :length_too_long | :invalid_length | :invalid_checksum} - @callback violates_length?(String.t()) :: boolean - @callback violates_length_too_short?(String.t()) :: boolean - @callback violates_length_too_long?(String.t()) :: boolean - @callback violates_checksum?(String.t()) :: boolean + @callback violates_length?(code :: String.t()) :: boolean + @callback violates_length_too_short?(code :: String.t()) :: boolean + @callback violates_length_too_long?(code :: String.t()) :: boolean + @callback violates_checksum?(code :: String.t()) :: boolean defmacro __using__(_) do quote do @@ -31,28 +31,25 @@ defmodule UkraineTaxidEx.BaseValidator do @doc "Check whether a given EDRPOU violates the required length" @impl BaseValidator - @spec violates_length?(String.t()) :: boolean - def violates_length?(string), - do: String.length(string) != length() + @spec violates_length?(code :: String.t()) :: boolean + def violates_length?(code), do: String.length(code) != length() @doc "Check whether a given EDRPOU too short" @impl BaseValidator - @spec violates_length_too_short?(String.t()) :: boolean - def violates_length_too_short?(string), - do: String.length(string) < length() + @spec violates_length_too_short?(code :: String.t()) :: boolean + def violates_length_too_short?(code), do: String.length(code) < length() @doc "Check whether a given EDRPOU too long" @impl BaseValidator - @spec violates_length_too_long?(String.t()) :: boolean - def violates_length_too_long?(string), - do: String.length(string) > length() + @spec violates_length_too_long?(code :: String.t()) :: boolean + def violates_length_too_long?(code), do: String.length(code) > length() @doc "Check whether a given EDRPOU has correct checksum" @impl BaseValidator - @spec violates_checksum?(String.t()) :: boolean - def violates_checksum?(string) do + @spec violates_checksum?(code :: String.t()) :: boolean + def violates_checksum?(code) do {digits, check_digit} = - string + code |> digits() |> digits_and_check_digit() diff --git a/lib/ukraine_taxid_ex/edrpou/parser.ex b/lib/ukraine_taxid_ex/edrpou/parser.ex index 544b68b..03c04aa 100644 --- a/lib/ukraine_taxid_ex/edrpou/parser.ex +++ b/lib/ukraine_taxid_ex/edrpou/parser.ex @@ -53,9 +53,7 @@ defmodule UkraineTaxidEx.Edrpou.Parser do | :length_too_long | :invalid_checksum} - defp generate({:error, error}), do: {:error, error} - - defp generate({:ok, string}) do + defp generate(string) do digits = digits(string) %{code: string, check_sum: check_sum(digits), check_digit: check_digit(digits)} diff --git a/lib/ukraine_taxid_ex/itin/error.ex b/lib/ukraine_taxid_ex/itin/error.ex index 4f5b49c..9e3273a 100644 --- a/lib/ukraine_taxid_ex/itin/error.ex +++ b/lib/ukraine_taxid_ex/itin/error.ex @@ -2,20 +2,20 @@ defmodule UkraineTaxidEx.Itin.Error do @type error() :: :invalid_length | :invalid_checksum - | :length_to_long - | :length_to_short + | :length_too_long + | :length_too_short @type errors() :: [error()] @errors [ :invalid_length, :invalid_checksum, - :length_to_long, - :length_to_short + :length_too_long, + :length_too_short ] @messages [ invalid_length: "ITIN violates the required length", invalid_checksum: "ITIN checksum is invalid", - length_to_long: "ITIN longer then required length", - length_to_short: "ITIN shorter then required length" + length_too_long: "ITIN longer then required length", + length_too_short: "ITIN shorter then required length" ] @spec message(error()) :: String.t() diff --git a/lib/ukraine_taxid_ex/itin/parser.ex b/lib/ukraine_taxid_ex/itin/parser.ex index 25058c4..c8da438 100644 --- a/lib/ukraine_taxid_ex/itin/parser.ex +++ b/lib/ukraine_taxid_ex/itin/parser.ex @@ -59,7 +59,7 @@ defmodule UkraineTaxidEx.Itin.Parser do use UkraineTaxidEx.BaseParser - defp generate({:ok, string}) do + defp generate(string) do digits = digits(string) %{ diff --git a/mix.exs b/mix.exs index 64e1560..e21c599 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule UkraineTaxidEx.MixProject do @module UkraineTaxidEx @source_url "https://g.tulz.dev/opensource/ukraine-taxid-ex" @docs_url "https://hexdocs.pm/#{@app}" - @version "0.1.0" + @version "0.1.1" def project do [