@excofy/utils 1.0.14 → 2.0.0
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 +22 -2
- package/dist/index.d.cts +10 -6
- package/dist/index.d.ts +10 -6
- package/dist/index.js +22 -2
- package/package.json +1 -1
- package/src/helpers/generateCode.ts +25 -7
- package/src/helpers/validator.ts +15 -0
package/dist/index.cjs
CHANGED
|
@@ -446,6 +446,15 @@ function createValidator() {
|
|
|
446
446
|
}
|
|
447
447
|
return validator;
|
|
448
448
|
},
|
|
449
|
+
sanitizeAlphanumeric() {
|
|
450
|
+
if (shouldSkipValidation()) return validator;
|
|
451
|
+
if (typeof current.value === "string") {
|
|
452
|
+
const cleaned = current.value.replace(/[^A-Za-z0-9]/g, "").trim();
|
|
453
|
+
current.value = cleaned;
|
|
454
|
+
current.inputs[current.field] = cleaned;
|
|
455
|
+
}
|
|
456
|
+
return validator;
|
|
457
|
+
},
|
|
449
458
|
sanitizeDigits() {
|
|
450
459
|
if (shouldSkipValidation()) return validator;
|
|
451
460
|
if (typeof current.value === "string") {
|
|
@@ -647,10 +656,21 @@ function createValidator() {
|
|
|
647
656
|
}
|
|
648
657
|
|
|
649
658
|
// src/helpers/generateCode.ts
|
|
650
|
-
var generateCode = (length = 6,
|
|
659
|
+
var generateCode = (length = 6, type = "alphanumeric") => {
|
|
651
660
|
const digits = "0123456789";
|
|
652
661
|
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
653
|
-
|
|
662
|
+
let charset;
|
|
663
|
+
switch (type) {
|
|
664
|
+
case "numeric":
|
|
665
|
+
charset = digits;
|
|
666
|
+
break;
|
|
667
|
+
case "letters":
|
|
668
|
+
charset = letters;
|
|
669
|
+
break;
|
|
670
|
+
default:
|
|
671
|
+
charset = digits + letters;
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
654
674
|
let result = "";
|
|
655
675
|
for (let i = 0; i < length; i++) {
|
|
656
676
|
const randomIndex = Math.floor(Math.random() * charset.length);
|
package/dist/index.d.cts
CHANGED
|
@@ -32,6 +32,7 @@ interface ValidatorField {
|
|
|
32
32
|
slug(message: string): ValidatorField;
|
|
33
33
|
sanitize(): ValidatorField;
|
|
34
34
|
sanitizeAlphaSpaces(): ValidatorField;
|
|
35
|
+
sanitizeAlphanumeric(): ValidatorField;
|
|
35
36
|
sanitizeDigits(): ValidatorField;
|
|
36
37
|
sanitizePreservingNewlines(): ValidatorField;
|
|
37
38
|
sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
|
|
@@ -89,16 +90,19 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
89
90
|
* Gera um código de validação.
|
|
90
91
|
*
|
|
91
92
|
* @param {number} [length=6] - Número de caracteres do código.
|
|
92
|
-
* @param {
|
|
93
|
-
*
|
|
93
|
+
* @param {'alphanumeric' | 'numeric' | 'letters'} [type='alphanumeric'] - Tipo de caracteres a serem usados.
|
|
94
|
+
* - 'alphanumeric': letras e números (padrão)
|
|
95
|
+
* - 'numeric': apenas dígitos numéricos
|
|
96
|
+
* - 'letters': apenas letras
|
|
94
97
|
* @returns {string} Código gerado.
|
|
95
98
|
*
|
|
96
99
|
* @example
|
|
97
|
-
* generateCode(); // "
|
|
98
|
-
* generateCode(8); // "
|
|
99
|
-
* generateCode(
|
|
100
|
+
* generateCode(); // "A9F3D2" (padrão)
|
|
101
|
+
* generateCode(8); // "B3K7E1A2" (padrão com 8 caracteres)
|
|
102
|
+
* generateCode(6, 'numeric'); // "839102"
|
|
103
|
+
* generateCode(8, 'letters'); // "XCVBHJKL"
|
|
100
104
|
*/
|
|
101
|
-
declare const generateCode: (length?: number,
|
|
105
|
+
declare const generateCode: (length?: number, type?: "alphanumeric" | "numeric" | "letters") => string;
|
|
102
106
|
|
|
103
107
|
declare const stringUtils: {
|
|
104
108
|
removeFileExtension: (fileName: string) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ interface ValidatorField {
|
|
|
32
32
|
slug(message: string): ValidatorField;
|
|
33
33
|
sanitize(): ValidatorField;
|
|
34
34
|
sanitizeAlphaSpaces(): ValidatorField;
|
|
35
|
+
sanitizeAlphanumeric(): ValidatorField;
|
|
35
36
|
sanitizeDigits(): ValidatorField;
|
|
36
37
|
sanitizePreservingNewlines(): ValidatorField;
|
|
37
38
|
sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
|
|
@@ -89,16 +90,19 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
89
90
|
* Gera um código de validação.
|
|
90
91
|
*
|
|
91
92
|
* @param {number} [length=6] - Número de caracteres do código.
|
|
92
|
-
* @param {
|
|
93
|
-
*
|
|
93
|
+
* @param {'alphanumeric' | 'numeric' | 'letters'} [type='alphanumeric'] - Tipo de caracteres a serem usados.
|
|
94
|
+
* - 'alphanumeric': letras e números (padrão)
|
|
95
|
+
* - 'numeric': apenas dígitos numéricos
|
|
96
|
+
* - 'letters': apenas letras
|
|
94
97
|
* @returns {string} Código gerado.
|
|
95
98
|
*
|
|
96
99
|
* @example
|
|
97
|
-
* generateCode(); // "
|
|
98
|
-
* generateCode(8); // "
|
|
99
|
-
* generateCode(
|
|
100
|
+
* generateCode(); // "A9F3D2" (padrão)
|
|
101
|
+
* generateCode(8); // "B3K7E1A2" (padrão com 8 caracteres)
|
|
102
|
+
* generateCode(6, 'numeric'); // "839102"
|
|
103
|
+
* generateCode(8, 'letters'); // "XCVBHJKL"
|
|
100
104
|
*/
|
|
101
|
-
declare const generateCode: (length?: number,
|
|
105
|
+
declare const generateCode: (length?: number, type?: "alphanumeric" | "numeric" | "letters") => string;
|
|
102
106
|
|
|
103
107
|
declare const stringUtils: {
|
|
104
108
|
removeFileExtension: (fileName: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -410,6 +410,15 @@ function createValidator() {
|
|
|
410
410
|
}
|
|
411
411
|
return validator;
|
|
412
412
|
},
|
|
413
|
+
sanitizeAlphanumeric() {
|
|
414
|
+
if (shouldSkipValidation()) return validator;
|
|
415
|
+
if (typeof current.value === "string") {
|
|
416
|
+
const cleaned = current.value.replace(/[^A-Za-z0-9]/g, "").trim();
|
|
417
|
+
current.value = cleaned;
|
|
418
|
+
current.inputs[current.field] = cleaned;
|
|
419
|
+
}
|
|
420
|
+
return validator;
|
|
421
|
+
},
|
|
413
422
|
sanitizeDigits() {
|
|
414
423
|
if (shouldSkipValidation()) return validator;
|
|
415
424
|
if (typeof current.value === "string") {
|
|
@@ -611,10 +620,21 @@ function createValidator() {
|
|
|
611
620
|
}
|
|
612
621
|
|
|
613
622
|
// src/helpers/generateCode.ts
|
|
614
|
-
var generateCode = (length = 6,
|
|
623
|
+
var generateCode = (length = 6, type = "alphanumeric") => {
|
|
615
624
|
const digits = "0123456789";
|
|
616
625
|
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
617
|
-
|
|
626
|
+
let charset;
|
|
627
|
+
switch (type) {
|
|
628
|
+
case "numeric":
|
|
629
|
+
charset = digits;
|
|
630
|
+
break;
|
|
631
|
+
case "letters":
|
|
632
|
+
charset = letters;
|
|
633
|
+
break;
|
|
634
|
+
default:
|
|
635
|
+
charset = digits + letters;
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
618
638
|
let result = "";
|
|
619
639
|
for (let i = 0; i < length; i++) {
|
|
620
640
|
const randomIndex = Math.floor(Math.random() * charset.length);
|
package/package.json
CHANGED
|
@@ -2,19 +2,37 @@
|
|
|
2
2
|
* Gera um código de validação.
|
|
3
3
|
*
|
|
4
4
|
* @param {number} [length=6] - Número de caracteres do código.
|
|
5
|
-
* @param {
|
|
6
|
-
*
|
|
5
|
+
* @param {'alphanumeric' | 'numeric' | 'letters'} [type='alphanumeric'] - Tipo de caracteres a serem usados.
|
|
6
|
+
* - 'alphanumeric': letras e números (padrão)
|
|
7
|
+
* - 'numeric': apenas dígitos numéricos
|
|
8
|
+
* - 'letters': apenas letras
|
|
7
9
|
* @returns {string} Código gerado.
|
|
8
10
|
*
|
|
9
11
|
* @example
|
|
10
|
-
* generateCode(); // "
|
|
11
|
-
* generateCode(8); // "
|
|
12
|
-
* generateCode(
|
|
12
|
+
* generateCode(); // "A9F3D2" (padrão)
|
|
13
|
+
* generateCode(8); // "B3K7E1A2" (padrão com 8 caracteres)
|
|
14
|
+
* generateCode(6, 'numeric'); // "839102"
|
|
15
|
+
* generateCode(8, 'letters'); // "XCVBHJKL"
|
|
13
16
|
*/
|
|
14
|
-
export const generateCode = (
|
|
17
|
+
export const generateCode = (
|
|
18
|
+
length = 6,
|
|
19
|
+
type: 'alphanumeric' | 'numeric' | 'letters' = 'alphanumeric'
|
|
20
|
+
): string => {
|
|
15
21
|
const digits = '0123456789';
|
|
16
22
|
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
17
|
-
|
|
23
|
+
let charset: string;
|
|
24
|
+
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'numeric':
|
|
27
|
+
charset = digits;
|
|
28
|
+
break;
|
|
29
|
+
case 'letters':
|
|
30
|
+
charset = letters;
|
|
31
|
+
break;
|
|
32
|
+
default:
|
|
33
|
+
charset = digits + letters;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
18
36
|
|
|
19
37
|
let result = '';
|
|
20
38
|
for (let i = 0; i < length; i++) {
|
package/src/helpers/validator.ts
CHANGED
|
@@ -39,6 +39,7 @@ interface ValidatorField {
|
|
|
39
39
|
slug(message: string): ValidatorField;
|
|
40
40
|
sanitize(): ValidatorField;
|
|
41
41
|
sanitizeAlphaSpaces(): ValidatorField;
|
|
42
|
+
sanitizeAlphanumeric(): ValidatorField;
|
|
42
43
|
sanitizeDigits(): ValidatorField;
|
|
43
44
|
sanitizePreservingNewlines(): ValidatorField;
|
|
44
45
|
sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
|
|
@@ -421,6 +422,20 @@ export function createValidator<
|
|
|
421
422
|
return validator;
|
|
422
423
|
},
|
|
423
424
|
|
|
425
|
+
sanitizeAlphanumeric() {
|
|
426
|
+
if (shouldSkipValidation()) return validator;
|
|
427
|
+
|
|
428
|
+
if (typeof current.value === 'string') {
|
|
429
|
+
const cleaned = current.value
|
|
430
|
+
.replace(/[^A-Za-z0-9]/g, '') // remove tudo que não é letra ou número
|
|
431
|
+
.trim();
|
|
432
|
+
current.value = cleaned;
|
|
433
|
+
current.inputs[current.field] = cleaned as TRaw[keyof TRaw];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return validator;
|
|
437
|
+
},
|
|
438
|
+
|
|
424
439
|
sanitizeDigits() {
|
|
425
440
|
if (shouldSkipValidation()) return validator;
|
|
426
441
|
|