validation-br 2.0.0-next-9 → 2.0.0-next-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/certidao.d.ts +111 -0
- package/dist/{certidao.js → cjs/certidao.js} +83 -38
- package/dist/cjs/package.json +3 -0
- package/dist/{uuid.js → cjs/uuid.js} +7 -4
- package/dist/esm/_exceptions/ValidationBRError.d.ts +24 -0
- package/dist/esm/_exceptions/ValidationBRError.js +40 -0
- package/dist/esm/_helpers/crc.d.ts +7 -0
- package/dist/esm/_helpers/crc.js +43 -0
- package/dist/esm/_helpers/crockford.d.ts +31 -0
- package/dist/esm/_helpers/crockford.js +58 -0
- package/dist/esm/_helpers/mask.d.ts +18 -0
- package/dist/esm/_helpers/mask.js +33 -0
- package/dist/esm/_helpers/random.d.ts +27 -0
- package/dist/esm/_helpers/random.js +50 -0
- package/dist/esm/_helpers/utils.d.ts +94 -0
- package/dist/esm/_helpers/utils.js +119 -0
- package/dist/esm/base.d.ts +33 -0
- package/dist/esm/base.js +45 -0
- package/dist/esm/carplate.d.ts +31 -0
- package/dist/esm/carplate.js +65 -0
- package/dist/esm/cbi_sinter.d.ts +78 -0
- package/dist/esm/cbi_sinter.js +119 -0
- package/dist/esm/cep.d.ts +61 -0
- package/dist/esm/cep.js +183 -0
- package/dist/esm/certidao.d.ts +111 -0
- package/dist/esm/certidao.js +170 -0
- package/dist/esm/cnh.d.ts +80 -0
- package/dist/esm/cnh.js +124 -0
- package/dist/esm/cnpj.d.ts +107 -0
- package/dist/esm/cnpj.js +170 -0
- package/dist/esm/cpf.d.ts +88 -0
- package/dist/esm/cpf.js +132 -0
- package/dist/esm/email.d.ts +12 -0
- package/dist/esm/email.js +36 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +39 -0
- package/dist/esm/judicial-process.d.ts +150 -0
- package/dist/esm/judicial-process.js +253 -0
- package/dist/esm/nup17.d.ts +98 -0
- package/dist/esm/nup17.js +150 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/phone.d.ts +103 -0
- package/dist/esm/phone.js +165 -0
- package/dist/esm/pis-pasep.d.ts +70 -0
- package/dist/esm/pis-pasep.js +111 -0
- package/dist/esm/pix-copy-paste.d.ts +26 -0
- package/dist/esm/pix-copy-paste.js +78 -0
- package/dist/esm/pix-key.d.ts +29 -0
- package/dist/esm/pix-key.js +83 -0
- package/dist/esm/postal-track-code.d.ts +76 -0
- package/dist/esm/postal-track-code.js +125 -0
- package/dist/esm/renavam.d.ts +31 -0
- package/dist/esm/renavam.js +108 -0
- package/dist/esm/tituloEleitor.d.ts +82 -0
- package/dist/esm/tituloEleitor.js +129 -0
- package/dist/esm/uf.d.ts +89 -0
- package/dist/esm/uf.js +223 -0
- package/dist/esm/uuid.d.ts +9 -0
- package/dist/esm/uuid.js +29 -0
- package/package.json +138 -132
- package/readme.md +412 -160
- package/dist/certidao.d.ts +0 -83
- /package/dist/{_exceptions → cjs/_exceptions}/ValidationBRError.d.ts +0 -0
- /package/dist/{_exceptions → cjs/_exceptions}/ValidationBRError.js +0 -0
- /package/dist/{_helpers → cjs/_helpers}/crc.d.ts +0 -0
- /package/dist/{_helpers → cjs/_helpers}/crc.js +0 -0
- /package/dist/{_helpers → cjs/_helpers}/crockford.d.ts +0 -0
- /package/dist/{_helpers → cjs/_helpers}/crockford.js +0 -0
- /package/dist/{_helpers → cjs/_helpers}/mask.d.ts +0 -0
- /package/dist/{_helpers → cjs/_helpers}/mask.js +0 -0
- /package/dist/{_helpers → cjs/_helpers}/random.d.ts +0 -0
- /package/dist/{_helpers → cjs/_helpers}/random.js +0 -0
- /package/dist/{_helpers → cjs/_helpers}/utils.d.ts +0 -0
- /package/dist/{_helpers → cjs/_helpers}/utils.js +0 -0
- /package/dist/{base.d.ts → cjs/base.d.ts} +0 -0
- /package/dist/{base.js → cjs/base.js} +0 -0
- /package/dist/{carplate.d.ts → cjs/carplate.d.ts} +0 -0
- /package/dist/{carplate.js → cjs/carplate.js} +0 -0
- /package/dist/{cbi_sinter.d.ts → cjs/cbi_sinter.d.ts} +0 -0
- /package/dist/{cbi_sinter.js → cjs/cbi_sinter.js} +0 -0
- /package/dist/{cep.d.ts → cjs/cep.d.ts} +0 -0
- /package/dist/{cep.js → cjs/cep.js} +0 -0
- /package/dist/{cnh.d.ts → cjs/cnh.d.ts} +0 -0
- /package/dist/{cnh.js → cjs/cnh.js} +0 -0
- /package/dist/{cnpj.d.ts → cjs/cnpj.d.ts} +0 -0
- /package/dist/{cnpj.js → cjs/cnpj.js} +0 -0
- /package/dist/{cpf.d.ts → cjs/cpf.d.ts} +0 -0
- /package/dist/{cpf.js → cjs/cpf.js} +0 -0
- /package/dist/{email.d.ts → cjs/email.d.ts} +0 -0
- /package/dist/{email.js → cjs/email.js} +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{index.js → cjs/index.js} +0 -0
- /package/dist/{judicial-process.d.ts → cjs/judicial-process.d.ts} +0 -0
- /package/dist/{judicial-process.js → cjs/judicial-process.js} +0 -0
- /package/dist/{nup17.d.ts → cjs/nup17.d.ts} +0 -0
- /package/dist/{nup17.js → cjs/nup17.js} +0 -0
- /package/dist/{phone.d.ts → cjs/phone.d.ts} +0 -0
- /package/dist/{phone.js → cjs/phone.js} +0 -0
- /package/dist/{pis-pasep.d.ts → cjs/pis-pasep.d.ts} +0 -0
- /package/dist/{pis-pasep.js → cjs/pis-pasep.js} +0 -0
- /package/dist/{pix-copy-paste.d.ts → cjs/pix-copy-paste.d.ts} +0 -0
- /package/dist/{pix-copy-paste.js → cjs/pix-copy-paste.js} +0 -0
- /package/dist/{pix-key.d.ts → cjs/pix-key.d.ts} +0 -0
- /package/dist/{pix-key.js → cjs/pix-key.js} +0 -0
- /package/dist/{postal-track-code.d.ts → cjs/postal-track-code.d.ts} +0 -0
- /package/dist/{postal-track-code.js → cjs/postal-track-code.js} +0 -0
- /package/dist/{renavam.d.ts → cjs/renavam.d.ts} +0 -0
- /package/dist/{renavam.js → cjs/renavam.js} +0 -0
- /package/dist/{tituloEleitor.d.ts → cjs/tituloEleitor.d.ts} +0 -0
- /package/dist/{tituloEleitor.js → cjs/tituloEleitor.js} +0 -0
- /package/dist/{uf.d.ts → cjs/uf.d.ts} +0 -0
- /package/dist/{uf.js → cjs/uf.js} +0 -0
- /package/dist/{uuid.d.ts → cjs/uuid.d.ts} +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calcula o DV verificador a partir das regras do MOD11:
|
|
3
|
+
* O valor da soma é dividido por 11. O resultado é o resto da divisão. Caso o resto seja
|
|
4
|
+
* menor que 2, ou seja, o valor da divisão seja 10 ou 11, o resultado é 0.
|
|
5
|
+
*
|
|
6
|
+
* @param {Integer} sum Soma
|
|
7
|
+
* @returns {Integer}
|
|
8
|
+
*/
|
|
9
|
+
export declare function sumToDV(sum: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Checa se o número repassado possui todos os digitos iguais
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* checkRepeatedSequence(12345678)
|
|
15
|
+
* // -> false
|
|
16
|
+
* checkRepeatedSequence(11111111)
|
|
17
|
+
* // -> true
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
export declare function checkRepeatedSequence(value: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Multiplica os elementos de uma string com os elementos de outra, ou de um array
|
|
23
|
+
* e soma o resultado ao final
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* sumElementsByMultipliers('123', '987') //-> 46
|
|
27
|
+
* sumElementsByMultipliers('123', [9, 8, 7]) //-> 46
|
|
28
|
+
*
|
|
29
|
+
* @param {String} value
|
|
30
|
+
* @param {String|Array} multiplier
|
|
31
|
+
* @returns {Integer} Somatório
|
|
32
|
+
*/
|
|
33
|
+
export declare function sumElementsByMultipliers(value: string, multiplier: number[]): number;
|
|
34
|
+
/**
|
|
35
|
+
* Limpa um número informado, retirando caracteres diferentes de números,
|
|
36
|
+
* preenchendo com zeros à esquerda se for menor que o tamanho exato e
|
|
37
|
+
* removendo uma parte do número se for maior que tamanho definido.
|
|
38
|
+
*
|
|
39
|
+
* 1) Retira caracteres não-numéricos
|
|
40
|
+
* 2) Preenche com zeros à esquerda se 'value' for menor que 'length'
|
|
41
|
+
* 3) Remove caracteres à direita se 'value' for maior que 'length'
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* clearValue(12345-6, 6) // -> 123456
|
|
45
|
+
* clearValue(12345678, 3) // -> 123
|
|
46
|
+
* clearValue(12345, 10) // -> 0000001234
|
|
47
|
+
*
|
|
48
|
+
* @param {Number|String} value
|
|
49
|
+
* @param {Number} length Tamanho exato. Se for null, só retira os caracteres não-numéricos
|
|
50
|
+
* @returns {String} Número com o tamanho exato
|
|
51
|
+
*/
|
|
52
|
+
export declare function clearValue(value: string | number, length?: number | null, options?: ClearValueOptions): string;
|
|
53
|
+
/**
|
|
54
|
+
* insertAtPosition()
|
|
55
|
+
* Insere um conjunto de caracteres em um local específico de uma string
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* insertAtPosition('AAABBB', 'C', 3) // -> AAACBBB
|
|
59
|
+
* insertAtPosition('000011122223445555', 99, 7) // -> 00001119922223445555
|
|
60
|
+
*
|
|
61
|
+
* @param {String|Number} value Valor original
|
|
62
|
+
* @param {String|Number} insertValue Valor que será inserido
|
|
63
|
+
* @param {String|Number} position Posição que receberá o novo valor
|
|
64
|
+
* @returns {String}
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
export declare function insertAtPosition(value: string, insertValue: string, position: number): string;
|
|
68
|
+
/**
|
|
69
|
+
* removeFromPosition()
|
|
70
|
+
* Retira um conjunto de caracteres de um local específico de uma string
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* removeFromPosition('00001119922223445555', 7,9) // -> 000011122223445555
|
|
74
|
+
* removeFromPosition('AAACBBB', 3,4) // -> AAABBB
|
|
75
|
+
*
|
|
76
|
+
* @param {String|Number} value Valor original
|
|
77
|
+
* @param {String|Number} startPosition
|
|
78
|
+
* @param {String|Number} endPosition
|
|
79
|
+
* @returns {String}
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
export declare function removeFromPosition(value: string, startPosition: number, endPosition: number): string;
|
|
83
|
+
/**
|
|
84
|
+
* Opções do clearValue
|
|
85
|
+
*/
|
|
86
|
+
interface ClearValueOptions {
|
|
87
|
+
fillZerosAtLeft?: boolean;
|
|
88
|
+
trimAtRight?: boolean;
|
|
89
|
+
rejectEmpty?: boolean;
|
|
90
|
+
rejectIfLonger?: boolean;
|
|
91
|
+
rejectIfShorter?: boolean;
|
|
92
|
+
rejectEqualSequence?: boolean;
|
|
93
|
+
}
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { EmptyValueException, TooLongException, RepeatedSequenceException, TooShortException } from '../_exceptions/ValidationBRError.js';
|
|
2
|
+
/**
|
|
3
|
+
* Calcula o DV verificador a partir das regras do MOD11:
|
|
4
|
+
* O valor da soma é dividido por 11. O resultado é o resto da divisão. Caso o resto seja
|
|
5
|
+
* menor que 2, ou seja, o valor da divisão seja 10 ou 11, o resultado é 0.
|
|
6
|
+
*
|
|
7
|
+
* @param {Integer} sum Soma
|
|
8
|
+
* @returns {Integer}
|
|
9
|
+
*/
|
|
10
|
+
export function sumToDV(sum) {
|
|
11
|
+
return sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Checa se o número repassado possui todos os digitos iguais
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* checkRepeatedSequence(12345678)
|
|
18
|
+
* // -> false
|
|
19
|
+
* checkRepeatedSequence(11111111)
|
|
20
|
+
* // -> true
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
export function checkRepeatedSequence(value) {
|
|
24
|
+
return [...value].every(digit => digit === value[0]);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Multiplica os elementos de uma string com os elementos de outra, ou de um array
|
|
28
|
+
* e soma o resultado ao final
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* sumElementsByMultipliers('123', '987') //-> 46
|
|
32
|
+
* sumElementsByMultipliers('123', [9, 8, 7]) //-> 46
|
|
33
|
+
*
|
|
34
|
+
* @param {String} value
|
|
35
|
+
* @param {String|Array} multiplier
|
|
36
|
+
* @returns {Integer} Somatório
|
|
37
|
+
*/
|
|
38
|
+
export function sumElementsByMultipliers(value, multiplier) {
|
|
39
|
+
return multiplier.reduce((accu, curr, i) => accu + curr * Number(value[i]), 0);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Limpa um número informado, retirando caracteres diferentes de números,
|
|
43
|
+
* preenchendo com zeros à esquerda se for menor que o tamanho exato e
|
|
44
|
+
* removendo uma parte do número se for maior que tamanho definido.
|
|
45
|
+
*
|
|
46
|
+
* 1) Retira caracteres não-numéricos
|
|
47
|
+
* 2) Preenche com zeros à esquerda se 'value' for menor que 'length'
|
|
48
|
+
* 3) Remove caracteres à direita se 'value' for maior que 'length'
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* clearValue(12345-6, 6) // -> 123456
|
|
52
|
+
* clearValue(12345678, 3) // -> 123
|
|
53
|
+
* clearValue(12345, 10) // -> 0000001234
|
|
54
|
+
*
|
|
55
|
+
* @param {Number|String} value
|
|
56
|
+
* @param {Number} length Tamanho exato. Se for null, só retira os caracteres não-numéricos
|
|
57
|
+
* @returns {String} Número com o tamanho exato
|
|
58
|
+
*/
|
|
59
|
+
export function clearValue(value, length = null, options) {
|
|
60
|
+
let clearedValue = String(value).replace(/([/.\s-])+/gi, '');
|
|
61
|
+
if (options) {
|
|
62
|
+
const shouldRejectEmpty = options.rejectEmpty === true && clearedValue.length === 0;
|
|
63
|
+
if (shouldRejectEmpty) {
|
|
64
|
+
throw new EmptyValueException();
|
|
65
|
+
}
|
|
66
|
+
const shouldrejectIfLonger = options.rejectIfLonger === true && length && clearedValue.length > length;
|
|
67
|
+
if (shouldrejectIfLonger) {
|
|
68
|
+
throw new TooLongException();
|
|
69
|
+
}
|
|
70
|
+
const shouldrejectIfShorter = options.rejectIfShorter === true && length && clearedValue.length < length;
|
|
71
|
+
if (shouldrejectIfShorter) {
|
|
72
|
+
throw new TooShortException();
|
|
73
|
+
}
|
|
74
|
+
const shouldRejectEqualSequence = options.rejectEqualSequence === true && length;
|
|
75
|
+
if (shouldRejectEqualSequence) {
|
|
76
|
+
if (checkRepeatedSequence(clearedValue))
|
|
77
|
+
throw new RepeatedSequenceException();
|
|
78
|
+
}
|
|
79
|
+
if (length && options.fillZerosAtLeft)
|
|
80
|
+
clearedValue = clearedValue.padStart(length, '0');
|
|
81
|
+
if (length && options.trimAtRight)
|
|
82
|
+
clearedValue = clearedValue.substring(0, length);
|
|
83
|
+
}
|
|
84
|
+
return clearedValue;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* insertAtPosition()
|
|
88
|
+
* Insere um conjunto de caracteres em um local específico de uma string
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* insertAtPosition('AAABBB', 'C', 3) // -> AAACBBB
|
|
92
|
+
* insertAtPosition('000011122223445555', 99, 7) // -> 00001119922223445555
|
|
93
|
+
*
|
|
94
|
+
* @param {String|Number} value Valor original
|
|
95
|
+
* @param {String|Number} insertValue Valor que será inserido
|
|
96
|
+
* @param {String|Number} position Posição que receberá o novo valor
|
|
97
|
+
* @returns {String}
|
|
98
|
+
*
|
|
99
|
+
*/
|
|
100
|
+
export function insertAtPosition(value, insertValue, position) {
|
|
101
|
+
return `${value.substring(0, position)}${insertValue}${value.substring(position)}`;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* removeFromPosition()
|
|
105
|
+
* Retira um conjunto de caracteres de um local específico de uma string
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* removeFromPosition('00001119922223445555', 7,9) // -> 000011122223445555
|
|
109
|
+
* removeFromPosition('AAACBBB', 3,4) // -> AAABBB
|
|
110
|
+
*
|
|
111
|
+
* @param {String|Number} value Valor original
|
|
112
|
+
* @param {String|Number} startPosition
|
|
113
|
+
* @param {String|Number} endPosition
|
|
114
|
+
* @returns {String}
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
export function removeFromPosition(value, startPosition, endPosition) {
|
|
118
|
+
return [value.slice(0, startPosition), value.slice(endPosition)].join('');
|
|
119
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare abstract class Base {
|
|
2
|
+
protected _value: string;
|
|
3
|
+
/**
|
|
4
|
+
* Estilo da máscara no formato: 000.000.000-00
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
protected abstract _mask: string | null;
|
|
8
|
+
constructor(_value: string, fakeFn?: Function);
|
|
9
|
+
get value(): string;
|
|
10
|
+
toString(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Deve remover apenas pontos e traços.
|
|
13
|
+
*/
|
|
14
|
+
protected abstract normalize(options?: any): void;
|
|
15
|
+
/**
|
|
16
|
+
* Deve aplicar as regras de validação do dígito verificador do documento
|
|
17
|
+
*/
|
|
18
|
+
protected abstract validate(options?: any): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Deve devolver o número com a máscara
|
|
21
|
+
*/
|
|
22
|
+
mask(): string;
|
|
23
|
+
/**
|
|
24
|
+
* checksum()
|
|
25
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
static checksum(value: string): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Deve gerar um número fake no padrão
|
|
31
|
+
*/
|
|
32
|
+
static fake(options?: any): any;
|
|
33
|
+
}
|
package/dist/esm/base.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Mask } from './_helpers/mask.js';
|
|
2
|
+
export class Base {
|
|
3
|
+
constructor(_value, fakeFn) {
|
|
4
|
+
this._value = _value;
|
|
5
|
+
}
|
|
6
|
+
get value() {
|
|
7
|
+
return this._value;
|
|
8
|
+
}
|
|
9
|
+
toString() {
|
|
10
|
+
return this.value;
|
|
11
|
+
}
|
|
12
|
+
//
|
|
13
|
+
//
|
|
14
|
+
//
|
|
15
|
+
//
|
|
16
|
+
//
|
|
17
|
+
/**
|
|
18
|
+
* Deve devolver o número com a máscara
|
|
19
|
+
*/
|
|
20
|
+
mask() {
|
|
21
|
+
if (!this._mask)
|
|
22
|
+
return this._value;
|
|
23
|
+
return new Mask(this._value).apply(this._mask);
|
|
24
|
+
}
|
|
25
|
+
//
|
|
26
|
+
//
|
|
27
|
+
// STATIC
|
|
28
|
+
//
|
|
29
|
+
//
|
|
30
|
+
/**
|
|
31
|
+
* checksum()
|
|
32
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
static checksum(value) {
|
|
36
|
+
throw new Error('Should implement on child');
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Deve gerar um número fake no padrão
|
|
40
|
+
*/
|
|
41
|
+
static fake(options) {
|
|
42
|
+
throw new Error('Should implement on child');
|
|
43
|
+
// Base.fakeFn(options)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placas de carro no formato mercosul e placas mais antigas
|
|
3
|
+
* AAA-0000 e AAA-0A00
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
import { Base } from "./base";
|
|
8
|
+
export declare class CarPlate extends Base {
|
|
9
|
+
protected _value: string;
|
|
10
|
+
protected _mask: string;
|
|
11
|
+
constructor(_value: string);
|
|
12
|
+
validate(): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Padroniza o valor, sem modificar a ponto de permitir que a validação aceite um valor inválido
|
|
15
|
+
* Ex.: AAA0-000 é inválido. Normalize() não pode modificar isso.
|
|
16
|
+
*/
|
|
17
|
+
protected normalize(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Remove a máscara após a normalização e a validação, para evitar que o valor
|
|
20
|
+
* AAAA-000 seja validado. Ele seria validado quando o traço fosse removido, então
|
|
21
|
+
* o _unmask é chamado apenas após a validação.
|
|
22
|
+
*/
|
|
23
|
+
private _unmask;
|
|
24
|
+
/**
|
|
25
|
+
* checksum()
|
|
26
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
static checksum(value?: string): string;
|
|
30
|
+
static fake(): CarPlate;
|
|
31
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placas de carro no formato mercosul e placas mais antigas
|
|
3
|
+
* AAA-0000 e AAA-0A00
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
import { Base } from "./base.js";
|
|
8
|
+
import { InvalidFormatException, NoChecksumException } from "./_exceptions/ValidationBRError.js";
|
|
9
|
+
import { Random } from "./_helpers/random.js";
|
|
10
|
+
export class CarPlate extends Base {
|
|
11
|
+
constructor(_value) {
|
|
12
|
+
super(_value);
|
|
13
|
+
this._value = _value;
|
|
14
|
+
this._mask = '000-0000';
|
|
15
|
+
this.normalize();
|
|
16
|
+
if (!this.validate())
|
|
17
|
+
throw new InvalidFormatException();
|
|
18
|
+
this._unmask();
|
|
19
|
+
}
|
|
20
|
+
//
|
|
21
|
+
//
|
|
22
|
+
//
|
|
23
|
+
//
|
|
24
|
+
//
|
|
25
|
+
validate() {
|
|
26
|
+
return /^[A-Z]{3}-?[0-9][A-Z0-9][0-9]{2}$/i.test(this._value);
|
|
27
|
+
}
|
|
28
|
+
//
|
|
29
|
+
//
|
|
30
|
+
// PROTECTED
|
|
31
|
+
//
|
|
32
|
+
//
|
|
33
|
+
/**
|
|
34
|
+
* Padroniza o valor, sem modificar a ponto de permitir que a validação aceite um valor inválido
|
|
35
|
+
* Ex.: AAA0-000 é inválido. Normalize() não pode modificar isso.
|
|
36
|
+
*/
|
|
37
|
+
normalize() {
|
|
38
|
+
this._value = this._value.toLocaleUpperCase().trim(); //.replace(/[-]+/g, '')
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove a máscara após a normalização e a validação, para evitar que o valor
|
|
42
|
+
* AAAA-000 seja validado. Ele seria validado quando o traço fosse removido, então
|
|
43
|
+
* o _unmask é chamado apenas após a validação.
|
|
44
|
+
*/
|
|
45
|
+
_unmask() {
|
|
46
|
+
this._value = this._value.replace(/[-]+/g, '');
|
|
47
|
+
}
|
|
48
|
+
//
|
|
49
|
+
//
|
|
50
|
+
// STATIC
|
|
51
|
+
//
|
|
52
|
+
//
|
|
53
|
+
/**
|
|
54
|
+
* checksum()
|
|
55
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
static checksum(value) {
|
|
59
|
+
throw new NoChecksumException();
|
|
60
|
+
}
|
|
61
|
+
static fake() {
|
|
62
|
+
const fake = `${Random.alpha(3)}${Random.number(1)}${Random.alpha()}${Random.number(2, true)}`;
|
|
63
|
+
return new CarPlate(fake);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CBISinter
|
|
3
|
+
* Valida o Código Imobiliário Brasileiro (CIB) no padrão SINTER.
|
|
4
|
+
*
|
|
5
|
+
* @doc
|
|
6
|
+
* O CIB (Código Imobiliário Brasileiro) é o código único de identificação do imóvel,
|
|
7
|
+
* estabelecido pelo SINTER (Sistema Nacional de Gestão de Informações Territoriais).
|
|
8
|
+
* O código é composto por 7 caracteres alfanuméricos seguidos de um dígito verificador.
|
|
9
|
+
*
|
|
10
|
+
* O algoritmo utiliza a Base 32 de Crockford, que:
|
|
11
|
+
* - É insensível a maiúsculas/minúsculas.
|
|
12
|
+
* - Remove os caracteres I, L, O e U para evitar confusões visuais e palavras obscenas.
|
|
13
|
+
* - Substitui 'I' e 'L' por '1'.
|
|
14
|
+
* - Substitui 'O' por '0'.
|
|
15
|
+
*
|
|
16
|
+
* 1) Partes do código
|
|
17
|
+
* A 3 N 8 Z 4 F - Y
|
|
18
|
+
* |___________| |
|
|
19
|
+
* Base DV
|
|
20
|
+
*
|
|
21
|
+
* 2) Cálculo do Dígito Verificador
|
|
22
|
+
*
|
|
23
|
+
* O cálculo utiliza o algoritmo de Módulo 31 com pesos específicos.
|
|
24
|
+
*
|
|
25
|
+
* Exemplo: A3N8Z4F
|
|
26
|
+
*
|
|
27
|
+
* Pesos: 4, 3, 9, 5, 7, 1, 8
|
|
28
|
+
*
|
|
29
|
+
* Caracteres Base32 (Valor):
|
|
30
|
+
* 0=0, 1=1, ..., 9=9
|
|
31
|
+
* A=10, B=11, C=12, D=13, E=14, F=15, G=16, H=17
|
|
32
|
+
* J=18, K=19, M=20, N=21, P=22, Q=23, R=24, S=25
|
|
33
|
+
* T=26, V=27, W=28, X=29, Y=30, Z=31
|
|
34
|
+
*
|
|
35
|
+
* Cálculo:
|
|
36
|
+
* A (10) * 4 = 40
|
|
37
|
+
* 3 (3) * 3 = 9
|
|
38
|
+
* N (21) * 9 = 189
|
|
39
|
+
* 8 (8) * 5 = 40
|
|
40
|
+
* Z (31) * 7 = 217
|
|
41
|
+
* 4 (4) * 1 = 4
|
|
42
|
+
* F (15) * 8 = 120
|
|
43
|
+
*
|
|
44
|
+
* Soma = 40 + 9 + 189 + 40 + 217 + 4 + 120 = 619
|
|
45
|
+
*
|
|
46
|
+
* Resto = 619 % 31 = 30
|
|
47
|
+
*
|
|
48
|
+
* O valor 30 corresponde ao caractere 'Y' na Base 32 de Crockford.
|
|
49
|
+
* Portanto, o DV é Y.
|
|
50
|
+
*
|
|
51
|
+
* Observação: O DV nunca pode ser "Z", pois o resto máximo da divisão por 31 é 30.
|
|
52
|
+
*/
|
|
53
|
+
import { Base } from "./base";
|
|
54
|
+
export declare class CBISinter extends Base {
|
|
55
|
+
protected _mask: string;
|
|
56
|
+
constructor(value: string);
|
|
57
|
+
/**
|
|
58
|
+
* Normaliza o valor informado no construtor, atualizando o _value interno.
|
|
59
|
+
*/
|
|
60
|
+
protected normalize(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Valida se o DV informado na string original coincide com o DV calculado.
|
|
63
|
+
*/
|
|
64
|
+
validate(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* checksum()
|
|
68
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
static checksum(value: string): string;
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* Cria uma instância com um número válido aleatório
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
static fake(): CBISinter;
|
|
78
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CBISinter
|
|
3
|
+
* Valida o Código Imobiliário Brasileiro (CIB) no padrão SINTER.
|
|
4
|
+
*
|
|
5
|
+
* @doc
|
|
6
|
+
* O CIB (Código Imobiliário Brasileiro) é o código único de identificação do imóvel,
|
|
7
|
+
* estabelecido pelo SINTER (Sistema Nacional de Gestão de Informações Territoriais).
|
|
8
|
+
* O código é composto por 7 caracteres alfanuméricos seguidos de um dígito verificador.
|
|
9
|
+
*
|
|
10
|
+
* O algoritmo utiliza a Base 32 de Crockford, que:
|
|
11
|
+
* - É insensível a maiúsculas/minúsculas.
|
|
12
|
+
* - Remove os caracteres I, L, O e U para evitar confusões visuais e palavras obscenas.
|
|
13
|
+
* - Substitui 'I' e 'L' por '1'.
|
|
14
|
+
* - Substitui 'O' por '0'.
|
|
15
|
+
*
|
|
16
|
+
* 1) Partes do código
|
|
17
|
+
* A 3 N 8 Z 4 F - Y
|
|
18
|
+
* |___________| |
|
|
19
|
+
* Base DV
|
|
20
|
+
*
|
|
21
|
+
* 2) Cálculo do Dígito Verificador
|
|
22
|
+
*
|
|
23
|
+
* O cálculo utiliza o algoritmo de Módulo 31 com pesos específicos.
|
|
24
|
+
*
|
|
25
|
+
* Exemplo: A3N8Z4F
|
|
26
|
+
*
|
|
27
|
+
* Pesos: 4, 3, 9, 5, 7, 1, 8
|
|
28
|
+
*
|
|
29
|
+
* Caracteres Base32 (Valor):
|
|
30
|
+
* 0=0, 1=1, ..., 9=9
|
|
31
|
+
* A=10, B=11, C=12, D=13, E=14, F=15, G=16, H=17
|
|
32
|
+
* J=18, K=19, M=20, N=21, P=22, Q=23, R=24, S=25
|
|
33
|
+
* T=26, V=27, W=28, X=29, Y=30, Z=31
|
|
34
|
+
*
|
|
35
|
+
* Cálculo:
|
|
36
|
+
* A (10) * 4 = 40
|
|
37
|
+
* 3 (3) * 3 = 9
|
|
38
|
+
* N (21) * 9 = 189
|
|
39
|
+
* 8 (8) * 5 = 40
|
|
40
|
+
* Z (31) * 7 = 217
|
|
41
|
+
* 4 (4) * 1 = 4
|
|
42
|
+
* F (15) * 8 = 120
|
|
43
|
+
*
|
|
44
|
+
* Soma = 40 + 9 + 189 + 40 + 217 + 4 + 120 = 619
|
|
45
|
+
*
|
|
46
|
+
* Resto = 619 % 31 = 30
|
|
47
|
+
*
|
|
48
|
+
* O valor 30 corresponde ao caractere 'Y' na Base 32 de Crockford.
|
|
49
|
+
* Portanto, o DV é Y.
|
|
50
|
+
*
|
|
51
|
+
* Observação: O DV nunca pode ser "Z", pois o resto máximo da divisão por 31 é 30.
|
|
52
|
+
*/
|
|
53
|
+
import { EmptyValueException, InvalidChecksumException, InvalidFormatException } from "./_exceptions/ValidationBRError.js";
|
|
54
|
+
import { Crockford } from "./_helpers/crockford.js";
|
|
55
|
+
import { Random } from "./_helpers/random.js";
|
|
56
|
+
import { Base } from "./base.js";
|
|
57
|
+
import { clearValue } from "./_helpers/utils.js";
|
|
58
|
+
export class CBISinter extends Base {
|
|
59
|
+
constructor(value) {
|
|
60
|
+
super(value);
|
|
61
|
+
this._mask = "0000000-0"; // Máscara apenas informativa
|
|
62
|
+
this.normalize();
|
|
63
|
+
if (!this.validate()) {
|
|
64
|
+
throw new InvalidChecksumException();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Normaliza o valor informado no construtor, atualizando o _value interno.
|
|
69
|
+
*/
|
|
70
|
+
normalize() {
|
|
71
|
+
this._value = Crockford.normalize(this._value);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Valida se o DV informado na string original coincide com o DV calculado.
|
|
75
|
+
*/
|
|
76
|
+
validate() {
|
|
77
|
+
const cib = clearValue(this._value, 8, {
|
|
78
|
+
rejectEmpty: true,
|
|
79
|
+
rejectIfLonger: true,
|
|
80
|
+
rejectIfShorter: true,
|
|
81
|
+
});
|
|
82
|
+
return CBISinter.checksum(cib.substring(0, 7)) === cib.substring(7, 8);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* checksum()
|
|
87
|
+
* Calcula o dígito verificador de um número SEM o dígito incluído
|
|
88
|
+
*
|
|
89
|
+
*/
|
|
90
|
+
static checksum(value) {
|
|
91
|
+
if (!value)
|
|
92
|
+
throw new EmptyValueException();
|
|
93
|
+
if (!/^[0-9a-z]{7}$/i.test(value))
|
|
94
|
+
throw new InvalidFormatException();
|
|
95
|
+
// Garante que temos pelo menos os 7 caracteres da base
|
|
96
|
+
// const base = value.substring(0, 7);
|
|
97
|
+
const base = value;
|
|
98
|
+
const weights = [4, 3, 9, 5, 7, 1, 8];
|
|
99
|
+
let sum = 0;
|
|
100
|
+
for (let i = 0; i < base.length; i++) {
|
|
101
|
+
const charValue = Crockford.charToInt(base[i]);
|
|
102
|
+
sum += charValue * weights[i];
|
|
103
|
+
}
|
|
104
|
+
const rest = sum % 31;
|
|
105
|
+
return Crockford.intToChar(rest);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
*
|
|
109
|
+
* Cria uma instância com um número válido aleatório
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
static fake() {
|
|
113
|
+
// Pego o alfabeto usado pelo Crockford como array
|
|
114
|
+
const alphabet = Crockford.getAlphabet().split('');
|
|
115
|
+
// Uso o random.fromArray para pegar 7 caracteres aleatórios de dentro do alfabeto
|
|
116
|
+
const base = Random.fromArray(alphabet, 7).join('');
|
|
117
|
+
return new CBISinter(`${base}${CBISinter.checksum(base)}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import ValidationBRError from './_exceptions/ValidationBRError';
|
|
2
|
+
import { Base } from './base';
|
|
3
|
+
import { UF } from './uf';
|
|
4
|
+
export declare class CEP extends Base {
|
|
5
|
+
protected _value: string;
|
|
6
|
+
protected _mask: string;
|
|
7
|
+
protected static _ranges: Record<string, [string, string][]>;
|
|
8
|
+
constructor(_value: string, options?: ValidationOptions);
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
protected normalize(): void;
|
|
15
|
+
/**
|
|
16
|
+
* validate()
|
|
17
|
+
* Valida se um número é válido e
|
|
18
|
+
* retorna uma exceção se não estiver
|
|
19
|
+
*
|
|
20
|
+
* @link https://regex101.com/r/uQJuUM/1
|
|
21
|
+
*
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
protected validate(options?: ValidationOptions): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* checksum()
|
|
27
|
+
* Not implemented
|
|
28
|
+
*/
|
|
29
|
+
static checksum(value: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Cria um número fake
|
|
32
|
+
* - Deve criar um fake dentro de uma faixa
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
static fake(options?: Partial<ValidationOptions>): CEP;
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* Verifica se um CEP está dentro de uma faixa e retorna o estado
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
static getUFByCEP(value: string): UF;
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* Pega aleatoriamente um CEP em um estado
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
static getRandomByUF(uf: UF): CEP;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* Pega as faixas de CEP por estado
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
static getRangesByUF(uf: UF): [string, string][];
|
|
54
|
+
}
|
|
55
|
+
interface ValidationOptions {
|
|
56
|
+
uf?: UF;
|
|
57
|
+
}
|
|
58
|
+
export declare class NotFoundCEPException extends ValidationBRError {
|
|
59
|
+
constructor();
|
|
60
|
+
}
|
|
61
|
+
export {};
|