Skip to content

Add CPF generator #501

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
daaf26f
Add CPF generator
rohlacanna Mar 10, 2023
4f15355
Update changelog and usage
rohlacanna May 27, 2024
b2333da
Merge branch 'master' into feature/cpf-generator
rohlacanna May 27, 2024
0d08ed2
feat: add release automation (#581)
igas Jan 31, 2025
03f721e
chore(deps): update dependency erlang to v27.2.1 (#583)
renovate[bot] Jan 31, 2025
bfea5c4
fix(deps): add missing @semantic-release/exec (#584)
igas Jan 31, 2025
8ed49eb
fix(ci): typo in workflow path (#585)
igas Jan 31, 2025
c25f43e
fix(ci): add workflow_call (#586)
igas Jan 31, 2025
43b1e73
chore(deps): update actions/setup-node action to v4.2.0 (#588)
renovate[bot] Jan 31, 2025
2cdca6c
chore(deps): update node.js to v22.13.1 (#587)
renovate[bot] Jan 31, 2025
2a39b40
chore(deps): update dependency elixir to v1.18.2 (#589)
renovate[bot] Jan 31, 2025
d49419a
fix(ci): allow write (#590)
igas Jan 31, 2025
d78c8f1
fix(ci): permissions (#591)
igas Jan 31, 2025
af4f497
chore(release): v0.19.0-alpha.1 [skip ci]
semantic-release-bot Jan 31, 2025
3f8cbc1
fix: makeup packages (#592)
igas Jan 31, 2025
d6cbea5
Revert "chore(release): v0.19.0-alpha.1 [skip ci]"
igas Jan 31, 2025
572dfb4
fix: docs generation (#594)
igas Feb 1, 2025
a55add8
chore(release): v0.19.0-alpha.1 [skip ci]
semantic-release-bot Feb 1, 2025
8cdee42
chore(deps): update dependency @semantic-release/exec to v7 (#593)
renovate[bot] Feb 1, 2025
700a45a
chore(deps): update dependency @semantic-release/exec to v7.0.2 (#595)
renovate[bot] Feb 2, 2025
4159a3c
chore(deps): update dependency @semantic-release/exec to v7.0.3 (#597)
renovate[bot] Feb 5, 2025
ab347a3
chore(deps): update dependency erlang to v27.2.2 (#601)
renovate[bot] Feb 7, 2025
731cba3
chore(deps): update dependency ex_doc to == 0.37.0 (#598)
renovate[bot] Feb 7, 2025
1f42d2b
chore(deps): lock file maintenance (#596)
renovate[bot] Feb 7, 2025
3d0938e
Merge branch 'master' into feature/cpf-generator
rohlacanna Feb 10, 2025
68de3bc
Merge branch 'elixirs:dev' into feature/cpf-generator
rohlacanna Feb 10, 2025
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
name: ci

on:
push:
pull_request:
workflow_call:

jobs:
credo:
runs-on: ubuntu-24.04
permissions:
contents: read
security-events: write
steps:
- uses: actions/checkout@v4.2.2
- uses: actions/cache@v4.2.0
Expand Down
70 changes: 70 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Release

on:
push:
branches:
- master
- dev

permissions:
contents: read

jobs:
test:
uses: ./.github/workflows/ci.yaml
permissions:
contents: read
security-events: write

release:
needs: test

runs-on: ubuntu-24.04

steps:
- name: Checkout code
uses: actions/checkout@v4.2.2
with:
persist-credentials: false

- name: Set up Elixir
id: setup-beam
uses: erlef/setup-beam@v1.18.2
with:
version-type: strict
version-file: .tool-versions

- name: Restore dependencies cache
uses: actions/cache@v4.2.0
env:
cache-key: deps
with:
key: ${{ env.cache-key }}-${{ runner.os }}-${{ steps.setup-beam.outputs.otp-version }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}-git-${{ github.sha }}
restore-keys: |
${{ env.cache-key }}-${{ runner.os }}-${{ steps.setup-beam.outputs.otp-version }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}
${{ env.cache-key }}-${{ runner.os }}-${{ steps.setup-beam.outputs.otp-version }}-${{ steps.setup-beam.outputs.elixir-version }}
path: |
deps
_build

- name: Install dependencies
run: |
mix local.hex --force
mix do deps.get, deps.compile

- name: Use Node.js LTS
uses: actions/setup-node@v4.2.0
with:
node-version-file: ".tool-versions"

- name: Install packages
run: npm ci

- name: Audit npm signatures
run: npm audit signatures

- name: Run Semantic Release
run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
*.ez
*.iml
*.swp
.DS_Store
.envrc
/.elixir_ls
/.idea
/.vscode
/_build
/deps
/.idea
/doc
/.elixir_ls
/.vscode
/node_modules
erl_crash.dump
*.ez
*.swp
*.iml
.DS_Store
.tool-versions
90 changes: 90 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"branches": [
"master",
{
"name": "dev",
"prerelease": "alpha",
"channel": "alpha"
}
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [],
"parserOpts": {
"noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES"]
}
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"parserOpts": {
"noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES"]
},
"writerOpts": {
"commitsSort": ["subject", "scope"]
}
}
],
[
"semantic-release-replace-plugin",
{
"replacements": [
{
"files": ["mix.exs"],
"from": "@version \".*\"",
"to": "@version \"${nextRelease.version}\"",
"results": [
{
"file": "mix.exs",
"hasChanged": true,
"numMatches": 1,
"numReplacements": 1
}
],
"countMatches": true
},
{
"files": ["README.md"],
"from": "\"~> .*\"",
"to": "\"~> ${nextRelease.version}\"",
"results": [
{
"file": "README.md",
"hasChanged": true,
"numMatches": 2,
"numReplacements": 2
}
],
"countMatches": true
}
]
}
],
[
"@semantic-release/changelog",
{
"changelogFile": "CHANGELOG.md",
"changelogTitle": "# Changelog"
}
],
[
"@semantic-release/git",
{
"assets": ["mix.exs", "README.md", "CHANGELOG.md"],
"message": "chore(release): v${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
],
"@semantic-release/github",
[
"@semantic-release/exec",
{
"publishCmd": "mix hex.publish --yes"
}
]
]
}
3 changes: 3 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
erlang 27.2.2
elixir 1.18.2
nodejs 22.13.1
26 changes: 25 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Change Log
# Changelog

## [0.19.0-alpha.1](https://github.com/elixirs/faker/compare/v0.18.0...v0.19.0-alpha.1) (2025-02-01)

### Features

* add release automation ([#581](https://github.com/elixirs/faker/issues/581)) ([0d08ed2](https://github.com/elixirs/faker/commit/0d08ed231c742b247e692f0b9a3773a4ce2e172b))

### Bug Fixes

* **deps:** add missing @semantic-release/exec ([#584](https://github.com/elixirs/faker/issues/584)) ([bfea5c4](https://github.com/elixirs/faker/commit/bfea5c430bb518e04fd1d508266ccb2c72c07cfd))
* **ci:** add workflow_call ([#586](https://github.com/elixirs/faker/issues/586)) ([c25f43e](https://github.com/elixirs/faker/commit/c25f43efa234d349a6adab22e60df58fcaf61db1))
* **ci:** allow write ([#590](https://github.com/elixirs/faker/issues/590)) ([d49419a](https://github.com/elixirs/faker/commit/d49419acdb81228ce0a5cfdfa8120fa427a892ad))
* docs generation ([#594](https://github.com/elixirs/faker/issues/594)) ([572dfb4](https://github.com/elixirs/faker/commit/572dfb4060a7d2a266da2edc3bde9111a8bd6c66))
* elixir 17 & 18 compatibility ([#565](https://github.com/elixirs/faker/issues/565)) ([f3d4808](https://github.com/elixirs/faker/commit/f3d480885b12fba632ff21cb3f3b74845c362cb4))
* elixir deprecations ([#571](https://github.com/elixirs/faker/issues/571)) ([b7e4af0](https://github.com/elixirs/faker/commit/b7e4af033b3d5e1630cf642af96c5d7c03100c6f))
* makeup packages ([#592](https://github.com/elixirs/faker/issues/592)) ([3f8cbc1](https://github.com/elixirs/faker/commit/3f8cbc143184344d09ef53e1740eff0c49379181))
* **ci:** permissions ([#591](https://github.com/elixirs/faker/issues/591)) ([d78c8f1](https://github.com/elixirs/faker/commit/d78c8f1abf683cf9b36908338b20e7dd064fe330))
* remove nil option for Airports IATA ([#492](https://github.com/elixirs/faker/issues/492)) ([59951ec](https://github.com/elixirs/faker/commit/59951ec3a72babae936fff22883b5e821971b8b6))
* **ci:** typo in workflow path ([#585](https://github.com/elixirs/faker/issues/585)) ([8ed49eb](https://github.com/elixirs/faker/commit/8ed49eba4096eac5b12b6913e4775cb2f69785c4))

All notable changes to this project will be documented in this file.

Expand Down Expand Up @@ -56,6 +75,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 All @@ -66,13 +86,15 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
### Deprecated

### Removed

- Travis CI for CI workflow [[@anthonator](https://github.com/anthonator)]

### Fixed

- `Faker.Team` `creature/0` and `name/0` documentation examples fixed [[@yuriploc](https://github.com/yuriploc)]
- Fix bidirectional formatting [[@MichalDolata](https://github.com/MichalDolata)]
- Extra split for Virgin Islands in the country sampler [[intercaetera](https://github.com/intercaetera)]

### Security

## 0.16.0
Expand Down Expand Up @@ -147,6 +169,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
- `Faker.Phone.PtBr`[[@netond](https://github.com/netond)]

### Changed

- Fix `Faker.Code.Iban.iban` and `Faker.Gov.It.fiscal_id` doctests [[@vbrazo](https://github.com/vbrazo)]
- `Faker.Address.Es` [[@emig](https://github.com/emig)]
- Set minimum Elixir version to 1.6
Expand All @@ -165,6 +188,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
- Elixir 1.4 and 1.5 support

### Fixed

- `Faker.Util.upper_letter/0` documentation examples [[@michaelfich](https://github.com/michaelfich)]

### Security
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ fake data.

## Quickstart

* add `{:faker, "~> 0.18"}` to your deps in `mix.exs`:
* add `{:faker, "~> 0.19.0-alpha.1"}` to your deps in `mix.exs`:

```elixir
defp deps do
[
{:faker, "~> 0.18", only: :test}
{:faker, "~> 0.19.0-alpha.1", only: :test}
]
end
```
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
8 changes: 4 additions & 4 deletions lib/faker/airports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1029,13 +1029,13 @@ defmodule Faker.Airports do
## Examples

iex> Faker.Airports.iata()
"BFU"
"UBJ"
iex> Faker.Airports.iata()
"FMM"
"CKB"
iex> Faker.Airports.iata()
"YUS"
"JAL"
iex> Faker.Airports.iata()
"YPH"
"PES"
"""
@spec iata() :: String.t()
sampler(:iata, [
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
Loading