@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
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
function isInvalidLength(cnpj) {
|
|
3
|
+
const CNPJ_LENGTH = 14;
|
|
4
|
+
return cnpj.length !== CNPJ_LENGTH;
|
|
5
|
+
}
|
|
6
|
+
function hasAllDigitsEqual(cnpj) {
|
|
7
|
+
const [firstDigit] = cnpj;
|
|
8
|
+
return [...cnpj].every((digit) => digit === firstDigit);
|
|
9
|
+
}
|
|
10
|
+
function calculateDigit(cnpj, multipliers) {
|
|
11
|
+
let total = 0;
|
|
12
|
+
for (let i = 0; i < multipliers.length; i++) {
|
|
13
|
+
total += parseInt(cnpj[i]) * multipliers[i];
|
|
14
|
+
}
|
|
15
|
+
const rest = total % 11;
|
|
16
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
17
|
+
}
|
|
18
|
+
function extractDigit(cnpj) {
|
|
19
|
+
return cnpj.slice(12);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
|
|
23
|
+
*
|
|
24
|
+
* This function performs:
|
|
25
|
+
* - Sanitization (removes non-digit characters).
|
|
26
|
+
* - Length check (must be 14 digits).
|
|
27
|
+
* - Repeating digits check (invalid if all digits are the same).
|
|
28
|
+
* - Verifies the two check digits with the proper weights.
|
|
29
|
+
*
|
|
30
|
+
* @param rawCnpj - CNPJ string, possibly formatted.
|
|
31
|
+
* @returns `true` if valid, otherwise `false`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* validateCnpj("12.345.678/0001-95"); // false
|
|
36
|
+
* validateCnpj("11.444.777/0001-61"); // true
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const validateCnpj = (rawCnpj) => {
|
|
40
|
+
if (!rawCnpj)
|
|
41
|
+
return false;
|
|
42
|
+
const cnpj = removeNonNumeric(rawCnpj);
|
|
43
|
+
if (isInvalidLength(cnpj))
|
|
44
|
+
return false;
|
|
45
|
+
if (hasAllDigitsEqual(cnpj))
|
|
46
|
+
return false;
|
|
47
|
+
const base = cnpj.slice(0, 12);
|
|
48
|
+
const digit1 = calculateDigit(base, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
|
|
49
|
+
const digit2 = calculateDigit(base + digit1, [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
|
|
50
|
+
return extractDigit(cnpj) === `${digit1}${digit2}`;
|
|
51
|
+
};
|
|
52
|
+
export { validateCnpj };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ValidateCpfFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
4
|
+
*
|
|
5
|
+
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
6
|
+
* This function checks if the provided CPF is valid by performing the following steps:
|
|
7
|
+
* - Removes any non-digit characters from the input.
|
|
8
|
+
* - Verifies if the CPF has the correct length (11 digits).
|
|
9
|
+
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
10
|
+
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
11
|
+
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
12
|
+
*
|
|
13
|
+
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
14
|
+
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* validateCpf("123.456.789-09"); // false
|
|
19
|
+
* validateCpf("111.444.777-35"); // true
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare const validateCpf: ValidateCpfFunction;
|
|
23
|
+
export { validateCpf };
|
|
24
|
+
//# sourceMappingURL=validateCpf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateCpf.d.ts","sourceRoot":"","sources":["../../src/validations/validateCpf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AA0BxD;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAWlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
function isInvalidLength(cpf) {
|
|
3
|
+
const CPF_LENGTH = 11;
|
|
4
|
+
return cpf.length !== CPF_LENGTH;
|
|
5
|
+
}
|
|
6
|
+
function hasAllDigitsEqual(cpf) {
|
|
7
|
+
const [firstCpfDigit] = cpf;
|
|
8
|
+
return [...cpf].every((digit) => digit === firstCpfDigit);
|
|
9
|
+
}
|
|
10
|
+
function calculateDigit(cpf, factor) {
|
|
11
|
+
let total = 0;
|
|
12
|
+
for (const digit of cpf) {
|
|
13
|
+
if (factor > 1)
|
|
14
|
+
total += parseInt(digit) * factor--;
|
|
15
|
+
}
|
|
16
|
+
const rest = total % 11;
|
|
17
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
18
|
+
}
|
|
19
|
+
function extractDigit(cpf) {
|
|
20
|
+
return cpf.slice(9);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
24
|
+
*
|
|
25
|
+
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
26
|
+
* This function checks if the provided CPF is valid by performing the following steps:
|
|
27
|
+
* - Removes any non-digit characters from the input.
|
|
28
|
+
* - Verifies if the CPF has the correct length (11 digits).
|
|
29
|
+
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
30
|
+
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
31
|
+
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
32
|
+
*
|
|
33
|
+
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
34
|
+
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* validateCpf("123.456.789-09"); // false
|
|
39
|
+
* validateCpf("111.444.777-35"); // true
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const validateCpf = (rawCpf) => {
|
|
43
|
+
if (!rawCpf)
|
|
44
|
+
return false;
|
|
45
|
+
const cpf = removeNonNumeric(rawCpf);
|
|
46
|
+
if (isInvalidLength(cpf))
|
|
47
|
+
return false;
|
|
48
|
+
if (hasAllDigitsEqual(cpf))
|
|
49
|
+
return false;
|
|
50
|
+
const digit1 = calculateDigit(cpf, 10);
|
|
51
|
+
const digit2 = calculateDigit(cpf, 11);
|
|
52
|
+
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
53
|
+
};
|
|
54
|
+
export { validateCpf };
|
|
@@ -1,8 +1,29 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { ValidateDateFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a date string based on the provided format and configuration.
|
|
4
|
+
*
|
|
5
|
+
* @param rawDate - The date string to validate.
|
|
6
|
+
* @param config - Optional configuration object to customize validation.
|
|
7
|
+
* @param config.inputFormat - The expected format of the input date.
|
|
8
|
+
* Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
|
|
9
|
+
* Defaults to "DD/MM/YYYY".
|
|
10
|
+
* @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
|
|
11
|
+
* @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
|
|
12
|
+
*
|
|
13
|
+
* @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
|
|
14
|
+
*
|
|
15
|
+
* @throws {Error} If an invalid date format is provided in the configuration.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* validateDate("31/12/2023"); // true
|
|
20
|
+
* validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
|
|
21
|
+
* validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
|
|
22
|
+
* validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
|
|
23
|
+
* validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
|
|
24
|
+
* validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const validateDate: ValidateDateFunction;
|
|
7
28
|
export { validateDate };
|
|
8
29
|
//# sourceMappingURL=validateDate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateDate.d.ts","sourceRoot":"","sources":["../../src/validations/validateDate.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"validateDate.d.ts","sourceRoot":"","sources":["../../src/validations/validateDate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBA8CnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,24 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Validates a date string based on the provided format and configuration.
|
|
3
|
+
*
|
|
4
|
+
* @param rawDate - The date string to validate.
|
|
5
|
+
* @param config - Optional configuration object to customize validation.
|
|
6
|
+
* @param config.inputFormat - The expected format of the input date.
|
|
7
|
+
* Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
|
|
8
|
+
* Defaults to "DD/MM/YYYY".
|
|
9
|
+
* @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
|
|
10
|
+
* @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
|
|
11
|
+
*
|
|
12
|
+
* @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
|
|
13
|
+
*
|
|
14
|
+
* @throws {Error} If an invalid date format is provided in the configuration.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* validateDate("31/12/2023"); // true
|
|
19
|
+
* validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
|
|
20
|
+
* validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
|
|
21
|
+
* validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
|
|
22
|
+
* validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
|
|
23
|
+
* validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const validateDate = (rawDate, config) => {
|
|
2
27
|
let day, month, year;
|
|
3
|
-
const
|
|
28
|
+
const inputFormat = config?.inputFormat || "DD/MM/YYYY";
|
|
29
|
+
const minYear = config?.minYear || 1900;
|
|
4
30
|
const maxYear = config?.maxYear || 3000;
|
|
5
|
-
if (
|
|
31
|
+
if (inputFormat === "DD/MM/YYYY") {
|
|
6
32
|
const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
|
|
7
|
-
if (!dateRegex.test(
|
|
33
|
+
if (!dateRegex.test(rawDate))
|
|
8
34
|
return false;
|
|
9
|
-
[, day, month, year] =
|
|
35
|
+
[, day, month, year] = rawDate.match(dateRegex) || [];
|
|
10
36
|
}
|
|
11
|
-
else if (
|
|
37
|
+
else if (inputFormat === "MM-DD-YYYY") {
|
|
12
38
|
const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
13
|
-
if (!dateRegex.test(
|
|
39
|
+
if (!dateRegex.test(rawDate))
|
|
14
40
|
return false;
|
|
15
|
-
[, month, day, year] =
|
|
41
|
+
[, month, day, year] = rawDate.match(dateRegex) || [];
|
|
16
42
|
}
|
|
17
|
-
else if (
|
|
43
|
+
else if (inputFormat === "YYYY-MM-DD") {
|
|
18
44
|
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
19
|
-
if (!dateRegex.test(
|
|
45
|
+
if (!dateRegex.test(rawDate))
|
|
20
46
|
return false;
|
|
21
|
-
[, year, month, day] =
|
|
47
|
+
[, year, month, day] = rawDate.match(dateRegex) || [];
|
|
22
48
|
}
|
|
23
49
|
else {
|
|
24
50
|
throw new Error("Invalid date format");
|
|
@@ -43,5 +69,5 @@ function validateDate(date, format, config) {
|
|
|
43
69
|
return false;
|
|
44
70
|
const isValidDate = new Date(yearNum, monthNum - 1, dayNum).getDate() === dayNum;
|
|
45
71
|
return isValidDate;
|
|
46
|
-
}
|
|
72
|
+
};
|
|
47
73
|
export { validateDate };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ValidatePasswordFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a password based on the following rules:
|
|
4
|
+
* - At least 8 characters
|
|
5
|
+
* - At least 1 uppercase letter
|
|
6
|
+
* - At least 1 letter (any case)
|
|
7
|
+
* - At least 1 number
|
|
8
|
+
* - At least 1 special character
|
|
9
|
+
*
|
|
10
|
+
* @param rawPassword - The raw password string.
|
|
11
|
+
* @returns `true` if password is valid, otherwise `false`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* validatePassword("Senha@123"); // true
|
|
16
|
+
* validatePassword("senha123"); // false (no uppercase, no special char)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
declare const validatePassword: ValidatePasswordFunction;
|
|
20
|
+
export { validatePassword };
|
|
21
|
+
//# sourceMappingURL=validatePassword.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validatePassword.d.ts","sourceRoot":"","sources":["../../src/validations/validatePassword.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAkBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a password based on the following rules:
|
|
3
|
+
* - At least 8 characters
|
|
4
|
+
* - At least 1 uppercase letter
|
|
5
|
+
* - At least 1 letter (any case)
|
|
6
|
+
* - At least 1 number
|
|
7
|
+
* - At least 1 special character
|
|
8
|
+
*
|
|
9
|
+
* @param rawPassword - The raw password string.
|
|
10
|
+
* @returns `true` if password is valid, otherwise `false`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* validatePassword("Senha@123"); // true
|
|
15
|
+
* validatePassword("senha123"); // false (no uppercase, no special char)
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
const validatePassword = (rawPassword) => {
|
|
19
|
+
if (!rawPassword)
|
|
20
|
+
return false;
|
|
21
|
+
const hasMinLength = rawPassword.length >= 8;
|
|
22
|
+
const hasUppercase = /[A-Z]/.test(rawPassword);
|
|
23
|
+
const hasLetter = /[a-z]/.test(rawPassword);
|
|
24
|
+
const hasNumber = /\d/.test(rawPassword);
|
|
25
|
+
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>_\-+=~`[\]\\\/]/.test(rawPassword);
|
|
26
|
+
return [
|
|
27
|
+
hasMinLength,
|
|
28
|
+
hasUppercase,
|
|
29
|
+
hasLetter,
|
|
30
|
+
hasNumber,
|
|
31
|
+
hasSpecialChar,
|
|
32
|
+
].every((condition) => condition);
|
|
33
|
+
};
|
|
34
|
+
export { validatePassword };
|
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ValidatePhoneFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a phone number against a list of country-specific formats.
|
|
4
|
+
*
|
|
5
|
+
* The function iterates through a predefined list of countries and checks if the
|
|
6
|
+
* provided phone number matches the format for any of the countries. It uses
|
|
7
|
+
* regular expressions to validate the phone number based on the country's code,
|
|
8
|
+
* prefix, and mask.
|
|
9
|
+
*
|
|
10
|
+
* Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
|
|
11
|
+
* allows for an optional ninth digit.
|
|
12
|
+
*
|
|
13
|
+
* @param rawPhone - The phone number to validate as a string.
|
|
14
|
+
* @returns `true` if the phone number matches any country's format, otherwise `false`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { validatePhone } from "./validatePhone";
|
|
19
|
+
*
|
|
20
|
+
* validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
|
|
21
|
+
* validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
|
|
22
|
+
* validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
|
|
23
|
+
* validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
|
|
24
|
+
* validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const validatePhone: ValidatePhoneFunction;
|
|
2
28
|
export { validatePhone };
|
|
3
29
|
//# sourceMappingURL=validatePhone.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAiBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -1,19 +1,44 @@
|
|
|
1
1
|
import { countries } from "@arkyn/templates";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Validates a phone number against a list of country-specific formats.
|
|
4
|
+
*
|
|
5
|
+
* The function iterates through a predefined list of countries and checks if the
|
|
6
|
+
* provided phone number matches the format for any of the countries. It uses
|
|
7
|
+
* regular expressions to validate the phone number based on the country's code,
|
|
8
|
+
* prefix, and mask.
|
|
9
|
+
*
|
|
10
|
+
* Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
|
|
11
|
+
* allows for an optional ninth digit.
|
|
12
|
+
*
|
|
13
|
+
* @param rawPhone - The phone number to validate as a string.
|
|
14
|
+
* @returns `true` if the phone number matches any country's format, otherwise `false`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { validatePhone } from "./validatePhone";
|
|
19
|
+
*
|
|
20
|
+
* validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
|
|
21
|
+
* validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
|
|
22
|
+
* validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
|
|
23
|
+
* validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
|
|
24
|
+
* validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const validatePhone = (rawPhone) => {
|
|
3
28
|
for (const country of countries) {
|
|
4
29
|
const countryCode = country.code;
|
|
5
30
|
const prefix = country.prefix ? `-${country.prefix}` : "";
|
|
6
31
|
const digitCount = country.mask.replace(/[^_]/g, "").length;
|
|
7
32
|
if (country.iso === "BR") {
|
|
8
33
|
const brazilRegex = new RegExp(`^\\${countryCode} \\d{2}9?\\d{8}$`);
|
|
9
|
-
if (brazilRegex.test(
|
|
34
|
+
if (brazilRegex.test(rawPhone))
|
|
10
35
|
return true;
|
|
11
36
|
continue;
|
|
12
37
|
}
|
|
13
38
|
const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
|
|
14
|
-
if (regex.test(
|
|
39
|
+
if (regex.test(rawPhone))
|
|
15
40
|
return true;
|
|
16
41
|
}
|
|
17
42
|
return false;
|
|
18
|
-
}
|
|
43
|
+
};
|
|
19
44
|
export { validatePhone };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ValidateRgFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a Brazilian RG (Registro Geral) in a generic way.
|
|
4
|
+
*
|
|
5
|
+
* This function does a basic structure validation:
|
|
6
|
+
* - Removes non-alphanumeric characters.
|
|
7
|
+
* - Ensures length is reasonable (7–9 digits).
|
|
8
|
+
* - Optionally allows for a final letter (verifier).
|
|
9
|
+
*
|
|
10
|
+
* @param rawRg - RG string, possibly formatted.
|
|
11
|
+
* @returns `true` if format seems valid, otherwise `false`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* validateRg("12.345.678-9"); // true
|
|
16
|
+
* validateRg("MG-12.345.678"); // false (not supported)
|
|
17
|
+
* validateRg("12345678X"); // true
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare const validateRg: ValidateRgFunction;
|
|
21
|
+
export { validateRg };
|
|
22
|
+
//# sourceMappingURL=validateRg.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateRg.d.ts","sourceRoot":"","sources":["../../src/validations/validateRg.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;;;;;;GAiBG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAajB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a Brazilian RG (Registro Geral) in a generic way.
|
|
3
|
+
*
|
|
4
|
+
* This function does a basic structure validation:
|
|
5
|
+
* - Removes non-alphanumeric characters.
|
|
6
|
+
* - Ensures length is reasonable (7–9 digits).
|
|
7
|
+
* - Optionally allows for a final letter (verifier).
|
|
8
|
+
*
|
|
9
|
+
* @param rawRg - RG string, possibly formatted.
|
|
10
|
+
* @returns `true` if format seems valid, otherwise `false`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* validateRg("12.345.678-9"); // true
|
|
15
|
+
* validateRg("MG-12.345.678"); // false (not supported)
|
|
16
|
+
* validateRg("12345678X"); // true
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
const validateRg = (rawRg) => {
|
|
20
|
+
if (!rawRg)
|
|
21
|
+
return false;
|
|
22
|
+
const validFormat = /^[0-9a-zA-Z.-]+$/.test(rawRg);
|
|
23
|
+
if (!validFormat)
|
|
24
|
+
return false;
|
|
25
|
+
const rg = rawRg.replace(/[^a-zA-Z0-9]/g, "");
|
|
26
|
+
if (rg.length < 7 || rg.length > 9)
|
|
27
|
+
return false;
|
|
28
|
+
const isValidFormat = /^[0-9]{7,8}[0-9Xx]?$/.test(rg);
|
|
29
|
+
return isValidFormat;
|
|
30
|
+
};
|
|
31
|
+
export { validateRg };
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkyn/shared",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"main": "./dist/bundle.js",
|
|
5
5
|
"module": "./src/index.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"author": "Arkyn | Lucas Gonçalves",
|
|
10
|
+
"description": "Shared utilities and types for projects.",
|
|
8
11
|
"scripts": {
|
|
9
12
|
"clean": "rm -rf dist",
|
|
10
13
|
"build": "bun run clean && bunx tsc --project tsconfig.json",
|
|
14
|
+
"test": "vitest --config vitest.config.ts",
|
|
11
15
|
"typecheck": "bunx tsc --project tsconfig.json --noEmit"
|
|
12
16
|
},
|
|
13
17
|
"dependencies": {
|
|
@@ -16,6 +20,7 @@
|
|
|
16
20
|
"devDependencies": {
|
|
17
21
|
"@types/uuid": "^10.0.0",
|
|
18
22
|
"bun-types": "latest",
|
|
19
|
-
"
|
|
23
|
+
"vitest": "^3.1.1",
|
|
24
|
+
"typescript": "^5.8.3"
|
|
20
25
|
}
|
|
21
26
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatDate } from "../formatDate";
|
|
3
|
+
|
|
4
|
+
describe("formatDate", () => {
|
|
5
|
+
it("should format a Brazilian date to ISO format", () => {
|
|
6
|
+
const result = formatDate(
|
|
7
|
+
"25/12/2023",
|
|
8
|
+
"00:00:00",
|
|
9
|
+
"brazilianDate",
|
|
10
|
+
"YYYY-MM-DD"
|
|
11
|
+
);
|
|
12
|
+
expect(result).toBe("2023-12-25");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should format an ISO date to Brazilian format", () => {
|
|
16
|
+
const result = formatDate(
|
|
17
|
+
"2023-12-25",
|
|
18
|
+
"00:00:00",
|
|
19
|
+
"isoDate",
|
|
20
|
+
"DD/MM/YYYY"
|
|
21
|
+
);
|
|
22
|
+
expect(result).toBe("25/12/2023");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should format a date with time components", () => {
|
|
26
|
+
const result = formatDate(
|
|
27
|
+
"2023-12-25",
|
|
28
|
+
"14:30:00",
|
|
29
|
+
"isoDate",
|
|
30
|
+
"YYYY-MM-DD hh:mm:ss"
|
|
31
|
+
);
|
|
32
|
+
expect(result).toBe("2023-12-25 14:30:00");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should apply timezone adjustment when formatting", () => {
|
|
36
|
+
const result = formatDate(
|
|
37
|
+
"2023-12-25",
|
|
38
|
+
"14:30:00",
|
|
39
|
+
"isoDate",
|
|
40
|
+
"YYYY-MM-DD hh:mm:ss",
|
|
41
|
+
-3
|
|
42
|
+
);
|
|
43
|
+
expect(result).toBe("2023-12-25 11:30:00");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should handle single-digit days and months in Brazilian format", () => {
|
|
47
|
+
const result = formatDate(
|
|
48
|
+
"5/1/2023",
|
|
49
|
+
"00:00:00",
|
|
50
|
+
"brazilianDate",
|
|
51
|
+
"YYYY-MM-DD"
|
|
52
|
+
);
|
|
53
|
+
expect(result).toBe("2023-01-05");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should handle single-digit days and months in ISO format", () => {
|
|
57
|
+
const result = formatDate("2023-1-5", "00:00:00", "isoDate", "DD/MM/YYYY");
|
|
58
|
+
expect(result).toBe("05/01/2023");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should handle leap years correctly", () => {
|
|
62
|
+
const result = formatDate(
|
|
63
|
+
"29/02/2024",
|
|
64
|
+
"00:00:00",
|
|
65
|
+
"brazilianDate",
|
|
66
|
+
"YYYY-MM-DD"
|
|
67
|
+
);
|
|
68
|
+
expect(result).toBe("2024-02-29");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should throw an error for invalid input format", () => {
|
|
72
|
+
expect(() =>
|
|
73
|
+
formatDate("25/12/2023", "00:00:00", "invalidFormat" as any, "YYYY-MM-DD")
|
|
74
|
+
).toThrow("Invalid input format");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should throw an error for invalid date string", () => {
|
|
78
|
+
expect(() =>
|
|
79
|
+
formatDate("invalid-date", "00:00:00", "brazilianDate", "YYYY-MM-DD")
|
|
80
|
+
).toThrow("Invalid date");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should throw an error for invalid time string", () => {
|
|
84
|
+
expect(() =>
|
|
85
|
+
formatDate("25/12/2023", "invalid-time", "brazilianDate", "YYYY-MM-DD")
|
|
86
|
+
).toThrow("Invalid date");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatJsonObject } from "../formatJsonObject";
|
|
3
|
+
|
|
4
|
+
describe("formatJsonObject", () => {
|
|
5
|
+
it("should format a simple JSON object", () => {
|
|
6
|
+
const obj = { name: "John", age: 30 };
|
|
7
|
+
const result = formatJsonObject(obj, 0);
|
|
8
|
+
expect(result).toBe(`{
|
|
9
|
+
"name": "John",
|
|
10
|
+
"age": 30
|
|
11
|
+
}`);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should format a nested JSON object", () => {
|
|
15
|
+
const obj = { name: "John", details: { age: 30, city: "New York" } };
|
|
16
|
+
const result = formatJsonObject(obj, 0);
|
|
17
|
+
expect(result).toBe(`{
|
|
18
|
+
"name": "John",
|
|
19
|
+
"details": {
|
|
20
|
+
"age": 30,
|
|
21
|
+
"city": "New York"
|
|
22
|
+
}
|
|
23
|
+
}`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should format a JSON array", () => {
|
|
27
|
+
const obj = ["apple", "banana", "cherry"];
|
|
28
|
+
const result = formatJsonObject(obj, 0);
|
|
29
|
+
expect(result).toBe(`[
|
|
30
|
+
"apple",
|
|
31
|
+
"banana",
|
|
32
|
+
"cherry"
|
|
33
|
+
]`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should format a nested JSON array", () => {
|
|
37
|
+
const obj = { fruits: ["apple", "banana", "cherry"] };
|
|
38
|
+
const result = formatJsonObject(obj, 0);
|
|
39
|
+
expect(result).toBe(`{
|
|
40
|
+
"fruits": [
|
|
41
|
+
"apple",
|
|
42
|
+
"banana",
|
|
43
|
+
"cherry"
|
|
44
|
+
]
|
|
45
|
+
}`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should format a primitive value", () => {
|
|
49
|
+
const result = formatJsonObject(42, 0);
|
|
50
|
+
expect(result).toBe("42");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should format a string value", () => {
|
|
54
|
+
const result = formatJsonObject("hello", 0);
|
|
55
|
+
expect(result).toBe('"hello"');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should format a JSON string", () => {
|
|
59
|
+
const jsonString = '{"name":"John","age":30}';
|
|
60
|
+
const result = formatJsonObject(jsonString, 0);
|
|
61
|
+
expect(result).toBe(`{
|
|
62
|
+
"name": "John",
|
|
63
|
+
"age": 30
|
|
64
|
+
}`);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("should handle invalid JSON strings as plain strings", () => {
|
|
68
|
+
const invalidJson = "{name:John}";
|
|
69
|
+
const result = formatJsonObject(invalidJson, 0);
|
|
70
|
+
expect(result).toBe('"{name:John}"');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should handle null values", () => {
|
|
74
|
+
const result = formatJsonObject(null, 0);
|
|
75
|
+
expect(result).toBe("null");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should handle empty objects", () => {
|
|
79
|
+
const result = formatJsonObject({}, 0);
|
|
80
|
+
expect(result).toBe(`{}`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should handle empty arrays", () => {
|
|
84
|
+
const result = formatJsonObject([], 0);
|
|
85
|
+
expect(result).toBe(`[]`);
|
|
86
|
+
});
|
|
87
|
+
});
|