@arkyn/shared 1.4.57 → 1.5.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/formats/formatBrazilianDateHour.d.ts +3 -0
- package/dist/formats/formatBrazilianDateHour.d.ts.map +1 -0
- package/dist/formats/formatBrazilianDateHour.js +12 -0
- package/dist/formats/formatBrazilianDateToDate.d.ts +3 -0
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +1 -0
- package/dist/formats/formatBrazilianDateToDate.js +9 -0
- package/dist/formats/formatDateHour.d.ts +3 -0
- package/dist/formats/formatDateHour.d.ts.map +1 -0
- package/dist/formats/formatDateHour.js +11 -0
- package/dist/formats/formatJsonObject.d.ts +1 -31
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +26 -67
- package/dist/formats/formatJsonString.d.ts +1 -37
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +4 -38
- package/dist/formats/formatToBRL.d.ts +3 -0
- package/dist/formats/formatToBRL.d.ts.map +1 -0
- package/dist/formats/formatToBRL.js +8 -0
- package/dist/formats/formatToCNPJ.d.ts +3 -0
- package/dist/formats/formatToCNPJ.d.ts.map +1 -0
- package/dist/formats/formatToCNPJ.js +9 -0
- package/dist/formats/formatToCPF.d.ts +3 -0
- package/dist/formats/formatToCPF.d.ts.map +1 -0
- package/dist/formats/formatToCPF.js +9 -0
- package/dist/formats/formatToCep.d.ts +1 -27
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +6 -31
- package/dist/formats/formatToCpfCnpj.d.ts +1 -22
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +7 -27
- package/dist/formats/formatToEllipsis.d.ts +1 -14
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +3 -20
- package/dist/formats/formatToHiddenDigits.d.ts +2 -30
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +3 -49
- package/dist/formats/formatToPhone.d.ts +1 -32
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +6 -128
- package/dist/generators/generateColorByString.d.ts +1 -13
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +4 -15
- package/dist/generators/generateId.d.ts +4 -26
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +5 -11
- package/dist/generators/generateSlug.d.ts +1 -15
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +2 -16
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -7
- package/dist/parsers/parseToCharacters.d.ts +17 -0
- package/dist/parsers/parseToCharacters.d.ts.map +1 -0
- package/dist/parsers/parseToCharacters.js +13 -0
- package/dist/services/calculateCardInstallment.d.ts +5 -31
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +11 -52
- package/dist/services/maskSensitiveData.d.ts +1 -24
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +2 -24
- package/dist/services/range.d.ts +4 -0
- package/dist/services/range.d.ts.map +1 -0
- package/dist/services/range.js +9 -0
- package/dist/services/truncateLargeFields.d.ts +1 -30
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +6 -34
- package/dist/validations/regex.d.ts +15 -0
- package/dist/validations/regex.d.ts.map +1 -0
- package/dist/validations/regex.js +25 -0
- package/dist/validations/validateCPF.d.ts +3 -0
- package/dist/validations/validateCPF.d.ts.map +1 -0
- package/dist/validations/validateCPF.js +36 -0
- package/dist/validations/validateDate.d.ts +6 -27
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +6 -32
- package/dist/validations/validatePhone.d.ts +1 -27
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +2 -27
- package/package.json +2 -4
- package/src/formats/formatBrazilianDateHour.ts +17 -0
- package/src/formats/formatBrazilianDateToDate.ts +13 -0
- package/src/formats/formatDateHour.ts +15 -0
- package/src/formats/formatJsonObject.ts +26 -68
- package/src/formats/formatJsonString.ts +4 -40
- package/src/formats/formatToBRL.ts +10 -0
- package/src/formats/formatToCNPJ.ts +10 -0
- package/src/formats/formatToCPF.ts +10 -0
- package/src/formats/formatToCep.ts +7 -35
- package/src/formats/formatToCpfCnpj.ts +7 -31
- package/src/formats/formatToEllipsis.ts +3 -23
- package/src/formats/formatToHiddenDigits.ts +7 -75
- package/src/formats/formatToPhone.ts +6 -156
- package/src/generators/generateColorByString.ts +4 -18
- package/src/generators/generateId.ts +5 -36
- package/src/generators/generateSlug.ts +2 -17
- package/src/index.ts +15 -7
- package/src/parsers/parseToCharacters.ts +34 -0
- package/src/services/calculateCardInstallment.ts +12 -57
- package/src/services/maskSensitiveData.ts +4 -29
- package/src/services/range.ts +15 -0
- package/src/services/truncateLargeFields.ts +6 -40
- package/src/validations/regex.ts +41 -0
- package/src/validations/validateCPF.ts +38 -0
- package/src/validations/validateDate.ts +11 -33
- package/src/validations/validatePhone.ts +2 -29
- package/tsconfig.json +1 -1
- package/dist/formats/formatDate.d.ts +0 -43
- package/dist/formats/formatDate.d.ts.map +0 -1
- package/dist/formats/formatDate.js +0 -77
- package/dist/formats/formatToCnpj.d.ts +0 -30
- package/dist/formats/formatToCnpj.d.ts.map +0 -1
- package/dist/formats/formatToCnpj.js +0 -35
- package/dist/formats/formatToCpf.d.ts +0 -30
- package/dist/formats/formatToCpf.d.ts.map +0 -1
- package/dist/formats/formatToCpf.js +0 -35
- package/dist/formats/formatToCurrency.d.ts +0 -29
- package/dist/formats/formatToCurrency.d.ts.map +0 -1
- package/dist/formats/formatToCurrency.js +0 -41
- package/dist/regex/index.d.ts +0 -6
- package/dist/regex/index.d.ts.map +0 -1
- package/dist/regex/index.js +0 -4
- package/dist/services/removeCurrencySymbols.d.ts +0 -20
- package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
- package/dist/services/removeCurrencySymbols.js +0 -23
- package/dist/services/removeNonNumeric.d.ts +0 -15
- package/dist/services/removeNonNumeric.d.ts.map +0 -1
- package/dist/services/removeNonNumeric.js +0 -16
- package/dist/validations/validateCpf.d.ts +0 -24
- package/dist/validations/validateCpf.d.ts.map +0 -1
- package/dist/validations/validateCpf.js +0 -56
- package/src/formats/formatDate.ts +0 -97
- package/src/formats/formatToCnpj.ts +0 -39
- package/src/formats/formatToCpf.ts +0 -39
- package/src/formats/formatToCurrency.ts +0 -53
- package/src/regex/index.ts +0 -8
- package/src/services/removeCurrencySymbols.ts +0 -25
- package/src/services/removeNonNumeric.ts +0 -18
- package/src/validations/validateCpf.ts +0 -64
- package/vitest.config.ts +0 -5
|
@@ -1,26 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Masks sensitive data in a JSON string by replacing the values of specified keys with "****".
|
|
4
|
-
*
|
|
5
|
-
* @param jsonString - The JSON string to be processed.
|
|
6
|
-
* @param sensitiveKeys - An array of keys whose values should be masked. Defaults to `["password", "confirmPassword", "creditCard"]`.
|
|
7
|
-
* @returns A JSON string with sensitive data masked. If the input is not a valid JSON string, it returns the original string.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const jsonString = JSON.stringify({
|
|
12
|
-
* username: "user123",
|
|
13
|
-
* password: "secret",
|
|
14
|
-
* profile: {
|
|
15
|
-
* creditCard: "1234-5678-9012-3456",
|
|
16
|
-
* },
|
|
17
|
-
* });
|
|
18
|
-
*
|
|
19
|
-
* const result = maskSensitiveData(jsonString, ["password", "creditCard"]);
|
|
20
|
-
* console.log(result);
|
|
21
|
-
* // Output: '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
declare const maskSensitiveData: MaskSensitiveDataFunction;
|
|
1
|
+
declare function maskSensitiveData(jsonString: string, sensitiveKeys?: string[]): string;
|
|
25
2
|
export { maskSensitiveData };
|
|
26
3
|
//# sourceMappingURL=maskSensitiveData.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maskSensitiveData.d.ts","sourceRoot":"","sources":["../../src/services/maskSensitiveData.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"maskSensitiveData.d.ts","sourceRoot":"","sources":["../../src/services/maskSensitiveData.ts"],"names":[],"mappings":"AAAA,iBAAS,iBAAiB,CACxB,UAAU,EAAE,MAAM,EAClB,aAAa,WAAgD,GAC5D,MAAM,CAkCR;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -1,26 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Masks sensitive data in a JSON string by replacing the values of specified keys with "****".
|
|
3
|
-
*
|
|
4
|
-
* @param jsonString - The JSON string to be processed.
|
|
5
|
-
* @param sensitiveKeys - An array of keys whose values should be masked. Defaults to `["password", "confirmPassword", "creditCard"]`.
|
|
6
|
-
* @returns A JSON string with sensitive data masked. If the input is not a valid JSON string, it returns the original string.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const jsonString = JSON.stringify({
|
|
11
|
-
* username: "user123",
|
|
12
|
-
* password: "secret",
|
|
13
|
-
* profile: {
|
|
14
|
-
* creditCard: "1234-5678-9012-3456",
|
|
15
|
-
* },
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* const result = maskSensitiveData(jsonString, ["password", "creditCard"]);
|
|
19
|
-
* console.log(result);
|
|
20
|
-
* // Output: '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
const maskSensitiveData = (jsonString, sensitiveKeys = ["password", "confirmPassword", "creditCard"]) => {
|
|
1
|
+
function maskSensitiveData(jsonString, sensitiveKeys = ["password", "confirmPassword", "creditCard"]) {
|
|
24
2
|
function maskValue(key, value) {
|
|
25
3
|
if (sensitiveKeys.includes(key))
|
|
26
4
|
return "****";
|
|
@@ -56,5 +34,5 @@ const maskSensitiveData = (jsonString, sensitiveKeys = ["password", "confirmPass
|
|
|
56
34
|
catch (error) {
|
|
57
35
|
return jsonString;
|
|
58
36
|
}
|
|
59
|
-
}
|
|
37
|
+
}
|
|
60
38
|
export { maskSensitiveData };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/services/range.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,cAAc,GAClB,OAAO,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,OAAO,MAAM,KACZ,CAAC,MAAM,EAAE,MAAM,CAMjB,CAAC;AAEF,QAAA,MAAM,MAAM,GAAI,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,MAAM,KAAG,OACjB,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const normalizeRange = (range, limit) => {
|
|
2
|
+
if (Array.isArray(range))
|
|
3
|
+
return range;
|
|
4
|
+
if (range >= 0)
|
|
5
|
+
return [0, range];
|
|
6
|
+
return [limit + 1 - Math.abs(range), limit];
|
|
7
|
+
};
|
|
8
|
+
const within = (range, value) => value >= range[0] && value <= range[1];
|
|
9
|
+
export { normalizeRange, within };
|
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Truncates large string fields in a JSON string to a specified maximum length.
|
|
4
|
-
*
|
|
5
|
-
* This function parses a JSON string, traverses its structure recursively, and truncates
|
|
6
|
-
* any string fields that exceed the specified maximum length. If a string field is truncated,
|
|
7
|
-
* it is replaced with a message indicating the original length of the field.
|
|
8
|
-
*
|
|
9
|
-
* @param jsonString - The JSON string to process.
|
|
10
|
-
* @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
|
|
11
|
-
* @returns A JSON string with large string fields truncated.
|
|
12
|
-
*
|
|
13
|
-
* @throws {Error} Throws an error if the input is not a valid JSON string.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const json = JSON.stringify({
|
|
18
|
-
* name: "John",
|
|
19
|
-
* description: "A very long description that exceeds the maximum length...",
|
|
20
|
-
* nested: {
|
|
21
|
-
* details: "Another long string that needs truncation."
|
|
22
|
-
* }
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* const result = truncateLargeFields(json, 50);
|
|
26
|
-
* console.log(result);
|
|
27
|
-
* // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
declare const truncateLargeFields: TruncateLargeFieldsFunction;
|
|
1
|
+
declare function truncateLargeFields(jsonString: string, maxLength?: number): string;
|
|
31
2
|
export { truncateLargeFields };
|
|
32
3
|
//# sourceMappingURL=truncateLargeFields.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncateLargeFields.d.ts","sourceRoot":"","sources":["../../src/services/truncateLargeFields.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"truncateLargeFields.d.ts","sourceRoot":"","sources":["../../src/services/truncateLargeFields.ts"],"names":[],"mappings":"AAAA,iBAAS,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,SAAO,GAAG,MAAM,CA6BzE;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
|
|
@@ -1,33 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* This function parses a JSON string, traverses its structure recursively, and truncates
|
|
5
|
-
* any string fields that exceed the specified maximum length. If a string field is truncated,
|
|
6
|
-
* it is replaced with a message indicating the original length of the field.
|
|
7
|
-
*
|
|
8
|
-
* @param jsonString - The JSON string to process.
|
|
9
|
-
* @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
|
|
10
|
-
* @returns A JSON string with large string fields truncated.
|
|
11
|
-
*
|
|
12
|
-
* @throws {Error} Throws an error if the input is not a valid JSON string.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const json = JSON.stringify({
|
|
17
|
-
* name: "John",
|
|
18
|
-
* description: "A very long description that exceeds the maximum length...",
|
|
19
|
-
* nested: {
|
|
20
|
-
* details: "Another long string that needs truncation."
|
|
21
|
-
* }
|
|
22
|
-
* });
|
|
23
|
-
*
|
|
24
|
-
* const result = truncateLargeFields(json, 50);
|
|
25
|
-
* console.log(result);
|
|
26
|
-
* // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
const truncateLargeFields = (jsonString, maxLength = 1000) => {
|
|
30
|
-
function truncateValue(value) {
|
|
1
|
+
function truncateLargeFields(jsonString, maxLength = 1000) {
|
|
2
|
+
function truncateValue(key, value) {
|
|
31
3
|
if (typeof value === "string" && value.length > maxLength) {
|
|
32
4
|
return `To large information: field as ${value.length} characters`;
|
|
33
5
|
}
|
|
@@ -35,15 +7,15 @@ const truncateLargeFields = (jsonString, maxLength = 1000) => {
|
|
|
35
7
|
}
|
|
36
8
|
function recursiveTruncate(obj) {
|
|
37
9
|
if (Array.isArray(obj)) {
|
|
38
|
-
return obj.map(
|
|
10
|
+
return obj.map(recursiveTruncate);
|
|
39
11
|
}
|
|
40
12
|
else if (obj !== null && typeof obj === "object") {
|
|
41
13
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
42
14
|
key,
|
|
43
|
-
recursiveTruncate(value),
|
|
15
|
+
truncateValue(key, recursiveTruncate(value)),
|
|
44
16
|
]));
|
|
45
17
|
}
|
|
46
|
-
return
|
|
18
|
+
return obj;
|
|
47
19
|
}
|
|
48
20
|
try {
|
|
49
21
|
const parsedJson = JSON.parse(jsonString);
|
|
@@ -53,5 +25,5 @@ const truncateLargeFields = (jsonString, maxLength = 1000) => {
|
|
|
53
25
|
catch (error) {
|
|
54
26
|
throw new Error("Invalid JSON string");
|
|
55
27
|
}
|
|
56
|
-
}
|
|
28
|
+
}
|
|
57
29
|
export { truncateLargeFields };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const regex: {
|
|
2
|
+
URL: RegExp;
|
|
3
|
+
HOUR: RegExp;
|
|
4
|
+
RG: RegExp;
|
|
5
|
+
PHONE: RegExp;
|
|
6
|
+
PASSWORD: RegExp;
|
|
7
|
+
DATEHOUR: RegExp;
|
|
8
|
+
CPF: RegExp;
|
|
9
|
+
CNPJ: RegExp;
|
|
10
|
+
CEP: RegExp;
|
|
11
|
+
CPFCNPJ: RegExp;
|
|
12
|
+
DATE: RegExp;
|
|
13
|
+
};
|
|
14
|
+
export { regex };
|
|
15
|
+
//# sourceMappingURL=regex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex.d.ts","sourceRoot":"","sources":["../../src/validations/regex.ts"],"names":[],"mappings":"AA0BA,QAAA,MAAM,KAAK;;;;;;;;;;;;CAYV,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const CEP = /^(\d{8}|\d{2}\.?\d{3}\-\d{3})$/;
|
|
2
|
+
const CNPJ = /(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/;
|
|
3
|
+
const CPF = /(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)/;
|
|
4
|
+
const DATE = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/;
|
|
5
|
+
const DATEHOUR = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
6
|
+
const HOUR = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
7
|
+
const PASSWORD = /(?=^.{8,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*/;
|
|
8
|
+
const PHONE = /([0-9]{2})?(\s|-)?(9?[0-9]{4})-?([0-9]{4}$)/;
|
|
9
|
+
const RG = /(^\d{1,2}).?(\d{3}).?(\d{3})-?(\d{1}|X|x$)/;
|
|
10
|
+
const URL = /^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$/;
|
|
11
|
+
const CPFCNPJ = /^(?:(\d{3}\.\d{3}\.\d{3}-\d{2})|(\d{11})|(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})|(\d{14}))$/;
|
|
12
|
+
const regex = {
|
|
13
|
+
URL,
|
|
14
|
+
HOUR,
|
|
15
|
+
RG,
|
|
16
|
+
PHONE,
|
|
17
|
+
PASSWORD,
|
|
18
|
+
DATEHOUR,
|
|
19
|
+
CPF,
|
|
20
|
+
CNPJ,
|
|
21
|
+
CEP,
|
|
22
|
+
CPFCNPJ,
|
|
23
|
+
DATE,
|
|
24
|
+
};
|
|
25
|
+
export { regex };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateCPF.d.ts","sourceRoot":"","sources":["../../src/validations/validateCPF.ts"],"names":[],"mappings":"AA2BA,iBAAS,WAAW,CAAC,MAAM,EAAE,MAAM,WAQlC;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function removeNonDigits(cpf) {
|
|
2
|
+
return cpf.replace(/\D/g, "");
|
|
3
|
+
}
|
|
4
|
+
function isInvalidLength(cpf) {
|
|
5
|
+
const CPF_LENGTH = 11;
|
|
6
|
+
return cpf.length !== CPF_LENGTH;
|
|
7
|
+
}
|
|
8
|
+
function hasAllDigitsEqual(cpf) {
|
|
9
|
+
const [firstCpfDigit] = cpf;
|
|
10
|
+
return [...cpf].every((digit) => digit === firstCpfDigit);
|
|
11
|
+
}
|
|
12
|
+
function calculateDigit(cpf, factor) {
|
|
13
|
+
let total = 0;
|
|
14
|
+
for (const digit of cpf) {
|
|
15
|
+
if (factor > 1)
|
|
16
|
+
total += parseInt(digit) * factor--;
|
|
17
|
+
}
|
|
18
|
+
const rest = total % 11;
|
|
19
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
20
|
+
}
|
|
21
|
+
function extractDigit(cpf) {
|
|
22
|
+
return cpf.slice(9);
|
|
23
|
+
}
|
|
24
|
+
function validateCPF(rawCpf) {
|
|
25
|
+
if (!rawCpf)
|
|
26
|
+
return false;
|
|
27
|
+
const cpf = removeNonDigits(rawCpf);
|
|
28
|
+
if (isInvalidLength(cpf))
|
|
29
|
+
return false;
|
|
30
|
+
if (hasAllDigitsEqual(cpf))
|
|
31
|
+
return false;
|
|
32
|
+
const digit1 = calculateDigit(cpf, 10);
|
|
33
|
+
const digit2 = calculateDigit(cpf, 11);
|
|
34
|
+
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
35
|
+
}
|
|
36
|
+
export { validateCPF };
|
|
@@ -1,29 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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;
|
|
1
|
+
type Format = "DD/MM/YYYY" | "MM-DD-YYYY" | "YYYY-MM-DD";
|
|
2
|
+
type Config = {
|
|
3
|
+
minYear?: number;
|
|
4
|
+
maxYear?: number;
|
|
5
|
+
};
|
|
6
|
+
declare function validateDate(date: string, format: Format, config?: Config): boolean;
|
|
28
7
|
export { validateDate };
|
|
29
8
|
//# 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,KAAK,MAAM,GAAG,YAAY,GAAG,YAAY,GAAG,YAAY,CAAC;AAEzD,KAAK,MAAM,GAAG;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,iBAAS,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CA6C5E;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,46 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
* Validates a date string based on the provided format and configuration.
|
|
3
|
-
*
|
|
4
|
-
* @param date - 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 = (date, config) => {
|
|
1
|
+
function validateDate(date, format, config) {
|
|
27
2
|
let day, month, year;
|
|
28
|
-
const
|
|
29
|
-
const minYear = config?.minYear || 1900;
|
|
3
|
+
const minYear = config?.minYear || 0;
|
|
30
4
|
const maxYear = config?.maxYear || 3000;
|
|
31
|
-
if (
|
|
5
|
+
if (format === "DD/MM/YYYY") {
|
|
32
6
|
const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
|
|
33
7
|
if (!dateRegex.test(date))
|
|
34
8
|
return false;
|
|
35
9
|
[, day, month, year] = date.match(dateRegex) || [];
|
|
36
10
|
}
|
|
37
|
-
else if (
|
|
11
|
+
else if (format === "MM-DD-YYYY") {
|
|
38
12
|
const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
39
13
|
if (!dateRegex.test(date))
|
|
40
14
|
return false;
|
|
41
15
|
[, month, day, year] = date.match(dateRegex) || [];
|
|
42
16
|
}
|
|
43
|
-
else if (
|
|
17
|
+
else if (format === "YYYY-MM-DD") {
|
|
44
18
|
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
45
19
|
if (!dateRegex.test(date))
|
|
46
20
|
return false;
|
|
@@ -69,5 +43,5 @@ const validateDate = (date, config) => {
|
|
|
69
43
|
return false;
|
|
70
44
|
const isValidDate = new Date(yearNum, monthNum - 1, dayNum).getDate() === dayNum;
|
|
71
45
|
return isValidDate;
|
|
72
|
-
}
|
|
46
|
+
}
|
|
73
47
|
export { validateDate };
|
|
@@ -1,29 +1,3 @@
|
|
|
1
|
-
|
|
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 phone - 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;
|
|
1
|
+
declare function validatePhone(phone: string): boolean;
|
|
28
2
|
export { validatePhone };
|
|
29
3
|
//# 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":"AAEA,iBAAS,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAiB7C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -1,30 +1,5 @@
|
|
|
1
1
|
import { countries } from "@arkyn/templates";
|
|
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 phone - 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 = (phone) => {
|
|
2
|
+
function validatePhone(phone) {
|
|
28
3
|
for (const country of countries) {
|
|
29
4
|
const countryCode = country.code;
|
|
30
5
|
const prefix = country.prefix ? `-${country.prefix}` : "";
|
|
@@ -40,5 +15,5 @@ const validatePhone = (phone) => {
|
|
|
40
15
|
return true;
|
|
41
16
|
}
|
|
42
17
|
return false;
|
|
43
|
-
}
|
|
18
|
+
}
|
|
44
19
|
export { validatePhone };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkyn/shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"main": "./dist/bundle.js",
|
|
5
5
|
"module": "./src/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -8,12 +8,10 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"clean": "rm -rf dist",
|
|
10
10
|
"build": "bun run clean && bunx tsc --project tsconfig.json",
|
|
11
|
-
"test": "vitest --config ./vitest.config.ts",
|
|
12
11
|
"typecheck": "bunx tsc --project tsconfig.json --noEmit"
|
|
13
12
|
},
|
|
14
13
|
"dependencies": {
|
|
15
|
-
"uuid": "^10.0.0"
|
|
16
|
-
"vitest": "^3.1.1"
|
|
14
|
+
"uuid": "^10.0.0"
|
|
17
15
|
},
|
|
18
16
|
"devDependencies": {
|
|
19
17
|
"@types/uuid": "^10.0.0",
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { regex } from "../validations/regex";
|
|
2
|
+
|
|
3
|
+
function formatBrazilianDateHour(date: string, hour: string) {
|
|
4
|
+
if (regex.HOUR.test(hour) === false) throw new Error("Invalid hour format");
|
|
5
|
+
|
|
6
|
+
const [day, month, year] = date.split("/").map(Number);
|
|
7
|
+
|
|
8
|
+
const dateHour = new Date(`${year}-${month}-${day}`);
|
|
9
|
+
const [hours, minutes] = hour.split(":").map(Number);
|
|
10
|
+
|
|
11
|
+
dateHour.setUTCHours(hours);
|
|
12
|
+
dateHour.setUTCMinutes(minutes);
|
|
13
|
+
|
|
14
|
+
return dateHour;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { formatBrazilianDateHour };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { regex } from "../validations/regex";
|
|
2
|
+
|
|
3
|
+
function formatBrazilianDateToDate(brazilianData: string) {
|
|
4
|
+
if (regex.DATE.test(brazilianData) === false)
|
|
5
|
+
throw new Error("Invalid date format");
|
|
6
|
+
|
|
7
|
+
const [day, month, year] = brazilianData.split("/").map(Number);
|
|
8
|
+
const formattedDate = `${year}-${month}-${day}`;
|
|
9
|
+
|
|
10
|
+
return new Date(formattedDate);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { formatBrazilianDateToDate };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { regex } from "../validations/regex";
|
|
2
|
+
|
|
3
|
+
function formatDateHour(date: string, hour: string = "00:00") {
|
|
4
|
+
if (regex.HOUR.test(hour) === false) throw new Error("Invalid hour format");
|
|
5
|
+
|
|
6
|
+
const dateHour = new Date(date);
|
|
7
|
+
const [hours, minutes] = hour.split(":").map(Number);
|
|
8
|
+
|
|
9
|
+
dateHour.setUTCHours(hours);
|
|
10
|
+
dateHour.setUTCMinutes(minutes);
|
|
11
|
+
|
|
12
|
+
return dateHour;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { formatDateHour };
|
|
@@ -1,77 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Formats a JSON object into a human-readable string with proper indentation.
|
|
5
|
-
*
|
|
6
|
-
* @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
|
|
7
|
-
* @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
|
|
8
|
-
* @returns A formatted string representation of the JSON object.
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
12
|
-
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
13
|
-
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
14
|
-
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
19
|
-
* const formatted = formatJsonObject(obj, 0);
|
|
20
|
-
* console.log(formatted);
|
|
21
|
-
* // Output:
|
|
22
|
-
* // {
|
|
23
|
-
* // "name": "John",
|
|
24
|
-
* // "age": 30,
|
|
25
|
-
* // "hobbies": [
|
|
26
|
-
* // "reading",
|
|
27
|
-
* // "gaming"
|
|
28
|
-
* // ]
|
|
29
|
-
* // }
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const formatJsonObject: FormatJsonObjectFunction = (obj, indentLevel) => {
|
|
1
|
+
function formatJsonObject(obj: any, indentLevel: number): string {
|
|
34
2
|
const indent = " ".repeat(indentLevel);
|
|
35
3
|
let formattedString = "";
|
|
36
4
|
|
|
37
5
|
if (typeof obj === "object" && obj !== null) {
|
|
38
6
|
if (Array.isArray(obj)) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
formattedString +=
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
formattedString += "\n";
|
|
51
|
-
});
|
|
52
|
-
formattedString += indent + "]";
|
|
53
|
-
}
|
|
7
|
+
formattedString += "[\n";
|
|
8
|
+
obj.forEach((item, index) => {
|
|
9
|
+
formattedString +=
|
|
10
|
+
indent + " " + formatJsonObject(item, indentLevel + 1);
|
|
11
|
+
if (index < obj.length - 1) {
|
|
12
|
+
formattedString += ",";
|
|
13
|
+
}
|
|
14
|
+
formattedString += "\n";
|
|
15
|
+
});
|
|
16
|
+
formattedString += indent + "]";
|
|
54
17
|
} else {
|
|
18
|
+
formattedString += "{\n";
|
|
55
19
|
const keys = Object.keys(obj);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
formattedString += ",";
|
|
70
|
-
}
|
|
71
|
-
formattedString += "\n";
|
|
72
|
-
});
|
|
73
|
-
formattedString += indent + "}";
|
|
74
|
-
}
|
|
20
|
+
keys.forEach((key, index) => {
|
|
21
|
+
formattedString +=
|
|
22
|
+
indent +
|
|
23
|
+
' "' +
|
|
24
|
+
key +
|
|
25
|
+
'": ' +
|
|
26
|
+
formatJsonObject(obj[key], indentLevel + 1);
|
|
27
|
+
if (index < keys.length - 1) {
|
|
28
|
+
formattedString += ",";
|
|
29
|
+
}
|
|
30
|
+
formattedString += "\n";
|
|
31
|
+
});
|
|
32
|
+
formattedString += indent + "}";
|
|
75
33
|
}
|
|
76
34
|
} else if (typeof obj === "string") {
|
|
77
35
|
try {
|
|
@@ -85,6 +43,6 @@ const formatJsonObject: FormatJsonObjectFunction = (obj, indentLevel) => {
|
|
|
85
43
|
}
|
|
86
44
|
|
|
87
45
|
return formattedString;
|
|
88
|
-
}
|
|
46
|
+
}
|
|
89
47
|
|
|
90
48
|
export { formatJsonObject };
|