Skip to content

Commit 409780d

Browse files
committed
fix: adjust apis
1 parent 0b57240 commit 409780d

44 files changed

Lines changed: 260 additions & 60 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/pt-br/utilities.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ formatCpf('746506880', { pad: true }); // 007.465.068-80
2525

2626
## parseCpf
2727

28-
Remove a formatação do CPF e retorna apenas os dígitos.
28+
Remove a formatação do CPF, mantém apenas os dígitos e limita o resultado a 11 dígitos.
2929

3030
```javascript
3131
import { parseCpf } from '@brazilian-utils/brazilian-utils';
@@ -67,7 +67,7 @@ formatCnpj('12OUT345000199', { version: 2 }); // 12.OUT.345/0001-99
6767

6868
## parseCnpj
6969

70-
Remove a formatação do CNPJ e retorna um valor normalizado.
70+
Remove a formatação do CNPJ, retorna um valor normalizado e limita o resultado a 14 caracteres.
7171

7272
```javascript
7373
import { parseCnpj } from '@brazilian-utils/brazilian-utils';
@@ -119,7 +119,7 @@ formatBoleto('1900000901149', { pad: true }); // 00000.00000 00000.000019 00000.
119119

120120
## parseBoleto
121121

122-
Remove a formatação do boleto e retorna apenas os dígitos.
122+
Remove a formatação do boleto, mantém apenas os dígitos e limita o resultado a 47 dígitos.
123123

124124
```javascript
125125
import { parseBoleto } from '@brazilian-utils/brazilian-utils';
@@ -182,12 +182,13 @@ formatPhone('11900000000', { mask: 'auto' }); // Detecta automaticamente a másc
182182

183183
## parsePhone
184184

185-
Remove a formatação do telefone e retorna apenas os dígitos.
185+
Remove a formatação do telefone, mantém apenas os dígitos e limita o resultado a 11 dígitos.
186186

187187
```javascript
188188
import { parsePhone } from '@brazilian-utils/brazilian-utils';
189189

190190
parsePhone('(11) 90000-0000'); // 11900000000
191+
parsePhone('+55 (11) 90000-0000'); // 55119000000
191192
```
192193

193194
## isValidMobilePhone
@@ -258,7 +259,7 @@ formatPis('123456789', { pad: true }); // 001.23456.78-9
258259

259260
## parsePis
260261

261-
Remove a formatação do PIS e retorna apenas os dígitos.
262+
Remove a formatação do PIS, mantém apenas os dígitos e limita o resultado a 11 dígitos.
262263

263264
```javascript
264265
import { parsePis } from '@brazilian-utils/brazilian-utils';
@@ -278,7 +279,7 @@ formatCep('92500000'); // 92500-000
278279

279280
## parseCep
280281

281-
Remove a formatação do CEP e retorna apenas os dígitos.
282+
Remove a formatação do CEP, mantém apenas os dígitos e limita o resultado a 8 dígitos.
282283

283284
```javascript
284285
import { parseCep } from '@brazilian-utils/brazilian-utils';
@@ -328,7 +329,7 @@ formatProcessoJuridico('00020802520125150049'); // 0002080-25.2012.515.0049
328329

329330
## parseProcessoJuridico
330331

331-
Remove a formatação do processo jurídico e retorna apenas os dígitos.
332+
Remove a formatação do processo jurídico, mantém apenas os dígitos e limita o resultado a 20 dígitos.
332333

333334
```javascript
334335
import { parseProcessoJuridico } from '@brazilian-utils/brazilian-utils';
@@ -518,7 +519,7 @@ isValidPassport('12345678'); // false
518519

519520
## formatPassport
520521

521-
Formata um número de passaporte brasileiro (maiúsculas, sem símbolos).
522+
Formata um número de passaporte brasileiro (maiúsculas, sem símbolos, limitado a 8 caracteres).
522523

523524
```javascript
524525
import { formatPassport } from '@brazilian-utils/brazilian-utils';
@@ -539,7 +540,7 @@ generatePassport(); // 'RY393097'
539540

540541
## parsePassport
541542

542-
Remove todos os caracteres não alfanuméricos (incluindo '-', '.' e espaços) de um número de passaporte.
543+
Remove todos os caracteres não alfanuméricos de um número de passaporte, converte para maiúsculas e limita o resultado a 8 caracteres.
543544

544545
```javascript
545546
import { parsePassport } from '@brazilian-utils/brazilian-utils';
@@ -591,7 +592,7 @@ generateCnh(); // '02650306461'
591592

592593
## parseCnh
593594

594-
Remove a formatação da CNH e retorna apenas os dígitos.
595+
Remove a formatação da CNH, mantém apenas os dígitos e limita o resultado a 11 dígitos.
595596

596597
```javascript
597598
import { parseCnh } from '@brazilian-utils/brazilian-utils';
@@ -668,7 +669,7 @@ generateLegalNature(); // '2062'
668669

669670
## parseLegalNature
670671

671-
Remove a formatação da natureza jurídica e retorna apenas os dígitos.
672+
Remove a formatação da natureza jurídica, mantém apenas os dígitos e limita o resultado a 4 dígitos.
672673

673674
```javascript
674675
import { parseLegalNature } from '@brazilian-utils/brazilian-utils';
@@ -738,7 +739,7 @@ getFormatLicensePlate('INVALID'); // null
738739

739740
## parseLicensePlate
740741

741-
Remove separadores de uma placa e normaliza para letras maiúsculas.
742+
Remove separadores de uma placa, normaliza para letras maiúsculas e limita o resultado a 7 caracteres.
742743

743744
```javascript
744745
import { parseLicensePlate } from '@brazilian-utils/brazilian-utils';
@@ -816,7 +817,7 @@ generateVoterId('SP'); // título de eleitor aleatório válido de São Paulo
816817

817818
## parseVoterId
818819

819-
Remove a formatação do título de eleitor e retorna apenas os dígitos.
820+
Remove a formatação do título de eleitor, mantém apenas os dígitos e limita o resultado a 12 dígitos.
820821

821822
```javascript
822823
import { parseVoterId } from '@brazilian-utils/brazilian-utils';

docs/utilities.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ formatCpf('746506880', { pad: true }); // 007.465.068-80
2525

2626
## parseCpf
2727

28-
Remove CPF formatting and return only digits.
28+
Remove CPF formatting, keep only digits, and cap the result to 11 digits.
2929

3030
```javascript
3131
import { parseCpf } from '@brazilian-utils/brazilian-utils';
@@ -67,7 +67,7 @@ formatCnpj('12OUT345000199', { version: 2 }); // 12.OUT.345/0001-99
6767

6868
## parseCnpj
6969

70-
Remove CNPJ formatting and return a normalized value.
70+
Remove CNPJ formatting, return a normalized value, and cap the result to 14 characters.
7171

7272
```javascript
7373
import { parseCnpj } from '@brazilian-utils/brazilian-utils';
@@ -119,7 +119,7 @@ formatBoleto('1900000901149', { pad: true }); // 00000.00000 00000.000019 00000.
119119

120120
## parseBoleto
121121

122-
Remove boleto formatting and return only digits.
122+
Remove boleto formatting, keep only digits, and cap the result to 47 digits.
123123

124124
```javascript
125125
import { parseBoleto } from '@brazilian-utils/brazilian-utils';
@@ -182,12 +182,13 @@ formatPhone('11900000000', { mask: 'auto' }); // Automatically detects mask base
182182

183183
## parsePhone
184184

185-
Remove phone formatting and return only digits.
185+
Remove phone formatting, keep only digits, and cap the result to 11 digits.
186186

187187
```javascript
188188
import { parsePhone } from '@brazilian-utils/brazilian-utils';
189189

190190
parsePhone('(11) 90000-0000'); // 11900000000
191+
parsePhone('+55 (11) 90000-0000'); // 55119000000
191192
```
192193

193194
## isValidMobilePhone
@@ -258,7 +259,7 @@ formatPis('123456789', { pad: true }); // 001.23456.78-9
258259

259260
## parsePis
260261

261-
Remove PIS formatting and return only digits.
262+
Remove PIS formatting, keep only digits, and cap the result to 11 digits.
262263

263264
```javascript
264265
import { parsePis } from '@brazilian-utils/brazilian-utils';
@@ -278,7 +279,7 @@ formatCep('92500000'); // 92500-000
278279

279280
## parseCep
280281

281-
Remove CEP formatting and return only digits.
282+
Remove CEP formatting, keep only digits, and cap the result to 8 digits.
282283

283284
```javascript
284285
import { parseCep } from '@brazilian-utils/brazilian-utils';
@@ -328,7 +329,7 @@ formatProcessoJuridico('00020802520125150049'); // 0002080-25.2012.515.0049
328329

329330
## parseProcessoJuridico
330331

331-
Remove processo jurídico formatting and return only digits.
332+
Remove processo jurídico formatting, keep only digits, and cap the result to 20 digits.
332333

333334
```javascript
334335
import { parseProcessoJuridico } from '@brazilian-utils/brazilian-utils';
@@ -518,7 +519,7 @@ isValidPassport('12345678'); // false
518519

519520
## formatPassport
520521

521-
Format a Brazilian passport number (uppercase, without symbols).
522+
Format a Brazilian passport number (uppercase, without symbols, capped to 8 characters).
522523

523524
```javascript
524525
import { formatPassport } from '@brazilian-utils/brazilian-utils';
@@ -539,7 +540,7 @@ generatePassport(); // 'RY393097'
539540

540541
## parsePassport
541542

542-
Remove all non-alphanumeric characters (including '-', '.', and whitespaces) from a passport number.
543+
Remove all non-alphanumeric characters from a passport number, uppercase the result, and cap it to 8 characters.
543544

544545
```javascript
545546
import { parsePassport } from '@brazilian-utils/brazilian-utils';
@@ -591,7 +592,7 @@ generateCnh(); // '02650306461'
591592

592593
## parseCnh
593594

594-
Remove CNH formatting and return only digits.
595+
Remove CNH formatting, keep only digits, and cap the result to 11 digits.
595596

596597
```javascript
597598
import { parseCnh } from '@brazilian-utils/brazilian-utils';
@@ -668,7 +669,7 @@ generateLegalNature(); // '2062'
668669

669670
## parseLegalNature
670671

671-
Remove legal nature formatting and return only digits.
672+
Remove legal nature formatting, keep only digits, and cap the result to 4 digits.
672673

673674
```javascript
674675
import { parseLegalNature } from '@brazilian-utils/brazilian-utils';
@@ -738,7 +739,7 @@ getFormatLicensePlate('INVALID'); // null
738739

739740
## parseLicensePlate
740741

741-
Remove separators from a license plate and normalize it to uppercase.
742+
Remove separators from a license plate, normalize it to uppercase, and cap it to 7 characters.
742743

743744
```javascript
744745
import { parseLicensePlate } from '@brazilian-utils/brazilian-utils';
@@ -816,7 +817,7 @@ generateVoterId('SP'); // valid random voter ID for Sao Paulo
816817

817818
## parseVoterId
818819

819-
Remove voter ID formatting and return only digits.
820+
Remove voter ID formatting, keep only digits, and cap the result to 12 digits.
820821

821822
```javascript
822823
import { parseVoterId } from '@brazilian-utils/brazilian-utils';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expect, it } from "../test/runtime";
2+
import { sanitizeToAlphanumeric } from "./sanitize-to-alphanumeric";
3+
4+
describe("sanitizeToAlphanumeric", () => {
5+
it("should remove non alphanumeric characters and uppercase the result", () => {
6+
expect(sanitizeToAlphanumeric("ab-12.3")).toBe("AB123");
7+
});
8+
9+
it("should support number input", () => {
10+
expect(sanitizeToAlphanumeric(12345)).toBe("12345");
11+
});
12+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Sanitizes the input value by removing all non-alphanumeric characters and uppercasing the result.
3+
*
4+
* @param {string|number} value - The input value to be sanitized. It can be a string or a number.
5+
* @returns {string} A string containing only uppercase alphanumeric characters from the input value.
6+
*
7+
* @example
8+
* ```typescript
9+
* sanitizeToAlphanumeric("abc123") // "ABC123"
10+
* sanitizeToAlphanumeric("Ab-12.3") // "AB123"
11+
* sanitizeToAlphanumeric(12345) // "12345"
12+
* ```
13+
*/
14+
export const sanitizeToAlphanumeric = (value: string | number): string =>
15+
value.toString().replace(/[^A-Za-z0-9]/g, "").toUpperCase();

src/format-cnh/format-cnh.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import { formatCnh } from "./format-cnh";
33

44
describe("formatCnh", () => {
55
it("should format CNH values", () => {
6-
expect(formatCnh("00000000119")).toBe("000000001-19");
6+
expect(formatCnh("")).toBe("");
7+
expect(formatCnh("0")).toBe("0");
8+
expect(formatCnh("00")).toBe("00");
9+
expect(formatCnh("000")).toBe("000");
10+
expect(formatCnh("0000")).toBe("0000");
11+
expect(formatCnh("00000")).toBe("00000");
12+
expect(formatCnh("000000")).toBe("000000");
13+
expect(formatCnh("0000000")).toBe("0000000");
14+
expect(formatCnh("00000000")).toBe("00000000");
715
expect(formatCnh("000000001")).toBe("000000001");
16+
expect(formatCnh("0000000011")).toBe("000000001-1");
17+
expect(formatCnh("00000000119")).toBe("000000001-19");
818
});
919

1020
it("should remove non numeric characters", () => {

src/format-cnpj/format-cnpj.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { type FormatParams, format } from "../_internals/format/format";
2+
import { sanitizeToAlphanumeric } from "../_internals/sanitize-to-alphanumeric/sanitize-to-alphanumeric";
23
import { sanitizeToDigits } from "../_internals/sanitize-to-digits/sanitize-to-digits";
34

45
export type FormatCnpjOptions = Pick<FormatParams, "pad"> & { version?: 1 | 2 };
56

67
const sanitize = (value: string | number, version?: FormatCnpjOptions["version"]) => {
78
if (version === 2) {
8-
const enhancedValue = value.toString();
9-
return enhancedValue.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
9+
return sanitizeToAlphanumeric(value);
1010
}
1111

1212
return sanitizeToDigits(value);

src/format-legal-nature/format-legal-nature.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { formatLegalNature } from "./format-legal-nature";
33

44
describe("formatLegalNature", () => {
55
it("should format legal nature values", () => {
6+
expect(formatLegalNature("")).toBe("");
7+
expect(formatLegalNature("2")).toBe("2");
8+
expect(formatLegalNature("20")).toBe("20");
9+
expect(formatLegalNature("206")).toBe("206");
610
expect(formatLegalNature("2062")).toBe("206-2");
711
});
812
});

src/format-license-plate/format-license-plate.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@ import { formatLicensePlate } from "./format-license-plate";
33

44
describe("formatLicensePlate", () => {
55
it("should format old pattern license plates", () => {
6+
expect(formatLicensePlate("")).toBe("");
7+
expect(formatLicensePlate("a")).toBe("A");
8+
expect(formatLicensePlate("ab")).toBe("AB");
9+
expect(formatLicensePlate("abc")).toBe("ABC");
10+
expect(formatLicensePlate("abc1")).toBe("ABC-1");
11+
expect(formatLicensePlate("abc12")).toBe("ABC-12");
12+
expect(formatLicensePlate("abc123")).toBe("ABC-123");
613
expect(formatLicensePlate("abc1234")).toBe("ABC-1234");
714
});
815

916
it("should keep mercosul plates normalized", () => {
17+
expect(formatLicensePlate("")).toBe("");
18+
expect(formatLicensePlate("a")).toBe("A");
19+
expect(formatLicensePlate("ab")).toBe("AB");
20+
expect(formatLicensePlate("abc")).toBe("ABC");
21+
expect(formatLicensePlate("abc1")).toBe("ABC-1");
22+
expect(formatLicensePlate("abc1d")).toBe("ABC1D");
23+
expect(formatLicensePlate("abc1d2")).toBe("ABC1D2");
1024
expect(formatLicensePlate("abc1d23")).toBe("ABC1D23");
1125
});
26+
27+
it("should remove non alphanumeric characters", () => {
28+
expect(formatLicensePlate("abc-1234")).toBe("ABC-1234");
29+
expect(formatLicensePlate("abc1-d23")).toBe("ABC1D23");
30+
});
1231
});

src/format-license-plate/format-license-plate.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@ import { OLD_FORMAT_SEPARATOR_INDEX } from "./constants";
44

55
export const formatLicensePlate = (value: string): string => {
66
const parsed = parseLicensePlate(value);
7-
const format = getFormatLicensePlate(parsed);
87

9-
if (!format) return "";
8+
if (!parsed) return "";
9+
10+
const format = getFormatLicensePlate(parsed);
1011

1112
if (format === "LLLNNNN") {
1213
return `${parsed.slice(0, OLD_FORMAT_SEPARATOR_INDEX)}-${parsed.slice(OLD_FORMAT_SEPARATOR_INDEX)}`;
1314
}
1415

15-
return parsed;
16+
if (format) return parsed;
17+
18+
if (!/^[A-Z]{1,3}$/.test(parsed.slice(0, Math.min(parsed.length, OLD_FORMAT_SEPARATOR_INDEX)))) {
19+
return "";
20+
}
21+
22+
if (parsed.length <= OLD_FORMAT_SEPARATOR_INDEX) return parsed;
23+
24+
const tail = parsed.slice(OLD_FORMAT_SEPARATOR_INDEX);
25+
26+
if (/^\d{1,4}$/.test(tail)) {
27+
return `${parsed.slice(0, OLD_FORMAT_SEPARATOR_INDEX)}-${tail}`;
28+
}
29+
30+
if (/^\d[A-Z]\d{0,2}$/.test(tail) || /^\d{2}[A-Z]\d?$/.test(tail)) {
31+
return parsed;
32+
}
33+
34+
return "";
1635
};

src/format-passport/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LENGTH = 8;

0 commit comments

Comments
 (0)