@arkyn/shared 1.4.52 → 2.0.1-beta.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/package.json +4 -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__/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 +7 -15
- 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__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/validateCpf.ts +64 -0
- package/src/validations/validateDate.ts +33 -11
- package/src/validations/validatePhone.ts +29 -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/formatJsonObject.d.ts +0 -3
- package/dist/formats/formatJsonObject.d.ts.map +0 -1
- package/dist/formats/formatJsonObject.js +0 -49
- package/dist/formats/formatJsonString.d.ts +0 -3
- package/dist/formats/formatJsonString.d.ts.map +0 -1
- package/dist/formats/formatJsonString.js +0 -12
- 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/formats/formatToCep.d.ts +0 -3
- package/dist/formats/formatToCep.d.ts.map +0 -1
- package/dist/formats/formatToCep.js +0 -9
- package/dist/formats/formatToCpfCnpj.d.ts +0 -3
- package/dist/formats/formatToCpfCnpj.d.ts.map +0 -1
- package/dist/formats/formatToCpfCnpj.js +0 -13
- package/dist/formats/formatToEllipsis.d.ts +0 -3
- package/dist/formats/formatToEllipsis.d.ts.map +0 -1
- package/dist/formats/formatToEllipsis.js +0 -4
- package/dist/formats/formatToHiddenDigits.d.ts +0 -4
- package/dist/formats/formatToHiddenDigits.d.ts.map +0 -1
- package/dist/formats/formatToHiddenDigits.js +0 -14
- package/dist/formats/formatToPhone.d.ts +0 -3
- package/dist/formats/formatToPhone.d.ts.map +0 -1
- package/dist/formats/formatToPhone.js +0 -9
- package/dist/generators/generateColorByString.d.ts +0 -3
- package/dist/generators/generateColorByString.d.ts.map +0 -1
- package/dist/generators/generateColorByString.js +0 -14
- package/dist/generators/generateId.d.ts +0 -6
- package/dist/generators/generateId.d.ts.map +0 -1
- package/dist/generators/generateId.js +0 -23
- package/dist/generators/generateSlug.d.ts +0 -3
- package/dist/generators/generateSlug.d.ts.map +0 -1
- package/dist/generators/generateSlug.js +0 -11
- package/dist/index.d.ts +0 -26
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -30
- 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/calculateCardInstallment.d.ts +0 -7
- package/dist/services/calculateCardInstallment.d.ts.map +0 -1
- package/dist/services/calculateCardInstallment.js +0 -14
- package/dist/services/maskSensitiveData.d.ts +0 -3
- package/dist/services/maskSensitiveData.d.ts.map +0 -1
- package/dist/services/maskSensitiveData.js +0 -38
- 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/services/truncateLargeFields.d.ts +0 -3
- package/dist/services/truncateLargeFields.d.ts.map +0 -1
- package/dist/services/truncateLargeFields.js +0 -29
- 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/dist/validations/validateDate.d.ts +0 -8
- package/dist/validations/validateDate.d.ts.map +0 -1
- package/dist/validations/validateDate.js +0 -47
- package/dist/validations/validatePhone.d.ts +0 -3
- package/dist/validations/validatePhone.d.ts.map +0 -1
- package/dist/validations/validatePhone.js +0 -19
- 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/validateCPF.ts +0 -38
- /package/src/{validations/regex.ts → regex/index.ts} +0 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateSlug } from "../generateSlug";
|
|
3
|
+
|
|
4
|
+
describe("generateSlug", () => {
|
|
5
|
+
it("should convert a string to a URL-friendly slug", () => {
|
|
6
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
7
|
+
expect(generateSlug("This is a Test")).toBe("this-is-a-test");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should remove diacritical marks (accents)", () => {
|
|
11
|
+
expect(generateSlug("Olá Mundo")).toBe("ola-mundo");
|
|
12
|
+
expect(generateSlug("Crème brûlée")).toBe("creme-brulee");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should remove non-alphanumeric characters except spaces and hyphens", () => {
|
|
16
|
+
expect(generateSlug("Hello, World!")).toBe("hello-world");
|
|
17
|
+
expect(generateSlug("C# Programming")).toBe("c-programming");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should collapse multiple spaces into a single hyphen", () => {
|
|
21
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should collapse multiple consecutive hyphens into a single hyphen", () => {
|
|
25
|
+
expect(generateSlug("Hello--World")).toBe("hello-world");
|
|
26
|
+
expect(generateSlug("Hello---World")).toBe("hello-world");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should trim leading and trailing hyphens", () => {
|
|
30
|
+
expect(generateSlug("-Hello World-")).toBe("hello-world");
|
|
31
|
+
expect(generateSlug("--Hello World--")).toBe("hello-world");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle empty strings", () => {
|
|
35
|
+
expect(generateSlug("")).toBe("");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should handle strings with only special characters", () => {
|
|
39
|
+
expect(generateSlug("!@#$%^&*()")).toBe("");
|
|
40
|
+
expect(generateSlug("---")).toBe("");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle strings with numbers", () => {
|
|
44
|
+
expect(generateSlug("Version 2.0")).toBe("version-20");
|
|
45
|
+
expect(generateSlug("123 Testing")).toBe("123-testing");
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -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";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { calculateCardInstallment } from "../calculateCardInstallment";
|
|
3
|
+
|
|
4
|
+
describe("calculateCardInstallment", () => {
|
|
5
|
+
it("should calculate the total and installment price with default fees", () => {
|
|
6
|
+
const result = calculateCardInstallment({
|
|
7
|
+
cashPrice: 1000,
|
|
8
|
+
numberInstallments: 12,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
expect(result).toEqual({
|
|
12
|
+
totalPrice: 1241.08,
|
|
13
|
+
installmentPrice: 103.42,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should calculate the total and installment price with custom fees", () => {
|
|
18
|
+
const result = calculateCardInstallment({
|
|
19
|
+
cashPrice: 1000,
|
|
20
|
+
numberInstallments: 12,
|
|
21
|
+
fees: 0.02,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(result).toEqual({
|
|
25
|
+
totalPrice: 1134.72,
|
|
26
|
+
installmentPrice: 94.56,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should calculate correctly for a single installment (no interest)", () => {
|
|
31
|
+
const result = calculateCardInstallment({
|
|
32
|
+
cashPrice: 1000,
|
|
33
|
+
numberInstallments: 1,
|
|
34
|
+
fees: 0,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
totalPrice: 1000,
|
|
39
|
+
installmentPrice: 1000,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should calculate correctly for zero fees", () => {
|
|
44
|
+
const result = calculateCardInstallment({
|
|
45
|
+
cashPrice: 500,
|
|
46
|
+
numberInstallments: 5,
|
|
47
|
+
fees: 0,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(result).toEqual({
|
|
51
|
+
totalPrice: 500,
|
|
52
|
+
installmentPrice: 100,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should handle edge case with zero installments", () => {
|
|
57
|
+
expect(() =>
|
|
58
|
+
calculateCardInstallment({
|
|
59
|
+
cashPrice: 1000,
|
|
60
|
+
numberInstallments: 0,
|
|
61
|
+
})
|
|
62
|
+
).toThrow("Number of installments must be greater than 0");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle edge case with negative fees", () => {
|
|
66
|
+
expect(() =>
|
|
67
|
+
calculateCardInstallment({
|
|
68
|
+
cashPrice: 1000,
|
|
69
|
+
numberInstallments: 12,
|
|
70
|
+
fees: -0.01,
|
|
71
|
+
})
|
|
72
|
+
).toThrow("Fees must be greater than or equal to 0");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should handle large numbers for cash price and installments", () => {
|
|
76
|
+
const result = calculateCardInstallment({
|
|
77
|
+
cashPrice: 1000000,
|
|
78
|
+
numberInstallments: 24,
|
|
79
|
+
fees: 0.01,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(result).toEqual({
|
|
83
|
+
totalPrice: 1129763.33,
|
|
84
|
+
installmentPrice: 47073.47,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { maskSensitiveData } from "../maskSensitiveData";
|
|
3
|
+
|
|
4
|
+
describe("maskSensitiveData", () => {
|
|
5
|
+
it("should mask sensitive keys in a flat JSON object", () => {
|
|
6
|
+
const jsonString = JSON.stringify({
|
|
7
|
+
username: "user123",
|
|
8
|
+
password: "secret",
|
|
9
|
+
email: "user@example.com",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
13
|
+
expect(result).toBe(
|
|
14
|
+
'{"username":"user123","password":"****","email":"user@example.com"}'
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should mask sensitive keys in nested objects", () => {
|
|
19
|
+
const jsonString = JSON.stringify({
|
|
20
|
+
profile: {
|
|
21
|
+
username: "user123",
|
|
22
|
+
creditCard: "1234-5678-9012-3456",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const result = maskSensitiveData(jsonString, ["creditCard"]);
|
|
27
|
+
expect(result).toBe(
|
|
28
|
+
'{"profile":{"username":"user123","creditCard":"****"}}'
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should mask sensitive keys in arrays", () => {
|
|
33
|
+
const jsonString = JSON.stringify({
|
|
34
|
+
users: [
|
|
35
|
+
{ username: "user1", password: "pass1" },
|
|
36
|
+
{ username: "user2", password: "pass2" },
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
41
|
+
expect(result).toBe(
|
|
42
|
+
'{"users":[{"username":"user1","password":"****"},{"username":"user2","password":"****"}]}'
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should handle non-JSON strings gracefully", () => {
|
|
47
|
+
const invalidJson = "not a json string";
|
|
48
|
+
const result = maskSensitiveData(invalidJson, ["password"]);
|
|
49
|
+
expect(result).toBe("not a json string");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should handle empty JSON objects", () => {
|
|
53
|
+
const jsonString = JSON.stringify({});
|
|
54
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
55
|
+
expect(result).toBe("{}");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should handle empty arrays", () => {
|
|
59
|
+
const jsonString = JSON.stringify([]);
|
|
60
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
61
|
+
expect(result).toBe("[]");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should use default sensitive keys if none are provided", () => {
|
|
65
|
+
const jsonString = JSON.stringify({
|
|
66
|
+
username: "user123",
|
|
67
|
+
password: "secret",
|
|
68
|
+
confirmPassword: "secret",
|
|
69
|
+
creditCard: "1234-5678-9012-3456",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const result = maskSensitiveData(jsonString);
|
|
73
|
+
expect(result).toBe(
|
|
74
|
+
'{"username":"user123","password":"****","confirmPassword":"****","creditCard":"****"}'
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should not mask keys that are not in the sensitive keys list", () => {
|
|
79
|
+
const jsonString = JSON.stringify({
|
|
80
|
+
username: "user123",
|
|
81
|
+
email: "user@example.com",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
85
|
+
expect(result).toBe('{"username":"user123","email":"user@example.com"}');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should handle deeply nested objects", () => {
|
|
89
|
+
const jsonString = JSON.stringify({
|
|
90
|
+
level1: {
|
|
91
|
+
level2: {
|
|
92
|
+
level3: {
|
|
93
|
+
password: "secret",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
100
|
+
expect(result).toBe('{"level1":{"level2":{"level3":{"password":"****"}}}}');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { removeCurrencySymbols } from "../removeCurrencySymbols";
|
|
3
|
+
|
|
4
|
+
describe("removeCurrencySymbols", () => {
|
|
5
|
+
it("should remove common currency symbols from the string", () => {
|
|
6
|
+
expect(removeCurrencySymbols("R$13,45")).toBe("13,45");
|
|
7
|
+
expect(removeCurrencySymbols("$123.45")).toBe("123.45");
|
|
8
|
+
expect(removeCurrencySymbols("€99.99")).toBe("99.99");
|
|
9
|
+
expect(removeCurrencySymbols("¥1,000")).toBe("1,000");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should handle strings without currency symbols", () => {
|
|
13
|
+
expect(removeCurrencySymbols("123.45")).toBe("123.45");
|
|
14
|
+
expect(removeCurrencySymbols("1,000")).toBe("1,000");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should handle strings with multiple currency symbols", () => {
|
|
18
|
+
expect(removeCurrencySymbols("R$ $123.45")).toBe("123.45");
|
|
19
|
+
expect(removeCurrencySymbols("€¥99,99")).toBe("99,99");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should handle strings with leading and trailing whitespace", () => {
|
|
23
|
+
expect(removeCurrencySymbols(" $123.45 ")).toBe("123.45");
|
|
24
|
+
expect(removeCurrencySymbols("\t€99.99\n")).toBe("99.99");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should return an empty string if the input is only currency symbols", () => {
|
|
28
|
+
expect(removeCurrencySymbols("R$")).toBe("");
|
|
29
|
+
expect(removeCurrencySymbols("$")).toBe("");
|
|
30
|
+
expect(removeCurrencySymbols("€")).toBe("");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should handle an empty string input", () => {
|
|
34
|
+
expect(removeCurrencySymbols("")).toBe("");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should handle strings with special characters but no currency symbols", () => {
|
|
38
|
+
expect(removeCurrencySymbols("123-45")).toBe("123-45");
|
|
39
|
+
expect(removeCurrencySymbols("1,000.00")).toBe("1,000.00");
|
|
40
|
+
});
|
|
41
|
+
});
|