Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 1.82 KB

EXAMPLES.livemd

File metadata and controls

90 lines (68 loc) · 1.82 KB

ElixirCLRut Examples

Installation

Mix.install([
  {:elixircl_rut, git: "https://github.com/ElixirCL/rut.git", branch: "main"}
])

Custom Validations

Custom Validations are used when you need to specify stricter rules than just the normal algorithm validations.

defmodule CustomValidations do
  alias ElixirCLRut.Token

  def length_above_3(input) do
    # fun fact: the person with the lowest rut is 10-8
    case Enum.count(input.from.normalized) < 3 do
      true -> Token.error(input, :length_less_than_3)
      false -> input
    end
  end

  def not_all_zeroes(input) do
    # get all the zeroes
    zeroes = Enum.filter(input.from.normalized, &(&1 == 0))

    # if we have all zeroes then is not valid
    case zeroes == input.from.normalized do
      true -> Token.error(input, :all_zeroes)
      false -> input
    end
  end
end
ElixirCLRut.validate("0-0")
|> CustomValidations.length_above_3()
|> CustomValidations.not_all_zeroes()

valid?

If you are happy with the standard validations you can use the quick function valid? that will return just a boolean

ElixirCLRut.valid?("1-9")

format

You can quickly get the format. This will append the correct digit if not available.

# "6.300.948-2"
ElixirCLRut.format("6,3.0.0,9.48....")

You can specify if the rut has the check digit by using format/2 or dashing the digit.

# "6.300.948-2"
ElixirCLRut.format("6300948-2")
ElixirCLRut.format("63009482", true)

You can also specify if the rut has the check digit by using dashed?:true and the separator character by passing separator: params.

# "6,300,948-2"
ElixirCLRut.format("63009482", dashed?:true, separator: ",")

from

You can get the check digit creating the struct

ElixirCLRut.from("6300948").checkdigit