@excofy/utils 1.0.7 → 1.0.8
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/index.cjs +80 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +80 -0
- package/package.json +1 -1
- package/src/helpers/document.ts +77 -0
- package/src/helpers/validator.ts +31 -0
package/dist/index.cjs
CHANGED
|
@@ -38,6 +38,60 @@ __export(index_exports, {
|
|
|
38
38
|
});
|
|
39
39
|
module.exports = __toCommonJS(index_exports);
|
|
40
40
|
|
|
41
|
+
// src/helpers/document.ts
|
|
42
|
+
function isValidCPF(cpf) {
|
|
43
|
+
if (!cpf) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
47
|
+
const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
48
|
+
let tempCpf = cpf.slice(0, 9);
|
|
49
|
+
let soma = 0;
|
|
50
|
+
for (let i = 0; i < 9; i++) {
|
|
51
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
|
|
52
|
+
}
|
|
53
|
+
let resto = soma % 11;
|
|
54
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
55
|
+
let digito = resto.toString();
|
|
56
|
+
tempCpf += digito;
|
|
57
|
+
soma = 0;
|
|
58
|
+
for (let i = 0; i < 10; i++) {
|
|
59
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
|
|
60
|
+
}
|
|
61
|
+
resto = soma % 11;
|
|
62
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
63
|
+
digito += resto;
|
|
64
|
+
return cpf.endsWith(digito);
|
|
65
|
+
}
|
|
66
|
+
function isValidCNPJ(cnpj) {
|
|
67
|
+
function validCalc(x, numbers2) {
|
|
68
|
+
const slice = numbers2.slice(0, x);
|
|
69
|
+
let factor = x - 7;
|
|
70
|
+
let sum = 0;
|
|
71
|
+
for (let i = x; i >= 1; i--) {
|
|
72
|
+
const n = slice[x - i];
|
|
73
|
+
sum += n * factor--;
|
|
74
|
+
if (factor < 2) factor = 9;
|
|
75
|
+
}
|
|
76
|
+
const result = 11 - sum % 11;
|
|
77
|
+
return result > 9 ? 0 : result;
|
|
78
|
+
}
|
|
79
|
+
function matchNumbers(value = "") {
|
|
80
|
+
const match = value.toString().match(/\d/g);
|
|
81
|
+
return Array.isArray(match) ? match.map(Number) : [];
|
|
82
|
+
}
|
|
83
|
+
if (!cnpj) return false;
|
|
84
|
+
const numbers = matchNumbers(cnpj);
|
|
85
|
+
if (numbers.length !== 14) return false;
|
|
86
|
+
const items = new Set(numbers);
|
|
87
|
+
if (items.size === 1) return false;
|
|
88
|
+
const digits = numbers.slice(12);
|
|
89
|
+
const digit0 = validCalc(12, numbers);
|
|
90
|
+
if (digit0 !== digits[0]) return false;
|
|
91
|
+
const digit1 = validCalc(13, numbers);
|
|
92
|
+
return digit1 === digits[1];
|
|
93
|
+
}
|
|
94
|
+
|
|
41
95
|
// src/helpers/sanitize.ts
|
|
42
96
|
var import_xss = require("xss");
|
|
43
97
|
var allTags = [
|
|
@@ -192,6 +246,32 @@ function createValidator() {
|
|
|
192
246
|
return !current.required && (current.value === void 0 || current.value === null || current.value === "");
|
|
193
247
|
};
|
|
194
248
|
const validator = {
|
|
249
|
+
cnpj: (message) => {
|
|
250
|
+
if (shouldSkipValidation()) {
|
|
251
|
+
return validator;
|
|
252
|
+
}
|
|
253
|
+
if (typeof current.value === "string") {
|
|
254
|
+
if (!isValidCNPJ(current.value)) {
|
|
255
|
+
current.pushError(message);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
current.pushError(message);
|
|
259
|
+
}
|
|
260
|
+
return validator;
|
|
261
|
+
},
|
|
262
|
+
cpf: (message) => {
|
|
263
|
+
if (shouldSkipValidation()) {
|
|
264
|
+
return validator;
|
|
265
|
+
}
|
|
266
|
+
if (typeof current.value === "string") {
|
|
267
|
+
if (!isValidCPF(current.value)) {
|
|
268
|
+
current.pushError(message);
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
current.pushError(message);
|
|
272
|
+
}
|
|
273
|
+
return validator;
|
|
274
|
+
},
|
|
195
275
|
each(callback) {
|
|
196
276
|
if (shouldSkipValidation()) {
|
|
197
277
|
return validator;
|
package/dist/index.d.cts
CHANGED
|
@@ -44,6 +44,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
44
44
|
validate<K extends keyof TRaw>(field: K): {
|
|
45
45
|
isRequired(message: string): {
|
|
46
46
|
type(type: TTypes, message: string): {
|
|
47
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
48
|
+
cpf: (message: string) => /*elided*/ any;
|
|
47
49
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
48
50
|
email: (message: string) => /*elided*/ any;
|
|
49
51
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
|
@@ -72,6 +74,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
72
74
|
};
|
|
73
75
|
isNotRequired(): {
|
|
74
76
|
type(type: TTypes, message: string): {
|
|
77
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
78
|
+
cpf: (message: string) => /*elided*/ any;
|
|
75
79
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
76
80
|
email: (message: string) => /*elided*/ any;
|
|
77
81
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
|
@@ -100,6 +104,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
100
104
|
};
|
|
101
105
|
conditionallyRequired(isRequired: boolean, message: string): {
|
|
102
106
|
type(type: TTypes, message: string): {
|
|
107
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
108
|
+
cpf: (message: string) => /*elided*/ any;
|
|
103
109
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
104
110
|
email: (message: string) => /*elided*/ any;
|
|
105
111
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
package/dist/index.d.ts
CHANGED
|
@@ -44,6 +44,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
44
44
|
validate<K extends keyof TRaw>(field: K): {
|
|
45
45
|
isRequired(message: string): {
|
|
46
46
|
type(type: TTypes, message: string): {
|
|
47
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
48
|
+
cpf: (message: string) => /*elided*/ any;
|
|
47
49
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
48
50
|
email: (message: string) => /*elided*/ any;
|
|
49
51
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
|
@@ -72,6 +74,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
72
74
|
};
|
|
73
75
|
isNotRequired(): {
|
|
74
76
|
type(type: TTypes, message: string): {
|
|
77
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
78
|
+
cpf: (message: string) => /*elided*/ any;
|
|
75
79
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
76
80
|
email: (message: string) => /*elided*/ any;
|
|
77
81
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
|
@@ -100,6 +104,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
100
104
|
};
|
|
101
105
|
conditionallyRequired(isRequired: boolean, message: string): {
|
|
102
106
|
type(type: TTypes, message: string): {
|
|
107
|
+
cnpj: (message: string) => /*elided*/ any;
|
|
108
|
+
cpf: (message: string) => /*elided*/ any;
|
|
103
109
|
each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
|
|
104
110
|
email: (message: string) => /*elided*/ any;
|
|
105
111
|
fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,60 @@ var __export = (target, all) => {
|
|
|
4
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
+
// src/helpers/document.ts
|
|
8
|
+
function isValidCPF(cpf) {
|
|
9
|
+
if (!cpf) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
13
|
+
const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
14
|
+
let tempCpf = cpf.slice(0, 9);
|
|
15
|
+
let soma = 0;
|
|
16
|
+
for (let i = 0; i < 9; i++) {
|
|
17
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
|
|
18
|
+
}
|
|
19
|
+
let resto = soma % 11;
|
|
20
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
21
|
+
let digito = resto.toString();
|
|
22
|
+
tempCpf += digito;
|
|
23
|
+
soma = 0;
|
|
24
|
+
for (let i = 0; i < 10; i++) {
|
|
25
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
|
|
26
|
+
}
|
|
27
|
+
resto = soma % 11;
|
|
28
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
29
|
+
digito += resto;
|
|
30
|
+
return cpf.endsWith(digito);
|
|
31
|
+
}
|
|
32
|
+
function isValidCNPJ(cnpj) {
|
|
33
|
+
function validCalc(x, numbers2) {
|
|
34
|
+
const slice = numbers2.slice(0, x);
|
|
35
|
+
let factor = x - 7;
|
|
36
|
+
let sum = 0;
|
|
37
|
+
for (let i = x; i >= 1; i--) {
|
|
38
|
+
const n = slice[x - i];
|
|
39
|
+
sum += n * factor--;
|
|
40
|
+
if (factor < 2) factor = 9;
|
|
41
|
+
}
|
|
42
|
+
const result = 11 - sum % 11;
|
|
43
|
+
return result > 9 ? 0 : result;
|
|
44
|
+
}
|
|
45
|
+
function matchNumbers(value = "") {
|
|
46
|
+
const match = value.toString().match(/\d/g);
|
|
47
|
+
return Array.isArray(match) ? match.map(Number) : [];
|
|
48
|
+
}
|
|
49
|
+
if (!cnpj) return false;
|
|
50
|
+
const numbers = matchNumbers(cnpj);
|
|
51
|
+
if (numbers.length !== 14) return false;
|
|
52
|
+
const items = new Set(numbers);
|
|
53
|
+
if (items.size === 1) return false;
|
|
54
|
+
const digits = numbers.slice(12);
|
|
55
|
+
const digit0 = validCalc(12, numbers);
|
|
56
|
+
if (digit0 !== digits[0]) return false;
|
|
57
|
+
const digit1 = validCalc(13, numbers);
|
|
58
|
+
return digit1 === digits[1];
|
|
59
|
+
}
|
|
60
|
+
|
|
7
61
|
// src/helpers/sanitize.ts
|
|
8
62
|
import { FilterXSS } from "xss";
|
|
9
63
|
var allTags = [
|
|
@@ -158,6 +212,32 @@ function createValidator() {
|
|
|
158
212
|
return !current.required && (current.value === void 0 || current.value === null || current.value === "");
|
|
159
213
|
};
|
|
160
214
|
const validator = {
|
|
215
|
+
cnpj: (message) => {
|
|
216
|
+
if (shouldSkipValidation()) {
|
|
217
|
+
return validator;
|
|
218
|
+
}
|
|
219
|
+
if (typeof current.value === "string") {
|
|
220
|
+
if (!isValidCNPJ(current.value)) {
|
|
221
|
+
current.pushError(message);
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
current.pushError(message);
|
|
225
|
+
}
|
|
226
|
+
return validator;
|
|
227
|
+
},
|
|
228
|
+
cpf: (message) => {
|
|
229
|
+
if (shouldSkipValidation()) {
|
|
230
|
+
return validator;
|
|
231
|
+
}
|
|
232
|
+
if (typeof current.value === "string") {
|
|
233
|
+
if (!isValidCPF(current.value)) {
|
|
234
|
+
current.pushError(message);
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
current.pushError(message);
|
|
238
|
+
}
|
|
239
|
+
return validator;
|
|
240
|
+
},
|
|
161
241
|
each(callback) {
|
|
162
242
|
if (shouldSkipValidation()) {
|
|
163
243
|
return validator;
|
package/package.json
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export function isValidCPF(cpf: string): boolean {
|
|
2
|
+
if (!cpf) {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
7
|
+
const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
8
|
+
let tempCpf = cpf.slice(0, 9);
|
|
9
|
+
let soma = 0;
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < 9; i++) {
|
|
12
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let resto = soma % 11;
|
|
16
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
17
|
+
|
|
18
|
+
let digito = resto.toString();
|
|
19
|
+
tempCpf += digito;
|
|
20
|
+
soma = 0;
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < 10; i++) {
|
|
23
|
+
soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
resto = soma % 11;
|
|
27
|
+
resto = resto < 2 ? 0 : 11 - resto;
|
|
28
|
+
digito += resto;
|
|
29
|
+
|
|
30
|
+
return cpf.endsWith(digito);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isValidCNPJ(cnpj: string): boolean {
|
|
34
|
+
function validCalc(x: number, numbers: number[]) {
|
|
35
|
+
const slice = numbers.slice(0, x);
|
|
36
|
+
let factor = x - 7;
|
|
37
|
+
let sum = 0;
|
|
38
|
+
|
|
39
|
+
for (let i = x; i >= 1; i--) {
|
|
40
|
+
const n = slice[x - i];
|
|
41
|
+
sum += n * factor--;
|
|
42
|
+
if (factor < 2) factor = 9;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const result = 11 - (sum % 11);
|
|
46
|
+
|
|
47
|
+
return result > 9 ? 0 : result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function matchNumbers(value: string | number | number[] = '') {
|
|
51
|
+
const match = value.toString().match(/\d/g);
|
|
52
|
+
return Array.isArray(match) ? match.map(Number) : [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!cnpj) return false;
|
|
56
|
+
|
|
57
|
+
// Elimina tudo que não é dígito
|
|
58
|
+
const numbers = matchNumbers(cnpj);
|
|
59
|
+
|
|
60
|
+
// Valida a quantidade de dígitos
|
|
61
|
+
if (numbers.length !== 14) return false;
|
|
62
|
+
|
|
63
|
+
// Elimina inválidos com todos os dígitos iguais
|
|
64
|
+
const items = new Set(numbers);
|
|
65
|
+
if (items.size === 1) return false;
|
|
66
|
+
|
|
67
|
+
// Separa os 2 últimos dígitos verificadores
|
|
68
|
+
const digits = numbers.slice(12);
|
|
69
|
+
|
|
70
|
+
// Valida 1o. dígito verificador
|
|
71
|
+
const digit0 = validCalc(12, numbers);
|
|
72
|
+
if (digit0 !== digits[0]) return false;
|
|
73
|
+
|
|
74
|
+
// Valida 2o. dígito verificador
|
|
75
|
+
const digit1 = validCalc(13, numbers);
|
|
76
|
+
return digit1 === digits[1];
|
|
77
|
+
}
|
package/src/helpers/validator.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isValidCNPJ, isValidCPF } from './document';
|
|
1
2
|
import { sanitizeValue, type AllowedTag } from './sanitize';
|
|
2
3
|
import { video } from './video';
|
|
3
4
|
|
|
@@ -128,6 +129,36 @@ export function createValidator<
|
|
|
128
129
|
};
|
|
129
130
|
|
|
130
131
|
const validator = {
|
|
132
|
+
cnpj: (message: string) => {
|
|
133
|
+
if (shouldSkipValidation()) {
|
|
134
|
+
return validator;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (typeof current.value === 'string') {
|
|
138
|
+
if (!isValidCNPJ(current.value)) {
|
|
139
|
+
current.pushError(message);
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
current.pushError(message);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return validator;
|
|
146
|
+
},
|
|
147
|
+
cpf: (message: string) => {
|
|
148
|
+
if (shouldSkipValidation()) {
|
|
149
|
+
return validator;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (typeof current.value === 'string') {
|
|
153
|
+
if (!isValidCPF(current.value)) {
|
|
154
|
+
current.pushError(message);
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
current.pushError(message);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return validator;
|
|
161
|
+
},
|
|
131
162
|
each(
|
|
132
163
|
callback: <U extends Record<string, TInputValue>>(
|
|
133
164
|
item: U,
|