@arkyn/shared 1.5.0 → 1.5.2
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/LICENSE.txt +24 -0
- package/README.md +113 -0
- package/dist/formats/formatDate.d.ts +43 -0
- package/dist/formats/formatDate.d.ts.map +1 -0
- package/dist/formats/formatDate.js +77 -0
- package/dist/formats/formatJsonObject.d.ts +31 -1
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +67 -26
- package/dist/formats/formatJsonString.d.ts +37 -1
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +38 -4
- package/dist/formats/formatToCep.d.ts +27 -1
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +31 -6
- package/dist/formats/formatToCnpj.d.ts +30 -0
- package/dist/formats/formatToCnpj.d.ts.map +1 -0
- package/dist/formats/formatToCnpj.js +35 -0
- package/dist/formats/formatToCpf.d.ts +30 -0
- package/dist/formats/formatToCpf.d.ts.map +1 -0
- package/dist/formats/formatToCpf.js +35 -0
- package/dist/formats/formatToCpfCnpj.d.ts +22 -1
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +27 -7
- package/dist/formats/formatToCurrency.d.ts +29 -0
- package/dist/formats/formatToCurrency.d.ts.map +1 -0
- package/dist/formats/formatToCurrency.js +41 -0
- package/dist/formats/formatToEllipsis.d.ts +14 -1
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +20 -3
- package/dist/formats/formatToHiddenDigits.d.ts +30 -2
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +49 -3
- package/dist/formats/formatToPhone.d.ts +32 -1
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +128 -6
- package/dist/generators/generateColorByString.d.ts +13 -1
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +15 -4
- package/dist/generators/generateId.d.ts +26 -4
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +11 -5
- package/dist/generators/generateSlug.d.ts +15 -1
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +16 -2
- package/dist/index.d.ts +9 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -12
- package/dist/services/calculateCardInstallment.d.ts +31 -5
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +52 -11
- package/dist/services/maskSensitiveData.d.ts +24 -1
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +24 -2
- package/dist/services/removeCurrencySymbols.d.ts +20 -0
- package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
- package/dist/services/removeCurrencySymbols.js +23 -0
- package/dist/services/removeNonNumeric.d.ts +15 -0
- package/dist/services/removeNonNumeric.d.ts.map +1 -0
- package/dist/services/removeNonNumeric.js +16 -0
- package/dist/services/truncateLargeFields.d.ts +30 -1
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +34 -6
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +54 -0
- package/dist/validations/validateDate.d.ts +27 -6
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +38 -12
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +27 -1
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +29 -4
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +7 -2
- package/src/formats/__test__/formatDate.spec.ts +88 -0
- package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
- package/src/formats/__test__/formatJsonString.spec.ts +83 -0
- package/src/formats/__test__/formatToCep.spec.ts +37 -0
- package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
- package/src/formats/__test__/formatToCpf.spec.ts +37 -0
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
- package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
- package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
- package/src/formats/__test__/formatToPhone.spec.ts +58 -0
- package/src/formats/formatDate.ts +97 -0
- package/src/formats/formatJsonObject.ts +68 -26
- package/src/formats/formatJsonString.ts +40 -4
- package/src/formats/formatToCep.ts +35 -7
- package/src/formats/formatToCnpj.ts +39 -0
- package/src/formats/formatToCpf.ts +39 -0
- package/src/formats/formatToCpfCnpj.ts +31 -7
- package/src/formats/formatToCurrency.ts +53 -0
- package/src/formats/formatToEllipsis.ts +23 -3
- package/src/formats/formatToHiddenDigits.ts +75 -7
- package/src/formats/formatToPhone.ts +156 -6
- package/src/generators/__test__/generateColorByString.spec.ts +37 -0
- package/src/generators/__test__/generateId.spec.ts +44 -0
- package/src/generators/__test__/generateSlug.spec.ts +47 -0
- package/src/generators/generateColorByString.ts +18 -4
- package/src/generators/generateId.ts +36 -5
- package/src/generators/generateSlug.ts +17 -2
- package/src/index.ts +10 -13
- package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
- package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
- package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
- package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
- package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
- package/src/services/calculateCardInstallment.ts +57 -12
- package/src/services/maskSensitiveData.ts +29 -4
- package/src/services/removeCurrencySymbols.ts +25 -0
- package/src/services/removeNonNumeric.ts +18 -0
- package/src/services/truncateLargeFields.ts +40 -6
- package/src/validations/__test__/validateCep.spec.ts +40 -0
- package/src/validations/__test__/validateCnpj.spec.ts +42 -0
- package/src/validations/__test__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePassword.spec.ts +43 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/__test__/validateRg.spec.ts +48 -0
- package/src/validations/validateCep.ts +40 -0
- package/src/validations/validateCnpj.ts +64 -0
- package/src/validations/validateCpf.ts +61 -0
- package/src/validations/validateDate.ts +39 -17
- package/src/validations/validatePassword.ts +41 -0
- package/src/validations/validatePhone.ts +31 -4
- package/src/validations/validateRg.ts +37 -0
- package/tsconfig.json +2 -2
- package/vitest.config.ts +5 -0
- package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
- package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateHour.js +0 -12
- package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateToDate.js +0 -9
- package/dist/formats/formatDateHour.d.ts +0 -3
- package/dist/formats/formatDateHour.d.ts.map +0 -1
- package/dist/formats/formatDateHour.js +0 -11
- package/dist/formats/formatToBRL.d.ts +0 -3
- package/dist/formats/formatToBRL.d.ts.map +0 -1
- package/dist/formats/formatToBRL.js +0 -8
- package/dist/formats/formatToCNPJ.d.ts +0 -3
- package/dist/formats/formatToCNPJ.d.ts.map +0 -1
- package/dist/formats/formatToCNPJ.js +0 -9
- package/dist/formats/formatToCPF.d.ts +0 -3
- package/dist/formats/formatToCPF.d.ts.map +0 -1
- package/dist/formats/formatToCPF.js +0 -9
- package/dist/parsers/parseToCharacters.d.ts +0 -17
- package/dist/parsers/parseToCharacters.d.ts.map +0 -1
- package/dist/parsers/parseToCharacters.js +0 -13
- package/dist/services/range.d.ts +0 -4
- package/dist/services/range.d.ts.map +0 -1
- package/dist/services/range.js +0 -9
- package/dist/validations/regex.d.ts +0 -15
- package/dist/validations/regex.d.ts.map +0 -1
- package/dist/validations/regex.js +0 -25
- package/dist/validations/validateCPF.d.ts +0 -3
- package/dist/validations/validateCPF.d.ts.map +0 -1
- package/dist/validations/validateCPF.js +0 -36
- package/src/formats/formatBrazilianDateHour.ts +0 -17
- package/src/formats/formatBrazilianDateToDate.ts +0 -13
- package/src/formats/formatDateHour.ts +0 -15
- package/src/formats/formatToBRL.ts +0 -10
- package/src/formats/formatToCNPJ.ts +0 -10
- package/src/formats/formatToCPF.ts +0 -10
- package/src/parsers/parseToCharacters.ts +0 -34
- package/src/services/range.ts +0 -15
- package/src/validations/regex.ts +0 -41
- package/src/validations/validateCPF.ts +0 -38
|
@@ -1,7 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { MaskSensitiveDataFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Masks sensitive data in a JSON string by replacing the values of specified keys with "****".
|
|
5
|
+
*
|
|
6
|
+
* @param jsonString - The JSON string to be processed.
|
|
7
|
+
* @param sensitiveKeys - An array of keys whose values should be masked. Defaults to `["password", "confirmPassword", "creditCard"]`.
|
|
8
|
+
* @returns A JSON string with sensitive data masked. If the input is not a valid JSON string, it returns the original string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const jsonString = JSON.stringify({
|
|
13
|
+
* username: "user123",
|
|
14
|
+
* password: "secret",
|
|
15
|
+
* profile: {
|
|
16
|
+
* creditCard: "1234-5678-9012-3456",
|
|
17
|
+
* },
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const result = maskSensitiveData(jsonString, ["password", "creditCard"]);
|
|
21
|
+
* console.log(result);
|
|
22
|
+
* // Output: '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const maskSensitiveData: MaskSensitiveDataFunction = (
|
|
27
|
+
jsonString,
|
|
3
28
|
sensitiveKeys = ["password", "confirmPassword", "creditCard"]
|
|
4
|
-
)
|
|
29
|
+
) => {
|
|
5
30
|
function maskValue(key: string, value: any): any {
|
|
6
31
|
if (sensitiveKeys.includes(key)) return "****";
|
|
7
32
|
return value;
|
|
@@ -35,6 +60,6 @@ function maskSensitiveData(
|
|
|
35
60
|
} catch (error) {
|
|
36
61
|
return jsonString;
|
|
37
62
|
}
|
|
38
|
-
}
|
|
63
|
+
};
|
|
39
64
|
|
|
40
65
|
export { maskSensitiveData };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes currency symbols from a given formatted string.
|
|
3
|
+
*
|
|
4
|
+
* This function takes a string that may contain currency symbols
|
|
5
|
+
* and removes them using a regular expression. The resulting string
|
|
6
|
+
* is also trimmed of any leading or trailing whitespace.
|
|
7
|
+
*
|
|
8
|
+
* @param formattedValue - The input string containing currency symbols.
|
|
9
|
+
* @returns A string with all currency symbols removed and trimmed of whitespace.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* removeCurrencySymbols("R$13,45"); // "13,45"
|
|
13
|
+
* removeCurrencySymbols("$123.45"); // "123.45"
|
|
14
|
+
* removeCurrencySymbols("€99.99"); // "99.99"
|
|
15
|
+
* removeCurrencySymbols("¥1,000"); // "1,000"
|
|
16
|
+
* removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const removeCurrencySymbols = (formattedValue: string): string => {
|
|
20
|
+
return formattedValue
|
|
21
|
+
.replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "") // Inclui "R$" e outros símbolos comuns
|
|
22
|
+
.trim();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { removeCurrencySymbols };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all non-numeric characters from a given string.
|
|
3
|
+
*
|
|
4
|
+
* @param prop - The input string from which non-numeric characters will be removed.
|
|
5
|
+
* @returns A new string containing only numeric characters from the input.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const result = removeNonNumeric("abc123def456");
|
|
10
|
+
* console.log(result); // Output: "123456"
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function removeNonNumeric(prop: string): string {
|
|
15
|
+
return prop.replace(/[^0-9]/g, "");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { removeNonNumeric };
|
|
@@ -1,5 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { TruncateLargeFieldsFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Truncates large string fields in a JSON string to a specified maximum length.
|
|
5
|
+
*
|
|
6
|
+
* This function parses a JSON string, traverses its structure recursively, and truncates
|
|
7
|
+
* any string fields that exceed the specified maximum length. If a string field is truncated,
|
|
8
|
+
* it is replaced with a message indicating the original length of the field.
|
|
9
|
+
*
|
|
10
|
+
* @param jsonString - The JSON string to process.
|
|
11
|
+
* @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
|
|
12
|
+
* @returns A JSON string with large string fields truncated.
|
|
13
|
+
*
|
|
14
|
+
* @throws {Error} Throws an error if the input is not a valid JSON string.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const json = JSON.stringify({
|
|
19
|
+
* name: "John",
|
|
20
|
+
* description: "A very long description that exceeds the maximum length...",
|
|
21
|
+
* nested: {
|
|
22
|
+
* details: "Another long string that needs truncation."
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const result = truncateLargeFields(json, 50);
|
|
27
|
+
* console.log(result);
|
|
28
|
+
* // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const truncateLargeFields: TruncateLargeFieldsFunction = (
|
|
33
|
+
jsonString,
|
|
34
|
+
maxLength = 1000
|
|
35
|
+
) => {
|
|
36
|
+
function truncateValue(value: any): any {
|
|
3
37
|
if (typeof value === "string" && value.length > maxLength) {
|
|
4
38
|
return `To large information: field as ${value.length} characters`;
|
|
5
39
|
}
|
|
@@ -8,16 +42,16 @@ function truncateLargeFields(jsonString: string, maxLength = 1000): string {
|
|
|
8
42
|
|
|
9
43
|
function recursiveTruncate(obj: any): any {
|
|
10
44
|
if (Array.isArray(obj)) {
|
|
11
|
-
return obj.map(recursiveTruncate);
|
|
45
|
+
return obj.map((item) => recursiveTruncate(item)); // Corrigido para processar elementos do array
|
|
12
46
|
} else if (obj !== null && typeof obj === "object") {
|
|
13
47
|
return Object.fromEntries(
|
|
14
48
|
Object.entries(obj).map(([key, value]) => [
|
|
15
49
|
key,
|
|
16
|
-
|
|
50
|
+
recursiveTruncate(value), // Corrigido para aplicar recursão corretamente
|
|
17
51
|
])
|
|
18
52
|
);
|
|
19
53
|
}
|
|
20
|
-
return obj;
|
|
54
|
+
return truncateValue(obj); // Corrigido para truncar valores diretamente
|
|
21
55
|
}
|
|
22
56
|
|
|
23
57
|
try {
|
|
@@ -27,6 +61,6 @@ function truncateLargeFields(jsonString: string, maxLength = 1000): string {
|
|
|
27
61
|
} catch (error) {
|
|
28
62
|
throw new Error("Invalid JSON string");
|
|
29
63
|
}
|
|
30
|
-
}
|
|
64
|
+
};
|
|
31
65
|
|
|
32
66
|
export { truncateLargeFields };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateCep } from "../validateCep";
|
|
3
|
+
|
|
4
|
+
describe("validateCep", () => {
|
|
5
|
+
it("should return true for a valid CEP with formatting", () => {
|
|
6
|
+
const validCep = "12345-678";
|
|
7
|
+
expect(validateCep(validCep)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return true for a valid CEP without formatting", () => {
|
|
11
|
+
const validCep = "12345678";
|
|
12
|
+
expect(validateCep(validCep)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a CEP with letters", () => {
|
|
16
|
+
const invalidCep = "ABCDE-123";
|
|
17
|
+
expect(validateCep(invalidCep)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a CEP with special characters", () => {
|
|
21
|
+
const invalidCep = "12345@678";
|
|
22
|
+
expect(validateCep(invalidCep)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for a CEP with invalid length", () => {
|
|
26
|
+
const shortCep = "12345";
|
|
27
|
+
const longCep = "123456789";
|
|
28
|
+
expect(validateCep(shortCep)).toBe(false);
|
|
29
|
+
expect(validateCep(longCep)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return false for an empty CEP", () => {
|
|
33
|
+
expect(validateCep("")).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should return false for a null or undefined CEP", () => {
|
|
37
|
+
expect(validateCep(null as unknown as string)).toBe(false);
|
|
38
|
+
expect(validateCep(undefined as unknown as string)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateCnpj } from "../validateCnpj";
|
|
3
|
+
|
|
4
|
+
describe("validateCnpj", () => {
|
|
5
|
+
it("should return true for a valid CNPJ", () => {
|
|
6
|
+
const validCnpj = "11.444.777/0001-61";
|
|
7
|
+
expect(validateCnpj(validCnpj)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for an invalid CNPJ", () => {
|
|
11
|
+
const invalidCnpj = "12.345.679/0001-95";
|
|
12
|
+
expect(validateCnpj(invalidCnpj)).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a CNPJ with all digits equal", () => {
|
|
16
|
+
const invalidCnpj = "11111111111111";
|
|
17
|
+
expect(validateCnpj(invalidCnpj)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a CNPJ with invalid length", () => {
|
|
21
|
+
const shortCnpj = "12345678";
|
|
22
|
+
const longCnpj = "123456789012345";
|
|
23
|
+
expect(validateCnpj(shortCnpj)).toBe(false);
|
|
24
|
+
expect(validateCnpj(longCnpj)).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should return false for an empty CNPJ", () => {
|
|
28
|
+
expect(validateCnpj("")).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should return false for a null or undefined CNPJ", () => {
|
|
32
|
+
expect(validateCnpj(null as unknown as string)).toBe(false);
|
|
33
|
+
expect(validateCnpj(undefined as unknown as string)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should handle CNPJs with non-digit characters", () => {
|
|
37
|
+
const validCnpj = "11.444.777/0001-61";
|
|
38
|
+
const formattedCnpj = "11444777000161";
|
|
39
|
+
expect(validateCnpj(validCnpj)).toBe(true);
|
|
40
|
+
expect(validateCnpj(formattedCnpj)).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validateCpf } from "../validateCpf";
|
|
3
|
+
|
|
4
|
+
describe("validateCpf", () => {
|
|
5
|
+
it("should return false for an empty CPF", () => {
|
|
6
|
+
expect(validateCpf("")).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("should return false for a CPF with invalid length", () => {
|
|
10
|
+
expect(validateCpf("123.456")).toBe(false);
|
|
11
|
+
expect(validateCpf("123.456.789.123")).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should return false for a CPF with all digits equal", () => {
|
|
15
|
+
expect(validateCpf("111.111.111-11")).toBe(false);
|
|
16
|
+
expect(validateCpf("222.222.222-22")).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return false for an invalid CPF", () => {
|
|
20
|
+
expect(validateCpf("123.456.789-19")).toBe(false);
|
|
21
|
+
expect(validateCpf("987.654.321-01")).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for a valid CPF", () => {
|
|
25
|
+
expect(validateCpf("111.444.777-35")).toBe(true);
|
|
26
|
+
expect(validateCpf("935.411.347-80")).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should ignore formatting characters and validate correctly", () => {
|
|
30
|
+
expect(validateCpf("11144477735")).toBe(true);
|
|
31
|
+
expect(validateCpf("935.411.347-80")).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should return false for a CPF with non-digit characters", () => {
|
|
35
|
+
expect(validateCpf("111.444.777-3a")).toBe(false);
|
|
36
|
+
expect(validateCpf("935.411.347-8x")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validateDate } from "../validateDate";
|
|
3
|
+
|
|
4
|
+
describe("validateDate", () => {
|
|
5
|
+
it("should return true for valid dates in DD/MM/YYYY format", () => {
|
|
6
|
+
expect(validateDate("31/12/2023")).toBe(true);
|
|
7
|
+
expect(validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
8
|
+
true
|
|
9
|
+
); // Leap year
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should return false for invalid dates in DD/MM/YYYY format", () => {
|
|
13
|
+
expect(validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
14
|
+
false
|
|
15
|
+
); // April has 30 days
|
|
16
|
+
expect(validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
17
|
+
false
|
|
18
|
+
); // Not a leap year
|
|
19
|
+
expect(validateDate("32/01/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
20
|
+
false
|
|
21
|
+
); // Invalid day
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for valid dates in MM-DD-YYYY format", () => {
|
|
25
|
+
expect(validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
26
|
+
true
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for invalid dates in MM-DD-YYYY format", () => {
|
|
31
|
+
expect(validateDate("04-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
32
|
+
false
|
|
33
|
+
); // April has 30 days
|
|
34
|
+
expect(validateDate("02-29-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
35
|
+
false
|
|
36
|
+
); // Not a leap year
|
|
37
|
+
expect(validateDate("13-01-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
38
|
+
false
|
|
39
|
+
); // Invalid month
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should return true for valid dates in YYYY-MM-DD format", () => {
|
|
43
|
+
expect(validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
44
|
+
true
|
|
45
|
+
);
|
|
46
|
+
expect(validateDate("2024-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
47
|
+
true
|
|
48
|
+
); // Leap year
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should return false for invalid dates in YYYY-MM-DD format", () => {
|
|
52
|
+
expect(validateDate("2023-04-31", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
53
|
+
false
|
|
54
|
+
); // April has 30 days
|
|
55
|
+
expect(validateDate("2023-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
56
|
+
false
|
|
57
|
+
); // Not a leap year
|
|
58
|
+
expect(validateDate("2023-13-01", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
59
|
+
false
|
|
60
|
+
); // Invalid month
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should respect minYear and maxYear configuration", () => {
|
|
64
|
+
expect(validateDate("01/01/1899", { minYear: 1900 })).toBe(false); // Below minYear
|
|
65
|
+
expect(validateDate("01/01/1900", { minYear: 1900 })).toBe(true); // Equal to minYear
|
|
66
|
+
expect(validateDate("31/12/3001", { maxYear: 3000 })).toBe(false); // Above maxYear
|
|
67
|
+
expect(validateDate("31/12/3000", { maxYear: 3000 })).toBe(true); // Equal to maxYear
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should throw an error for unsupported date formats", () => {
|
|
71
|
+
expect(() =>
|
|
72
|
+
validateDate("2023.12.31", { inputFormat: "YYYY.MM.DD" } as any)
|
|
73
|
+
).toThrow("Invalid date format");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should return false for completely invalid date strings", () => {
|
|
77
|
+
expect(validateDate("invalid-date")).toBe(false);
|
|
78
|
+
expect(validateDate("12345678")).toBe(false);
|
|
79
|
+
expect(validateDate("")).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validatePassword } from "../validatePassword";
|
|
3
|
+
|
|
4
|
+
describe("validatePassword", () => {
|
|
5
|
+
it("should return true for a valid password", () => {
|
|
6
|
+
const validPassword = "StrongP@ssw0rd!";
|
|
7
|
+
expect(validatePassword(validPassword)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for a password shorter than the minimum length", () => {
|
|
11
|
+
const shortPassword = "Short1!";
|
|
12
|
+
expect(validatePassword(shortPassword)).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a password without an uppercase letter", () => {
|
|
16
|
+
const noUppercase = "weakp@ssw0rd!";
|
|
17
|
+
expect(validatePassword(noUppercase)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a password without a lowercase letter", () => {
|
|
21
|
+
const noLowercase = "WEAKP@SSW0RD!";
|
|
22
|
+
expect(validatePassword(noLowercase)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for a password without a number", () => {
|
|
26
|
+
const noNumber = "NoNumber@!";
|
|
27
|
+
expect(validatePassword(noNumber)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for a password without a special character", () => {
|
|
31
|
+
const noSpecialChar = "NoSpecialChar1";
|
|
32
|
+
expect(validatePassword(noSpecialChar)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should return false for an empty password", () => {
|
|
36
|
+
expect(validatePassword("")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should return false for a null or undefined password", () => {
|
|
40
|
+
expect(validatePassword(null as unknown as string)).toBe(false);
|
|
41
|
+
expect(validatePassword(undefined as unknown as string)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validatePhone } from "../validatePhone";
|
|
3
|
+
|
|
4
|
+
describe("validatePhone", () => {
|
|
5
|
+
it("should return true for valid Brazilian phone numbers", () => {
|
|
6
|
+
expect(validatePhone("+55 32912345678")).toBe(true); // Valid with 9th digit
|
|
7
|
+
expect(validatePhone("+55 3212345678")).toBe(true); // Valid without 9th digit
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for invalid Brazilian phone numbers", () => {
|
|
11
|
+
expect(validatePhone("+5532912345678")).toBe(false); // Missing space
|
|
12
|
+
expect(validatePhone("+55 123456789")).toBe(false); // Invalid format
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return true for valid American Samoa phone numbers", () => {
|
|
16
|
+
expect(validatePhone("+1-684 1234567")).toBe(true); // Valid format
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return false for invalid phone numbers", () => {
|
|
20
|
+
expect(validatePhone("+1-684 12345")).toBe(false); // Too few digits
|
|
21
|
+
expect(validatePhone("+99 1234567890")).toBe(false); // Non-existent country code
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for valid phone numbers from other countries", () => {
|
|
25
|
+
expect(validatePhone("+44 1234567890")).toBe(true); // Example for UK
|
|
26
|
+
expect(validatePhone("+91 9876543210")).toBe(true); // Example for India
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return false for phone numbers with incorrect prefixes", () => {
|
|
30
|
+
expect(validatePhone("+44-20 12345678")).toBe(false); // Invalid prefix for UK
|
|
31
|
+
expect(validatePhone("+91-80 12345678")).toBe(false); // Invalid prefix for India
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle edge cases gracefully", () => {
|
|
35
|
+
expect(validatePhone("")).toBe(false); // Empty string
|
|
36
|
+
expect(validatePhone("+55")).toBe(false); // Only country code
|
|
37
|
+
expect(validatePhone("+55 329123456789")).toBe(false); // Too many digits
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateRg } from "../validateRg";
|
|
3
|
+
|
|
4
|
+
describe("validateRg", () => {
|
|
5
|
+
it("should return true for a valid RG with formatting", () => {
|
|
6
|
+
const validRg = "12.345.678-9";
|
|
7
|
+
expect(validateRg(validRg)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return true for a valid RG without formatting", () => {
|
|
11
|
+
const validRg = "123456789";
|
|
12
|
+
expect(validateRg(validRg)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return true for a valid RG with a verifier letter", () => {
|
|
16
|
+
const validRg = "12345678X";
|
|
17
|
+
expect(validateRg(validRg)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for an RG with invalid length (too short)", () => {
|
|
21
|
+
const invalidRg = "123456";
|
|
22
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for an RG with invalid length (too long)", () => {
|
|
26
|
+
const invalidRg = "1234567890";
|
|
27
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for an RG with special characters", () => {
|
|
31
|
+
const invalidRg = "12.345.678@9";
|
|
32
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should return false for an RG with letters in the middle", () => {
|
|
36
|
+
const invalidRg = "12345A678";
|
|
37
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return false for an empty RG", () => {
|
|
41
|
+
expect(validateRg("")).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should return false for a null or undefined RG", () => {
|
|
45
|
+
expect(validateRg(null as unknown as string)).toBe(false);
|
|
46
|
+
expect(validateRg(undefined as unknown as string)).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ValidateCepFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Removes all non-digit characters from the CEP.
|
|
6
|
+
* @param cep - Raw CEP string.
|
|
7
|
+
* @returns Only numeric characters.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validates a Brazilian CEP (Código de Endereçamento Postal).
|
|
12
|
+
*
|
|
13
|
+
* A valid CEP has 8 numeric digits.
|
|
14
|
+
*
|
|
15
|
+
* @param rawCep - CEP string, may include formatting (e.g., "12345-678").
|
|
16
|
+
* @returns `true` if the CEP is valid, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* validateCep("12345-678"); // true
|
|
21
|
+
* validateCep("12345678"); // true
|
|
22
|
+
* validateCep("ABCDE-123"); // false
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const validateCep: ValidateCepFunction = (rawCep) => {
|
|
27
|
+
if (!rawCep) return false;
|
|
28
|
+
|
|
29
|
+
const validFormat = /^[0-9-]+$/.test(rawCep);
|
|
30
|
+
if (!validFormat) return false;
|
|
31
|
+
|
|
32
|
+
const cep = removeNonNumeric(rawCep);
|
|
33
|
+
|
|
34
|
+
const CEP_LENGTH = 8;
|
|
35
|
+
const isOnlyDigits = /^\d{8}$/.test(cep);
|
|
36
|
+
|
|
37
|
+
return cep.length === CEP_LENGTH && isOnlyDigits;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { validateCep };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ValidateCnpjFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
function isInvalidLength(cnpj: string) {
|
|
5
|
+
const CNPJ_LENGTH = 14;
|
|
6
|
+
return cnpj.length !== CNPJ_LENGTH;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function hasAllDigitsEqual(cnpj: string) {
|
|
10
|
+
const [firstDigit] = cnpj;
|
|
11
|
+
return [...cnpj].every((digit) => digit === firstDigit);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function calculateDigit(cnpj: string, multipliers: number[]) {
|
|
15
|
+
let total = 0;
|
|
16
|
+
for (let i = 0; i < multipliers.length; i++) {
|
|
17
|
+
total += parseInt(cnpj[i]) * multipliers[i];
|
|
18
|
+
}
|
|
19
|
+
const rest = total % 11;
|
|
20
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function extractDigit(cnpj: string) {
|
|
24
|
+
return cnpj.slice(12);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
|
|
29
|
+
*
|
|
30
|
+
* This function performs:
|
|
31
|
+
* - Sanitization (removes non-digit characters).
|
|
32
|
+
* - Length check (must be 14 digits).
|
|
33
|
+
* - Repeating digits check (invalid if all digits are the same).
|
|
34
|
+
* - Verifies the two check digits with the proper weights.
|
|
35
|
+
*
|
|
36
|
+
* @param rawCnpj - CNPJ string, possibly formatted.
|
|
37
|
+
* @returns `true` if valid, otherwise `false`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* validateCnpj("12.345.678/0001-95"); // false
|
|
42
|
+
* validateCnpj("11.444.777/0001-61"); // true
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
const validateCnpj: ValidateCnpjFunction = (rawCnpj) => {
|
|
47
|
+
if (!rawCnpj) return false;
|
|
48
|
+
|
|
49
|
+
const cnpj = removeNonNumeric(rawCnpj);
|
|
50
|
+
|
|
51
|
+
if (isInvalidLength(cnpj)) return false;
|
|
52
|
+
if (hasAllDigitsEqual(cnpj)) return false;
|
|
53
|
+
|
|
54
|
+
const base = cnpj.slice(0, 12);
|
|
55
|
+
const digit1 = calculateDigit(base, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
|
|
56
|
+
const digit2 = calculateDigit(
|
|
57
|
+
base + digit1,
|
|
58
|
+
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return extractDigit(cnpj) === `${digit1}${digit2}`;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { validateCnpj };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ValidateCpfFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
function isInvalidLength(cpf: string) {
|
|
5
|
+
const CPF_LENGTH = 11;
|
|
6
|
+
return cpf.length !== CPF_LENGTH;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function hasAllDigitsEqual(cpf: string) {
|
|
10
|
+
const [firstCpfDigit] = cpf;
|
|
11
|
+
return [...cpf].every((digit) => digit === firstCpfDigit);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function calculateDigit(cpf: string, factor: number) {
|
|
15
|
+
let total = 0;
|
|
16
|
+
for (const digit of cpf) {
|
|
17
|
+
if (factor > 1) total += parseInt(digit) * factor--;
|
|
18
|
+
}
|
|
19
|
+
const rest = total % 11;
|
|
20
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function extractDigit(cpf: string) {
|
|
24
|
+
return cpf.slice(9);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
29
|
+
*
|
|
30
|
+
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
31
|
+
* This function checks if the provided CPF is valid by performing the following steps:
|
|
32
|
+
* - Removes any non-digit characters from the input.
|
|
33
|
+
* - Verifies if the CPF has the correct length (11 digits).
|
|
34
|
+
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
35
|
+
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
36
|
+
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
37
|
+
*
|
|
38
|
+
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
39
|
+
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* validateCpf("123.456.789-09"); // false
|
|
44
|
+
* validateCpf("111.444.777-35"); // true
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
const validateCpf: ValidateCpfFunction = (rawCpf) => {
|
|
49
|
+
if (!rawCpf) return false;
|
|
50
|
+
const cpf = removeNonNumeric(rawCpf);
|
|
51
|
+
|
|
52
|
+
if (isInvalidLength(cpf)) return false;
|
|
53
|
+
if (hasAllDigitsEqual(cpf)) return false;
|
|
54
|
+
|
|
55
|
+
const digit1 = calculateDigit(cpf, 10);
|
|
56
|
+
const digit2 = calculateDigit(cpf, 11);
|
|
57
|
+
|
|
58
|
+
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { validateCpf };
|