ElixirCLRut Examples
Installation
Mix.install([
{:elixircl_rut, git: "https://github.com/ElixirCL/rut.git", branch: "main"}
])
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.
# "6.300.948-2"
ElixirCLRut.format("6,3.0.0,9.48.2...")
It will assume the rut has digit. Otherwise you can pass false
to include it.
using format/2
or dashing the digit.
# "6.300.948-2"
ElixirCLRut.format("6300948-2")
ElixirCLRut.format("6300948", false)
Pass the separator too
# "6,300,948-2"
ElixirCLRut.format("63009482", ",")
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. It will assume it includes
the checkdigit. So you must pass false
to calculate it.
ElixirCLRut.from("6300948", false).checkdigit
Or use the wrapper
# 2
ElixirCLRut.dv("6300948", false)
# 2
ElixirCLRut.dv("63009482")
clean
You can clean the rut too.
ElixirCLRut.clean("63fk00s948-2")
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.valid("0-0")
|> CustomValidations.length_above_3()
|> CustomValidations.not_all_zeroes()
Check the optional validations
ElixirCLRut.valid("1-9")
|> ElixirCLRut.Validations.equal_or_above_1M()
|> ElixirCLRut.Validations.equal_or_below_100M()
ElixirCLRut.valid("1000000-9")
|> ElixirCLRut.Validations.equal_or_above_1M()
|> ElixirCLRut.Validations.equal_or_below_100M()
ElixirCLRut.valid("100.000.001-5")
|> ElixirCLRut.Validations.equal_or_above_1M()
|> ElixirCLRut.Validations.equal_or_below_100M()