-
Notifications
You must be signed in to change notification settings - Fork 129
[2.4.0 stack 2/18] Internals: mod11 variants, shared helpers, never-throw, removeAccents #508
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
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfadaca
refactor(internals): one function per folder, mod11 variants
hyanmandian 481fb92
fix: never throw on nullish input across the public API
hyanmandian f84dc93
perf: remove top-level allocations to restore tree-shaking
hyanmandian 1a49717
refactor(cnh): extract checksum verifier calculations into internals
hyanmandian 18ff45e
feat(remove-accents): add removeAccents
hyanmandian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/_internals/calculate-cnh-first-verifier/calculate-cnh-first-verifier.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { describe, expect, test } from "../test/runtime"; | ||
| import { calculateCnhFirstVerifier } from "./calculate-cnh-first-verifier"; | ||
|
|
||
| describe("calculateCnhFirstVerifier", () => { | ||
| test("should calculate the first verifier without decrement", () => { | ||
| expect(calculateCnhFirstVerifier("000000001")).toEqual({ firstVerifier: 1, decrement: 0 }); | ||
| }); | ||
|
|
||
| test("should calculate the first verifier with decrement when remainder is 10 or more", () => { | ||
| expect(calculateCnhFirstVerifier("000000093")).toEqual({ firstVerifier: 0, decrement: 2 }); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
src/_internals/calculate-cnh-first-verifier/calculate-cnh-first-verifier.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| export type CnhFirstVerifier = { | ||
| /** The first check digit, 0 to 9. */ | ||
| firstVerifier: number; | ||
| /** The correction to apply to the second verifier, 0 or 2. */ | ||
| decrement: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Calculates the first verification digit of a Brazilian CNH (Carteira Nacional de Habilitação) | ||
| * from its 9-digit base number. | ||
| * | ||
| * @param {string} base - The 9-digit CNH base number. | ||
| * @returns {CnhFirstVerifier} The first verification digit and the decrement that must be | ||
| * applied when calculating the second verification digit. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * calculateCnhFirstVerifier("000000093"); // { firstVerifier: 0, decrement: 2 } | ||
| * ``` | ||
| */ | ||
| export const calculateCnhFirstVerifier = (base: string): CnhFirstVerifier => { | ||
| let sum = 0; | ||
|
|
||
| for (let i = 0; i < 9; i++) { | ||
| sum += (base.charCodeAt(i) - 48) * (9 - i); | ||
| } | ||
|
|
||
| const remainder = sum % 11; | ||
|
|
||
| if (remainder >= 10) return { firstVerifier: 0, decrement: 2 }; | ||
|
|
||
| return { firstVerifier: remainder, decrement: 0 }; | ||
| }; |
12 changes: 12 additions & 0 deletions
12
src/_internals/calculate-cnh-second-verifier/calculate-cnh-second-verifier.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { describe, expect, test } from "../test/runtime"; | ||
| import { calculateCnhSecondVerifier } from "./calculate-cnh-second-verifier"; | ||
|
|
||
| describe("calculateCnhSecondVerifier", () => { | ||
| test("should calculate the second verifier", () => { | ||
| expect(calculateCnhSecondVerifier({ base: "000000001", decrement: 0 })).toBe(9); | ||
| }); | ||
|
|
||
| test("should wrap around when the decrement makes the result negative", () => { | ||
| expect(calculateCnhSecondVerifier({ base: "000000093", decrement: 2 })).toBe(9); | ||
| }); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
src/_internals/calculate-cnh-second-verifier/calculate-cnh-second-verifier.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| export type CalculateCnhSecondVerifierParams = { | ||
| /** The 9 digit CNH registry number. */ | ||
| base: string; | ||
| /** The correction the first verifier reported, 0 or 2. */ | ||
| decrement: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Calculates the second verification digit of a Brazilian CNH (Carteira Nacional de Habilitação) | ||
| * from its 9-digit base number and the decrement produced by `calculateCnhFirstVerifier`. | ||
| * | ||
| * @param {CalculateCnhSecondVerifierParams} params - The calculation parameters. | ||
| * @param {string} params.base - The 9-digit CNH base number. | ||
| * @param {number} params.decrement - The decrement calculated alongside the first verification digit. | ||
| * @returns {number} The calculated second verification digit (0-9). | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * calculateCnhSecondVerifier({ base: "000000093", decrement: 2 }); // 9 | ||
| * ``` | ||
| */ | ||
| export const calculateCnhSecondVerifier = ({ | ||
| base, | ||
| decrement, | ||
| }: CalculateCnhSecondVerifierParams): number => { | ||
| let sum = 0; | ||
|
|
||
| for (let i = 0; i < 9; i++) { | ||
| sum += (base.charCodeAt(i) - 48) * (i + 1); | ||
| } | ||
|
|
||
| let secondVerifier = (sum % 11) - decrement; | ||
|
|
||
| if (secondVerifier < 0) secondVerifier += 11; | ||
|
|
||
| if (secondVerifier >= 10) secondVerifier = 0; | ||
|
|
||
| return secondVerifier; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /** Digits of a CEP. */ | ||
| export const CEP_LENGTH = 8; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /** Digits of a CPF. */ | ||
| export const CPF_LENGTH = 11; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** Digits of a PIS (Programa de Integração Social) number. */ | ||
| export const PIS_LENGTH = 11; | ||
|
|
||
| /** | ||
| * Weights used to calculate the PIS (Programa de Integração Social) check digit. | ||
| */ | ||
| export const PIS_WEIGHTS = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /** Digits of a processo jurídico number (`NNNNNNNDDAAAAJTROOOO`, Resolução CNJ nº 65/2008). */ | ||
| export const PROCESSO_JURIDICO_LENGTH = 20; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Restore the
exportmodifier onisRetryableFetchError.The
"./*"export pattern permits deep imports. The previous version exportedisRetryableFetchError, but the current declaration is local. This breaks consumers that import the helper directly. Restoreexport const isRetryableFetchError.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not changing this. src/_internals is not an entry point: the "./*" export maps to the per-util bundles that vp pack emits from src/, and _internals is bundled into them, so @brazilian-utils/brazilian-utils/fetch-with-retry never existed. knip flagged the export as unused, which is why it went.