@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.
Files changed (139) hide show
  1. package/dist/formats/formatDate.d.ts +43 -0
  2. package/dist/formats/formatDate.d.ts.map +1 -0
  3. package/dist/formats/formatDate.js +77 -0
  4. package/dist/formats/formatJsonObject.d.ts +31 -1
  5. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  6. package/dist/formats/formatJsonObject.js +67 -26
  7. package/dist/formats/formatJsonString.d.ts +37 -1
  8. package/dist/formats/formatJsonString.d.ts.map +1 -1
  9. package/dist/formats/formatJsonString.js +38 -4
  10. package/dist/formats/formatToCep.d.ts +27 -1
  11. package/dist/formats/formatToCep.d.ts.map +1 -1
  12. package/dist/formats/formatToCep.js +31 -6
  13. package/dist/formats/formatToCnpj.d.ts +30 -0
  14. package/dist/formats/formatToCnpj.d.ts.map +1 -0
  15. package/dist/formats/formatToCnpj.js +35 -0
  16. package/dist/formats/formatToCpf.d.ts +30 -0
  17. package/dist/formats/formatToCpf.d.ts.map +1 -0
  18. package/dist/formats/formatToCpf.js +35 -0
  19. package/dist/formats/formatToCpfCnpj.d.ts +22 -1
  20. package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
  21. package/dist/formats/formatToCpfCnpj.js +27 -7
  22. package/dist/formats/formatToCurrency.d.ts +29 -0
  23. package/dist/formats/formatToCurrency.d.ts.map +1 -0
  24. package/dist/formats/formatToCurrency.js +41 -0
  25. package/dist/formats/formatToEllipsis.d.ts +14 -1
  26. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  27. package/dist/formats/formatToEllipsis.js +20 -3
  28. package/dist/formats/formatToHiddenDigits.d.ts +30 -2
  29. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  30. package/dist/formats/formatToHiddenDigits.js +49 -3
  31. package/dist/formats/formatToPhone.d.ts +32 -1
  32. package/dist/formats/formatToPhone.d.ts.map +1 -1
  33. package/dist/formats/formatToPhone.js +128 -6
  34. package/dist/generators/generateColorByString.d.ts +13 -1
  35. package/dist/generators/generateColorByString.d.ts.map +1 -1
  36. package/dist/generators/generateColorByString.js +15 -4
  37. package/dist/generators/generateId.d.ts +26 -4
  38. package/dist/generators/generateId.d.ts.map +1 -1
  39. package/dist/generators/generateId.js +11 -5
  40. package/dist/generators/generateSlug.d.ts +15 -1
  41. package/dist/generators/generateSlug.d.ts.map +1 -1
  42. package/dist/generators/generateSlug.js +16 -2
  43. package/dist/index.d.ts +7 -10
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +7 -15
  46. package/dist/regex/index.d.ts +6 -0
  47. package/dist/regex/index.d.ts.map +1 -0
  48. package/dist/regex/index.js +4 -0
  49. package/dist/services/calculateCardInstallment.d.ts +31 -5
  50. package/dist/services/calculateCardInstallment.d.ts.map +1 -1
  51. package/dist/services/calculateCardInstallment.js +52 -11
  52. package/dist/services/maskSensitiveData.d.ts +24 -1
  53. package/dist/services/maskSensitiveData.d.ts.map +1 -1
  54. package/dist/services/maskSensitiveData.js +24 -2
  55. package/dist/services/removeCurrencySymbols.d.ts +20 -0
  56. package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
  57. package/dist/services/removeCurrencySymbols.js +23 -0
  58. package/dist/services/removeNonNumeric.d.ts +15 -0
  59. package/dist/services/removeNonNumeric.d.ts.map +1 -0
  60. package/dist/services/removeNonNumeric.js +16 -0
  61. package/dist/services/truncateLargeFields.d.ts +30 -1
  62. package/dist/services/truncateLargeFields.d.ts.map +1 -1
  63. package/dist/services/truncateLargeFields.js +34 -6
  64. package/dist/validations/validateCpf.d.ts +24 -0
  65. package/dist/validations/validateCpf.d.ts.map +1 -0
  66. package/dist/validations/validateCpf.js +56 -0
  67. package/dist/validations/validateDate.d.ts +27 -6
  68. package/dist/validations/validateDate.d.ts.map +1 -1
  69. package/dist/validations/validateDate.js +32 -6
  70. package/dist/validations/validatePhone.d.ts +27 -1
  71. package/dist/validations/validatePhone.d.ts.map +1 -1
  72. package/dist/validations/validatePhone.js +27 -2
  73. package/package.json +4 -2
  74. package/src/formats/formatDate.ts +97 -0
  75. package/src/formats/formatJsonObject.ts +68 -26
  76. package/src/formats/formatJsonString.ts +40 -4
  77. package/src/formats/formatToCep.ts +35 -7
  78. package/src/formats/formatToCnpj.ts +39 -0
  79. package/src/formats/formatToCpf.ts +39 -0
  80. package/src/formats/formatToCpfCnpj.ts +31 -7
  81. package/src/formats/formatToCurrency.ts +53 -0
  82. package/src/formats/formatToEllipsis.ts +23 -3
  83. package/src/formats/formatToHiddenDigits.ts +75 -7
  84. package/src/formats/formatToPhone.ts +156 -6
  85. package/src/generators/generateColorByString.ts +18 -4
  86. package/src/generators/generateId.ts +36 -5
  87. package/src/generators/generateSlug.ts +17 -2
  88. package/src/index.ts +7 -15
  89. package/src/regex/index.ts +8 -0
  90. package/src/services/calculateCardInstallment.ts +57 -12
  91. package/src/services/maskSensitiveData.ts +29 -4
  92. package/src/services/removeCurrencySymbols.ts +25 -0
  93. package/src/services/removeNonNumeric.ts +18 -0
  94. package/src/services/truncateLargeFields.ts +40 -6
  95. package/src/validations/validateCpf.ts +64 -0
  96. package/src/validations/validateDate.ts +33 -11
  97. package/src/validations/validatePhone.ts +29 -2
  98. package/tsconfig.json +1 -1
  99. package/vitest.config.ts +5 -0
  100. package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
  101. package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
  102. package/dist/formats/formatBrazilianDateHour.js +0 -12
  103. package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
  104. package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
  105. package/dist/formats/formatBrazilianDateToDate.js +0 -9
  106. package/dist/formats/formatDateHour.d.ts +0 -3
  107. package/dist/formats/formatDateHour.d.ts.map +0 -1
  108. package/dist/formats/formatDateHour.js +0 -11
  109. package/dist/formats/formatToBRL.d.ts +0 -3
  110. package/dist/formats/formatToBRL.d.ts.map +0 -1
  111. package/dist/formats/formatToBRL.js +0 -8
  112. package/dist/formats/formatToCNPJ.d.ts +0 -3
  113. package/dist/formats/formatToCNPJ.d.ts.map +0 -1
  114. package/dist/formats/formatToCNPJ.js +0 -9
  115. package/dist/formats/formatToCPF.d.ts +0 -3
  116. package/dist/formats/formatToCPF.d.ts.map +0 -1
  117. package/dist/formats/formatToCPF.js +0 -9
  118. package/dist/parsers/parseToCharacters.d.ts +0 -17
  119. package/dist/parsers/parseToCharacters.d.ts.map +0 -1
  120. package/dist/parsers/parseToCharacters.js +0 -13
  121. package/dist/services/range.d.ts +0 -4
  122. package/dist/services/range.d.ts.map +0 -1
  123. package/dist/services/range.js +0 -9
  124. package/dist/validations/regex.d.ts +0 -15
  125. package/dist/validations/regex.d.ts.map +0 -1
  126. package/dist/validations/regex.js +0 -25
  127. package/dist/validations/validateCPF.d.ts +0 -3
  128. package/dist/validations/validateCPF.d.ts.map +0 -1
  129. package/dist/validations/validateCPF.js +0 -36
  130. package/src/formats/formatBrazilianDateHour.ts +0 -17
  131. package/src/formats/formatBrazilianDateToDate.ts +0 -13
  132. package/src/formats/formatDateHour.ts +0 -15
  133. package/src/formats/formatToBRL.ts +0 -10
  134. package/src/formats/formatToCNPJ.ts +0 -10
  135. package/src/formats/formatToCPF.ts +0 -10
  136. package/src/parsers/parseToCharacters.ts +0 -34
  137. package/src/services/range.ts +0 -15
  138. package/src/validations/regex.ts +0 -41
  139. package/src/validations/validateCPF.ts +0 -38
@@ -1,13 +1,33 @@
1
- import { formatToCNPJ } from "./formatToCNPJ";
2
- import { formatToCPF } from "./formatToCPF";
3
- function formatToCpfCnpj(value) {
1
+ import { formatToCnpj } from "./formatToCnpj";
2
+ import { formatToCpf } from "./formatToCpf";
3
+ /**
4
+ * Formats a given string value into either a CPF or CNPJ format based on its length.
5
+ *
6
+ * - If the input contains 11 numeric characters, it is formatted as a CPF.
7
+ * - If the input contains 14 numeric characters, it is formatted as a CNPJ.
8
+ * - Throws an error if the input length is neither 11 nor 14 after removing non-numeric characters.
9
+ *
10
+ * @param value - The string value to be formatted. It may contain non-numeric characters, which will be removed.
11
+ * @returns The formatted CPF or CNPJ string.
12
+ * @throws {Error} If the input does not have a valid CPF or CNPJ length.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * formatToCpfCnpj("123.456.789-09"); // Returns "123.456.789-09" (CPF format)
17
+ * formatToCpfCnpj("12.345.678/0001-95"); // Returns "12.345.678/0001-95" (CNPJ format)
18
+ * formatToCpfCnpj("12345678909"); // Returns "123.456.789-09" (CPF format)
19
+ * formatToCpfCnpj("12345678000195"); // Returns "12.345.678/0001-95" (CNPJ format)
20
+ * formatToCpfCnpj("123"); // Throws an error: "Invalid CPF or CNPJ length"
21
+ * ```
22
+ */
23
+ const formatToCpfCnpj = (value) => {
4
24
  const cleaned = value.replace(/\D/g, "");
5
25
  if (cleaned.length === 11) {
6
- return formatToCPF(cleaned);
26
+ return formatToCpf(cleaned);
7
27
  }
8
28
  else if (cleaned.length === 14) {
9
- return formatToCNPJ(cleaned);
29
+ return formatToCnpj(cleaned);
10
30
  }
11
- return value;
12
- }
31
+ throw new Error("Invalid CPF or CNPJ length");
32
+ };
13
33
  export { formatToCpfCnpj };
@@ -0,0 +1,29 @@
1
+ import type { FormatToCurrency } from "@arkyn/types";
2
+ /**
3
+ * Formats a numeric value into a currency string based on the specified currency and configuration.
4
+ *
5
+ * @param value - The numeric value to be formatted.
6
+ * @param currency - The currency code used to determine the formatting style.
7
+ * @param config - Optional configuration object.
8
+ * @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
9
+ *
10
+ * @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
15
+ * console.log(formatted); // "$1,234.56"
16
+ *
17
+ * const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
18
+ * console.log(withoutPrefix); // "1,234.56"
19
+ *
20
+ * const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
21
+ * console.log(formattedBRL); // "R$ 1.234,56"
22
+ *
23
+ * const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
24
+ * console.log(withoutPrefixBRL); // "1.234,56"
25
+ * ```
26
+ */
27
+ declare const formatToCurrency: FormatToCurrency;
28
+ export { formatToCurrency };
29
+ //# sourceMappingURL=formatToCurrency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatToCurrency.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCurrency.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIrD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,gBAmBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -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
- declare function formatToEllipsis(text: string, size?: number): string;
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,iBAAS,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,SAAK,GAAG,MAAM,CAEzD;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
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
- function formatToEllipsis(text, size = 40) {
2
- return text.length > size ? `${text.substring(0, size)}...` : text;
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 { FormatToHiddenDigitsOptions } from "@arkyn/types";
2
- declare const formatToHiddenDigits: (value: string, options?: FormatToHiddenDigitsOptions) => string;
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,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAKhE,QAAA,MAAM,oBAAoB,GACxB,OAAO,MAAM,EACb,UAAS,2BAAgC,KACxC,MAUF,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
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
- import { parseToCharacters } from "../parsers/parseToCharacters";
2
- import { normalizeRange, within } from "../services/range";
3
- const formatToHiddenDigits = (value, options = {}) => {
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
- declare function formatToPhone(value: string): string;
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":"AAAA,iBAAS,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
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
- function formatToPhone(value) {
2
- const cleaned = value.replace(/\D/g, "");
3
- const match = cleaned.match(/^(\d{2})(\d{5})(\d{4})$/);
4
- if (match) {
5
- return `(${match[1]}) ${match[2]}-${match[3]}`;
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 value;
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 };
@@ -1,3 +1,15 @@
1
- declare function generateColorByString(string: string): string;
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,iBAAS,qBAAqB,CAAC,MAAM,EAAE,MAAM,UAgB5C;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
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
- function generateColorByString(string) {
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 < string.length; i++) {
4
- hash = string.charCodeAt(i) + ((hash << 5) - 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
- declare function generateId(type: "text" | "binary"): {
2
- v4: string | Uint8Array<ArrayBuffer>;
3
- v7: string | Uint8Array<ArrayBuffer>;
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":"AAsBA,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ;;;EAK1C;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
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
- return {
19
- v4: uuidV4()[type],
20
- v7: uuidV7()[type],
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
- declare function generateSlug(string: string): string;
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,MAAM,EAAE,MAAM,UAanC;AAED,OAAO,EAAE,YAAY,EAAE,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"}
@@ -1,5 +1,19 @@
1
- function generateSlug(string) {
2
- let slug = string.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
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
+ function generateSlug(prop) {
16
+ let slug = prop.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
3
17
  slug = slug
4
18
  .replace(/[^\w\s-]/g, "")
5
19
  .replace(/\s+/g, "-")
package/dist/index.d.ts CHANGED
@@ -1,26 +1,23 @@
1
- export { formatBrazilianDateHour } from "./formats/formatBrazilianDateHour";
2
- export { formatBrazilianDateToDate } from "./formats/formatBrazilianDateToDate";
3
- export { formatDateHour } from "./formats/formatDateHour";
1
+ export { formatDate } from "./formats/formatDate";
4
2
  export { formatJsonObject } from "./formats/formatJsonObject";
5
3
  export { formatJsonString } from "./formats/formatJsonString";
6
- export { formatToBRL } from "./formats/formatToBRL";
7
4
  export { formatToCep } from "./formats/formatToCep";
8
- export { formatToCNPJ } from "./formats/formatToCNPJ";
9
- export { formatToCPF } from "./formats/formatToCPF";
5
+ export { formatToCnpj } from "./formats/formatToCnpj";
6
+ export { formatToCpf } from "./formats/formatToCpf";
10
7
  export { formatToCpfCnpj } from "./formats/formatToCpfCnpj";
8
+ export { formatToCurrency } from "./formats/formatToCurrency";
11
9
  export { formatToEllipsis } from "./formats/formatToEllipsis";
12
10
  export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
13
11
  export { formatToPhone } from "./formats/formatToPhone";
14
12
  export { generateColorByString } from "./generators/generateColorByString";
15
13
  export { generateId } from "./generators/generateId";
16
14
  export { generateSlug } from "./generators/generateSlug";
17
- export { parseToCharacters } from "./parsers/parseToCharacters";
15
+ export { regex } from "./regex";
18
16
  export { calculateCardInstallment } from "./services/calculateCardInstallment";
19
17
  export { maskSensitiveData } from "./services/maskSensitiveData";
20
- export { normalizeRange, within } from "./services/range";
18
+ export { removeNonNumeric } from "./services/removeNonNumeric";
21
19
  export { truncateLargeFields } from "./services/truncateLargeFields";
22
- export { regex } from "./validations/regex";
23
- export { validateCPF } from "./validations/validateCPF";
20
+ export { validateCpf } from "./validations/validateCpf";
24
21
  export { validateDate } from "./validations/validateDate";
25
22
  export { validatePhone } from "./validations/validatePhone";
26
23
  //# sourceMappingURL=index.d.ts.map