@arkyn/shared 1.4.52 → 1.4.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/formats/formatDate.d.ts +43 -0
- package/dist/formats/formatDate.d.ts.map +1 -0
- package/dist/formats/formatDate.js +77 -0
- package/dist/formats/formatJsonObject.d.ts +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 +7 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -15
- package/dist/regex/index.d.ts +6 -0
- package/dist/regex/index.d.ts.map +1 -0
- package/dist/regex/index.js +4 -0
- package/dist/services/calculateCardInstallment.d.ts +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/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +56 -0
- package/dist/validations/validateDate.d.ts +27 -6
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +32 -6
- package/dist/validations/validatePhone.d.ts +27 -1
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +27 -2
- package/package.json +4 -2
- 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/generateColorByString.ts +18 -4
- package/src/generators/generateId.ts +36 -5
- package/src/generators/generateSlug.ts +17 -2
- package/src/index.ts +7 -15
- package/src/regex/index.ts +8 -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/validateCpf.ts +64 -0
- package/src/validations/validateDate.ts +33 -11
- package/src/validations/validatePhone.ts +29 -2
- package/tsconfig.json +1 -1
- 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,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 };
|
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import type { GenerateColorByStringFunction } from "@arkyn/types";
|
|
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) => {
|
|
2
16
|
var hash = 0;
|
|
3
17
|
|
|
4
|
-
for (var i = 0; i <
|
|
5
|
-
hash =
|
|
18
|
+
for (var i = 0; i < prop.length; i++) {
|
|
19
|
+
hash = prop.charCodeAt(i) + ((hash << 5) - hash);
|
|
6
20
|
}
|
|
7
21
|
|
|
8
22
|
var red = (hash & 0xff0000) >> 16;
|
|
@@ -14,6 +28,6 @@ function generateColorByString(string: string) {
|
|
|
14
28
|
var blueHex = blue.toString(16).padStart(2, "0");
|
|
15
29
|
|
|
16
30
|
return "#" + redHex + greenHex + blueHex;
|
|
17
|
-
}
|
|
31
|
+
};
|
|
18
32
|
|
|
19
33
|
export { generateColorByString };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { v4, v7 } from "uuid";
|
|
2
2
|
|
|
3
3
|
function hexToBin(hex: string) {
|
|
4
|
+
hex = hex.replace(/-/g, "");
|
|
4
5
|
const buffer = new Uint8Array(hex.length / 2);
|
|
5
6
|
|
|
6
7
|
for (let i = 0; i < hex.length; i += 2) {
|
|
@@ -20,11 +21,41 @@ function uuidV7() {
|
|
|
20
21
|
return { text: uuid, binary: hexToBin(uuid) };
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Generates a unique identifier (UUID) in the specified format and type.
|
|
26
|
+
*
|
|
27
|
+
* @param type - The desired output type of the UUID. Can be:
|
|
28
|
+
* - `"text"`: Returns the UUID as a string.
|
|
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");
|
|
28
59
|
}
|
|
29
60
|
|
|
30
61
|
export { generateId };
|
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generates a URL-friendly slug from a given string.
|
|
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, "");
|
|
3
18
|
|
|
4
19
|
slug = slug
|
|
5
20
|
.replace(/[^\w\s-]/g, "")
|
package/src/index.ts
CHANGED
|
@@ -1,34 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
export { formatBrazilianDateHour } from "./formats/formatBrazilianDateHour";
|
|
3
|
-
export { formatBrazilianDateToDate } from "./formats/formatBrazilianDateToDate";
|
|
4
|
-
export { formatDateHour } from "./formats/formatDateHour";
|
|
1
|
+
export { formatDate } from "./formats/formatDate";
|
|
5
2
|
export { formatJsonObject } from "./formats/formatJsonObject";
|
|
6
3
|
export { formatJsonString } from "./formats/formatJsonString";
|
|
7
|
-
export { formatToBRL } from "./formats/formatToBRL";
|
|
8
4
|
export { formatToCep } from "./formats/formatToCep";
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
5
|
+
export { formatToCnpj } from "./formats/formatToCnpj";
|
|
6
|
+
export { formatToCpf } from "./formats/formatToCpf";
|
|
11
7
|
export { formatToCpfCnpj } from "./formats/formatToCpfCnpj";
|
|
8
|
+
export { formatToCurrency } from "./formats/formatToCurrency";
|
|
12
9
|
export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
13
10
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
14
11
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
15
12
|
|
|
16
|
-
// generators
|
|
17
13
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
18
14
|
export { generateId } from "./generators/generateId";
|
|
19
15
|
export { generateSlug } from "./generators/generateSlug";
|
|
20
16
|
|
|
21
|
-
|
|
22
|
-
export { parseToCharacters } from "./parsers/parseToCharacters";
|
|
17
|
+
export { regex } from "./regex";
|
|
23
18
|
|
|
24
|
-
// services
|
|
25
19
|
export { calculateCardInstallment } from "./services/calculateCardInstallment";
|
|
26
20
|
export { maskSensitiveData } from "./services/maskSensitiveData";
|
|
27
|
-
export {
|
|
21
|
+
export { removeNonNumeric } from "./services/removeNonNumeric";
|
|
28
22
|
export { truncateLargeFields } from "./services/truncateLargeFields";
|
|
29
23
|
|
|
30
|
-
|
|
31
|
-
export { regex } from "./validations/regex";
|
|
32
|
-
export { validateCPF } from "./validations/validateCPF";
|
|
24
|
+
export { validateCpf } from "./validations/validateCpf";
|
|
33
25
|
export { validateDate } from "./validations/validateDate";
|
|
34
26
|
export { validatePhone } from "./validations/validatePhone";
|
|
@@ -1,21 +1,66 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CalculateCardInstallmentFunction } from "@arkyn/types";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the installment price and total price for a card payment plan.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* **Important:** When the interest amount (`fees`) is equal to 0 or the number of installments (`numberInstallments`) is equal to 1, no interest will be charged.
|
|
8
|
+
*
|
|
9
|
+
* @throws Will throw an error if the number of installments is less than or equal to 0.
|
|
10
|
+
* @throws Will throw an error if the fees are less than 0.
|
|
11
|
+
*
|
|
12
|
+
* @param props - The input parameters for the calculation.
|
|
13
|
+
* @param props.cashPrice - The total cash price of the product or service.
|
|
14
|
+
* @param props.numberInstallments - The number of installments for the payment plan.
|
|
15
|
+
* @param props.fees - The interest rate per installment (default is 0.0349).
|
|
16
|
+
*
|
|
17
|
+
* @returns An object containing:
|
|
18
|
+
* - `totalPrice`: The total price to be paid, rounded to two decimal places.
|
|
19
|
+
* - `installmentPrice`: The price of each installment, rounded to two decimal places.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const result = calculateCardInstallment({
|
|
24
|
+
* cashPrice: 1000,
|
|
25
|
+
* numberInstallments: 12,
|
|
26
|
+
* fees: 0.02,
|
|
27
|
+
* });
|
|
28
|
+
* console.log(result);
|
|
29
|
+
* // Output: { totalPrice: 1124.62, installmentPrice: 93.72 }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
5
32
|
|
|
6
|
-
|
|
7
|
-
|
|
33
|
+
const calculateCardInstallment: CalculateCardInstallmentFunction = (props) => {
|
|
34
|
+
const { cashPrice, numberInstallments, fees = 0.0349 } = props;
|
|
8
35
|
|
|
9
|
-
|
|
10
|
-
|
|
36
|
+
if (fees === 0 || numberInstallments === 1) {
|
|
37
|
+
return {
|
|
38
|
+
totalPrice: cashPrice,
|
|
39
|
+
installmentPrice: cashPrice / numberInstallments,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
11
42
|
|
|
12
|
-
|
|
13
|
-
|
|
43
|
+
if (numberInstallments <= 0) {
|
|
44
|
+
throw new Error("Number of installments must be greater than 0");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (fees < 0) {
|
|
48
|
+
throw new Error("Fees must be greater than or equal to 0");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let installmentPrice = 0;
|
|
52
|
+
let totalPrice = 0;
|
|
53
|
+
|
|
54
|
+
let numerator = Math.pow(1 + fees, numberInstallments) * fees;
|
|
55
|
+
let denominator = Math.pow(1 + fees, numberInstallments) - 1;
|
|
56
|
+
|
|
57
|
+
installmentPrice = cashPrice * (numerator / denominator);
|
|
58
|
+
totalPrice = numberInstallments * installmentPrice;
|
|
14
59
|
|
|
15
60
|
return {
|
|
16
|
-
|
|
17
|
-
|
|
61
|
+
totalPrice: +totalPrice.toFixed(2),
|
|
62
|
+
installmentPrice: +installmentPrice.toFixed(2),
|
|
18
63
|
};
|
|
19
|
-
}
|
|
64
|
+
};
|
|
20
65
|
|
|
21
66
|
export { calculateCardInstallment };
|
|
@@ -1,7 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { MaskSensitiveDataFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Masks sensitive data in a JSON string by replacing the values of specified keys with "****".
|
|
5
|
+
*
|
|
6
|
+
* @param jsonString - The JSON string to be processed.
|
|
7
|
+
* @param sensitiveKeys - An array of keys whose values should be masked. Defaults to `["password", "confirmPassword", "creditCard"]`.
|
|
8
|
+
* @returns A JSON string with sensitive data masked. If the input is not a valid JSON string, it returns the original string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const jsonString = JSON.stringify({
|
|
13
|
+
* username: "user123",
|
|
14
|
+
* password: "secret",
|
|
15
|
+
* profile: {
|
|
16
|
+
* creditCard: "1234-5678-9012-3456",
|
|
17
|
+
* },
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const result = maskSensitiveData(jsonString, ["password", "creditCard"]);
|
|
21
|
+
* console.log(result);
|
|
22
|
+
* // Output: '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const maskSensitiveData: MaskSensitiveDataFunction = (
|
|
27
|
+
jsonString,
|
|
3
28
|
sensitiveKeys = ["password", "confirmPassword", "creditCard"]
|
|
4
|
-
)
|
|
29
|
+
) => {
|
|
5
30
|
function maskValue(key: string, value: any): any {
|
|
6
31
|
if (sensitiveKeys.includes(key)) return "****";
|
|
7
32
|
return value;
|
|
@@ -35,6 +60,6 @@ function maskSensitiveData(
|
|
|
35
60
|
} catch (error) {
|
|
36
61
|
return jsonString;
|
|
37
62
|
}
|
|
38
|
-
}
|
|
63
|
+
};
|
|
39
64
|
|
|
40
65
|
export { maskSensitiveData };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes currency symbols from a given formatted string.
|
|
3
|
+
*
|
|
4
|
+
* This function takes a string that may contain currency symbols
|
|
5
|
+
* and removes them using a regular expression. The resulting string
|
|
6
|
+
* is also trimmed of any leading or trailing whitespace.
|
|
7
|
+
*
|
|
8
|
+
* @param formattedValue - The input string containing currency symbols.
|
|
9
|
+
* @returns A string with all currency symbols removed and trimmed of whitespace.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* removeCurrencySymbols("R$13,45"); // "13,45"
|
|
13
|
+
* removeCurrencySymbols("$123.45"); // "123.45"
|
|
14
|
+
* removeCurrencySymbols("€99.99"); // "99.99"
|
|
15
|
+
* removeCurrencySymbols("¥1,000"); // "1,000"
|
|
16
|
+
* removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const removeCurrencySymbols = (formattedValue: string): string => {
|
|
20
|
+
return formattedValue
|
|
21
|
+
.replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "") // Inclui "R$" e outros símbolos comuns
|
|
22
|
+
.trim();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { removeCurrencySymbols };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all non-numeric characters from a given string.
|
|
3
|
+
*
|
|
4
|
+
* @param prop - The input string from which non-numeric characters will be removed.
|
|
5
|
+
* @returns A new string containing only numeric characters from the input.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const result = removeNonNumeric("abc123def456");
|
|
10
|
+
* console.log(result); // Output: "123456"
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function removeNonNumeric(prop: string): string {
|
|
15
|
+
return prop.replace(/[^0-9]/g, "");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { removeNonNumeric };
|
|
@@ -1,5 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { TruncateLargeFieldsFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Truncates large string fields in a JSON string to a specified maximum length.
|
|
5
|
+
*
|
|
6
|
+
* This function parses a JSON string, traverses its structure recursively, and truncates
|
|
7
|
+
* any string fields that exceed the specified maximum length. If a string field is truncated,
|
|
8
|
+
* it is replaced with a message indicating the original length of the field.
|
|
9
|
+
*
|
|
10
|
+
* @param jsonString - The JSON string to process.
|
|
11
|
+
* @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
|
|
12
|
+
* @returns A JSON string with large string fields truncated.
|
|
13
|
+
*
|
|
14
|
+
* @throws {Error} Throws an error if the input is not a valid JSON string.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const json = JSON.stringify({
|
|
19
|
+
* name: "John",
|
|
20
|
+
* description: "A very long description that exceeds the maximum length...",
|
|
21
|
+
* nested: {
|
|
22
|
+
* details: "Another long string that needs truncation."
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const result = truncateLargeFields(json, 50);
|
|
27
|
+
* console.log(result);
|
|
28
|
+
* // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const truncateLargeFields: TruncateLargeFieldsFunction = (
|
|
33
|
+
jsonString,
|
|
34
|
+
maxLength = 1000
|
|
35
|
+
) => {
|
|
36
|
+
function truncateValue(value: any): any {
|
|
3
37
|
if (typeof value === "string" && value.length > maxLength) {
|
|
4
38
|
return `To large information: field as ${value.length} characters`;
|
|
5
39
|
}
|
|
@@ -8,16 +42,16 @@ function truncateLargeFields(jsonString: string, maxLength = 1000): string {
|
|
|
8
42
|
|
|
9
43
|
function recursiveTruncate(obj: any): any {
|
|
10
44
|
if (Array.isArray(obj)) {
|
|
11
|
-
return obj.map(recursiveTruncate);
|
|
45
|
+
return obj.map((item) => recursiveTruncate(item)); // Corrigido para processar elementos do array
|
|
12
46
|
} else if (obj !== null && typeof obj === "object") {
|
|
13
47
|
return Object.fromEntries(
|
|
14
48
|
Object.entries(obj).map(([key, value]) => [
|
|
15
49
|
key,
|
|
16
|
-
|
|
50
|
+
recursiveTruncate(value), // Corrigido para aplicar recursão corretamente
|
|
17
51
|
])
|
|
18
52
|
);
|
|
19
53
|
}
|
|
20
|
-
return obj;
|
|
54
|
+
return truncateValue(obj); // Corrigido para truncar valores diretamente
|
|
21
55
|
}
|
|
22
56
|
|
|
23
57
|
try {
|
|
@@ -27,6 +61,6 @@ function truncateLargeFields(jsonString: string, maxLength = 1000): string {
|
|
|
27
61
|
} catch (error) {
|
|
28
62
|
throw new Error("Invalid JSON string");
|
|
29
63
|
}
|
|
30
|
-
}
|
|
64
|
+
};
|
|
31
65
|
|
|
32
66
|
export { truncateLargeFields };
|