@arkyn/shared 3.0.1-beta.55 → 3.0.1-beta.56
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/package.json +2 -2
- package/src/formats/formatDate.ts +0 -92
- package/src/formats/formatJsonObject.ts +0 -90
- package/src/formats/formatJsonString.ts +0 -50
- package/src/formats/formatToCapitalizeFirstWordLetter.ts +0 -46
- package/src/formats/formatToCep.ts +0 -39
- package/src/formats/formatToCnpj.ts +0 -40
- package/src/formats/formatToCpf.ts +0 -40
- package/src/formats/formatToCpfCnpj.ts +0 -38
- package/src/formats/formatToCurrency.ts +0 -63
- package/src/formats/formatToDate.ts +0 -70
- package/src/formats/formatToEllipsis.ts +0 -25
- package/src/formats/formatToHiddenDigits.ts +0 -92
- package/src/formats/formatToPhone.ts +0 -170
- package/src/generators/generateColorByString.ts +0 -33
- package/src/generators/generateId.ts +0 -61
- package/src/generators/generateSlug.ts +0 -31
- package/src/index.ts +0 -38
- package/src/services/calculateCardInstallment.ts +0 -73
- package/src/services/ensureQuotes.ts +0 -25
- package/src/services/isHtml.ts +0 -26
- package/src/services/maskSensitiveData.ts +0 -68
- package/src/services/removeCurrencySymbols.ts +0 -29
- package/src/services/removeNonNumeric.ts +0 -20
- package/src/services/stripHtmlTags.ts +0 -20
- package/src/services/truncateLargeFields.ts +0 -69
- package/src/validations/validateCep.ts +0 -41
- package/src/validations/validateCnpj.ts +0 -65
- package/src/validations/validateCpf.ts +0 -62
- package/src/validations/validateDate.ts +0 -86
- package/src/validations/validatePassword.ts +0 -41
- package/src/validations/validatePhone.ts +0 -50
- package/src/validations/validateRg.ts +0 -37
package/package.json
CHANGED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
type InputFormatTypes = "brazilianDate" | "isoDate" | "timestamp";
|
|
2
|
-
|
|
3
|
-
type FormatDateFunction = (
|
|
4
|
-
date: string[], // [date: string, time?: string]
|
|
5
|
-
inputFormat: InputFormatTypes,
|
|
6
|
-
outputFormat: string,
|
|
7
|
-
timezone?: number
|
|
8
|
-
) => string;
|
|
9
|
-
|
|
10
|
-
function formatDateString(date: Date, format: string): string {
|
|
11
|
-
const pad = (num: number) => num.toString().padStart(2, "0");
|
|
12
|
-
|
|
13
|
-
const replacements: Record<string, string> = {
|
|
14
|
-
YYYY: date.getFullYear().toString(),
|
|
15
|
-
YY: date.getFullYear().toString().slice(-2),
|
|
16
|
-
MM: pad(date.getMonth() + 1),
|
|
17
|
-
DD: pad(date.getDate()),
|
|
18
|
-
hh: pad(date.getHours()),
|
|
19
|
-
mm: pad(date.getMinutes()),
|
|
20
|
-
ss: pad(date.getSeconds()),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
return format.replace(
|
|
24
|
-
/YYYY|YY|MM|DD|hh|mm|ss/g,
|
|
25
|
-
(match) => replacements[match]
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Formats a date and time string based on the provided input and output formats.
|
|
31
|
-
*
|
|
32
|
-
* @param {[string, string]} dateTime - An array containing the date and optional time.
|
|
33
|
-
* - The first element is the date string.
|
|
34
|
-
* - The second element is the time string (default is "00:00:00").
|
|
35
|
-
* @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
|
|
36
|
-
* - "brazilianDate": Expects the date in "DD/MM/YYYY" format.
|
|
37
|
-
* - "isoDate": Expects the date in "YYYY-MM-DD" format.
|
|
38
|
-
* - "timestamp": Expects the date in "YYYY/MM/DD" format.
|
|
39
|
-
* @param {string} outputFormat - The desired output format for the date.
|
|
40
|
-
* - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
|
|
41
|
-
* @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
|
|
42
|
-
* - Defaults to 0 (UTC).
|
|
43
|
-
* @returns {string} The formatted date string based on the output format.
|
|
44
|
-
* @throws {Error} If the input format is invalid.
|
|
45
|
-
* @throws {Error} If the date is invalid.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* // Format a Brazilian date to ISO format
|
|
49
|
-
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
|
|
50
|
-
* // Returns: "2023-12-25 15:30:00"
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* // Format an ISO date to a custom format with timezone adjustment
|
|
54
|
-
* formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
|
|
55
|
-
* // Returns: "25/12/2023 12:30:00"
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
const formatDate: FormatDateFunction = (
|
|
59
|
-
[date, time = "00:00:00"],
|
|
60
|
-
inputFormat,
|
|
61
|
-
outputFormat,
|
|
62
|
-
timezone = 0
|
|
63
|
-
) => {
|
|
64
|
-
const dateParts = date.split(/[-/]/).map(Number);
|
|
65
|
-
const timeParts = time.split(".")[0].split(":").map(Number);
|
|
66
|
-
|
|
67
|
-
let day, month, year;
|
|
68
|
-
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
69
|
-
|
|
70
|
-
switch (inputFormat) {
|
|
71
|
-
case "brazilianDate":
|
|
72
|
-
[day, month, year] = dateParts;
|
|
73
|
-
break;
|
|
74
|
-
case "isoDate":
|
|
75
|
-
[year, month, day] = dateParts;
|
|
76
|
-
break;
|
|
77
|
-
case "timestamp":
|
|
78
|
-
[year, month, day] = dateParts.map(Number);
|
|
79
|
-
break;
|
|
80
|
-
default:
|
|
81
|
-
throw new Error("Invalid input format");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
85
|
-
if (isNaN(formattedDate.getTime())) throw new Error("Invalid date");
|
|
86
|
-
|
|
87
|
-
formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
|
|
88
|
-
|
|
89
|
-
return formatDateString(formattedDate, outputFormat);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export { formatDate };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
type FormatJsonObjectFunction = (jsonString: any, identLevel: number) => string;
|
|
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) => {
|
|
34
|
-
const indent = " ".repeat(indentLevel);
|
|
35
|
-
let formattedString = "";
|
|
36
|
-
|
|
37
|
-
if (typeof obj === "object" && obj !== null) {
|
|
38
|
-
if (Array.isArray(obj)) {
|
|
39
|
-
if (obj.length === 0) {
|
|
40
|
-
// Caso especial para arrays vazios
|
|
41
|
-
formattedString += "[]";
|
|
42
|
-
} else {
|
|
43
|
-
formattedString += "[\n";
|
|
44
|
-
obj.forEach((item, index) => {
|
|
45
|
-
formattedString +=
|
|
46
|
-
indent + " " + formatJsonObject(item, indentLevel + 1);
|
|
47
|
-
if (index < obj.length - 1) {
|
|
48
|
-
formattedString += ",";
|
|
49
|
-
}
|
|
50
|
-
formattedString += "\n";
|
|
51
|
-
});
|
|
52
|
-
formattedString += indent + "]";
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
const keys = Object.keys(obj);
|
|
56
|
-
if (keys.length === 0) {
|
|
57
|
-
// Caso especial para objetos vazios
|
|
58
|
-
formattedString += "{}";
|
|
59
|
-
} else {
|
|
60
|
-
formattedString += "{\n";
|
|
61
|
-
keys.forEach((key, index) => {
|
|
62
|
-
formattedString +=
|
|
63
|
-
indent +
|
|
64
|
-
' "' +
|
|
65
|
-
key +
|
|
66
|
-
'": ' +
|
|
67
|
-
formatJsonObject(obj[key], indentLevel + 1);
|
|
68
|
-
if (index < keys.length - 1) {
|
|
69
|
-
formattedString += ",";
|
|
70
|
-
}
|
|
71
|
-
formattedString += "\n";
|
|
72
|
-
});
|
|
73
|
-
formattedString += indent + "}";
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
} else if (typeof obj === "string") {
|
|
77
|
-
try {
|
|
78
|
-
const parsedObj = JSON.parse(obj);
|
|
79
|
-
formattedString += formatJsonObject(parsedObj, indentLevel);
|
|
80
|
-
} catch {
|
|
81
|
-
formattedString += '"' + obj + '"';
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
formattedString += obj;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return formattedString;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export { formatJsonObject };
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { formatJsonObject } from "./formatJsonObject";
|
|
2
|
-
|
|
3
|
-
type FormatJsonStringFunction = (jsonString: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a JSON string into a more readable format.
|
|
7
|
-
*
|
|
8
|
-
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
9
|
-
* and then formats it using the `formatJsonObject` function. If the input string
|
|
10
|
-
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
11
|
-
*
|
|
12
|
-
* @param jsonString - The JSON string to be formatted.
|
|
13
|
-
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
14
|
-
*
|
|
15
|
-
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
20
|
-
* const formatted = formatJsonString(jsonString);
|
|
21
|
-
* console.log(formatted);
|
|
22
|
-
* // Output:
|
|
23
|
-
* // {
|
|
24
|
-
* // "name": "John",
|
|
25
|
-
* // "age": 30,
|
|
26
|
-
* // "hobbies": [
|
|
27
|
-
* // "reading",
|
|
28
|
-
* // "gaming"
|
|
29
|
-
* // ]
|
|
30
|
-
* // }
|
|
31
|
-
|
|
32
|
-
* const invalidJsonString = '{"name":"John", "age":30,';
|
|
33
|
-
* const formatted = formatJsonString(invalidJsonString);
|
|
34
|
-
* console.log(formatted);
|
|
35
|
-
* // Output:
|
|
36
|
-
* // (Logs "Invalid JSON string: ..." to the console)
|
|
37
|
-
* // ""
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
const formatJsonString: FormatJsonStringFunction = (jsonString) => {
|
|
42
|
-
try {
|
|
43
|
-
const jsonObject = JSON.parse(jsonString);
|
|
44
|
-
return formatJsonObject(jsonObject, 0);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
throw new Error(`Invalid JSON string \n ${error}`);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export { formatJsonString };
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Formats a sentence by capitalizing the first letter of each word.
|
|
3
|
-
*
|
|
4
|
-
* This function takes a string and capitalizes the first letter of each word
|
|
5
|
-
* while the remaining letters are lowercase.
|
|
6
|
-
* Words are separated by spaces.
|
|
7
|
-
*
|
|
8
|
-
* @param sentence - The sentence to be formatted.
|
|
9
|
-
* @returns The sentence formatted with the first letter of each word capitalized.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // Basic example
|
|
14
|
-
* formatToCapitalizeFirstWordLetter("hello world");
|
|
15
|
-
* // Returns: "Hello World"
|
|
16
|
-
*
|
|
17
|
-
* // With capitalized text.
|
|
18
|
-
* formatToCapitalizeFirstWordLetter("HELLO WORLD");
|
|
19
|
-
* // Returns: "Hello World"
|
|
20
|
-
*
|
|
21
|
-
* // With mixed text.
|
|
22
|
-
* formatToCapitalizeFirstWordLetter("hELLO WoRLd"); * // Returns: "Hello World"
|
|
23
|
-
*
|
|
24
|
-
* // With multiple words
|
|
25
|
-
* formatToCapitalizeFirstWordLetter("javascript is an amazing language");
|
|
26
|
-
* // Returns: "Javascript is an amazing language"
|
|
27
|
-
*
|
|
28
|
-
* // Empty string
|
|
29
|
-
* formatToCapitalizeFirstWordLetter("");
|
|
30
|
-
* // Returns: ""
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
function formatToCapitalizeFirstWordLetter(sentence: string) {
|
|
35
|
-
const words = sentence.split(" ");
|
|
36
|
-
|
|
37
|
-
const capitalizedWords = words.map((word) => {
|
|
38
|
-
const firstLetter = word.charAt(0).toUpperCase();
|
|
39
|
-
const restOfWord = word.slice(1).toLowerCase();
|
|
40
|
-
return firstLetter + restOfWord;
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return capitalizedWords.join(" ");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { formatToCapitalizeFirstWordLetter };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCepFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
7
|
-
*
|
|
8
|
-
* The function removes all non-numeric characters from the input string
|
|
9
|
-
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
10
|
-
* If the input does not match the expected format, an error is thrown.
|
|
11
|
-
*
|
|
12
|
-
* @param value - The input string to be formatted as a CEP.
|
|
13
|
-
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
14
|
-
* @throws {Error} If the input does not match the expected CEP format.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { formatToCep } from "./formatToCep";
|
|
19
|
-
*
|
|
20
|
-
* const formattedCep = formatToCep("12345678");
|
|
21
|
-
* console.log(formattedCep); // Output: "12345-678"
|
|
22
|
-
*
|
|
23
|
-
* try {
|
|
24
|
-
* formatToCep("1234");
|
|
25
|
-
* } catch (error) {
|
|
26
|
-
* console.error(error.message); // Output: "Invalid CEP format"
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const formatToCep: FormatToCepFunction = (value) => {
|
|
32
|
-
const cleaned = removeNonNumeric(value);
|
|
33
|
-
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
34
|
-
|
|
35
|
-
if (match) return `${match[1]}-${match[2]}`;
|
|
36
|
-
throw new Error("Invalid CEP format");
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export { formatToCep };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCnpjFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
7
|
-
*
|
|
8
|
-
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
9
|
-
*
|
|
10
|
-
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
11
|
-
* Non-numeric characters will be removed before formatting.
|
|
12
|
-
*
|
|
13
|
-
* @returns A string formatted as a CNPJ.
|
|
14
|
-
*
|
|
15
|
-
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import { formatToCnpj } from "./formatToCNPJ";
|
|
20
|
-
*
|
|
21
|
-
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
22
|
-
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
23
|
-
*
|
|
24
|
-
* try {
|
|
25
|
-
* formatToCnpj("12345");
|
|
26
|
-
* } catch (error) {
|
|
27
|
-
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
28
|
-
* }
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const formatToCnpj: FormatToCnpjFunction = (value) => {
|
|
33
|
-
const cleaned = removeNonNumeric(value);
|
|
34
|
-
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
35
|
-
if (match)
|
|
36
|
-
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
37
|
-
throw new Error("Invalid CNPJ length");
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export { formatToCnpj };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCpfFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
7
|
-
*
|
|
8
|
-
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
9
|
-
* This function ensures the input is cleaned of non-numeric characters and
|
|
10
|
-
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
11
|
-
*
|
|
12
|
-
* @param value - The input string to be formatted as a CPF.
|
|
13
|
-
* @returns The formatted CPF string.
|
|
14
|
-
* @throws {Error} If the input string does not match the expected CPF format.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { formatToCpf } from "./formatToCPF";
|
|
19
|
-
*
|
|
20
|
-
* const formattedCpf = formatToCpf("12345678909");
|
|
21
|
-
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
22
|
-
|
|
23
|
-
* try {
|
|
24
|
-
* const formattedCpf = formatToCpf("12345");
|
|
25
|
-
* } catch (error) {
|
|
26
|
-
* console.error(error.message); // Output: "Invalid CPF format"
|
|
27
|
-
* }
|
|
28
|
-
*
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const formatToCpf: FormatToCpfFunction = (value) => {
|
|
33
|
-
const cleaned = removeNonNumeric(value);
|
|
34
|
-
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
35
|
-
|
|
36
|
-
if (match) return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
37
|
-
throw new Error("Invalid CPF format");
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export { formatToCpf };
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { formatToCnpj } from "./formatToCnpj";
|
|
2
|
-
import { formatToCpf } from "./formatToCpf";
|
|
3
|
-
|
|
4
|
-
type FormatToCpfCnpjFunction = (value: string) => string;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Formats a given string value into either a CPF or CNPJ format based on its length.
|
|
8
|
-
*
|
|
9
|
-
* - If the input contains 11 numeric characters, it is formatted as a CPF.
|
|
10
|
-
* - If the input contains 14 numeric characters, it is formatted as a CNPJ.
|
|
11
|
-
* - Throws an error if the input length is neither 11 nor 14 after removing non-numeric characters.
|
|
12
|
-
*
|
|
13
|
-
* @param value - The string value to be formatted. It may contain non-numeric characters, which will be removed.
|
|
14
|
-
* @returns The formatted CPF or CNPJ string.
|
|
15
|
-
* @throws {Error} If the input does not have a valid CPF or CNPJ length.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* formatToCpfCnpj("123.456.789-09"); // Returns "123.456.789-09" (CPF format)
|
|
20
|
-
* formatToCpfCnpj("12.345.678/0001-95"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
21
|
-
* formatToCpfCnpj("12345678909"); // Returns "123.456.789-09" (CPF format)
|
|
22
|
-
* formatToCpfCnpj("12345678000195"); // Returns "12.345.678/0001-95" (CNPJ format)
|
|
23
|
-
* formatToCpfCnpj("123"); // Throws an error: "Invalid CPF or CNPJ length"
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
const formatToCpfCnpj: FormatToCpfCnpjFunction = (value) => {
|
|
28
|
-
const cleaned = value.replace(/\D/g, "");
|
|
29
|
-
if (cleaned.length === 11) {
|
|
30
|
-
return formatToCpf(cleaned);
|
|
31
|
-
} else if (cleaned.length === 14) {
|
|
32
|
-
return formatToCnpj(cleaned);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
throw new Error("Invalid CPF or CNPJ length");
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export { formatToCpfCnpj };
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { countryCurrencies } from "@arkyn/templates";
|
|
2
|
-
import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
|
|
3
|
-
|
|
4
|
-
type Currencies = keyof typeof countryCurrencies;
|
|
5
|
-
|
|
6
|
-
type Config = {
|
|
7
|
-
showPrefix?: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
type FormatToCurrency = (
|
|
11
|
-
value: number,
|
|
12
|
-
currency: Currencies,
|
|
13
|
-
config?: Config
|
|
14
|
-
) => string;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
18
|
-
*
|
|
19
|
-
* @param value - The numeric value to be formatted.
|
|
20
|
-
* @param currency - The currency code used to determine the formatting style.
|
|
21
|
-
* @param config - Optional configuration object.
|
|
22
|
-
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
23
|
-
*
|
|
24
|
-
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
29
|
-
* console.log(formatted); // "$1,234.56"
|
|
30
|
-
*
|
|
31
|
-
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
32
|
-
* console.log(withoutPrefix); // "1,234.56"
|
|
33
|
-
*
|
|
34
|
-
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
35
|
-
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
36
|
-
*
|
|
37
|
-
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
38
|
-
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
const formatToCurrency: FormatToCurrency = (
|
|
43
|
-
value,
|
|
44
|
-
currency,
|
|
45
|
-
config = { showPrefix: true }
|
|
46
|
-
) => {
|
|
47
|
-
if (!countryCurrencies[currency]) {
|
|
48
|
-
throw new Error("Unsupported currency code");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const { countryCurrency, countryLanguage } = countryCurrencies[currency];
|
|
52
|
-
|
|
53
|
-
const format = new Intl.NumberFormat(countryLanguage, {
|
|
54
|
-
style: "currency",
|
|
55
|
-
currency: countryCurrency,
|
|
56
|
-
}).format(value);
|
|
57
|
-
|
|
58
|
-
return config.showPrefix
|
|
59
|
-
? format.replace(/\s/g, " ")
|
|
60
|
-
: removeCurrencySymbols(format).replace(/\s/g, " ");
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export { formatToCurrency };
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
type InputFormatTypes = "brazilianDate" | "timestamp" | "isoDate";
|
|
2
|
-
|
|
3
|
-
type FormatToDateFunction = (
|
|
4
|
-
date: string[], // [date: string, time?: string]
|
|
5
|
-
inputFormat: InputFormatTypes,
|
|
6
|
-
timezone?: number
|
|
7
|
-
) => Date;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Converts a date and time input into a JavaScript `Date` object, formatted according to the specified input format and timezone.
|
|
11
|
-
*
|
|
12
|
-
* @param param0 - A tuple containing the date string and an optional time string.
|
|
13
|
-
* The date string format depends on the `inputFormat` parameter.
|
|
14
|
-
* The time string defaults to "00:00:00" if not provided.
|
|
15
|
-
* @param inputFormat - The format of the input date string.
|
|
16
|
-
* Supported formats are:
|
|
17
|
-
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
18
|
-
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
19
|
-
* - `"timestamp"`: Expects the date in `YYYY-MM-DD` format (similar to `isoDate`).
|
|
20
|
-
* @param timezone - An optional timezone offset in hours. Defaults to `0` (UTC).
|
|
21
|
-
*
|
|
22
|
-
* @returns A `Date` object representing the parsed date and time, adjusted for the specified timezone.
|
|
23
|
-
*
|
|
24
|
-
* @throws {Error} If the `inputFormat` is invalid.
|
|
25
|
-
* @throws {Error} If the provided date or time is invalid.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* import { formatToDate } from "./formatToIsoDate";
|
|
30
|
-
*
|
|
31
|
-
* const date = formatToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
|
|
32
|
-
* console.log(date); // Outputs a Date object for "2023-12-25T18:30:00.000Z" (UTC)
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
const formatToDate: FormatToDateFunction = (
|
|
37
|
-
[date, time = "00:00:00"],
|
|
38
|
-
inputFormat,
|
|
39
|
-
timezone = 0
|
|
40
|
-
) => {
|
|
41
|
-
const dateParts = date.split(/[-/]/).map(Number);
|
|
42
|
-
const timeParts = time.split(".")[0].split(":").map(Number);
|
|
43
|
-
|
|
44
|
-
let day, month, year;
|
|
45
|
-
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
46
|
-
|
|
47
|
-
switch (inputFormat) {
|
|
48
|
-
case "brazilianDate":
|
|
49
|
-
[day, month, year] = dateParts;
|
|
50
|
-
break;
|
|
51
|
-
case "isoDate":
|
|
52
|
-
[year, month, day] = dateParts;
|
|
53
|
-
break;
|
|
54
|
-
case "timestamp":
|
|
55
|
-
[year, month, day] = dateParts.map(Number);
|
|
56
|
-
break;
|
|
57
|
-
default:
|
|
58
|
-
throw new Error("Invalid input format");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const utcDate = new Date(
|
|
62
|
-
Date.UTC(year, month - 1, day, hours - timezone, minutes, seconds)
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
if (isNaN(utcDate.getTime())) throw new Error("Invalid date");
|
|
66
|
-
|
|
67
|
-
return utcDate;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export { formatToDate };
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
type FormatToEllipsisFunction = (value: string, maxLength: number) => string;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Truncates a given text to a specified maximum length and appends an ellipsis ("...")
|
|
5
|
-
* if the text exceeds the maximum length.
|
|
6
|
-
*
|
|
7
|
-
* @param text - The input string to be truncated.
|
|
8
|
-
* @param maxLength - The maximum allowed length of the string before truncation.
|
|
9
|
-
* @returns The truncated string with an ellipsis if the input exceeds the maximum length,
|
|
10
|
-
* or the original string if it does not.
|
|
11
|
-
* @example
|
|
12
|
-
* const result = formatToEllipsis("Hello, world!", 5);
|
|
13
|
-
* console.log(result); // Output: "Hello..."
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const formatToEllipsis: FormatToEllipsisFunction = (text, maxLength) => {
|
|
17
|
-
if (text.length > maxLength) {
|
|
18
|
-
let trimmedText = text.substring(0, maxLength).trimEnd();
|
|
19
|
-
trimmedText = trimmedText.replace(/[.,!?;:]$/, "");
|
|
20
|
-
return `${trimmedText}...`;
|
|
21
|
-
}
|
|
22
|
-
return text;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export { formatToEllipsis };
|