Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CPF generator #501

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
- `Faker.Aws.PtBr.region_name/0` [[@f-francine](https://github.com/f-francine)]
- `Faker.Fruit.PtBr` [[@f-francine](https://github.com/f-francine)]
- `Faker.Commerce.PtBr` [[@f-francine](https://github.com/f-francine)]
- `Faker.Gov.PtBr.cpf/0` [[@rohlacanna](https://github.com/rohlacanna)]

### Changed

Expand Down
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<!-- G -->
- [Faker.Gov.Us](lib/faker/gov/us.ex)
- [Faker.Gov.It](lib/faker/gov/it.ex)
- [Faker.Gov.PtBr](lib/faker/gov/pt_br.ex)
<!-- H -->
<!-- I -->
- [Faker.Industry](lib/faker/industry.ex)
Expand Down
59 changes: 59 additions & 0 deletions lib/faker/gov/pt_br.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defmodule Faker.Gov.PtBr do
@moduledoc """
Generating BR Taxpayer Identification numbers
"""

@doc """
Returns a random BR CPF number

## Examples

iex> Faker.Gov.PtBr.cpf
"396.517.511-55"
iex> Faker.Gov.PtBr.cpf
"457.899.570-88"
iex> Faker.Gov.PtBr.cpf
"283.058.997-33"
iex> Faker.Gov.PtBr.cpf
"294.304.877-66"
"""
@spec cpf() :: String.t()
def cpf do
"#{area()}.#{group()}.#{serial()}-#{check_digits()}"
|> String.replace(~r/(\d{3})(\d{3})(\d{3})(\d{2})/, "\\1.\\2.\\3-\\4")
end

defp area do
"#{Faker.random_between(0, 999)}" |> String.pad_leading(3, "0")
end

defp group do
"#{Faker.random_between(0, 999)}" |> String.pad_leading(3, "0")
end

defp serial do
"#{Faker.random_between(0, 999)}" |> String.pad_leading(3, "0")
end

defp check_digits do
base = "#{area()}#{group()}#{serial()}"

first_check_digit = calculate_cpf_check_digit(base)
second_check_digit = calculate_cpf_check_digit("#{base}#{first_check_digit}")

"#{first_check_digit}#{second_check_digit}"
end

defp calculate_cpf_check_digit(base) do
digits = base |> String.graphemes() |> Enum.map(&String.to_integer/1)

sum =
Enum.zip(digits, [10, 9, 8, 7, 6, 5, 4, 3, 2])
|> Enum.map(fn {digit, weight} -> digit * weight end)
|> Enum.reduce(&+/2)

remainder = rem(sum, 11)

if remainder < 2, do: 0, else: 11 - remainder
end
end
37 changes: 37 additions & 0 deletions test/faker/gov/pt_br.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Faker.Gov.PtBrTest do
use ExUnit.Case, async: true

import Faker.Gov.PtBr

doctest Faker.Gov.PtBr

describe "cpf" do
test "returns a valid cpf" do
assert Regex.match?(~r/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/, Faker.Gov.PtBr.cpf())
end
end

describe "area" do
test "returns a three-digit number" do
assert String.length(Faker.Gov.PtBr.area()) == 3
end
end

describe "group" do
test "returns a three-digit number" do
assert String.length(Faker.Gov.PtBr.group()) == 3
end
end

describe "serial" do
test "returns a three-digit number" do
assert String.length(Faker.Gov.PtBr.serial()) == 3
end
end

describe "check_digits" do
test "returns a two-digit number" do
assert String.length(Faker.Gov.PtBr.check_digits()) == 2
end
end
end