@arkyn/shared 1.4.52 → 2.0.1-beta.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/dist/formats/__test__/formatDate.spec.d.ts +2 -0
- package/dist/formats/__test__/formatDate.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatDate.spec.js +41 -0
- package/dist/formats/__test__/formatJsonObject.spec.d.ts +2 -0
- package/dist/formats/__test__/formatJsonObject.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatJsonObject.spec.js +76 -0
- package/dist/formats/__test__/formatJsonString.spec.d.ts +2 -0
- package/dist/formats/__test__/formatJsonString.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatJsonString.spec.js +71 -0
- package/dist/formats/__test__/formatToCep.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToCep.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToCep.spec.js +30 -0
- package/dist/formats/__test__/formatToCnpj.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToCnpj.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToCnpj.spec.js +27 -0
- package/dist/formats/__test__/formatToCpfCnpj.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToCpfCnpj.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToCpfCnpj.spec.js +32 -0
- package/dist/formats/__test__/formatToCurrency.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToCurrency.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToCurrency.spec.js +39 -0
- package/dist/formats/__test__/formatToEllipsis.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToEllipsis.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToEllipsis.spec.js +36 -0
- package/dist/formats/__test__/formatToHiddenDigits.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToHiddenDigits.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToHiddenDigits.spec.js +47 -0
- package/dist/formats/__test__/formatToPhone.spec.d.ts +2 -0
- package/dist/formats/__test__/formatToPhone.spec.d.ts.map +1 -0
- package/dist/formats/__test__/formatToPhone.spec.js +38 -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/__test__/generateColorByString.spec.d.ts +2 -0
- package/dist/generators/__test__/generateColorByString.spec.d.ts.map +1 -0
- package/dist/generators/__test__/generateColorByString.spec.js +31 -0
- package/dist/generators/__test__/generateId.spec.d.ts +2 -0
- package/dist/generators/__test__/generateId.spec.d.ts.map +1 -0
- package/dist/generators/__test__/generateId.spec.js +30 -0
- package/dist/generators/__test__/generateSlug.spec.d.ts +2 -0
- package/dist/generators/__test__/generateSlug.spec.d.ts.map +1 -0
- package/dist/generators/__test__/generateSlug.spec.js +38 -0
- 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/{validations/regex.d.ts → regex/index.d.ts} +1 -1
- package/dist/regex/index.d.ts.map +1 -0
- package/dist/services/__test__/calculateCardInstallment.spec.d.ts +2 -0
- package/dist/services/__test__/calculateCardInstallment.spec.d.ts.map +1 -0
- package/dist/services/__test__/calculateCardInstallment.spec.js +71 -0
- package/dist/services/__test__/maskSensitiveData.spec.d.ts +2 -0
- package/dist/services/__test__/maskSensitiveData.spec.d.ts.map +1 -0
- package/dist/services/__test__/maskSensitiveData.spec.js +79 -0
- package/dist/services/__test__/removeCurrencySymbols.spec.d.ts +2 -0
- package/dist/services/__test__/removeCurrencySymbols.spec.d.ts.map +1 -0
- package/dist/services/__test__/removeCurrencySymbols.spec.js +34 -0
- package/dist/services/__test__/removeNonNumeric.spec.d.ts +2 -0
- package/dist/services/__test__/removeNonNumeric.spec.d.ts.map +1 -0
- package/dist/services/__test__/removeNonNumeric.spec.js +27 -0
- package/dist/services/__test__/truncateLargeFields.spec.d.ts +2 -0
- package/dist/services/__test__/truncateLargeFields.spec.d.ts.map +1 -0
- package/dist/services/__test__/truncateLargeFields.spec.js +65 -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/__test__/validateCpf.spec.d.ts +2 -0
- package/dist/validations/__test__/validateCpf.spec.d.ts.map +1 -0
- package/dist/validations/__test__/validateCpf.spec.js +31 -0
- package/dist/validations/__test__/validateDate.spec.d.ts +2 -0
- package/dist/validations/__test__/validateDate.spec.d.ts.map +1 -0
- package/dist/validations/__test__/validateDate.spec.js +44 -0
- package/dist/validations/__test__/validatePhone.spec.d.ts +2 -0
- package/dist/validations/__test__/validatePhone.spec.d.ts.map +1 -0
- package/dist/validations/__test__/validatePhone.spec.js +32 -0
- 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/__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/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.map +0 -1
- 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/validateCPF.ts +0 -38
- /package/dist/{validations/regex.js → regex/index.js} +0 -0
- /package/src/{validations/regex.ts → regex/index.ts} +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { countryCurrencies } from "@arkyn/templates";
|
|
2
|
+
import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
|
|
3
|
+
/**
|
|
4
|
+
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The numeric value to be formatted.
|
|
7
|
+
* @param currency - The currency code used to determine the formatting style.
|
|
8
|
+
* @param config - Optional configuration object.
|
|
9
|
+
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
10
|
+
*
|
|
11
|
+
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
16
|
+
* console.log(formatted); // "$1,234.56"
|
|
17
|
+
*
|
|
18
|
+
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
19
|
+
* console.log(withoutPrefix); // "1,234.56"
|
|
20
|
+
*
|
|
21
|
+
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
22
|
+
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
23
|
+
*
|
|
24
|
+
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
25
|
+
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const formatToCurrency = (value, currency, config = { showPrefix: true }) => {
|
|
29
|
+
if (!countryCurrencies[currency]) {
|
|
30
|
+
throw new Error("Unsupported currency code");
|
|
31
|
+
}
|
|
32
|
+
const { countryCurrency, countryLanguage } = countryCurrencies[currency];
|
|
33
|
+
const format = new Intl.NumberFormat(countryLanguage, {
|
|
34
|
+
style: "currency",
|
|
35
|
+
currency: countryCurrency,
|
|
36
|
+
}).format(value);
|
|
37
|
+
return config.showPrefix
|
|
38
|
+
? format.replace(/\s/g, " ")
|
|
39
|
+
: removeCurrencySymbols(format).replace(/\s/g, " ");
|
|
40
|
+
};
|
|
41
|
+
export { formatToCurrency };
|
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FormatToEllipsisFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Truncates a given text to a specified maximum length and appends an ellipsis ("...")
|
|
4
|
+
* if the text exceeds the maximum length.
|
|
5
|
+
*
|
|
6
|
+
* @param text - The input string to be truncated.
|
|
7
|
+
* @param maxLength - The maximum allowed length of the string before truncation.
|
|
8
|
+
* @returns The truncated string with an ellipsis if the input exceeds the maximum length,
|
|
9
|
+
* or the original string if it does not.
|
|
10
|
+
* @example
|
|
11
|
+
* const result = formatToEllipsis("Hello, world!", 5);
|
|
12
|
+
* console.log(result); // Output: "Hello..."
|
|
13
|
+
*/
|
|
14
|
+
declare const formatToEllipsis: FormatToEllipsisFunction;
|
|
2
15
|
export { formatToEllipsis };
|
|
3
16
|
//# sourceMappingURL=formatToEllipsis.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatToEllipsis.d.ts","sourceRoot":"","sources":["../../src/formats/formatToEllipsis.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"formatToEllipsis.d.ts","sourceRoot":"","sources":["../../src/formats/formatToEllipsis.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;GAWG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a given text to a specified maximum length and appends an ellipsis ("...")
|
|
3
|
+
* if the text exceeds the maximum length.
|
|
4
|
+
*
|
|
5
|
+
* @param text - The input string to be truncated.
|
|
6
|
+
* @param maxLength - The maximum allowed length of the string before truncation.
|
|
7
|
+
* @returns The truncated string with an ellipsis if the input exceeds the maximum length,
|
|
8
|
+
* or the original string if it does not.
|
|
9
|
+
* @example
|
|
10
|
+
* const result = formatToEllipsis("Hello, world!", 5);
|
|
11
|
+
* console.log(result); // Output: "Hello..."
|
|
12
|
+
*/
|
|
13
|
+
const formatToEllipsis = (text, maxLength) => {
|
|
14
|
+
if (text.length > maxLength) {
|
|
15
|
+
let trimmedText = text.substring(0, maxLength).trimEnd();
|
|
16
|
+
trimmedText = trimmedText.replace(/[.,!?;:]$/, "");
|
|
17
|
+
return `${trimmedText}...`;
|
|
18
|
+
}
|
|
19
|
+
return text;
|
|
20
|
+
};
|
|
4
21
|
export { formatToEllipsis };
|
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { FormatToHiddenDigitsFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a string by hiding specific digits within a given range.
|
|
4
|
+
*
|
|
5
|
+
* This function takes a string input and replaces digits within a specified range
|
|
6
|
+
* with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The input string to be formatted.
|
|
9
|
+
* @param options - Configuration options for formatting.
|
|
10
|
+
* @param options.range - The range of digits to hide. It can be:
|
|
11
|
+
* - A single number (e.g., `3`), which hides the first `n` digits if positive,
|
|
12
|
+
* or the last `n` digits if negative.
|
|
13
|
+
* - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
|
|
14
|
+
* - Defaults to `3`, hiding the first three digits.
|
|
15
|
+
* @param options.hider - The character used to hide digits. Defaults to `"*"`.
|
|
16
|
+
*
|
|
17
|
+
* @returns The formatted string with specified digits hidden.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { formatToHiddenDigits } from "./formatToHiddenDigits";
|
|
22
|
+
*
|
|
23
|
+
* formatToHiddenDigits("123-456-7890", { range: 3 });
|
|
24
|
+
* // Output: "***-456-7890"
|
|
25
|
+
*
|
|
26
|
+
* formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
|
|
27
|
+
* // Output: "123-###-7890"
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare const formatToHiddenDigits: FormatToHiddenDigitsFunction;
|
|
3
31
|
export { formatToHiddenDigits };
|
|
4
32
|
//# sourceMappingURL=formatToHiddenDigits.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatToHiddenDigits.d.ts","sourceRoot":"","sources":["../../src/formats/formatToHiddenDigits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"formatToHiddenDigits.d.ts","sourceRoot":"","sources":["../../src/formats/formatToHiddenDigits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,cAAc,CAAC;AA+CjE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,QAAA,MAAM,oBAAoB,EAAE,4BAU3B,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -1,6 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const DIGIT = /^\d$/;
|
|
2
|
+
const parseToCharacters = (value) => {
|
|
3
|
+
let digits = 0;
|
|
4
|
+
const children = value
|
|
5
|
+
.split("")
|
|
6
|
+
.map((character) => {
|
|
7
|
+
if (DIGIT.test(character))
|
|
8
|
+
return { character, kind: "digit", digit: ++digits };
|
|
9
|
+
return { character, kind: "other" };
|
|
10
|
+
});
|
|
11
|
+
return { digits, children, kind: "root" };
|
|
12
|
+
};
|
|
13
|
+
const normalizeRange = (range, limit) => {
|
|
14
|
+
if (Array.isArray(range))
|
|
15
|
+
return range;
|
|
16
|
+
if (range >= 0)
|
|
17
|
+
return [0, range];
|
|
18
|
+
return [limit + 1 - Math.abs(range), limit];
|
|
19
|
+
};
|
|
20
|
+
const within = (range, value) => value >= range[0] && value <= range[1];
|
|
21
|
+
/**
|
|
22
|
+
* Formats a string by hiding specific digits within a given range.
|
|
23
|
+
*
|
|
24
|
+
* This function takes a string input and replaces digits within a specified range
|
|
25
|
+
* with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The input string to be formatted.
|
|
28
|
+
* @param options - Configuration options for formatting.
|
|
29
|
+
* @param options.range - The range of digits to hide. It can be:
|
|
30
|
+
* - A single number (e.g., `3`), which hides the first `n` digits if positive,
|
|
31
|
+
* or the last `n` digits if negative.
|
|
32
|
+
* - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
|
|
33
|
+
* - Defaults to `3`, hiding the first three digits.
|
|
34
|
+
* @param options.hider - The character used to hide digits. Defaults to `"*"`.
|
|
35
|
+
*
|
|
36
|
+
* @returns The formatted string with specified digits hidden.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import { formatToHiddenDigits } from "./formatToHiddenDigits";
|
|
41
|
+
*
|
|
42
|
+
* formatToHiddenDigits("123-456-7890", { range: 3 });
|
|
43
|
+
* // Output: "***-456-7890"
|
|
44
|
+
*
|
|
45
|
+
* formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
|
|
46
|
+
* // Output: "123-###-7890"
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
const formatToHiddenDigits = (value, options) => {
|
|
4
50
|
const characters = parseToCharacters(value);
|
|
5
51
|
const range = normalizeRange(options.range ?? 3, characters.digits);
|
|
6
52
|
return characters.children
|
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FormatToPhoneFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a phone number string based on the provided country code and optional prefix.
|
|
4
|
+
*
|
|
5
|
+
* The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
|
|
6
|
+
* The function determines the appropriate formatting mask based on the country and applies it to the phone number.
|
|
7
|
+
*
|
|
8
|
+
* @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
|
|
9
|
+
* Example formats:
|
|
10
|
+
* - "+55-11 912345678"
|
|
11
|
+
* - "+1 1234567890"
|
|
12
|
+
*
|
|
13
|
+
* @returns The formatted phone number string based on the country's formatting rules.
|
|
14
|
+
*
|
|
15
|
+
* @throws {Error} If the input phone number does not match the expected format.
|
|
16
|
+
* @throws {Error} If the country code or phone number is missing from the input string.
|
|
17
|
+
* @throws {Error} If the provided country code and prefix combination is invalid.
|
|
18
|
+
* @throws {Error} If the provided country code is invalid.
|
|
19
|
+
* @throws {Error} If the provided country code has a prefix but none is supplied in the input.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { formatToPhone } from "./formatToPhone";
|
|
24
|
+
*
|
|
25
|
+
* const formattedPhone1 = formatToPhone("+55 11912345678");
|
|
26
|
+
* console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
|
|
27
|
+
*
|
|
28
|
+
* const formattedPhone2 = formatToPhone("+1-123 4567890");
|
|
29
|
+
* console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare const formatToPhone: FormatToPhoneFunction;
|
|
2
33
|
export { formatToPhone };
|
|
3
34
|
//# sourceMappingURL=formatToPhone.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAe,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAqGvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAwBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -1,9 +1,131 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { countries } from "@arkyn/templates";
|
|
2
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
+
function getMask(value) {
|
|
4
|
+
const mask = value.length > 10 ? "NINE" : "EIGTH";
|
|
5
|
+
return mask;
|
|
6
|
+
}
|
|
7
|
+
const TYPES = {
|
|
8
|
+
EIGTH: "(99) 9999-9999",
|
|
9
|
+
NINE: "(99) 99999-9999",
|
|
10
|
+
};
|
|
11
|
+
const MAX_LENGTH = removeNonNumeric(TYPES.NINE).length;
|
|
12
|
+
function applyMask(value, maskPattern) {
|
|
13
|
+
let result = "";
|
|
14
|
+
let digitIndex = 0;
|
|
15
|
+
for (let i = 0; i < maskPattern.length; i++) {
|
|
16
|
+
if (maskPattern[i] === "9") {
|
|
17
|
+
if (digitIndex < value.length) {
|
|
18
|
+
result += value[digitIndex];
|
|
19
|
+
digitIndex++;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
if (digitIndex < value.length) {
|
|
27
|
+
result += maskPattern[i];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
function formatPhoneNumber(phoneNumber, country) {
|
|
37
|
+
if (country.code === "+55") {
|
|
38
|
+
let value = removeNonNumeric(phoneNumber);
|
|
39
|
+
const mask = getMask(value);
|
|
40
|
+
let nextLength = value.length;
|
|
41
|
+
if (nextLength > MAX_LENGTH)
|
|
42
|
+
return value;
|
|
43
|
+
value = applyMask(value, TYPES[mask]);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
const mask = country.mask;
|
|
47
|
+
let formattedNumber = mask;
|
|
48
|
+
if (country.prefix) {
|
|
49
|
+
const prefixRegex = /\$+/g;
|
|
50
|
+
formattedNumber = formattedNumber.replace(prefixRegex, country.prefix);
|
|
51
|
+
}
|
|
52
|
+
for (let i = 0, j = 0; i < formattedNumber.length && j < phoneNumber.length; i++) {
|
|
53
|
+
if (formattedNumber[i] === "_") {
|
|
54
|
+
formattedNumber =
|
|
55
|
+
formattedNumber.substring(0, i) +
|
|
56
|
+
phoneNumber[j] +
|
|
57
|
+
formattedNumber.substring(i + 1);
|
|
58
|
+
j++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return formattedNumber;
|
|
62
|
+
}
|
|
63
|
+
function getCountryWithPrefixCode(countryCode, prefix) {
|
|
64
|
+
const country = countries.find((country) => country.code === countryCode && country.prefix === prefix);
|
|
65
|
+
if (!country)
|
|
66
|
+
throw new Error("Invalid country code or prefix");
|
|
67
|
+
if (country.prefix !== prefix) {
|
|
68
|
+
throw new Error("Invalid country code or prefix");
|
|
69
|
+
}
|
|
70
|
+
if (!country.prefix) {
|
|
71
|
+
throw new Error("Invalid country code or prefix");
|
|
6
72
|
}
|
|
7
|
-
return
|
|
73
|
+
return country;
|
|
74
|
+
}
|
|
75
|
+
function getCountryWithoutPrefixCode(countryCode) {
|
|
76
|
+
const country = countries.find((country) => country.code === countryCode);
|
|
77
|
+
if (!country)
|
|
78
|
+
throw new Error("Invalid country code");
|
|
79
|
+
if (country.prefix)
|
|
80
|
+
throw new Error("Invalid country code");
|
|
81
|
+
return country;
|
|
8
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Formats a phone number string based on the provided country code and optional prefix.
|
|
85
|
+
*
|
|
86
|
+
* The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
|
|
87
|
+
* The function determines the appropriate formatting mask based on the country and applies it to the phone number.
|
|
88
|
+
*
|
|
89
|
+
* @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
|
|
90
|
+
* Example formats:
|
|
91
|
+
* - "+55-11 912345678"
|
|
92
|
+
* - "+1 1234567890"
|
|
93
|
+
*
|
|
94
|
+
* @returns The formatted phone number string based on the country's formatting rules.
|
|
95
|
+
*
|
|
96
|
+
* @throws {Error} If the input phone number does not match the expected format.
|
|
97
|
+
* @throws {Error} If the country code or phone number is missing from the input string.
|
|
98
|
+
* @throws {Error} If the provided country code and prefix combination is invalid.
|
|
99
|
+
* @throws {Error} If the provided country code is invalid.
|
|
100
|
+
* @throws {Error} If the provided country code has a prefix but none is supplied in the input.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* import { formatToPhone } from "./formatToPhone";
|
|
105
|
+
*
|
|
106
|
+
* const formattedPhone1 = formatToPhone("+55 11912345678");
|
|
107
|
+
* console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
|
|
108
|
+
*
|
|
109
|
+
* const formattedPhone2 = formatToPhone("+1-123 4567890");
|
|
110
|
+
* console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
const formatToPhone = (prop) => {
|
|
114
|
+
const phoneRegex = /^\+\d{1,4}(-\d{1,4})? \d+$/;
|
|
115
|
+
if (!phoneRegex.test(prop)) {
|
|
116
|
+
throw new Error("Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>");
|
|
117
|
+
}
|
|
118
|
+
const countryCode = prop.split(" ")[0].split("-")[0];
|
|
119
|
+
const prefixCode = prop.split(" ")[0].split("-")[1];
|
|
120
|
+
const phoneNumber = prop.split(" ")[1];
|
|
121
|
+
if (!countryCode || !phoneNumber) {
|
|
122
|
+
throw new Error("Invalid phone number format");
|
|
123
|
+
}
|
|
124
|
+
if (prefixCode) {
|
|
125
|
+
const country = getCountryWithPrefixCode(countryCode, prefixCode);
|
|
126
|
+
return formatPhoneNumber(phoneNumber, country);
|
|
127
|
+
}
|
|
128
|
+
const country = getCountryWithoutPrefixCode(countryCode);
|
|
129
|
+
return formatPhoneNumber(phoneNumber, country);
|
|
130
|
+
};
|
|
9
131
|
export { formatToPhone };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateColorByString.spec.d.ts","sourceRoot":"","sources":["../../../src/generators/__test__/generateColorByString.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateColorByString } from "../generateColorByString";
|
|
3
|
+
describe("generateColorByString", () => {
|
|
4
|
+
it("should generate a consistent hex color for the same input string", () => {
|
|
5
|
+
const color1 = generateColorByString("example");
|
|
6
|
+
const color2 = generateColorByString("example");
|
|
7
|
+
expect(color1).toBe(color2);
|
|
8
|
+
});
|
|
9
|
+
it("should generate different hex colors for different input strings", () => {
|
|
10
|
+
const color1 = generateColorByString("example1");
|
|
11
|
+
const color2 = generateColorByString("example2");
|
|
12
|
+
expect(color1).not.toBe(color2);
|
|
13
|
+
});
|
|
14
|
+
it("should return a valid hex color code", () => {
|
|
15
|
+
const color = generateColorByString("test");
|
|
16
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
17
|
+
});
|
|
18
|
+
it("should handle empty strings and return a valid hex color", () => {
|
|
19
|
+
const color = generateColorByString("");
|
|
20
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
21
|
+
});
|
|
22
|
+
it("should handle strings with special characters", () => {
|
|
23
|
+
const color = generateColorByString("!@#$%^&*()");
|
|
24
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
25
|
+
});
|
|
26
|
+
it("should handle long strings and return a valid hex color", () => {
|
|
27
|
+
const longString = "a".repeat(1000);
|
|
28
|
+
const color = generateColorByString(longString);
|
|
29
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateId.spec.d.ts","sourceRoot":"","sources":["../../../src/generators/__test__/generateId.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateId } from "../generateId";
|
|
3
|
+
describe("generateId", () => {
|
|
4
|
+
it("should generate a valid UUID v4 as text", () => {
|
|
5
|
+
const id = generateId("text", "v4");
|
|
6
|
+
expect(typeof id).toBe("string");
|
|
7
|
+
expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
|
|
8
|
+
});
|
|
9
|
+
it("should generate a valid UUID v4 as binary", () => {
|
|
10
|
+
const id = generateId("binary", "v4");
|
|
11
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
12
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
13
|
+
});
|
|
14
|
+
it("should generate a valid UUID v7 as text", () => {
|
|
15
|
+
const id = generateId("text", "v7");
|
|
16
|
+
expect(typeof id).toBe("string");
|
|
17
|
+
expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
|
|
18
|
+
});
|
|
19
|
+
it("should generate a valid UUID v7 as binary", () => {
|
|
20
|
+
const id = generateId("binary", "v7");
|
|
21
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
22
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
23
|
+
});
|
|
24
|
+
it("should throw an error for invalid type", () => {
|
|
25
|
+
expect(() => generateId("invalid", "v4")).toThrow("Invalid type or format");
|
|
26
|
+
});
|
|
27
|
+
it("should throw an error for invalid format", () => {
|
|
28
|
+
expect(() => generateId("text", "v8")).toThrow("Invalid type or format");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateSlug.spec.d.ts","sourceRoot":"","sources":["../../../src/generators/__test__/generateSlug.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateSlug } from "../generateSlug";
|
|
3
|
+
describe("generateSlug", () => {
|
|
4
|
+
it("should convert a string to a URL-friendly slug", () => {
|
|
5
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
6
|
+
expect(generateSlug("This is a Test")).toBe("this-is-a-test");
|
|
7
|
+
});
|
|
8
|
+
it("should remove diacritical marks (accents)", () => {
|
|
9
|
+
expect(generateSlug("Olá Mundo")).toBe("ola-mundo");
|
|
10
|
+
expect(generateSlug("Crème brûlée")).toBe("creme-brulee");
|
|
11
|
+
});
|
|
12
|
+
it("should remove non-alphanumeric characters except spaces and hyphens", () => {
|
|
13
|
+
expect(generateSlug("Hello, World!")).toBe("hello-world");
|
|
14
|
+
expect(generateSlug("C# Programming")).toBe("c-programming");
|
|
15
|
+
});
|
|
16
|
+
it("should collapse multiple spaces into a single hyphen", () => {
|
|
17
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
18
|
+
});
|
|
19
|
+
it("should collapse multiple consecutive hyphens into a single hyphen", () => {
|
|
20
|
+
expect(generateSlug("Hello--World")).toBe("hello-world");
|
|
21
|
+
expect(generateSlug("Hello---World")).toBe("hello-world");
|
|
22
|
+
});
|
|
23
|
+
it("should trim leading and trailing hyphens", () => {
|
|
24
|
+
expect(generateSlug("-Hello World-")).toBe("hello-world");
|
|
25
|
+
expect(generateSlug("--Hello World--")).toBe("hello-world");
|
|
26
|
+
});
|
|
27
|
+
it("should handle empty strings", () => {
|
|
28
|
+
expect(generateSlug("")).toBe("");
|
|
29
|
+
});
|
|
30
|
+
it("should handle strings with only special characters", () => {
|
|
31
|
+
expect(generateSlug("!@#$%^&*()")).toBe("");
|
|
32
|
+
expect(generateSlug("---")).toBe("");
|
|
33
|
+
});
|
|
34
|
+
it("should handle strings with numbers", () => {
|
|
35
|
+
expect(generateSlug("Version 2.0")).toBe("version-20");
|
|
36
|
+
expect(generateSlug("123 Testing")).toBe("123-testing");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import type { GenerateColorByStringFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a hexadecimal color code based on the input string.
|
|
4
|
+
* The function creates a hash from the string and uses it to calculate
|
|
5
|
+
* RGB values, which are then converted to a hexadecimal color code.
|
|
6
|
+
*
|
|
7
|
+
* @param prop - The input string used to generate the color.
|
|
8
|
+
* @returns A hexadecimal color code (e.g., "#a1b2c3") derived from the input string.
|
|
9
|
+
* @example
|
|
10
|
+
* const color = generateColorByString("example");
|
|
11
|
+
* console.log(color); // Outputs a consistent hex color like "#5e8f9a"
|
|
12
|
+
*/
|
|
13
|
+
declare const generateColorByString: GenerateColorByStringFunction;
|
|
2
14
|
export { generateColorByString };
|
|
3
15
|
//# sourceMappingURL=generateColorByString.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateColorByString.d.ts","sourceRoot":"","sources":["../../src/generators/generateColorByString.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"generateColorByString.d.ts","sourceRoot":"","sources":["../../src/generators/generateColorByString.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAC;AAElE;;;;;;;;;;GAUG;AAEH,QAAA,MAAM,qBAAqB,EAAE,6BAgB5B,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generates a hexadecimal color code based on the input string.
|
|
3
|
+
* The function creates a hash from the string and uses it to calculate
|
|
4
|
+
* RGB values, which are then converted to a hexadecimal color code.
|
|
5
|
+
*
|
|
6
|
+
* @param prop - The input string used to generate the color.
|
|
7
|
+
* @returns A hexadecimal color code (e.g., "#a1b2c3") derived from the input string.
|
|
8
|
+
* @example
|
|
9
|
+
* const color = generateColorByString("example");
|
|
10
|
+
* console.log(color); // Outputs a consistent hex color like "#5e8f9a"
|
|
11
|
+
*/
|
|
12
|
+
const generateColorByString = (prop) => {
|
|
2
13
|
var hash = 0;
|
|
3
|
-
for (var i = 0; i <
|
|
4
|
-
hash =
|
|
14
|
+
for (var i = 0; i < prop.length; i++) {
|
|
15
|
+
hash = prop.charCodeAt(i) + ((hash << 5) - hash);
|
|
5
16
|
}
|
|
6
17
|
var red = (hash & 0xff0000) >> 16;
|
|
7
18
|
var green = (hash & 0x00ff00) >> 8;
|
|
@@ -10,5 +21,5 @@ function generateColorByString(string) {
|
|
|
10
21
|
var greenHex = green.toString(16).padStart(2, "0");
|
|
11
22
|
var blueHex = blue.toString(16).padStart(2, "0");
|
|
12
23
|
return "#" + redHex + greenHex + blueHex;
|
|
13
|
-
}
|
|
24
|
+
};
|
|
14
25
|
export { generateColorByString };
|
|
@@ -1,6 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generates a unique identifier (UUID) in the specified format and type.
|
|
3
|
+
*
|
|
4
|
+
* @param type - The desired output type of the UUID. Can be:
|
|
5
|
+
* - `"text"`: Returns the UUID as a string.
|
|
6
|
+
* - `"binary"`: Returns the UUID as a `Uint8Array` in binary format.
|
|
7
|
+
* @param format - The version of the UUID to generate. Can be:
|
|
8
|
+
* - `"v4"`: Generates a random UUID (version 4).
|
|
9
|
+
* - `"v7"`: Generates a time-ordered UUID (version 7).
|
|
10
|
+
* @returns The generated UUID in the specified type and format.
|
|
11
|
+
* - If `type` is `"text"`, a string representation of the UUID is returned.
|
|
12
|
+
* - If `type` is `"binary"`, a `Uint8Array` representation of the UUID is returned.
|
|
13
|
+
* @throws {Error} If an invalid `type` or `format` is provided.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Generate a version 4 UUID as a string
|
|
17
|
+
* const idTextV4 = generateId("text", "v4");
|
|
18
|
+
* console.log(idTextV4); // e.g., "550e8400-e29b-41d4-a716-446655440000"
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Generate a version 7 UUID as binary
|
|
22
|
+
* const idBinaryV7 = generateId("binary", "v7");
|
|
23
|
+
* console.log(idBinaryV7); // Uint8Array([...])
|
|
24
|
+
*/
|
|
25
|
+
declare function generateId(type: "text", format: "v4" | "v7"): string;
|
|
26
|
+
declare function generateId(type: "binary", format: "v4" | "v7"): Uint8Array;
|
|
5
27
|
export { generateId };
|
|
6
28
|
//# sourceMappingURL=generateId.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateId.d.ts","sourceRoot":"","sources":["../../src/generators/generateId.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateId.d.ts","sourceRoot":"","sources":["../../src/generators/generateId.ts"],"names":[],"mappings":"AAuBA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,MAAM,CAAC;AAC/D,iBAAS,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,UAAU,CAAC;AAYrE,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { v4, v7 } from "uuid";
|
|
2
2
|
function hexToBin(hex) {
|
|
3
|
+
hex = hex.replace(/-/g, "");
|
|
3
4
|
const buffer = new Uint8Array(hex.length / 2);
|
|
4
5
|
for (let i = 0; i < hex.length; i += 2) {
|
|
5
6
|
buffer[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
@@ -14,10 +15,15 @@ function uuidV7() {
|
|
|
14
15
|
const uuid = v7();
|
|
15
16
|
return { text: uuid, binary: hexToBin(uuid) };
|
|
16
17
|
}
|
|
17
|
-
function generateId(type) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
function generateId(type, format) {
|
|
19
|
+
if (type === "text" && format === "v4")
|
|
20
|
+
return uuidV4().text;
|
|
21
|
+
if (type === "binary" && format === "v4")
|
|
22
|
+
return uuidV4().binary;
|
|
23
|
+
if (type === "text" && format === "v7")
|
|
24
|
+
return uuidV7().text;
|
|
25
|
+
if (type === "binary" && format === "v7")
|
|
26
|
+
return uuidV7().binary;
|
|
27
|
+
throw new Error("Invalid type or format");
|
|
22
28
|
}
|
|
23
29
|
export { generateId };
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
|
|
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
|
+
declare function generateSlug(prop: string): string;
|
|
2
16
|
export { generateSlug };
|
|
3
17
|
//# sourceMappingURL=generateSlug.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateSlug.d.ts","sourceRoot":"","sources":["../../src/generators/generateSlug.ts"],"names":[],"mappings":"AAAA,iBAAS,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"generateSlug.d.ts","sourceRoot":"","sources":["../../src/generators/generateSlug.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,iBAAS,YAAY,CAAC,IAAI,EAAE,MAAM,UAajC;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|