@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,49 +1,13 @@
|
|
|
1
|
-
import type { FormatJsonStringFunction } from "@arkyn/types";
|
|
2
1
|
import { formatJsonObject } from "./formatJsonObject";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
* Formats a JSON string into a more readable format.
|
|
6
|
-
*
|
|
7
|
-
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
8
|
-
* and then formats it using the `formatJsonObject` function. If the input string
|
|
9
|
-
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
10
|
-
*
|
|
11
|
-
* @param jsonString - The JSON string to be formatted.
|
|
12
|
-
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
13
|
-
*
|
|
14
|
-
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
19
|
-
* const formatted = formatJsonString(jsonString);
|
|
20
|
-
* console.log(formatted);
|
|
21
|
-
* // Output:
|
|
22
|
-
* // {
|
|
23
|
-
* // "name": "John",
|
|
24
|
-
* // "age": 30,
|
|
25
|
-
* // "hobbies": [
|
|
26
|
-
* // "reading",
|
|
27
|
-
* // "gaming"
|
|
28
|
-
* // ]
|
|
29
|
-
* // }
|
|
30
|
-
|
|
31
|
-
* const invalidJsonString = '{"name":"John", "age":30,';
|
|
32
|
-
* const formatted = formatJsonString(invalidJsonString);
|
|
33
|
-
* console.log(formatted);
|
|
34
|
-
* // Output:
|
|
35
|
-
* // (Logs "Invalid JSON string: ..." to the console)
|
|
36
|
-
* // ""
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
const formatJsonString: FormatJsonStringFunction = (jsonString) => {
|
|
3
|
+
function formatJsonString(jsonString: string): string {
|
|
41
4
|
try {
|
|
42
5
|
const jsonObject = JSON.parse(jsonString);
|
|
43
6
|
return formatJsonObject(jsonObject, 0);
|
|
44
7
|
} catch (error) {
|
|
45
|
-
|
|
8
|
+
console.error("Invalid JSON string:", error);
|
|
9
|
+
return "";
|
|
46
10
|
}
|
|
47
|
-
}
|
|
11
|
+
}
|
|
48
12
|
|
|
49
13
|
export { formatJsonString };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function formatToBRL(value: number, showPrefix = true) {
|
|
2
|
+
const format = new Intl.NumberFormat("pt-BR", {
|
|
3
|
+
style: "currency",
|
|
4
|
+
currency: "BRL",
|
|
5
|
+
}).format(value);
|
|
6
|
+
|
|
7
|
+
return showPrefix ? format : format.replace("R$", "").trim();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { formatToBRL };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function formatToCNPJ(value: string): string {
|
|
2
|
+
const cleaned = value.replace(/\D/g, "");
|
|
3
|
+
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
4
|
+
if (match) {
|
|
5
|
+
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { formatToCNPJ };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function formatToCPF(value: string): string {
|
|
2
|
+
const cleaned = value.replace(/\D/g, "");
|
|
3
|
+
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
4
|
+
if (match) {
|
|
5
|
+
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { formatToCPF };
|
|
@@ -1,38 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
6
|
-
*
|
|
7
|
-
* The function removes all non-numeric characters from the input string
|
|
8
|
-
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
9
|
-
* If the input does not match the expected format, an error is thrown.
|
|
10
|
-
*
|
|
11
|
-
* @param value - The input string to be formatted as a CEP.
|
|
12
|
-
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
13
|
-
* @throws {Error} If the input does not match the expected CEP format.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { formatToCep } from "./formatToCep";
|
|
18
|
-
*
|
|
19
|
-
* const formattedCep = formatToCep("12345678");
|
|
20
|
-
* console.log(formattedCep); // Output: "12345-678"
|
|
21
|
-
*
|
|
22
|
-
* try {
|
|
23
|
-
* formatToCep("1234");
|
|
24
|
-
* } catch (error) {
|
|
25
|
-
* console.error(error.message); // Output: "Invalid CEP format"
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
const formatToCep: FormatToCepFunction = (value) => {
|
|
31
|
-
const cleaned = removeNonNumeric(value);
|
|
1
|
+
function formatToCep(value: string): string {
|
|
2
|
+
const cleaned = value.replace(/\D/g, "");
|
|
32
3
|
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
4
|
+
if (match) {
|
|
5
|
+
return `${match[1]}-${match[2]}`;
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
37
9
|
|
|
38
10
|
export { formatToCep };
|
|
@@ -1,38 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { formatToCNPJ } from "./formatToCNPJ";
|
|
2
|
+
import { formatToCPF } from "./formatToCPF";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
import { formatToCpf } from "./formatToCpf";
|
|
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) => {
|
|
4
|
+
function formatToCpfCnpj(value: string): string {
|
|
28
5
|
const cleaned = value.replace(/\D/g, "");
|
|
29
6
|
if (cleaned.length === 11) {
|
|
30
|
-
return
|
|
7
|
+
return formatToCPF(cleaned);
|
|
31
8
|
} else if (cleaned.length === 14) {
|
|
32
|
-
return
|
|
9
|
+
return formatToCNPJ(cleaned);
|
|
33
10
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
37
13
|
|
|
38
14
|
export { formatToCpfCnpj };
|
|
@@ -1,25 +1,5 @@
|
|
|
1
|
-
|
|
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
|
-
};
|
|
1
|
+
function formatToEllipsis(text: string, size = 40): string {
|
|
2
|
+
return text.length > size ? `${text.substring(0, size)}...` : text;
|
|
3
|
+
}
|
|
24
4
|
|
|
25
5
|
export { formatToEllipsis };
|
|
@@ -1,80 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FormatToHiddenDigitsOptions } from "@arkyn/types";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { parseToCharacters } from "../parsers/parseToCharacters";
|
|
4
|
+
import { normalizeRange, within } from "../services/range";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type OtherCharacterNode = {
|
|
12
|
-
kind: "other";
|
|
13
|
-
character: string;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type RootCharacterNode = {
|
|
17
|
-
kind: "root";
|
|
18
|
-
digits: number;
|
|
19
|
-
children: (DigitCharacterNode | OtherCharacterNode)[];
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const parseToCharacters = (value: string): RootCharacterNode => {
|
|
23
|
-
let digits = 0;
|
|
24
|
-
|
|
25
|
-
const children = value
|
|
26
|
-
.split("")
|
|
27
|
-
.map((character: string): DigitCharacterNode | OtherCharacterNode => {
|
|
28
|
-
if (DIGIT.test(character))
|
|
29
|
-
return { character, kind: "digit", digit: ++digits };
|
|
30
|
-
return { character, kind: "other" };
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
return { digits, children, kind: "root" };
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const normalizeRange = (
|
|
37
|
-
range: number | [number, number],
|
|
38
|
-
limit: number
|
|
39
|
-
): [number, number] => {
|
|
40
|
-
if (Array.isArray(range)) return range;
|
|
41
|
-
if (range >= 0) return [0, range];
|
|
42
|
-
return [limit + 1 - Math.abs(range), limit];
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const within = (range: [number, number], value: number): boolean =>
|
|
46
|
-
value >= range[0] && value <= range[1];
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Formats a string by hiding specific digits within a given range.
|
|
50
|
-
*
|
|
51
|
-
* This function takes a string input and replaces digits within a specified range
|
|
52
|
-
* with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
|
|
53
|
-
*
|
|
54
|
-
* @param value - The input string to be formatted.
|
|
55
|
-
* @param options - Configuration options for formatting.
|
|
56
|
-
* @param options.range - The range of digits to hide. It can be:
|
|
57
|
-
* - A single number (e.g., `3`), which hides the first `n` digits if positive,
|
|
58
|
-
* or the last `n` digits if negative.
|
|
59
|
-
* - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
|
|
60
|
-
* - Defaults to `3`, hiding the first three digits.
|
|
61
|
-
* @param options.hider - The character used to hide digits. Defaults to `"*"`.
|
|
62
|
-
*
|
|
63
|
-
* @returns The formatted string with specified digits hidden.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```typescript
|
|
67
|
-
* import { formatToHiddenDigits } from "./formatToHiddenDigits";
|
|
68
|
-
*
|
|
69
|
-
* formatToHiddenDigits("123-456-7890", { range: 3 });
|
|
70
|
-
* // Output: "***-456-7890"
|
|
71
|
-
*
|
|
72
|
-
* formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
|
|
73
|
-
* // Output: "123-###-7890"
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
const formatToHiddenDigits: FormatToHiddenDigitsFunction = (value, options) => {
|
|
6
|
+
const formatToHiddenDigits = (
|
|
7
|
+
value: string,
|
|
8
|
+
options: FormatToHiddenDigitsOptions = {}
|
|
9
|
+
): string => {
|
|
78
10
|
const characters = parseToCharacters(value);
|
|
79
11
|
const range = normalizeRange(options.range ?? 3, characters.digits);
|
|
80
12
|
return characters.children
|
|
@@ -1,160 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function getMask(value: string): "NINE" | "EIGTH" {
|
|
7
|
-
const mask = value.length > 10 ? "NINE" : "EIGTH";
|
|
8
|
-
return mask;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const TYPES = {
|
|
12
|
-
EIGTH: "(99) 9999-9999",
|
|
13
|
-
NINE: "(99) 99999-9999",
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const MAX_LENGTH = removeNonNumeric(TYPES.NINE).length;
|
|
17
|
-
|
|
18
|
-
function applyMask(value: string, maskPattern: string) {
|
|
19
|
-
let result = "";
|
|
20
|
-
let digitIndex = 0;
|
|
21
|
-
|
|
22
|
-
for (let i = 0; i < maskPattern.length; i++) {
|
|
23
|
-
if (maskPattern[i] === "9") {
|
|
24
|
-
if (digitIndex < value.length) {
|
|
25
|
-
result += value[digitIndex];
|
|
26
|
-
digitIndex++;
|
|
27
|
-
} else {
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
if (digitIndex < value.length) {
|
|
32
|
-
result += maskPattern[i];
|
|
33
|
-
} else {
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function formatPhoneNumber(phoneNumber: string, country: CountryType): string {
|
|
43
|
-
if (country.code === "+55") {
|
|
44
|
-
let value = removeNonNumeric(phoneNumber);
|
|
45
|
-
const mask = getMask(value);
|
|
46
|
-
|
|
47
|
-
let nextLength = value.length;
|
|
48
|
-
if (nextLength > MAX_LENGTH) return value;
|
|
49
|
-
|
|
50
|
-
value = applyMask(value, TYPES[mask] as "EIGTH" | "NINE");
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const mask = country.mask;
|
|
55
|
-
let formattedNumber = mask;
|
|
56
|
-
|
|
57
|
-
if (country.prefix) {
|
|
58
|
-
const prefixRegex = /\$+/g;
|
|
59
|
-
formattedNumber = formattedNumber.replace(prefixRegex, country.prefix);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
for (
|
|
63
|
-
let i = 0, j = 0;
|
|
64
|
-
i < formattedNumber.length && j < phoneNumber.length;
|
|
65
|
-
i++
|
|
66
|
-
) {
|
|
67
|
-
if (formattedNumber[i] === "_") {
|
|
68
|
-
formattedNumber =
|
|
69
|
-
formattedNumber.substring(0, i) +
|
|
70
|
-
phoneNumber[j] +
|
|
71
|
-
formattedNumber.substring(i + 1);
|
|
72
|
-
j++;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return formattedNumber;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function getCountryWithPrefixCode(countryCode: string, prefix: string) {
|
|
80
|
-
const country = countries.find(
|
|
81
|
-
(country) => country.code === countryCode && country.prefix === prefix
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
if (!country) throw new Error("Invalid country code or prefix");
|
|
85
|
-
|
|
86
|
-
if (country.prefix !== prefix) {
|
|
87
|
-
throw new Error("Invalid country code or prefix");
|
|
1
|
+
function formatToPhone(value: string): string {
|
|
2
|
+
const cleaned = value.replace(/\D/g, "");
|
|
3
|
+
const match = cleaned.match(/^(\d{2})(\d{5})(\d{4})$/);
|
|
4
|
+
if (match) {
|
|
5
|
+
return `(${match[1]}) ${match[2]}-${match[3]}`;
|
|
88
6
|
}
|
|
89
|
-
|
|
90
|
-
if (!country.prefix) {
|
|
91
|
-
throw new Error("Invalid country code or prefix");
|
|
92
|
-
}
|
|
93
|
-
return country;
|
|
7
|
+
return value;
|
|
94
8
|
}
|
|
95
9
|
|
|
96
|
-
function getCountryWithoutPrefixCode(countryCode: string) {
|
|
97
|
-
const country = countries.find((country) => country.code === countryCode);
|
|
98
|
-
if (!country) throw new Error("Invalid country code");
|
|
99
|
-
if (country.prefix) throw new Error("Invalid country code");
|
|
100
|
-
return country;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Formats a phone number string based on the provided country code and optional prefix.
|
|
105
|
-
*
|
|
106
|
-
* The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
|
|
107
|
-
* The function determines the appropriate formatting mask based on the country and applies it to the phone number.
|
|
108
|
-
*
|
|
109
|
-
* @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
|
|
110
|
-
* Example formats:
|
|
111
|
-
* - "+55-11 912345678"
|
|
112
|
-
* - "+1 1234567890"
|
|
113
|
-
*
|
|
114
|
-
* @returns The formatted phone number string based on the country's formatting rules.
|
|
115
|
-
*
|
|
116
|
-
* @throws {Error} If the input phone number does not match the expected format.
|
|
117
|
-
* @throws {Error} If the country code or phone number is missing from the input string.
|
|
118
|
-
* @throws {Error} If the provided country code and prefix combination is invalid.
|
|
119
|
-
* @throws {Error} If the provided country code is invalid.
|
|
120
|
-
* @throws {Error} If the provided country code has a prefix but none is supplied in the input.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* import { formatToPhone } from "./formatToPhone";
|
|
125
|
-
*
|
|
126
|
-
* const formattedPhone1 = formatToPhone("+55 11912345678");
|
|
127
|
-
* console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
|
|
128
|
-
*
|
|
129
|
-
* const formattedPhone2 = formatToPhone("+1-123 4567890");
|
|
130
|
-
* console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
const formatToPhone: FormatToPhoneFunction = (prop) => {
|
|
135
|
-
const phoneRegex = /^\+\d{1,4}(-\d{1,4})? \d+$/;
|
|
136
|
-
|
|
137
|
-
if (!phoneRegex.test(prop)) {
|
|
138
|
-
throw new Error(
|
|
139
|
-
"Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const countryCode = prop.split(" ")[0].split("-")[0];
|
|
144
|
-
const prefixCode = prop.split(" ")[0].split("-")[1];
|
|
145
|
-
const phoneNumber = prop.split(" ")[1];
|
|
146
|
-
|
|
147
|
-
if (!countryCode || !phoneNumber) {
|
|
148
|
-
throw new Error("Invalid phone number format");
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (prefixCode) {
|
|
152
|
-
const country = getCountryWithPrefixCode(countryCode, prefixCode);
|
|
153
|
-
return formatPhoneNumber(phoneNumber, country);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const country = getCountryWithoutPrefixCode(countryCode);
|
|
157
|
-
return formatPhoneNumber(phoneNumber, country);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
10
|
export { formatToPhone };
|
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Generates a hexadecimal color code based on the input string.
|
|
5
|
-
* The function creates a hash from the string and uses it to calculate
|
|
6
|
-
* RGB values, which are then converted to a hexadecimal color code.
|
|
7
|
-
*
|
|
8
|
-
* @param prop - The input string used to generate the color.
|
|
9
|
-
* @returns A hexadecimal color code (e.g., "#a1b2c3") derived from the input string.
|
|
10
|
-
* @example
|
|
11
|
-
* const color = generateColorByString("example");
|
|
12
|
-
* console.log(color); // Outputs a consistent hex color like "#5e8f9a"
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const generateColorByString: GenerateColorByStringFunction = (prop) => {
|
|
1
|
+
function generateColorByString(string: string) {
|
|
16
2
|
var hash = 0;
|
|
17
3
|
|
|
18
|
-
for (var i = 0; i <
|
|
19
|
-
hash =
|
|
4
|
+
for (var i = 0; i < string.length; i++) {
|
|
5
|
+
hash = string.charCodeAt(i) + ((hash << 5) - hash);
|
|
20
6
|
}
|
|
21
7
|
|
|
22
8
|
var red = (hash & 0xff0000) >> 16;
|
|
@@ -28,6 +14,6 @@ const generateColorByString: GenerateColorByStringFunction = (prop) => {
|
|
|
28
14
|
var blueHex = blue.toString(16).padStart(2, "0");
|
|
29
15
|
|
|
30
16
|
return "#" + redHex + greenHex + blueHex;
|
|
31
|
-
}
|
|
17
|
+
}
|
|
32
18
|
|
|
33
19
|
export { generateColorByString };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { v4, v7 } from "uuid";
|
|
2
2
|
|
|
3
3
|
function hexToBin(hex: string) {
|
|
4
|
-
hex = hex.replace(/-/g, "");
|
|
5
4
|
const buffer = new Uint8Array(hex.length / 2);
|
|
6
5
|
|
|
7
6
|
for (let i = 0; i < hex.length; i += 2) {
|
|
@@ -21,41 +20,11 @@ function uuidV7() {
|
|
|
21
20
|
return { text: uuid, binary: hexToBin(uuid) };
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
* - `"binary"`: Returns the UUID as a `Uint8Array` in binary format.
|
|
30
|
-
* @param format - The version of the UUID to generate. Can be:
|
|
31
|
-
* - `"v4"`: Generates a random UUID (version 4).
|
|
32
|
-
* - `"v7"`: Generates a time-ordered UUID (version 7).
|
|
33
|
-
* @returns The generated UUID in the specified type and format.
|
|
34
|
-
* - If `type` is `"text"`, a string representation of the UUID is returned.
|
|
35
|
-
* - If `type` is `"binary"`, a `Uint8Array` representation of the UUID is returned.
|
|
36
|
-
* @throws {Error} If an invalid `type` or `format` is provided.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* // Generate a version 4 UUID as a string
|
|
40
|
-
* const idTextV4 = generateId("text", "v4");
|
|
41
|
-
* console.log(idTextV4); // e.g., "550e8400-e29b-41d4-a716-446655440000"
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* // Generate a version 7 UUID as binary
|
|
45
|
-
* const idBinaryV7 = generateId("binary", "v7");
|
|
46
|
-
* console.log(idBinaryV7); // Uint8Array([...])
|
|
47
|
-
*/
|
|
48
|
-
function generateId(type: "text", format: "v4" | "v7"): string;
|
|
49
|
-
function generateId(type: "binary", format: "v4" | "v7"): Uint8Array;
|
|
50
|
-
function generateId(
|
|
51
|
-
type: "text" | "binary",
|
|
52
|
-
format: "v4" | "v7"
|
|
53
|
-
): string | Uint8Array {
|
|
54
|
-
if (type === "text" && format === "v4") return uuidV4().text;
|
|
55
|
-
if (type === "binary" && format === "v4") return uuidV4().binary;
|
|
56
|
-
if (type === "text" && format === "v7") return uuidV7().text;
|
|
57
|
-
if (type === "binary" && format === "v7") return uuidV7().binary;
|
|
58
|
-
throw new Error("Invalid type or format");
|
|
23
|
+
function generateId(type: "text" | "binary") {
|
|
24
|
+
return {
|
|
25
|
+
v4: uuidV4()[type],
|
|
26
|
+
v7: uuidV7()[type],
|
|
27
|
+
};
|
|
59
28
|
}
|
|
60
29
|
|
|
61
30
|
export { generateId };
|
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* The function performs the following transformations:
|
|
5
|
-
* - Normalizes the string to remove diacritical marks (e.g., accents).
|
|
6
|
-
* - Removes non-alphanumeric characters except for spaces and hyphens.
|
|
7
|
-
* - Replaces spaces with hyphens.
|
|
8
|
-
* - Converts the string to lowercase.
|
|
9
|
-
* - Collapses multiple consecutive hyphens into a single hyphen.
|
|
10
|
-
* - Trims leading and trailing hyphens.
|
|
11
|
-
*
|
|
12
|
-
* @param string - The input string to be converted into a slug.
|
|
13
|
-
* @returns A URL-friendly slug derived from the input string.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
function generateSlug(prop: string) {
|
|
17
|
-
let slug = prop.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
1
|
+
function generateSlug(string: string) {
|
|
2
|
+
let slug = string.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
18
3
|
|
|
19
4
|
slug = slug
|
|
20
5
|
.replace(/[^\w\s-]/g, "")
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
// formats
|
|
2
|
+
export { formatBrazilianDateHour } from "./formats/formatBrazilianDateHour";
|
|
3
|
+
export { formatBrazilianDateToDate } from "./formats/formatBrazilianDateToDate";
|
|
4
|
+
export { formatDateHour } from "./formats/formatDateHour";
|
|
2
5
|
export { formatJsonObject } from "./formats/formatJsonObject";
|
|
3
6
|
export { formatJsonString } from "./formats/formatJsonString";
|
|
7
|
+
export { formatToBRL } from "./formats/formatToBRL";
|
|
4
8
|
export { formatToCep } from "./formats/formatToCep";
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
9
|
+
export { formatToCNPJ } from "./formats/formatToCNPJ";
|
|
10
|
+
export { formatToCPF } from "./formats/formatToCPF";
|
|
7
11
|
export { formatToCpfCnpj } from "./formats/formatToCpfCnpj";
|
|
8
|
-
export { formatToCurrency } from "./formats/formatToCurrency";
|
|
9
12
|
export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
10
13
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
11
14
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
12
15
|
|
|
16
|
+
// generators
|
|
13
17
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
14
18
|
export { generateId } from "./generators/generateId";
|
|
15
19
|
export { generateSlug } from "./generators/generateSlug";
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
// parsers
|
|
22
|
+
export { parseToCharacters } from "./parsers/parseToCharacters";
|
|
18
23
|
|
|
24
|
+
// services
|
|
19
25
|
export { calculateCardInstallment } from "./services/calculateCardInstallment";
|
|
20
26
|
export { maskSensitiveData } from "./services/maskSensitiveData";
|
|
21
|
-
export {
|
|
27
|
+
export { normalizeRange, within } from "./services/range";
|
|
22
28
|
export { truncateLargeFields } from "./services/truncateLargeFields";
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
// validations
|
|
31
|
+
export { regex } from "./validations/regex";
|
|
32
|
+
export { validateCPF } from "./validations/validateCPF";
|
|
25
33
|
export { validateDate } from "./validations/validateDate";
|
|
26
34
|
export { validatePhone } from "./validations/validatePhone";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const DIGIT = /^\d$/;
|
|
2
|
+
|
|
3
|
+
type DigitCharacterNode = {
|
|
4
|
+
kind: "digit";
|
|
5
|
+
digit: number;
|
|
6
|
+
character: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type OtherCharacterNode = {
|
|
10
|
+
kind: "other";
|
|
11
|
+
character: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type RootCharacterNode = {
|
|
15
|
+
kind: "root";
|
|
16
|
+
digits: number;
|
|
17
|
+
children: (DigitCharacterNode | OtherCharacterNode)[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const parseToCharacters = (value: string): RootCharacterNode => {
|
|
21
|
+
let digits = 0;
|
|
22
|
+
|
|
23
|
+
const children = value
|
|
24
|
+
.split("")
|
|
25
|
+
.map((character: string): DigitCharacterNode | OtherCharacterNode => {
|
|
26
|
+
if (DIGIT.test(character))
|
|
27
|
+
return { character, kind: "digit", digit: ++digits };
|
|
28
|
+
return { character, kind: "other" };
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return { digits, children, kind: "root" };
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { parseToCharacters };
|