@arkyn/shared 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +24 -0
- package/README.md +113 -0
- package/dist/formats/formatDate.d.ts +43 -0
- package/dist/formats/formatDate.d.ts.map +1 -0
- package/dist/formats/formatDate.js +77 -0
- package/dist/formats/formatJsonObject.d.ts +31 -1
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +67 -26
- package/dist/formats/formatJsonString.d.ts +37 -1
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +38 -4
- package/dist/formats/formatToCep.d.ts +27 -1
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +31 -6
- package/dist/formats/formatToCnpj.d.ts +30 -0
- package/dist/formats/formatToCnpj.d.ts.map +1 -0
- package/dist/formats/formatToCnpj.js +35 -0
- package/dist/formats/formatToCpf.d.ts +30 -0
- package/dist/formats/formatToCpf.d.ts.map +1 -0
- package/dist/formats/formatToCpf.js +35 -0
- package/dist/formats/formatToCpfCnpj.d.ts +22 -1
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +27 -7
- package/dist/formats/formatToCurrency.d.ts +29 -0
- package/dist/formats/formatToCurrency.d.ts.map +1 -0
- package/dist/formats/formatToCurrency.js +41 -0
- package/dist/formats/formatToEllipsis.d.ts +14 -1
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +20 -3
- package/dist/formats/formatToHiddenDigits.d.ts +30 -2
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +49 -3
- package/dist/formats/formatToPhone.d.ts +32 -1
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +128 -6
- package/dist/generators/generateColorByString.d.ts +13 -1
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +15 -4
- package/dist/generators/generateId.d.ts +26 -4
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +11 -5
- package/dist/generators/generateSlug.d.ts +15 -1
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +16 -2
- package/dist/index.d.ts +9 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -12
- package/dist/services/calculateCardInstallment.d.ts +31 -5
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +52 -11
- package/dist/services/maskSensitiveData.d.ts +24 -1
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +24 -2
- package/dist/services/removeCurrencySymbols.d.ts +20 -0
- package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
- package/dist/services/removeCurrencySymbols.js +23 -0
- package/dist/services/removeNonNumeric.d.ts +15 -0
- package/dist/services/removeNonNumeric.d.ts.map +1 -0
- package/dist/services/removeNonNumeric.js +16 -0
- package/dist/services/truncateLargeFields.d.ts +30 -1
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +34 -6
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +54 -0
- package/dist/validations/validateDate.d.ts +27 -6
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +38 -12
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +27 -1
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +29 -4
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +7 -2
- package/src/formats/__test__/formatDate.spec.ts +88 -0
- package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
- package/src/formats/__test__/formatJsonString.spec.ts +83 -0
- package/src/formats/__test__/formatToCep.spec.ts +37 -0
- package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
- package/src/formats/__test__/formatToCpf.spec.ts +37 -0
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
- package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
- package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
- package/src/formats/__test__/formatToPhone.spec.ts +58 -0
- package/src/formats/formatDate.ts +97 -0
- package/src/formats/formatJsonObject.ts +68 -26
- package/src/formats/formatJsonString.ts +40 -4
- package/src/formats/formatToCep.ts +35 -7
- package/src/formats/formatToCnpj.ts +39 -0
- package/src/formats/formatToCpf.ts +39 -0
- package/src/formats/formatToCpfCnpj.ts +31 -7
- package/src/formats/formatToCurrency.ts +53 -0
- package/src/formats/formatToEllipsis.ts +23 -3
- package/src/formats/formatToHiddenDigits.ts +75 -7
- package/src/formats/formatToPhone.ts +156 -6
- package/src/generators/__test__/generateColorByString.spec.ts +37 -0
- package/src/generators/__test__/generateId.spec.ts +44 -0
- package/src/generators/__test__/generateSlug.spec.ts +47 -0
- package/src/generators/generateColorByString.ts +18 -4
- package/src/generators/generateId.ts +36 -5
- package/src/generators/generateSlug.ts +17 -2
- package/src/index.ts +10 -13
- package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
- package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
- package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
- package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
- package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
- package/src/services/calculateCardInstallment.ts +57 -12
- package/src/services/maskSensitiveData.ts +29 -4
- package/src/services/removeCurrencySymbols.ts +25 -0
- package/src/services/removeNonNumeric.ts +18 -0
- package/src/services/truncateLargeFields.ts +40 -6
- package/src/validations/__test__/validateCep.spec.ts +40 -0
- package/src/validations/__test__/validateCnpj.spec.ts +42 -0
- package/src/validations/__test__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePassword.spec.ts +43 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/__test__/validateRg.spec.ts +48 -0
- package/src/validations/validateCep.ts +40 -0
- package/src/validations/validateCnpj.ts +64 -0
- package/src/validations/validateCpf.ts +61 -0
- package/src/validations/validateDate.ts +39 -17
- package/src/validations/validatePassword.ts +41 -0
- package/src/validations/validatePhone.ts +31 -4
- package/src/validations/validateRg.ts +37 -0
- package/tsconfig.json +2 -2
- package/vitest.config.ts +5 -0
- package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
- package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateHour.js +0 -12
- package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateToDate.js +0 -9
- package/dist/formats/formatDateHour.d.ts +0 -3
- package/dist/formats/formatDateHour.d.ts.map +0 -1
- package/dist/formats/formatDateHour.js +0 -11
- package/dist/formats/formatToBRL.d.ts +0 -3
- package/dist/formats/formatToBRL.d.ts.map +0 -1
- package/dist/formats/formatToBRL.js +0 -8
- package/dist/formats/formatToCNPJ.d.ts +0 -3
- package/dist/formats/formatToCNPJ.d.ts.map +0 -1
- package/dist/formats/formatToCNPJ.js +0 -9
- package/dist/formats/formatToCPF.d.ts +0 -3
- package/dist/formats/formatToCPF.d.ts.map +0 -1
- package/dist/formats/formatToCPF.js +0 -9
- package/dist/parsers/parseToCharacters.d.ts +0 -17
- package/dist/parsers/parseToCharacters.d.ts.map +0 -1
- package/dist/parsers/parseToCharacters.js +0 -13
- package/dist/services/range.d.ts +0 -4
- package/dist/services/range.d.ts.map +0 -1
- package/dist/services/range.js +0 -9
- package/dist/validations/regex.d.ts +0 -15
- package/dist/validations/regex.d.ts.map +0 -1
- package/dist/validations/regex.js +0 -25
- package/dist/validations/validateCPF.d.ts +0 -3
- package/dist/validations/validateCPF.d.ts.map +0 -1
- package/dist/validations/validateCPF.js +0 -36
- package/src/formats/formatBrazilianDateHour.ts +0 -17
- package/src/formats/formatBrazilianDateToDate.ts +0 -13
- package/src/formats/formatDateHour.ts +0 -15
- package/src/formats/formatToBRL.ts +0 -10
- package/src/formats/formatToCNPJ.ts +0 -10
- package/src/formats/formatToCPF.ts +0 -10
- package/src/parsers/parseToCharacters.ts +0 -34
- package/src/services/range.ts +0 -15
- package/src/validations/regex.ts +0 -41
- package/src/validations/validateCPF.ts +0 -38
|
@@ -1,13 +1,49 @@
|
|
|
1
|
+
import type { FormatJsonStringFunction } from "@arkyn/types";
|
|
1
2
|
import { formatJsonObject } from "./formatJsonObject";
|
|
2
3
|
|
|
3
|
-
|
|
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) => {
|
|
4
41
|
try {
|
|
5
42
|
const jsonObject = JSON.parse(jsonString);
|
|
6
43
|
return formatJsonObject(jsonObject, 0);
|
|
7
44
|
} catch (error) {
|
|
8
|
-
|
|
9
|
-
return "";
|
|
45
|
+
throw new Error(`Invalid JSON string \n ${error}`);
|
|
10
46
|
}
|
|
11
|
-
}
|
|
47
|
+
};
|
|
12
48
|
|
|
13
49
|
export { formatJsonString };
|
|
@@ -1,10 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { FormatToCepFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
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);
|
|
3
32
|
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
33
|
+
|
|
34
|
+
if (match) return `${match[1]}-${match[2]}`;
|
|
35
|
+
throw new Error("Invalid CEP format");
|
|
36
|
+
};
|
|
9
37
|
|
|
10
38
|
export { formatToCep };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { FormatToCnpjFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
6
|
+
*
|
|
7
|
+
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
10
|
+
* Non-numeric characters will be removed before formatting.
|
|
11
|
+
*
|
|
12
|
+
* @returns A string formatted as a CNPJ.
|
|
13
|
+
*
|
|
14
|
+
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { formatToCnpj } from "./formatToCNPJ";
|
|
19
|
+
*
|
|
20
|
+
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
21
|
+
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
22
|
+
*
|
|
23
|
+
* try {
|
|
24
|
+
* formatToCnpj("12345");
|
|
25
|
+
* } catch (error) {
|
|
26
|
+
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const formatToCnpj: FormatToCnpjFunction = (value) => {
|
|
32
|
+
const cleaned = removeNonNumeric(value);
|
|
33
|
+
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
34
|
+
if (match)
|
|
35
|
+
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
36
|
+
throw new Error("Invalid CNPJ length");
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { formatToCnpj };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { FormatToCpfFunction } from "@arkyn/types";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
6
|
+
*
|
|
7
|
+
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
8
|
+
* This function ensures the input is cleaned of non-numeric characters and
|
|
9
|
+
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
10
|
+
*
|
|
11
|
+
* @param value - The input string to be formatted as a CPF.
|
|
12
|
+
* @returns The formatted CPF string.
|
|
13
|
+
* @throws {Error} If the input string does not match the expected CPF format.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { formatToCpf } from "./formatToCPF";
|
|
18
|
+
*
|
|
19
|
+
* const formattedCpf = formatToCpf("12345678909");
|
|
20
|
+
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
21
|
+
|
|
22
|
+
* try {
|
|
23
|
+
* const formattedCpf = formatToCpf("12345");
|
|
24
|
+
* } catch (error) {
|
|
25
|
+
* console.error(error.message); // Output: "Invalid CPF format"
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const formatToCpf: FormatToCpfFunction = (value) => {
|
|
32
|
+
const cleaned = removeNonNumeric(value);
|
|
33
|
+
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
34
|
+
|
|
35
|
+
if (match) return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
36
|
+
throw new Error("Invalid CPF format");
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { formatToCpf };
|
|
@@ -1,14 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { formatToCPF } from "./formatToCPF";
|
|
1
|
+
import type { FormatToCpfCnpjFunction } from "@arkyn/types";
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
import { formatToCnpj } from "./formatToCnpj";
|
|
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) => {
|
|
5
28
|
const cleaned = value.replace(/\D/g, "");
|
|
6
29
|
if (cleaned.length === 11) {
|
|
7
|
-
return
|
|
30
|
+
return formatToCpf(cleaned);
|
|
8
31
|
} else if (cleaned.length === 14) {
|
|
9
|
-
return
|
|
32
|
+
return formatToCnpj(cleaned);
|
|
10
33
|
}
|
|
11
|
-
|
|
12
|
-
|
|
34
|
+
|
|
35
|
+
throw new Error("Invalid CPF or CNPJ length");
|
|
36
|
+
};
|
|
13
37
|
|
|
14
38
|
export { formatToCpfCnpj };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { countryCurrencies } from "@arkyn/templates";
|
|
2
|
+
import type { FormatToCurrency } from "@arkyn/types";
|
|
3
|
+
|
|
4
|
+
import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The numeric value to be formatted.
|
|
10
|
+
* @param currency - The currency code used to determine the formatting style.
|
|
11
|
+
* @param config - Optional configuration object.
|
|
12
|
+
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
13
|
+
*
|
|
14
|
+
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
19
|
+
* console.log(formatted); // "$1,234.56"
|
|
20
|
+
*
|
|
21
|
+
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
22
|
+
* console.log(withoutPrefix); // "1,234.56"
|
|
23
|
+
*
|
|
24
|
+
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
25
|
+
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
26
|
+
*
|
|
27
|
+
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
28
|
+
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const formatToCurrency: FormatToCurrency = (
|
|
33
|
+
value,
|
|
34
|
+
currency,
|
|
35
|
+
config = { showPrefix: true }
|
|
36
|
+
) => {
|
|
37
|
+
if (!countryCurrencies[currency]) {
|
|
38
|
+
throw new Error("Unsupported currency code");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const { countryCurrency, countryLanguage } = countryCurrencies[currency];
|
|
42
|
+
|
|
43
|
+
const format = new Intl.NumberFormat(countryLanguage, {
|
|
44
|
+
style: "currency",
|
|
45
|
+
currency: countryCurrency,
|
|
46
|
+
}).format(value);
|
|
47
|
+
|
|
48
|
+
return config.showPrefix
|
|
49
|
+
? format.replace(/\s/g, " ")
|
|
50
|
+
: removeCurrencySymbols(format).replace(/\s/g, " ");
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { formatToCurrency };
|
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { FormatToEllipsisFunction } from "@arkyn/types";
|
|
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
|
+
};
|
|
4
24
|
|
|
5
25
|
export { formatToEllipsis };
|
|
@@ -1,12 +1,80 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FormatToHiddenDigitsFunction } from "@arkyn/types";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import { normalizeRange, within } from "../services/range";
|
|
3
|
+
const DIGIT = /^\d$/;
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
type DigitCharacterNode = {
|
|
6
|
+
kind: "digit";
|
|
7
|
+
digit: number;
|
|
8
|
+
character: string;
|
|
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) => {
|
|
10
78
|
const characters = parseToCharacters(value);
|
|
11
79
|
const range = normalizeRange(options.range ?? 3, characters.digits);
|
|
12
80
|
return characters.children
|
|
@@ -1,10 +1,160 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { countries } from "@arkyn/templates";
|
|
2
|
+
import type { CountryType, FormatToPhoneFunction } from "@arkyn/types";
|
|
3
|
+
|
|
4
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
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");
|
|
6
88
|
}
|
|
7
|
-
|
|
89
|
+
|
|
90
|
+
if (!country.prefix) {
|
|
91
|
+
throw new Error("Invalid country code or prefix");
|
|
92
|
+
}
|
|
93
|
+
return country;
|
|
8
94
|
}
|
|
9
95
|
|
|
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
|
+
|
|
10
160
|
export { formatToPhone };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateColorByString } from "../generateColorByString";
|
|
3
|
+
|
|
4
|
+
describe("generateColorByString", () => {
|
|
5
|
+
it("should generate a consistent hex color for the same input string", () => {
|
|
6
|
+
const color1 = generateColorByString("example");
|
|
7
|
+
const color2 = generateColorByString("example");
|
|
8
|
+
expect(color1).toBe(color2);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should generate different hex colors for different input strings", () => {
|
|
12
|
+
const color1 = generateColorByString("example1");
|
|
13
|
+
const color2 = generateColorByString("example2");
|
|
14
|
+
expect(color1).not.toBe(color2);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return a valid hex color code", () => {
|
|
18
|
+
const color = generateColorByString("test");
|
|
19
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should handle empty strings and return a valid hex color", () => {
|
|
23
|
+
const color = generateColorByString("");
|
|
24
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should handle strings with special characters", () => {
|
|
28
|
+
const color = generateColorByString("!@#$%^&*()");
|
|
29
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should handle long strings and return a valid hex color", () => {
|
|
33
|
+
const longString = "a".repeat(1000);
|
|
34
|
+
const color = generateColorByString(longString);
|
|
35
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateId } from "../generateId";
|
|
3
|
+
|
|
4
|
+
describe("generateId", () => {
|
|
5
|
+
it("should generate a valid UUID v4 as text", () => {
|
|
6
|
+
const id = generateId("text", "v4");
|
|
7
|
+
expect(typeof id).toBe("string");
|
|
8
|
+
expect(id).toMatch(
|
|
9
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should generate a valid UUID v4 as binary", () => {
|
|
14
|
+
const id = generateId("binary", "v4");
|
|
15
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
16
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should generate a valid UUID v7 as text", () => {
|
|
20
|
+
const id = generateId("text", "v7");
|
|
21
|
+
expect(typeof id).toBe("string");
|
|
22
|
+
expect(id).toMatch(
|
|
23
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should generate a valid UUID v7 as binary", () => {
|
|
28
|
+
const id = generateId("binary", "v7");
|
|
29
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
30
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should throw an error for invalid type", () => {
|
|
34
|
+
expect(() => generateId("invalid" as any, "v4")).toThrow(
|
|
35
|
+
"Invalid type or format"
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should throw an error for invalid format", () => {
|
|
40
|
+
expect(() => generateId("text", "v8" as any)).toThrow(
|
|
41
|
+
"Invalid type or format"
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
});
|