@arkyn/shared 3.0.1-beta.14 → 3.0.1-beta.141

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 (147) hide show
  1. package/README.md +452 -67
  2. package/dist/bundle.js +3749 -0
  3. package/dist/bundle.umd.cjs +10 -0
  4. package/dist/formats/formatDate.d.ts +44 -16
  5. package/dist/formats/formatDate.d.ts.map +1 -1
  6. package/dist/formats/formatDate.js +61 -26
  7. package/dist/formats/formatJsonObject.d.ts +6 -7
  8. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  9. package/dist/formats/formatJsonObject.js +17 -17
  10. package/dist/formats/formatToCep.d.ts +5 -12
  11. package/dist/formats/formatToCep.d.ts.map +1 -1
  12. package/dist/formats/formatToCep.js +11 -16
  13. package/dist/formats/formatToCnpj.d.ts +3 -13
  14. package/dist/formats/formatToCnpj.d.ts.map +1 -1
  15. package/dist/formats/formatToCnpj.js +9 -17
  16. package/dist/formats/formatToCpf.d.ts +5 -13
  17. package/dist/formats/formatToCpf.d.ts.map +1 -1
  18. package/dist/formats/formatToCpf.js +11 -17
  19. package/dist/formats/formatToCurrency.d.ts +18 -14
  20. package/dist/formats/formatToCurrency.d.ts.map +1 -1
  21. package/dist/formats/formatToCurrency.js +21 -14
  22. package/dist/formats/formatToEllipsis.d.ts +8 -5
  23. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  24. package/dist/formats/formatToEllipsis.js +21 -7
  25. package/dist/formats/formatToHiddenDigits.d.ts +16 -16
  26. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  27. package/dist/formats/formatToHiddenDigits.js +19 -19
  28. package/dist/formats/formatToPhone.d.ts +11 -22
  29. package/dist/formats/formatToPhone.d.ts.map +1 -1
  30. package/dist/formats/formatToPhone.js +31 -118
  31. package/dist/generators/generateColorByString.d.ts +7 -4
  32. package/dist/generators/generateColorByString.d.ts.map +1 -1
  33. package/dist/generators/generateColorByString.js +10 -6
  34. package/dist/generators/generateId.d.ts +3 -3
  35. package/dist/generators/generateSlug.d.ts +10 -3
  36. package/dist/generators/generateSlug.d.ts.map +1 -1
  37. package/dist/generators/generateSlug.js +11 -4
  38. package/dist/index.d.ts +11 -16
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +13 -17
  41. package/dist/{services/truncateLargeFields.d.ts → parsers/parseLargeFields.d.ts} +9 -11
  42. package/dist/parsers/parseLargeFields.d.ts.map +1 -0
  43. package/dist/{services/truncateLargeFields.js → parsers/parseLargeFields.js} +12 -13
  44. package/dist/parsers/parseSensitiveData.d.ts +23 -0
  45. package/dist/parsers/parseSensitiveData.d.ts.map +1 -0
  46. package/dist/{services/maskSensitiveData.js → parsers/parseSensitiveData.js} +10 -12
  47. package/dist/parsers/parseToDate.d.ts +41 -0
  48. package/dist/parsers/parseToDate.d.ts.map +1 -0
  49. package/dist/parsers/parseToDate.js +67 -0
  50. package/dist/services/validateDateService.d.ts +85 -0
  51. package/dist/services/validateDateService.d.ts.map +1 -0
  52. package/dist/services/validateDateService.js +132 -0
  53. package/dist/{services → utilities}/calculateCardInstallment.d.ts +16 -18
  54. package/dist/utilities/calculateCardInstallment.d.ts.map +1 -0
  55. package/dist/{services → utilities}/calculateCardInstallment.js +12 -15
  56. package/dist/utilities/ensureQuotes.d.ts +21 -0
  57. package/dist/utilities/ensureQuotes.d.ts.map +1 -0
  58. package/dist/{services → utilities}/ensureQuotes.js +12 -4
  59. package/dist/utilities/findCountryMask.d.ts +45 -0
  60. package/dist/utilities/findCountryMask.d.ts.map +1 -0
  61. package/dist/utilities/findCountryMask.js +73 -0
  62. package/dist/utilities/isHtml.d.ts +22 -0
  63. package/dist/utilities/isHtml.d.ts.map +1 -0
  64. package/dist/utilities/isHtml.js +24 -0
  65. package/dist/{services → utilities}/removeCurrencySymbols.d.ts +6 -4
  66. package/dist/utilities/removeCurrencySymbols.d.ts.map +1 -0
  67. package/dist/{services → utilities}/removeCurrencySymbols.js +8 -7
  68. package/dist/utilities/removeNonNumeric.d.ts +16 -0
  69. package/dist/utilities/removeNonNumeric.d.ts.map +1 -0
  70. package/dist/utilities/removeNonNumeric.js +17 -0
  71. package/dist/utilities/stripHtmlTags.d.ts +19 -0
  72. package/dist/utilities/stripHtmlTags.d.ts.map +1 -0
  73. package/dist/utilities/stripHtmlTags.js +24 -0
  74. package/package.json +26 -6
  75. package/dist/formats/formatToCpfCnpj.d.ts +0 -24
  76. package/dist/formats/formatToCpfCnpj.d.ts.map +0 -1
  77. package/dist/formats/formatToCpfCnpj.js +0 -33
  78. package/dist/formats/formatToDate.d.ts +0 -32
  79. package/dist/formats/formatToDate.d.ts.map +0 -1
  80. package/dist/formats/formatToDate.js +0 -50
  81. package/dist/services/calculateCardInstallment.d.ts.map +0 -1
  82. package/dist/services/ensureQuotes.d.ts +0 -14
  83. package/dist/services/ensureQuotes.d.ts.map +0 -1
  84. package/dist/services/maskSensitiveData.d.ts +0 -26
  85. package/dist/services/maskSensitiveData.d.ts.map +0 -1
  86. package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
  87. package/dist/services/removeNonNumeric.d.ts +0 -16
  88. package/dist/services/removeNonNumeric.d.ts.map +0 -1
  89. package/dist/services/removeNonNumeric.js +0 -16
  90. package/dist/services/stripHtmlTags.d.ts +0 -16
  91. package/dist/services/stripHtmlTags.d.ts.map +0 -1
  92. package/dist/services/stripHtmlTags.js +0 -16
  93. package/dist/services/truncateLargeFields.d.ts.map +0 -1
  94. package/dist/validations/validateCep.d.ts +0 -24
  95. package/dist/validations/validateCep.d.ts.map +0 -1
  96. package/dist/validations/validateCep.js +0 -33
  97. package/dist/validations/validateCnpj.d.ts +0 -22
  98. package/dist/validations/validateCnpj.d.ts.map +0 -1
  99. package/dist/validations/validateCnpj.js +0 -52
  100. package/dist/validations/validateCpf.d.ts +0 -24
  101. package/dist/validations/validateCpf.d.ts.map +0 -1
  102. package/dist/validations/validateCpf.js +0 -54
  103. package/dist/validations/validateDate.d.ts +0 -34
  104. package/dist/validations/validateDate.d.ts.map +0 -1
  105. package/dist/validations/validateDate.js +0 -73
  106. package/dist/validations/validatePassword.d.ts +0 -21
  107. package/dist/validations/validatePassword.d.ts.map +0 -1
  108. package/dist/validations/validatePassword.js +0 -34
  109. package/dist/validations/validatePhone.d.ts +0 -29
  110. package/dist/validations/validatePhone.d.ts.map +0 -1
  111. package/dist/validations/validatePhone.js +0 -44
  112. package/dist/validations/validateRg.d.ts +0 -22
  113. package/dist/validations/validateRg.d.ts.map +0 -1
  114. package/dist/validations/validateRg.js +0 -31
  115. package/src/formats/formatDate.ts +0 -92
  116. package/src/formats/formatJsonObject.ts +0 -90
  117. package/src/formats/formatJsonString.ts +0 -50
  118. package/src/formats/formatToCapitalizeFirstWordLetter.ts +0 -46
  119. package/src/formats/formatToCep.ts +0 -39
  120. package/src/formats/formatToCnpj.ts +0 -40
  121. package/src/formats/formatToCpf.ts +0 -40
  122. package/src/formats/formatToCpfCnpj.ts +0 -38
  123. package/src/formats/formatToCurrency.ts +0 -63
  124. package/src/formats/formatToDate.ts +0 -70
  125. package/src/formats/formatToEllipsis.ts +0 -25
  126. package/src/formats/formatToHiddenDigits.ts +0 -92
  127. package/src/formats/formatToPhone.ts +0 -170
  128. package/src/generators/generateColorByString.ts +0 -33
  129. package/src/generators/generateId.ts +0 -61
  130. package/src/generators/generateSlug.ts +0 -31
  131. package/src/index.ts +0 -37
  132. package/src/services/calculateCardInstallment.ts +0 -73
  133. package/src/services/ensureQuotes.ts +0 -25
  134. package/src/services/maskSensitiveData.ts +0 -68
  135. package/src/services/removeCurrencySymbols.ts +0 -29
  136. package/src/services/removeNonNumeric.ts +0 -20
  137. package/src/services/stripHtmlTags.ts +0 -20
  138. package/src/services/truncateLargeFields.ts +0 -69
  139. package/src/validations/validateCep.ts +0 -41
  140. package/src/validations/validateCnpj.ts +0 -65
  141. package/src/validations/validateCpf.ts +0 -62
  142. package/src/validations/validateDate.ts +0 -86
  143. package/src/validations/validatePassword.ts +0 -41
  144. package/src/validations/validatePhone.ts +0 -50
  145. package/src/validations/validateRg.ts +0 -37
  146. package/tsconfig.json +0 -20
  147. package/vitest.config.ts +0 -5
@@ -1,16 +1,19 @@
1
- type FormatToEllipsisFunction = (value: string, maxLength: number) => string;
2
1
  /**
3
2
  * Truncates a given text to a specified maximum length and appends an ellipsis ("...")
4
3
  * if the text exceeds the maximum length.
5
4
  *
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,
5
+ * @param {string} text - The input string to be truncated.
6
+ *
7
+ * @param {number} maxLength - The maximum allowed length of the string before truncation.
8
+ *
9
+ * @returns {string} The truncated string with an ellipsis if the input exceeds the maximum length,
9
10
  * or the original string if it does not.
10
11
  * @example
12
+ * ```typescript
11
13
  * const result = formatToEllipsis("Hello, world!", 5);
12
14
  * console.log(result); // Output: "Hello..."
15
+ * ```
13
16
  */
14
- declare const formatToEllipsis: FormatToEllipsisFunction;
17
+ declare function formatToEllipsis(text: string, maxLength: number): string;
15
18
  export { formatToEllipsis };
16
19
  //# sourceMappingURL=formatToEllipsis.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatToEllipsis.d.ts","sourceRoot":"","sources":["../../src/formats/formatToEllipsis.ts"],"names":[],"mappings":"AAAA,KAAK,wBAAwB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC;AAE7E;;;;;;;;;;;GAWG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"formatToEllipsis.d.ts","sourceRoot":"","sources":["../../src/formats/formatToEllipsis.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,iBAAS,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAsBjE;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -2,20 +2,34 @@
2
2
  * Truncates a given text to a specified maximum length and appends an ellipsis ("...")
3
3
  * if the text exceeds the maximum length.
4
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,
5
+ * @param {string} text - The input string to be truncated.
6
+ *
7
+ * @param {number} maxLength - The maximum allowed length of the string before truncation.
8
+ *
9
+ * @returns {string} The truncated string with an ellipsis if the input exceeds the maximum length,
8
10
  * or the original string if it does not.
9
11
  * @example
12
+ * ```typescript
10
13
  * const result = formatToEllipsis("Hello, world!", 5);
11
14
  * console.log(result); // Output: "Hello..."
15
+ * ```
12
16
  */
13
- const formatToEllipsis = (text, maxLength) => {
17
+ function formatToEllipsis(text, maxLength) {
14
18
  if (text.length > maxLength) {
15
- let trimmedText = text.substring(0, maxLength).trimEnd();
16
- trimmedText = trimmedText.replace(/[.,!?;:]$/, "");
19
+ let trimmedText = text.substring(0, maxLength);
20
+ // Find the last space to avoid breaking words
21
+ const lastSpaceIndex = trimmedText.lastIndexOf(" ");
22
+ if (lastSpaceIndex > 0) {
23
+ trimmedText = trimmedText.substring(0, lastSpaceIndex);
24
+ }
25
+ // Remove trailing punctuation
26
+ trimmedText = trimmedText.replace(/[\s.,!?;:]+$/, "");
27
+ // If after removing punctuation the text is empty or only contains punctuation/spaces, return only "..."
28
+ if (trimmedText.trim().length === 0 || /^[.,!?;:\s]+$/.test(trimmedText)) {
29
+ return "...";
30
+ }
17
31
  return `${trimmedText}...`;
18
32
  }
19
33
  return text;
20
- };
34
+ }
21
35
  export { formatToEllipsis };
@@ -1,35 +1,35 @@
1
- type FormatToHiddenDigitsFunction = (value: string, options: {
2
- range?: number | [number, number];
3
- hider?: string;
4
- }) => string;
5
1
  /**
6
2
  * Formats a string by hiding specific digits within a given range.
7
3
  *
8
4
  * This function takes a string input and replaces digits within a specified range
9
5
  * with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
10
6
  *
11
- * @param value - The input string to be formatted.
12
- * @param options - Configuration options for formatting.
13
- * @param options.range - The range of digits to hide. It can be:
7
+ * @param {string} value - The input string to be formatted.
8
+ * @param {FormatToHiddenDigitsOptions} options - Configuration options for formatting.
9
+ * @param {number | [number, number]} options.range - The range of digits to hide. It can be:
14
10
  * - A single number (e.g., `3`), which hides the first `n` digits if positive,
15
11
  * or the last `n` digits if negative.
16
12
  * - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
17
13
  * - Defaults to `3`, hiding the first three digits.
18
- * @param options.hider - The character used to hide digits. Defaults to `"*"`.
14
+ * @param {string} options.hider - The character used to hide digits. Defaults to `"*"`.
19
15
  *
20
- * @returns The formatted string with specified digits hidden.
16
+ * @returns {string} The formatted string with specified digits hidden.
21
17
  *
22
18
  * @example
23
19
  * ```typescript
24
- * import { formatToHiddenDigits } from "./formatToHiddenDigits";
25
- *
26
- * formatToHiddenDigits("123-456-7890", { range: 3 });
27
- * // Output: "***-456-7890"
20
+ * const formatted = formatToHiddenDigits("123-456-7890", { range: 3 });
21
+ * console.log(formatted); // Output: "***-456-7890"
22
+ * ```
28
23
  *
29
- * formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
30
- * // Output: "123-###-7890"
24
+ * @example
25
+ * ```typescript
26
+ * const formatted = formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
27
+ * console.log(formatted); // Output: "123-###-7890"
31
28
  * ```
32
29
  */
33
- declare const formatToHiddenDigits: FormatToHiddenDigitsFunction;
30
+ declare function formatToHiddenDigits(value: string, options?: {
31
+ range?: number | [number, number];
32
+ hider?: string;
33
+ }): string;
34
34
  export { formatToHiddenDigits };
35
35
  //# sourceMappingURL=formatToHiddenDigits.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatToHiddenDigits.d.ts","sourceRoot":"","sources":["../../src/formats/formatToHiddenDigits.ts"],"names":[],"mappings":"AAmBA,KAAK,4BAA4B,GAAG,CAClC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,KAC3D,MAAM,CAAC;AA4BZ;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,QAAA,MAAM,oBAAoB,EAAE,4BAU3B,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
1
+ {"version":3,"file":"formatToHiddenDigits.d.ts","sourceRoot":"","sources":["../../src/formats/formatToHiddenDigits.ts"],"names":[],"mappings":"AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,iBAAS,oBAAoB,CAC3B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9D,MAAM,CAWR;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
@@ -24,37 +24,37 @@ const within = (range, value) => value >= range[0] && value <= range[1];
24
24
  * This function takes a string input and replaces digits within a specified range
25
25
  * with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
26
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:
27
+ * @param {string} value - The input string to be formatted.
28
+ * @param {FormatToHiddenDigitsOptions} options - Configuration options for formatting.
29
+ * @param {number | [number, number]} options.range - The range of digits to hide. It can be:
30
30
  * - A single number (e.g., `3`), which hides the first `n` digits if positive,
31
31
  * or the last `n` digits if negative.
32
32
  * - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
33
33
  * - Defaults to `3`, hiding the first three digits.
34
- * @param options.hider - The character used to hide digits. Defaults to `"*"`.
34
+ * @param {string} options.hider - The character used to hide digits. Defaults to `"*"`.
35
35
  *
36
- * @returns The formatted string with specified digits hidden.
36
+ * @returns {string} The formatted string with specified digits hidden.
37
37
  *
38
38
  * @example
39
39
  * ```typescript
40
- * import { formatToHiddenDigits } from "./formatToHiddenDigits";
41
- *
42
- * formatToHiddenDigits("123-456-7890", { range: 3 });
43
- * // Output: "***-456-7890"
40
+ * const formatted = formatToHiddenDigits("123-456-7890", { range: 3 });
41
+ * console.log(formatted); // Output: "***-456-7890"
42
+ * ```
44
43
  *
45
- * formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
46
- * // Output: "123-###-7890"
44
+ * @example
45
+ * ```typescript
46
+ * const formatted = formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
47
+ * console.log(formatted); // Output: "123-###-7890"
47
48
  * ```
48
49
  */
49
- const formatToHiddenDigits = (value, options) => {
50
+ function formatToHiddenDigits(value, options) {
50
51
  const characters = parseToCharacters(value);
51
- const range = normalizeRange(options.range ?? 3, characters.digits);
52
- return characters.children
53
- .map((node) => {
52
+ const range = normalizeRange(options?.range ?? 3, characters.digits);
53
+ const mappedCharacters = characters.children.map((node) => {
54
54
  if (node.kind === "digit" && within(range, node.digit))
55
- return options.hider ?? "*";
55
+ return options?.hider ?? "*";
56
56
  return node.character;
57
- })
58
- .join("");
59
- };
57
+ });
58
+ return mappedCharacters.join("");
59
+ }
60
60
  export { formatToHiddenDigits };
@@ -1,34 +1,23 @@
1
- type FormatToPhoneFunction = (prop: string) => string;
2
1
  /**
3
- * Formats a phone number string based on the provided country code and optional prefix.
2
+ * Formats a phone number string according to the country mask defined in `@arkyn/templates`.
4
3
  *
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.
4
+ * The function parses the input using libphonenumber-js to determine the country and
5
+ * national number, then applies the corresponding country's mask (underscore `_` used
6
+ * as digit placeholder) replacing placeholders with actual digits.
7
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 32912345678"
11
- * - "+1 1234567890"
8
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
12
9
  *
13
- * @returns The formatted phone number string based on the country's formatting rules.
10
+ * @returns {string} The phone number formatted following the country's mask.
14
11
  *
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.
12
+ * @throws {Error} If the phone number is invalid or if no country mask is found for the parsed country.
20
13
  *
21
14
  * @example
22
15
  * ```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)
16
+ * console.log(formatToPhone("+5534920524282")); // Output: "(34) 92052-4282" (Brazilian format)
17
+ * console.log(formatToPhone("+553420524282")); // Output: "(34) 2052-4282" (Brazilian format with optional ninth digit)
18
+ * console.log(formatToPhone("+12125550199")); // Output: "(212) 555-0199" (American Samoa format)
30
19
  * ```
31
20
  */
32
- declare const formatToPhone: FormatToPhoneFunction;
21
+ declare function formatToPhone(phoneNumber: string): string;
33
22
  export { formatToPhone };
34
23
  //# sourceMappingURL=formatToPhone.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"AAaA,KAAK,qBAAqB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;AAmGtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAwBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
1
+ {"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,iBAAS,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CA0BlD;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -1,131 +1,44 @@
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");
72
- }
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;
82
- }
1
+ import { parsePhoneNumberWithError } from "libphonenumber-js";
2
+ import { findCountryMask } from "../utilities/findCountryMask";
83
3
  /**
84
- * Formats a phone number string based on the provided country code and optional prefix.
4
+ * Formats a phone number string according to the country mask defined in `@arkyn/templates`.
85
5
  *
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.
6
+ * The function parses the input using libphonenumber-js to determine the country and
7
+ * national number, then applies the corresponding country's mask (underscore `_` used
8
+ * as digit placeholder) replacing placeholders with actual digits.
88
9
  *
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 32912345678"
92
- * - "+1 1234567890"
10
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
93
11
  *
94
- * @returns The formatted phone number string based on the country's formatting rules.
12
+ * @returns {string} The phone number formatted following the country's mask.
95
13
  *
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.
14
+ * @throws {Error} If the phone number is invalid or if no country mask is found for the parsed country.
101
15
  *
102
16
  * @example
103
17
  * ```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)
18
+ * console.log(formatToPhone("+5534920524282")); // Output: "(34) 92052-4282" (Brazilian format)
19
+ * console.log(formatToPhone("+553420524282")); // Output: "(34) 2052-4282" (Brazilian format with optional ninth digit)
20
+ * console.log(formatToPhone("+12125550199")); // Output: "(212) 555-0199" (American Samoa format)
111
21
  * ```
112
22
  */
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");
23
+ function formatToPhone(phoneNumber) {
24
+ try {
25
+ const parsedPhone = parsePhoneNumberWithError(phoneNumber);
26
+ const phoneNumberDigits = parsedPhone.nationalNumber.toString();
27
+ let formattedNumber = findCountryMask(phoneNumber)[0];
28
+ for (let i = 0, j = 0; i < formattedNumber.length && j < phoneNumberDigits.length; i++) {
29
+ if (formattedNumber[i] === "_") {
30
+ formattedNumber =
31
+ formattedNumber.substring(0, i) +
32
+ phoneNumberDigits[j] +
33
+ formattedNumber.substring(i + 1);
34
+ j++;
35
+ }
36
+ }
37
+ return formattedNumber;
123
38
  }
124
- if (prefixCode) {
125
- const country = getCountryWithPrefixCode(countryCode, prefixCode);
126
- return formatPhoneNumber(phoneNumber, country);
39
+ catch (rawError) {
40
+ const error = rawError;
41
+ throw new Error(error.message);
127
42
  }
128
- const country = getCountryWithoutPrefixCode(countryCode);
129
- return formatPhoneNumber(phoneNumber, country);
130
- };
43
+ }
131
44
  export { formatToPhone };
@@ -1,15 +1,18 @@
1
- type GenerateColorByStringFunction = (prop: string) => string;
2
1
  /**
3
2
  * Generates a hexadecimal color code based on the input string.
4
3
  * The function creates a hash from the string and uses it to calculate
5
4
  * RGB values, which are then converted to a hexadecimal color code.
6
5
  *
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.
6
+ * @param {string} rawString - The input string used to generate the color.
7
+ *
8
+ * @returns {string} A hexadecimal color code (e.g., "#a1b2c3") derived from the input string.
9
+ *
9
10
  * @example
11
+ * ```typescript
10
12
  * const color = generateColorByString("example");
11
13
  * console.log(color); // Outputs a consistent hex color like "#5e8f9a"
14
+ * ```
12
15
  */
13
- declare const generateColorByString: GenerateColorByStringFunction;
16
+ declare function generateColorByString(rawString: string): string;
14
17
  export { generateColorByString };
15
18
  //# sourceMappingURL=generateColorByString.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"generateColorByString.d.ts","sourceRoot":"","sources":["../../src/generators/generateColorByString.ts"],"names":[],"mappings":"AAAA,KAAK,6BAA6B,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;AAE9D;;;;;;;;;;GAUG;AAEH,QAAA,MAAM,qBAAqB,EAAE,6BAgB5B,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"generateColorByString.d.ts","sourceRoot":"","sources":["../../src/generators/generateColorByString.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,iBAAS,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAgBxD;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
@@ -3,16 +3,20 @@
3
3
  * The function creates a hash from the string and uses it to calculate
4
4
  * RGB values, which are then converted to a hexadecimal color code.
5
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.
6
+ * @param {string} rawString - The input string used to generate the color.
7
+ *
8
+ * @returns {string} A hexadecimal color code (e.g., "#a1b2c3") derived from the input string.
9
+ *
8
10
  * @example
11
+ * ```typescript
9
12
  * const color = generateColorByString("example");
10
13
  * console.log(color); // Outputs a consistent hex color like "#5e8f9a"
14
+ * ```
11
15
  */
12
- const generateColorByString = (prop) => {
16
+ function generateColorByString(rawString) {
13
17
  var hash = 0;
14
- for (var i = 0; i < prop.length; i++) {
15
- hash = prop.charCodeAt(i) + ((hash << 5) - hash);
18
+ for (var i = 0; i < rawString.length; i++) {
19
+ hash = rawString.charCodeAt(i) + ((hash << 5) - hash);
16
20
  }
17
21
  var red = (hash & 0xff0000) >> 16;
18
22
  var green = (hash & 0x00ff00) >> 8;
@@ -21,5 +25,5 @@ const generateColorByString = (prop) => {
21
25
  var greenHex = green.toString(16).padStart(2, "0");
22
26
  var blueHex = blue.toString(16).padStart(2, "0");
23
27
  return "#" + redHex + greenHex + blueHex;
24
- };
28
+ }
25
29
  export { generateColorByString };
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Generates a unique identifier (UUID) in the specified format and type.
3
3
  *
4
- * @param type - The desired output type of the UUID. Can be:
4
+ * @param {"text" | "binary"} type - The desired output type of the UUID. Can be:
5
5
  * - `"text"`: Returns the UUID as a string.
6
6
  * - `"binary"`: Returns the UUID as a `Uint8Array` in binary format.
7
- * @param format - The version of the UUID to generate. Can be:
7
+ * @param {"v4" | "v7"} format - The version of the UUID to generate. Can be:
8
8
  * - `"v4"`: Generates a random UUID (version 4).
9
9
  * - `"v7"`: Generates a time-ordered UUID (version 7).
10
- * @returns The generated UUID in the specified type and format.
10
+ * @returns {string | Uint8Array} The generated UUID in the specified type and format.
11
11
  * - If `type` is `"text"`, a string representation of the UUID is returned.
12
12
  * - If `type` is `"binary"`, a `Uint8Array` representation of the UUID is returned.
13
13
  * @throws {Error} If an invalid `type` or `format` is provided.
@@ -9,9 +9,16 @@
9
9
  * - Collapses multiple consecutive hyphens into a single hyphen.
10
10
  * - Trims leading and trailing hyphens.
11
11
  *
12
- * @param string - The input string to be converted into a slug.
13
- * @returns A URL-friendly slug derived from the input string.
12
+ * @param {string} rawString - The input string to be converted into a slug.
13
+ *
14
+ * @returns {string} A URL-friendly slug derived from the input string.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const slug = generateSlug("Hello, World! This is a Test.");
19
+ * console.log(slug); // Outputs: "hello-world-this-is-a-test"
20
+ * ```
14
21
  */
15
- declare function generateSlug(prop: string): string;
22
+ declare function generateSlug(rawString: string): string;
16
23
  export { generateSlug };
17
24
  //# sourceMappingURL=generateSlug.d.ts.map
@@ -1 +1 @@
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
+ {"version":3,"file":"generateSlug.d.ts","sourceRoot":"","sources":["../../src/generators/generateSlug.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,iBAAS,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAa/C;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -9,11 +9,18 @@
9
9
  * - Collapses multiple consecutive hyphens into a single hyphen.
10
10
  * - Trims leading and trailing hyphens.
11
11
  *
12
- * @param string - The input string to be converted into a slug.
13
- * @returns A URL-friendly slug derived from the input string.
12
+ * @param {string} rawString - The input string to be converted into a slug.
13
+ *
14
+ * @returns {string} A URL-friendly slug derived from the input string.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const slug = generateSlug("Hello, World! This is a Test.");
19
+ * console.log(slug); // Outputs: "hello-world-this-is-a-test"
20
+ * ```
14
21
  */
15
- function generateSlug(prop) {
16
- let slug = prop.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
22
+ function generateSlug(rawString) {
23
+ let slug = rawString.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
17
24
  slug = slug
18
25
  .replace(/[^\w\s-]/g, "")
19
26
  .replace(/\s+/g, "-")
package/dist/index.d.ts CHANGED
@@ -5,27 +5,22 @@ export { formatToCapitalizeFirstWordLetter } from "./formats/formatToCapitalizeF
5
5
  export { formatToCep } from "./formats/formatToCep";
6
6
  export { formatToCnpj } from "./formats/formatToCnpj";
7
7
  export { formatToCpf } from "./formats/formatToCpf";
8
- export { formatToCpfCnpj } from "./formats/formatToCpfCnpj";
9
8
  export { formatToCurrency } from "./formats/formatToCurrency";
10
- export { formatToDate } from "./formats/formatToDate";
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 { calculateCardInstallment } from "./services/calculateCardInstallment";
18
- export { ensureQuotes } from "./services/ensureQuotes";
19
- export { maskSensitiveData } from "./services/maskSensitiveData";
20
- export { removeCurrencySymbols } from "./services/removeCurrencySymbols";
21
- export { removeNonNumeric } from "./services/removeNonNumeric";
22
- export { stripHtmlTags } from "./services/stripHtmlTags";
23
- export { truncateLargeFields } from "./services/truncateLargeFields";
24
- export { validateCep } from "./validations/validateCep";
25
- export { validateCnpj } from "./validations/validateCnpj";
26
- export { validateCpf } from "./validations/validateCpf";
27
- export { validateDate } from "./validations/validateDate";
28
- export { validatePassword } from "./validations/validatePassword";
29
- export { validatePhone } from "./validations/validatePhone";
30
- export { validateRg } from "./validations/validateRg";
15
+ export { parseLargeFields } from "./parsers/parseLargeFields";
16
+ export { parseSensitiveData } from "./parsers/parseSensitiveData";
17
+ export { parseToDate } from "./parsers/parseToDate";
18
+ export { ValidateDateService } from "./services/validateDateService";
19
+ export { calculateCardInstallment } from "./utilities/calculateCardInstallment";
20
+ export { ensureQuotes } from "./utilities/ensureQuotes";
21
+ export { findCountryMask } from "./utilities/findCountryMask";
22
+ export { isHtml } from "./utilities/isHtml";
23
+ export { removeCurrencySymbols } from "./utilities/removeCurrencySymbols";
24
+ export { removeNonNumeric } from "./utilities/removeNonNumeric";
25
+ export { stripHtmlTags } from "./utilities/stripHtmlTags";
31
26
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,iCAAiC,EAAE,MAAM,6CAA6C,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,iCAAiC,EAAE,MAAM,6CAA6C,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC"}