@arkyn/shared 2.0.1-beta.0 → 2.0.1-beta.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/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 +33 -0
- package/dist/formats/formatJsonObject.d.ts.map +1 -0
- package/dist/formats/formatJsonObject.js +90 -0
- package/dist/formats/formatJsonString.d.ts +39 -0
- package/dist/formats/formatJsonString.d.ts.map +1 -0
- package/dist/formats/formatJsonString.js +46 -0
- package/dist/formats/formatToCep.d.ts +29 -0
- package/dist/formats/formatToCep.d.ts.map +1 -0
- package/dist/formats/formatToCep.js +34 -0
- 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 +24 -0
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -0
- package/dist/formats/formatToCpfCnpj.js +33 -0
- 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 +16 -0
- package/dist/formats/formatToEllipsis.d.ts.map +1 -0
- package/dist/formats/formatToEllipsis.js +21 -0
- package/dist/formats/formatToHiddenDigits.d.ts +32 -0
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -0
- package/dist/formats/formatToHiddenDigits.js +60 -0
- package/dist/formats/formatToPhone.d.ts +34 -0
- package/dist/formats/formatToPhone.d.ts.map +1 -0
- package/dist/formats/formatToPhone.js +131 -0
- package/dist/generators/generateColorByString.d.ts +15 -0
- package/dist/generators/generateColorByString.d.ts.map +1 -0
- package/dist/generators/generateColorByString.js +25 -0
- package/dist/generators/generateId.d.ts +28 -0
- package/dist/generators/generateId.d.ts.map +1 -0
- package/dist/generators/generateId.js +29 -0
- package/dist/generators/generateSlug.d.ts +17 -0
- package/dist/generators/generateSlug.d.ts.map +1 -0
- package/dist/generators/generateSlug.js +25 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/regex/index.d.ts +6 -0
- package/dist/regex/index.d.ts.map +1 -0
- package/dist/regex/index.js +4 -0
- package/dist/services/calculateCardInstallment.d.ts +33 -0
- package/dist/services/calculateCardInstallment.d.ts.map +1 -0
- package/dist/services/calculateCardInstallment.js +55 -0
- package/dist/services/maskSensitiveData.d.ts +26 -0
- package/dist/services/maskSensitiveData.d.ts.map +1 -0
- package/dist/services/maskSensitiveData.js +60 -0
- 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 +32 -0
- package/dist/services/truncateLargeFields.d.ts.map +1 -0
- package/dist/services/truncateLargeFields.js +57 -0
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +56 -0
- package/dist/validations/validateDate.d.ts +29 -0
- package/dist/validations/validateDate.d.ts.map +1 -0
- package/dist/validations/validateDate.js +73 -0
- package/dist/validations/validatePhone.d.ts +29 -0
- package/dist/validations/validatePhone.d.ts.map +1 -0
- package/dist/validations/validatePhone.js +44 -0
- package/package.json +1 -1
- package/src/regex/index.ts +2 -35
- package/tsconfig.json +2 -2
- package/src/formats/__test__/formatDate.spec.ts +0 -88
- package/src/formats/__test__/formatJsonObject.spec.ts +0 -87
- package/src/formats/__test__/formatJsonString.spec.ts +0 -83
- package/src/formats/__test__/formatToCep.spec.ts +0 -37
- package/src/formats/__test__/formatToCnpj.spec.ts +0 -35
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +0 -43
- package/src/formats/__test__/formatToCurrency.spec.ts +0 -50
- package/src/formats/__test__/formatToEllipsis.spec.ts +0 -44
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +0 -58
- package/src/formats/__test__/formatToPhone.spec.ts +0 -58
- package/src/generators/__test__/generateColorByString.spec.ts +0 -37
- package/src/generators/__test__/generateId.spec.ts +0 -44
- package/src/generators/__test__/generateSlug.spec.ts +0 -47
- package/src/services/__test__/calculateCardInstallment.spec.ts +0 -87
- package/src/services/__test__/maskSensitiveData.spec.ts +0 -102
- package/src/services/__test__/removeCurrencySymbols.spec.ts +0 -41
- package/src/services/__test__/removeNonNumeric.spec.ts +0 -33
- package/src/services/__test__/truncateLargeFields.spec.ts +0 -90
- package/src/validations/__test__/validateCpf.spec.ts +0 -38
- package/src/validations/__test__/validateDate.spec.ts +0 -81
- package/src/validations/__test__/validatePhone.spec.ts +0 -39
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FormatDateFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a date string from a given input format to a specified output format,
|
|
4
|
+
* with optional timezone adjustment.
|
|
5
|
+
*
|
|
6
|
+
* @param date - The date string to be formatted. It should match the specified `inputFormat`.
|
|
7
|
+
* @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
|
|
8
|
+
* @param inputFormat - The format of the input date string. Supported formats:
|
|
9
|
+
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
10
|
+
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
11
|
+
* - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
|
|
12
|
+
*
|
|
13
|
+
* @param outputFormat - The desired format for the output date string. Supported tokens:
|
|
14
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
15
|
+
* - `YY`: Last two digits of the year (e.g., 23)
|
|
16
|
+
* - `MM`: Month (zero-padded, e.g., 01)
|
|
17
|
+
* - `DD`: Day of the month (zero-padded, e.g., 09)
|
|
18
|
+
* - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
|
|
19
|
+
* - `mm`: Minutes (zero-padded, e.g., 05)
|
|
20
|
+
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
21
|
+
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
22
|
+
*
|
|
23
|
+
* @returns The formatted date string in the specified `outputFormat`.
|
|
24
|
+
*
|
|
25
|
+
* @throws Will throw an error if:
|
|
26
|
+
* - The `inputFormat` is invalid.
|
|
27
|
+
* - The `date` string does not match the expected `inputFormat`.
|
|
28
|
+
* - The `time` string is not in the format `hh:mm:ss`.
|
|
29
|
+
* - The resulting date is invalid.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { formatDate } from "./formatDate";
|
|
34
|
+
*
|
|
35
|
+
* const date = "25/12/2023";
|
|
36
|
+
* const time = "14:30:00";
|
|
37
|
+
* const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
|
|
38
|
+
* console.log(formatted); // Outputs: "2023-12-25 14:30:00"
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare const formatDate: FormatDateFunction;
|
|
42
|
+
export { formatDate };
|
|
43
|
+
//# sourceMappingURL=formatDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAqBvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAiCjB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function formatDateString(date, format) {
|
|
2
|
+
const pad = (num) => num.toString().padStart(2, "0");
|
|
3
|
+
const replacements = {
|
|
4
|
+
YYYY: date.getFullYear().toString(),
|
|
5
|
+
YY: date.getFullYear().toString().slice(-2),
|
|
6
|
+
MM: pad(date.getMonth() + 1),
|
|
7
|
+
DD: pad(date.getDate()),
|
|
8
|
+
hh: pad(date.getHours()),
|
|
9
|
+
mm: pad(date.getMinutes()),
|
|
10
|
+
ss: pad(date.getSeconds()),
|
|
11
|
+
};
|
|
12
|
+
return format.replace(/YYYY|YY|MM|DD|hh|mm|ss/g, (match) => replacements[match]);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Formats a date string from a given input format to a specified output format,
|
|
16
|
+
* with optional timezone adjustment.
|
|
17
|
+
*
|
|
18
|
+
* @param date - The date string to be formatted. It should match the specified `inputFormat`.
|
|
19
|
+
* @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
|
|
20
|
+
* @param inputFormat - The format of the input date string. Supported formats:
|
|
21
|
+
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
22
|
+
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
23
|
+
* - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
|
|
24
|
+
*
|
|
25
|
+
* @param outputFormat - The desired format for the output date string. Supported tokens:
|
|
26
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
27
|
+
* - `YY`: Last two digits of the year (e.g., 23)
|
|
28
|
+
* - `MM`: Month (zero-padded, e.g., 01)
|
|
29
|
+
* - `DD`: Day of the month (zero-padded, e.g., 09)
|
|
30
|
+
* - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
|
|
31
|
+
* - `mm`: Minutes (zero-padded, e.g., 05)
|
|
32
|
+
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
33
|
+
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
34
|
+
*
|
|
35
|
+
* @returns The formatted date string in the specified `outputFormat`.
|
|
36
|
+
*
|
|
37
|
+
* @throws Will throw an error if:
|
|
38
|
+
* - The `inputFormat` is invalid.
|
|
39
|
+
* - The `date` string does not match the expected `inputFormat`.
|
|
40
|
+
* - The `time` string is not in the format `hh:mm:ss`.
|
|
41
|
+
* - The resulting date is invalid.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { formatDate } from "./formatDate";
|
|
46
|
+
*
|
|
47
|
+
* const date = "25/12/2023";
|
|
48
|
+
* const time = "14:30:00";
|
|
49
|
+
* const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
|
|
50
|
+
* console.log(formatted); // Outputs: "2023-12-25 14:30:00"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
const formatDate = (date, time, inputFormat, outputFormat, timezone = 0) => {
|
|
54
|
+
const dateParts = date.split(/[-/]/).map(Number);
|
|
55
|
+
const timeParts = time.split(":").map(Number);
|
|
56
|
+
let day, month, year;
|
|
57
|
+
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
58
|
+
switch (inputFormat) {
|
|
59
|
+
case "brazilianDate":
|
|
60
|
+
[day, month, year] = dateParts;
|
|
61
|
+
break;
|
|
62
|
+
case "isoDate":
|
|
63
|
+
[year, month, day] = dateParts;
|
|
64
|
+
break;
|
|
65
|
+
case "timestamp":
|
|
66
|
+
[year, month, day] = dateParts.map(Number);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
throw new Error("Invalid input format");
|
|
70
|
+
}
|
|
71
|
+
const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
72
|
+
if (isNaN(formattedDate.getTime()))
|
|
73
|
+
throw new Error("Invalid date");
|
|
74
|
+
formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
|
|
75
|
+
return formatDateString(formattedDate, outputFormat);
|
|
76
|
+
};
|
|
77
|
+
export { formatDate };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FormatJsonObjectFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON object into a human-readable string with proper indentation.
|
|
4
|
+
*
|
|
5
|
+
* @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
|
|
6
|
+
* @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
|
|
7
|
+
* @returns A formatted string representation of the JSON object.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
11
|
+
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
12
|
+
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
13
|
+
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
18
|
+
* const formatted = formatJsonObject(obj, 0);
|
|
19
|
+
* console.log(formatted);
|
|
20
|
+
* // Output:
|
|
21
|
+
* // {
|
|
22
|
+
* // "name": "John",
|
|
23
|
+
* // "age": 30,
|
|
24
|
+
* // "hobbies": [
|
|
25
|
+
* // "reading",
|
|
26
|
+
* // "gaming"
|
|
27
|
+
* // ]
|
|
28
|
+
* // }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare const formatJsonObject: FormatJsonObjectFunction;
|
|
32
|
+
export { formatJsonObject };
|
|
33
|
+
//# sourceMappingURL=formatJsonObject.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatJsonObject.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonObject.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAuDvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a JSON object into a human-readable string with proper indentation.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
|
|
5
|
+
* @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
|
|
6
|
+
* @returns A formatted string representation of the JSON object.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
10
|
+
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
11
|
+
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
12
|
+
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
17
|
+
* const formatted = formatJsonObject(obj, 0);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
const formatJsonObject = (obj, indentLevel) => {
|
|
31
|
+
const indent = " ".repeat(indentLevel);
|
|
32
|
+
let formattedString = "";
|
|
33
|
+
if (typeof obj === "object" && obj !== null) {
|
|
34
|
+
if (Array.isArray(obj)) {
|
|
35
|
+
if (obj.length === 0) {
|
|
36
|
+
// Caso especial para arrays vazios
|
|
37
|
+
formattedString += "[]";
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
formattedString += "[\n";
|
|
41
|
+
obj.forEach((item, index) => {
|
|
42
|
+
formattedString +=
|
|
43
|
+
indent + " " + formatJsonObject(item, indentLevel + 1);
|
|
44
|
+
if (index < obj.length - 1) {
|
|
45
|
+
formattedString += ",";
|
|
46
|
+
}
|
|
47
|
+
formattedString += "\n";
|
|
48
|
+
});
|
|
49
|
+
formattedString += indent + "]";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const keys = Object.keys(obj);
|
|
54
|
+
if (keys.length === 0) {
|
|
55
|
+
// Caso especial para objetos vazios
|
|
56
|
+
formattedString += "{}";
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
formattedString += "{\n";
|
|
60
|
+
keys.forEach((key, index) => {
|
|
61
|
+
formattedString +=
|
|
62
|
+
indent +
|
|
63
|
+
' "' +
|
|
64
|
+
key +
|
|
65
|
+
'": ' +
|
|
66
|
+
formatJsonObject(obj[key], indentLevel + 1);
|
|
67
|
+
if (index < keys.length - 1) {
|
|
68
|
+
formattedString += ",";
|
|
69
|
+
}
|
|
70
|
+
formattedString += "\n";
|
|
71
|
+
});
|
|
72
|
+
formattedString += indent + "}";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (typeof obj === "string") {
|
|
77
|
+
try {
|
|
78
|
+
const parsedObj = JSON.parse(obj);
|
|
79
|
+
formattedString += formatJsonObject(parsedObj, indentLevel);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
formattedString += '"' + obj + '"';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
formattedString += obj;
|
|
87
|
+
}
|
|
88
|
+
return formattedString;
|
|
89
|
+
};
|
|
90
|
+
export { formatJsonObject };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { FormatJsonStringFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON string into a more readable format.
|
|
4
|
+
*
|
|
5
|
+
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
6
|
+
* and then formats it using the `formatJsonObject` function. If the input string
|
|
7
|
+
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
8
|
+
*
|
|
9
|
+
* @param jsonString - The JSON string to be formatted.
|
|
10
|
+
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
11
|
+
*
|
|
12
|
+
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
17
|
+
* const formatted = formatJsonString(jsonString);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
28
|
+
|
|
29
|
+
* const invalidJsonString = '{"name":"John", "age":30,';
|
|
30
|
+
* const formatted = formatJsonString(invalidJsonString);
|
|
31
|
+
* console.log(formatted);
|
|
32
|
+
* // Output:
|
|
33
|
+
* // (Logs "Invalid JSON string: ..." to the console)
|
|
34
|
+
* // ""
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare const formatJsonString: FormatJsonStringFunction;
|
|
38
|
+
export { formatJsonString };
|
|
39
|
+
//# sourceMappingURL=formatJsonString.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatJsonString.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonString.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAG7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { formatJsonObject } from "./formatJsonObject";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON string into a more readable format.
|
|
4
|
+
*
|
|
5
|
+
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
6
|
+
* and then formats it using the `formatJsonObject` function. If the input string
|
|
7
|
+
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
8
|
+
*
|
|
9
|
+
* @param jsonString - The JSON string to be formatted.
|
|
10
|
+
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
11
|
+
*
|
|
12
|
+
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
17
|
+
* const formatted = formatJsonString(jsonString);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
28
|
+
|
|
29
|
+
* const invalidJsonString = '{"name":"John", "age":30,';
|
|
30
|
+
* const formatted = formatJsonString(invalidJsonString);
|
|
31
|
+
* console.log(formatted);
|
|
32
|
+
* // Output:
|
|
33
|
+
* // (Logs "Invalid JSON string: ..." to the console)
|
|
34
|
+
* // ""
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const formatJsonString = (jsonString) => {
|
|
38
|
+
try {
|
|
39
|
+
const jsonObject = JSON.parse(jsonString);
|
|
40
|
+
return formatJsonObject(jsonObject, 0);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new Error(`Invalid JSON string \n ${error}`);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
export { formatJsonString };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FormatToCepFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
4
|
+
*
|
|
5
|
+
* The function removes all non-numeric characters from the input string
|
|
6
|
+
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
7
|
+
* If the input does not match the expected format, an error is thrown.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CEP.
|
|
10
|
+
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
11
|
+
* @throws {Error} If the input does not match the expected CEP format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCep } from "./formatToCep";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCep = formatToCep("12345678");
|
|
18
|
+
* console.log(formattedCep); // Output: "12345-678"
|
|
19
|
+
*
|
|
20
|
+
* try {
|
|
21
|
+
* formatToCep("1234");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CEP format"
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const formatToCep: FormatToCepFunction;
|
|
28
|
+
export { formatToCep };
|
|
29
|
+
//# sourceMappingURL=formatToCep.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCep.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAMlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
4
|
+
*
|
|
5
|
+
* The function removes all non-numeric characters from the input string
|
|
6
|
+
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
7
|
+
* If the input does not match the expected format, an error is thrown.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CEP.
|
|
10
|
+
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
11
|
+
* @throws {Error} If the input does not match the expected CEP format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCep } from "./formatToCep";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCep = formatToCep("12345678");
|
|
18
|
+
* console.log(formattedCep); // Output: "12345-678"
|
|
19
|
+
*
|
|
20
|
+
* try {
|
|
21
|
+
* formatToCep("1234");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CEP format"
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const formatToCep = (value) => {
|
|
28
|
+
const cleaned = removeNonNumeric(value);
|
|
29
|
+
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
30
|
+
if (match)
|
|
31
|
+
return `${match[1]}-${match[2]}`;
|
|
32
|
+
throw new Error("Invalid CEP format");
|
|
33
|
+
};
|
|
34
|
+
export { formatToCep };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FormatToCnpjFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
4
|
+
*
|
|
5
|
+
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
8
|
+
* Non-numeric characters will be removed before formatting.
|
|
9
|
+
*
|
|
10
|
+
* @returns A string formatted as a CNPJ.
|
|
11
|
+
*
|
|
12
|
+
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { formatToCnpj } from "./formatToCNPJ";
|
|
17
|
+
*
|
|
18
|
+
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
19
|
+
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* formatToCnpj("12345");
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const formatToCnpj: FormatToCnpjFunction;
|
|
29
|
+
export { formatToCnpj };
|
|
30
|
+
//# sourceMappingURL=formatToCnpj.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCnpj.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCnpj.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBAMnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
4
|
+
*
|
|
5
|
+
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
8
|
+
* Non-numeric characters will be removed before formatting.
|
|
9
|
+
*
|
|
10
|
+
* @returns A string formatted as a CNPJ.
|
|
11
|
+
*
|
|
12
|
+
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { formatToCnpj } from "./formatToCNPJ";
|
|
17
|
+
*
|
|
18
|
+
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
19
|
+
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* formatToCnpj("12345");
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const formatToCnpj = (value) => {
|
|
29
|
+
const cleaned = removeNonNumeric(value);
|
|
30
|
+
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
31
|
+
if (match)
|
|
32
|
+
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
33
|
+
throw new Error("Invalid CNPJ length");
|
|
34
|
+
};
|
|
35
|
+
export { formatToCnpj };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FormatToCpfFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
4
|
+
*
|
|
5
|
+
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
6
|
+
* This function ensures the input is cleaned of non-numeric characters and
|
|
7
|
+
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CPF.
|
|
10
|
+
* @returns The formatted CPF string.
|
|
11
|
+
* @throws {Error} If the input string does not match the expected CPF format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCpf } from "./formatToCPF";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCpf = formatToCpf("12345678909");
|
|
18
|
+
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
19
|
+
|
|
20
|
+
* try {
|
|
21
|
+
* const formattedCpf = formatToCpf("12345");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CPF format"
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const formatToCpf: FormatToCpfFunction;
|
|
29
|
+
export { formatToCpf };
|
|
30
|
+
//# sourceMappingURL=formatToCpf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCpf.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCpf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAMlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
4
|
+
*
|
|
5
|
+
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
6
|
+
* This function ensures the input is cleaned of non-numeric characters and
|
|
7
|
+
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CPF.
|
|
10
|
+
* @returns The formatted CPF string.
|
|
11
|
+
* @throws {Error} If the input string does not match the expected CPF format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCpf } from "./formatToCPF";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCpf = formatToCpf("12345678909");
|
|
18
|
+
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
19
|
+
|
|
20
|
+
* try {
|
|
21
|
+
* const formattedCpf = formatToCpf("12345");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CPF format"
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const formatToCpf = (value) => {
|
|
29
|
+
const cleaned = removeNonNumeric(value);
|
|
30
|
+
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
31
|
+
if (match)
|
|
32
|
+
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
33
|
+
throw new Error("Invalid CPF format");
|
|
34
|
+
};
|
|
35
|
+
export { formatToCpf };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FormatToCpfCnpjFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string value into either a CPF or CNPJ format based on its length.
|
|
4
|
+
*
|
|
5
|
+
* - If the input contains 11 numeric characters, it is formatted as a CPF.
|
|
6
|
+
* - If the input contains 14 numeric characters, it is formatted as a CNPJ.
|
|
7
|
+
* - Throws an error if the input length is neither 11 nor 14 after removing non-numeric characters.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The string value to be formatted. It may contain non-numeric characters, which will be removed.
|
|
10
|
+
* @returns The formatted CPF or CNPJ string.
|
|
11
|
+
* @throws {Error} If the input does not have a valid CPF or CNPJ length.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* formatToCpfCnpj("123.456.789-09"); // Returns "123.456.789-09" (CPF format)
|
|
16
|
+
* formatToCpfCnpj("12.345.678/0001-95"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
17
|
+
* formatToCpfCnpj("12345678909"); // Returns "123.456.789-09" (CPF format)
|
|
18
|
+
* formatToCpfCnpj("12345678000195"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
19
|
+
* formatToCpfCnpj("123"); // Throws an error: "Invalid CPF or CNPJ length"
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare const formatToCpfCnpj: FormatToCpfCnpjFunction;
|
|
23
|
+
export { formatToCpfCnpj };
|
|
24
|
+
//# sourceMappingURL=formatToCpfCnpj.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCpfCnpj.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCpfCnpj.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAK5D;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,QAAA,MAAM,eAAe,EAAE,uBAStB,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { formatToCnpj } from "./formatToCnpj";
|
|
2
|
+
import { formatToCpf } from "./formatToCpf";
|
|
3
|
+
/**
|
|
4
|
+
* Formats a given string value into either a CPF or CNPJ format based on its length.
|
|
5
|
+
*
|
|
6
|
+
* - If the input contains 11 numeric characters, it is formatted as a CPF.
|
|
7
|
+
* - If the input contains 14 numeric characters, it is formatted as a CNPJ.
|
|
8
|
+
* - Throws an error if the input length is neither 11 nor 14 after removing non-numeric characters.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The string value to be formatted. It may contain non-numeric characters, which will be removed.
|
|
11
|
+
* @returns The formatted CPF or CNPJ string.
|
|
12
|
+
* @throws {Error} If the input does not have a valid CPF or CNPJ length.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* formatToCpfCnpj("123.456.789-09"); // Returns "123.456.789-09" (CPF format)
|
|
17
|
+
* formatToCpfCnpj("12.345.678/0001-95"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
18
|
+
* formatToCpfCnpj("12345678909"); // Returns "123.456.789-09" (CPF format)
|
|
19
|
+
* formatToCpfCnpj("12345678000195"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
20
|
+
* formatToCpfCnpj("123"); // Throws an error: "Invalid CPF or CNPJ length"
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
const formatToCpfCnpj = (value) => {
|
|
24
|
+
const cleaned = value.replace(/\D/g, "");
|
|
25
|
+
if (cleaned.length === 11) {
|
|
26
|
+
return formatToCpf(cleaned);
|
|
27
|
+
}
|
|
28
|
+
else if (cleaned.length === 14) {
|
|
29
|
+
return formatToCnpj(cleaned);
|
|
30
|
+
}
|
|
31
|
+
throw new Error("Invalid CPF or CNPJ length");
|
|
32
|
+
};
|
|
33
|
+
export { formatToCpfCnpj };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FormatToCurrency } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The numeric value to be formatted.
|
|
6
|
+
* @param currency - The currency code used to determine the formatting style.
|
|
7
|
+
* @param config - Optional configuration object.
|
|
8
|
+
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
9
|
+
*
|
|
10
|
+
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
15
|
+
* console.log(formatted); // "$1,234.56"
|
|
16
|
+
*
|
|
17
|
+
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
18
|
+
* console.log(withoutPrefix); // "1,234.56"
|
|
19
|
+
*
|
|
20
|
+
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
21
|
+
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
22
|
+
*
|
|
23
|
+
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
24
|
+
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const formatToCurrency: FormatToCurrency;
|
|
28
|
+
export { formatToCurrency };
|
|
29
|
+
//# sourceMappingURL=formatToCurrency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCurrency.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCurrency.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIrD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,gBAmBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|