-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
73 lines (61 loc) · 1.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Validates a CNPJ
* @param cnpj The CNPJ value to be validated
*/
export function validate(cnpj: string | number): boolean {
// Remove period, slash and dash characters from CNPJ
const cleaned = cnpj.toString().replace(/[\.\/\-]/g, '')
if (
// Must be defined
!cleaned ||
// Must have 14 characters
cleaned.length !== 14 ||
// Must be digits and not be sequential characters (e.g.: 11111111111111, etc)
/^(\d)\1+$/.test(cleaned)
) {
return false
}
let registration = cleaned.substr(0, 12)
registration += digit(registration)
registration += digit(registration)
return registration.substr(-2) === cleaned.substr(-2)
}
/**
* Formats a CNPJ value
* @param cnpj The CNPJ to be formatted
* @return The formatted CNPJ
*/
export function format(cnpj: string | number): string {
return (
cnpj
.toString()
// Remove non digit characters
.replace(/[^\d]/g, '')
// Apply formatting
.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '$1.$2.$3/$4-$5')
)
}
/**
* Generates a valid CNPJ
* @return The generated CNPJ
*/
export function generate(): string {
let cnpj = ''
let i = 12
while (i--) {
cnpj += Math.floor(Math.random() * 9)
}
cnpj += digit(cnpj)
cnpj += digit(cnpj)
return format(cnpj)
}
function digit(numbers: string): number {
let index = 2
const sum = [...numbers].reverse().reduce((buffer, number) => {
buffer += Number(number) * index
index = index === 9 ? 2 : index + 1
return buffer
}, 0)
const mod = sum % 11
return mod < 2 ? 0 : 11 - mod
}