@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.
Files changed (219) hide show
  1. package/dist/formats/__test__/formatDate.spec.d.ts +2 -0
  2. package/dist/formats/__test__/formatDate.spec.d.ts.map +1 -0
  3. package/dist/formats/__test__/formatDate.spec.js +41 -0
  4. package/dist/formats/__test__/formatJsonObject.spec.d.ts +2 -0
  5. package/dist/formats/__test__/formatJsonObject.spec.d.ts.map +1 -0
  6. package/dist/formats/__test__/formatJsonObject.spec.js +76 -0
  7. package/dist/formats/__test__/formatJsonString.spec.d.ts +2 -0
  8. package/dist/formats/__test__/formatJsonString.spec.d.ts.map +1 -0
  9. package/dist/formats/__test__/formatJsonString.spec.js +71 -0
  10. package/dist/formats/__test__/formatToCep.spec.d.ts +2 -0
  11. package/dist/formats/__test__/formatToCep.spec.d.ts.map +1 -0
  12. package/dist/formats/__test__/formatToCep.spec.js +30 -0
  13. package/dist/formats/__test__/formatToCnpj.spec.d.ts +2 -0
  14. package/dist/formats/__test__/formatToCnpj.spec.d.ts.map +1 -0
  15. package/dist/formats/__test__/formatToCnpj.spec.js +27 -0
  16. package/dist/formats/__test__/formatToCpfCnpj.spec.d.ts +2 -0
  17. package/dist/formats/__test__/formatToCpfCnpj.spec.d.ts.map +1 -0
  18. package/dist/formats/__test__/formatToCpfCnpj.spec.js +32 -0
  19. package/dist/formats/__test__/formatToCurrency.spec.d.ts +2 -0
  20. package/dist/formats/__test__/formatToCurrency.spec.d.ts.map +1 -0
  21. package/dist/formats/__test__/formatToCurrency.spec.js +39 -0
  22. package/dist/formats/__test__/formatToEllipsis.spec.d.ts +2 -0
  23. package/dist/formats/__test__/formatToEllipsis.spec.d.ts.map +1 -0
  24. package/dist/formats/__test__/formatToEllipsis.spec.js +36 -0
  25. package/dist/formats/__test__/formatToHiddenDigits.spec.d.ts +2 -0
  26. package/dist/formats/__test__/formatToHiddenDigits.spec.d.ts.map +1 -0
  27. package/dist/formats/__test__/formatToHiddenDigits.spec.js +47 -0
  28. package/dist/formats/__test__/formatToPhone.spec.d.ts +2 -0
  29. package/dist/formats/__test__/formatToPhone.spec.d.ts.map +1 -0
  30. package/dist/formats/__test__/formatToPhone.spec.js +38 -0
  31. package/dist/formats/formatDate.d.ts +43 -0
  32. package/dist/formats/formatDate.d.ts.map +1 -0
  33. package/dist/formats/formatDate.js +77 -0
  34. package/dist/formats/formatJsonObject.d.ts +31 -1
  35. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  36. package/dist/formats/formatJsonObject.js +67 -26
  37. package/dist/formats/formatJsonString.d.ts +37 -1
  38. package/dist/formats/formatJsonString.d.ts.map +1 -1
  39. package/dist/formats/formatJsonString.js +38 -4
  40. package/dist/formats/formatToCep.d.ts +27 -1
  41. package/dist/formats/formatToCep.d.ts.map +1 -1
  42. package/dist/formats/formatToCep.js +31 -6
  43. package/dist/formats/formatToCnpj.d.ts +30 -0
  44. package/dist/formats/formatToCnpj.d.ts.map +1 -0
  45. package/dist/formats/formatToCnpj.js +35 -0
  46. package/dist/formats/formatToCpf.d.ts +30 -0
  47. package/dist/formats/formatToCpf.d.ts.map +1 -0
  48. package/dist/formats/formatToCpf.js +35 -0
  49. package/dist/formats/formatToCpfCnpj.d.ts +22 -1
  50. package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
  51. package/dist/formats/formatToCpfCnpj.js +27 -7
  52. package/dist/formats/formatToCurrency.d.ts +29 -0
  53. package/dist/formats/formatToCurrency.d.ts.map +1 -0
  54. package/dist/formats/formatToCurrency.js +41 -0
  55. package/dist/formats/formatToEllipsis.d.ts +14 -1
  56. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  57. package/dist/formats/formatToEllipsis.js +20 -3
  58. package/dist/formats/formatToHiddenDigits.d.ts +30 -2
  59. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  60. package/dist/formats/formatToHiddenDigits.js +49 -3
  61. package/dist/formats/formatToPhone.d.ts +32 -1
  62. package/dist/formats/formatToPhone.d.ts.map +1 -1
  63. package/dist/formats/formatToPhone.js +128 -6
  64. package/dist/generators/__test__/generateColorByString.spec.d.ts +2 -0
  65. package/dist/generators/__test__/generateColorByString.spec.d.ts.map +1 -0
  66. package/dist/generators/__test__/generateColorByString.spec.js +31 -0
  67. package/dist/generators/__test__/generateId.spec.d.ts +2 -0
  68. package/dist/generators/__test__/generateId.spec.d.ts.map +1 -0
  69. package/dist/generators/__test__/generateId.spec.js +30 -0
  70. package/dist/generators/__test__/generateSlug.spec.d.ts +2 -0
  71. package/dist/generators/__test__/generateSlug.spec.d.ts.map +1 -0
  72. package/dist/generators/__test__/generateSlug.spec.js +38 -0
  73. package/dist/generators/generateColorByString.d.ts +13 -1
  74. package/dist/generators/generateColorByString.d.ts.map +1 -1
  75. package/dist/generators/generateColorByString.js +15 -4
  76. package/dist/generators/generateId.d.ts +26 -4
  77. package/dist/generators/generateId.d.ts.map +1 -1
  78. package/dist/generators/generateId.js +11 -5
  79. package/dist/generators/generateSlug.d.ts +15 -1
  80. package/dist/generators/generateSlug.d.ts.map +1 -1
  81. package/dist/generators/generateSlug.js +16 -2
  82. package/dist/index.d.ts +7 -10
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +7 -15
  85. package/dist/{validations/regex.d.ts → regex/index.d.ts} +1 -1
  86. package/dist/regex/index.d.ts.map +1 -0
  87. package/dist/services/__test__/calculateCardInstallment.spec.d.ts +2 -0
  88. package/dist/services/__test__/calculateCardInstallment.spec.d.ts.map +1 -0
  89. package/dist/services/__test__/calculateCardInstallment.spec.js +71 -0
  90. package/dist/services/__test__/maskSensitiveData.spec.d.ts +2 -0
  91. package/dist/services/__test__/maskSensitiveData.spec.d.ts.map +1 -0
  92. package/dist/services/__test__/maskSensitiveData.spec.js +79 -0
  93. package/dist/services/__test__/removeCurrencySymbols.spec.d.ts +2 -0
  94. package/dist/services/__test__/removeCurrencySymbols.spec.d.ts.map +1 -0
  95. package/dist/services/__test__/removeCurrencySymbols.spec.js +34 -0
  96. package/dist/services/__test__/removeNonNumeric.spec.d.ts +2 -0
  97. package/dist/services/__test__/removeNonNumeric.spec.d.ts.map +1 -0
  98. package/dist/services/__test__/removeNonNumeric.spec.js +27 -0
  99. package/dist/services/__test__/truncateLargeFields.spec.d.ts +2 -0
  100. package/dist/services/__test__/truncateLargeFields.spec.d.ts.map +1 -0
  101. package/dist/services/__test__/truncateLargeFields.spec.js +65 -0
  102. package/dist/services/calculateCardInstallment.d.ts +31 -5
  103. package/dist/services/calculateCardInstallment.d.ts.map +1 -1
  104. package/dist/services/calculateCardInstallment.js +52 -11
  105. package/dist/services/maskSensitiveData.d.ts +24 -1
  106. package/dist/services/maskSensitiveData.d.ts.map +1 -1
  107. package/dist/services/maskSensitiveData.js +24 -2
  108. package/dist/services/removeCurrencySymbols.d.ts +20 -0
  109. package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
  110. package/dist/services/removeCurrencySymbols.js +23 -0
  111. package/dist/services/removeNonNumeric.d.ts +15 -0
  112. package/dist/services/removeNonNumeric.d.ts.map +1 -0
  113. package/dist/services/removeNonNumeric.js +16 -0
  114. package/dist/services/truncateLargeFields.d.ts +30 -1
  115. package/dist/services/truncateLargeFields.d.ts.map +1 -1
  116. package/dist/services/truncateLargeFields.js +34 -6
  117. package/dist/validations/__test__/validateCpf.spec.d.ts +2 -0
  118. package/dist/validations/__test__/validateCpf.spec.d.ts.map +1 -0
  119. package/dist/validations/__test__/validateCpf.spec.js +31 -0
  120. package/dist/validations/__test__/validateDate.spec.d.ts +2 -0
  121. package/dist/validations/__test__/validateDate.spec.d.ts.map +1 -0
  122. package/dist/validations/__test__/validateDate.spec.js +44 -0
  123. package/dist/validations/__test__/validatePhone.spec.d.ts +2 -0
  124. package/dist/validations/__test__/validatePhone.spec.d.ts.map +1 -0
  125. package/dist/validations/__test__/validatePhone.spec.js +32 -0
  126. package/dist/validations/validateCpf.d.ts +24 -0
  127. package/dist/validations/validateCpf.d.ts.map +1 -0
  128. package/dist/validations/validateCpf.js +56 -0
  129. package/dist/validations/validateDate.d.ts +27 -6
  130. package/dist/validations/validateDate.d.ts.map +1 -1
  131. package/dist/validations/validateDate.js +32 -6
  132. package/dist/validations/validatePhone.d.ts +27 -1
  133. package/dist/validations/validatePhone.d.ts.map +1 -1
  134. package/dist/validations/validatePhone.js +27 -2
  135. package/package.json +4 -2
  136. package/src/formats/__test__/formatDate.spec.ts +88 -0
  137. package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
  138. package/src/formats/__test__/formatJsonString.spec.ts +83 -0
  139. package/src/formats/__test__/formatToCep.spec.ts +37 -0
  140. package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
  141. package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
  142. package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
  143. package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
  144. package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
  145. package/src/formats/__test__/formatToPhone.spec.ts +58 -0
  146. package/src/formats/formatDate.ts +97 -0
  147. package/src/formats/formatJsonObject.ts +68 -26
  148. package/src/formats/formatJsonString.ts +40 -4
  149. package/src/formats/formatToCep.ts +35 -7
  150. package/src/formats/formatToCnpj.ts +39 -0
  151. package/src/formats/formatToCpf.ts +39 -0
  152. package/src/formats/formatToCpfCnpj.ts +31 -7
  153. package/src/formats/formatToCurrency.ts +53 -0
  154. package/src/formats/formatToEllipsis.ts +23 -3
  155. package/src/formats/formatToHiddenDigits.ts +75 -7
  156. package/src/formats/formatToPhone.ts +156 -6
  157. package/src/generators/__test__/generateColorByString.spec.ts +37 -0
  158. package/src/generators/__test__/generateId.spec.ts +44 -0
  159. package/src/generators/__test__/generateSlug.spec.ts +47 -0
  160. package/src/generators/generateColorByString.ts +18 -4
  161. package/src/generators/generateId.ts +36 -5
  162. package/src/generators/generateSlug.ts +17 -2
  163. package/src/index.ts +7 -15
  164. package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
  165. package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
  166. package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
  167. package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
  168. package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
  169. package/src/services/calculateCardInstallment.ts +57 -12
  170. package/src/services/maskSensitiveData.ts +29 -4
  171. package/src/services/removeCurrencySymbols.ts +25 -0
  172. package/src/services/removeNonNumeric.ts +18 -0
  173. package/src/services/truncateLargeFields.ts +40 -6
  174. package/src/validations/__test__/validateCpf.spec.ts +38 -0
  175. package/src/validations/__test__/validateDate.spec.ts +81 -0
  176. package/src/validations/__test__/validatePhone.spec.ts +39 -0
  177. package/src/validations/validateCpf.ts +64 -0
  178. package/src/validations/validateDate.ts +33 -11
  179. package/src/validations/validatePhone.ts +29 -2
  180. package/vitest.config.ts +5 -0
  181. package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
  182. package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
  183. package/dist/formats/formatBrazilianDateHour.js +0 -12
  184. package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
  185. package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
  186. package/dist/formats/formatBrazilianDateToDate.js +0 -9
  187. package/dist/formats/formatDateHour.d.ts +0 -3
  188. package/dist/formats/formatDateHour.d.ts.map +0 -1
  189. package/dist/formats/formatDateHour.js +0 -11
  190. package/dist/formats/formatToBRL.d.ts +0 -3
  191. package/dist/formats/formatToBRL.d.ts.map +0 -1
  192. package/dist/formats/formatToBRL.js +0 -8
  193. package/dist/formats/formatToCNPJ.d.ts +0 -3
  194. package/dist/formats/formatToCNPJ.d.ts.map +0 -1
  195. package/dist/formats/formatToCNPJ.js +0 -9
  196. package/dist/formats/formatToCPF.d.ts +0 -3
  197. package/dist/formats/formatToCPF.d.ts.map +0 -1
  198. package/dist/formats/formatToCPF.js +0 -9
  199. package/dist/parsers/parseToCharacters.d.ts +0 -17
  200. package/dist/parsers/parseToCharacters.d.ts.map +0 -1
  201. package/dist/parsers/parseToCharacters.js +0 -13
  202. package/dist/services/range.d.ts +0 -4
  203. package/dist/services/range.d.ts.map +0 -1
  204. package/dist/services/range.js +0 -9
  205. package/dist/validations/regex.d.ts.map +0 -1
  206. package/dist/validations/validateCPF.d.ts +0 -3
  207. package/dist/validations/validateCPF.d.ts.map +0 -1
  208. package/dist/validations/validateCPF.js +0 -36
  209. package/src/formats/formatBrazilianDateHour.ts +0 -17
  210. package/src/formats/formatBrazilianDateToDate.ts +0 -13
  211. package/src/formats/formatDateHour.ts +0 -15
  212. package/src/formats/formatToBRL.ts +0 -10
  213. package/src/formats/formatToCNPJ.ts +0 -10
  214. package/src/formats/formatToCPF.ts +0 -10
  215. package/src/parsers/parseToCharacters.ts +0 -34
  216. package/src/services/range.ts +0 -15
  217. package/src/validations/validateCPF.ts +0 -38
  218. /package/dist/{validations/regex.js → regex/index.js} +0 -0
  219. /package/src/{validations/regex.ts → regex/index.ts} +0 -0
@@ -1,35 +1,77 @@
1
- function formatJsonObject(obj: any, indentLevel: number): string {
1
+ import type { FormatJsonObjectFunction } from "@arkyn/types";
2
+
3
+ /**
4
+ * Formats a JSON object into a human-readable string with proper indentation.
5
+ *
6
+ * @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
7
+ * @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
8
+ * @returns A formatted string representation of the JSON object.
9
+ *
10
+ * @remarks
11
+ * - If the input is an object, it will be formatted with keys and values properly indented.
12
+ * - If the input is an array, each element will be formatted and indented on a new line.
13
+ * - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
14
+ * - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
19
+ * const formatted = formatJsonObject(obj, 0);
20
+ * console.log(formatted);
21
+ * // Output:
22
+ * // {
23
+ * // "name": "John",
24
+ * // "age": 30,
25
+ * // "hobbies": [
26
+ * // "reading",
27
+ * // "gaming"
28
+ * // ]
29
+ * // }
30
+ * ```
31
+ */
32
+
33
+ const formatJsonObject: FormatJsonObjectFunction = (obj, indentLevel) => {
2
34
  const indent = " ".repeat(indentLevel);
3
35
  let formattedString = "";
4
36
 
5
37
  if (typeof obj === "object" && obj !== null) {
6
38
  if (Array.isArray(obj)) {
7
- formattedString += "[\n";
8
- obj.forEach((item, index) => {
9
- formattedString +=
10
- indent + " " + formatJsonObject(item, indentLevel + 1);
11
- if (index < obj.length - 1) {
12
- formattedString += ",";
13
- }
14
- formattedString += "\n";
15
- });
16
- formattedString += indent + "]";
39
+ if (obj.length === 0) {
40
+ // Caso especial para arrays vazios
41
+ formattedString += "[]";
42
+ } else {
43
+ formattedString += "[\n";
44
+ obj.forEach((item, index) => {
45
+ formattedString +=
46
+ indent + " " + formatJsonObject(item, indentLevel + 1);
47
+ if (index < obj.length - 1) {
48
+ formattedString += ",";
49
+ }
50
+ formattedString += "\n";
51
+ });
52
+ formattedString += indent + "]";
53
+ }
17
54
  } else {
18
- formattedString += "{\n";
19
55
  const keys = Object.keys(obj);
20
- keys.forEach((key, index) => {
21
- formattedString +=
22
- indent +
23
- ' "' +
24
- key +
25
- '": ' +
26
- formatJsonObject(obj[key], indentLevel + 1);
27
- if (index < keys.length - 1) {
28
- formattedString += ",";
29
- }
30
- formattedString += "\n";
31
- });
32
- formattedString += indent + "}";
56
+ if (keys.length === 0) {
57
+ // Caso especial para objetos vazios
58
+ formattedString += "{}";
59
+ } else {
60
+ formattedString += "{\n";
61
+ keys.forEach((key, index) => {
62
+ formattedString +=
63
+ indent +
64
+ ' "' +
65
+ key +
66
+ '": ' +
67
+ formatJsonObject(obj[key], indentLevel + 1);
68
+ if (index < keys.length - 1) {
69
+ formattedString += ",";
70
+ }
71
+ formattedString += "\n";
72
+ });
73
+ formattedString += indent + "}";
74
+ }
33
75
  }
34
76
  } else if (typeof obj === "string") {
35
77
  try {
@@ -43,6 +85,6 @@ function formatJsonObject(obj: any, indentLevel: number): string {
43
85
  }
44
86
 
45
87
  return formattedString;
46
- }
88
+ };
47
89
 
48
90
  export { formatJsonObject };
@@ -1,13 +1,49 @@
1
+ import type { FormatJsonStringFunction } from "@arkyn/types";
1
2
  import { formatJsonObject } from "./formatJsonObject";
2
3
 
3
- function formatJsonString(jsonString: string): string {
4
+ /**
5
+ * Formats a JSON string into a more readable format.
6
+ *
7
+ * This function attempts to parse the provided JSON string into a JavaScript object,
8
+ * and then formats it using the `formatJsonObject` function. If the input string
9
+ * is not a valid JSON, it logs an error to the console and returns an empty string.
10
+ *
11
+ * @param jsonString - The JSON string to be formatted.
12
+ * @returns A formatted JSON string, or an empty string if the input is invalid.
13
+ *
14
+ * @throws Will log an error to the console if the input is not a valid JSON string.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
19
+ * const formatted = formatJsonString(jsonString);
20
+ * console.log(formatted);
21
+ * // Output:
22
+ * // {
23
+ * // "name": "John",
24
+ * // "age": 30,
25
+ * // "hobbies": [
26
+ * // "reading",
27
+ * // "gaming"
28
+ * // ]
29
+ * // }
30
+
31
+ * const invalidJsonString = '{"name":"John", "age":30,';
32
+ * const formatted = formatJsonString(invalidJsonString);
33
+ * console.log(formatted);
34
+ * // Output:
35
+ * // (Logs "Invalid JSON string: ..." to the console)
36
+ * // ""
37
+ * ```
38
+ */
39
+
40
+ const formatJsonString: FormatJsonStringFunction = (jsonString) => {
4
41
  try {
5
42
  const jsonObject = JSON.parse(jsonString);
6
43
  return formatJsonObject(jsonObject, 0);
7
44
  } catch (error) {
8
- console.error("Invalid JSON string:", error);
9
- return "";
45
+ throw new Error(`Invalid JSON string \n ${error}`);
10
46
  }
11
- }
47
+ };
12
48
 
13
49
  export { formatJsonString };
@@ -1,10 +1,38 @@
1
- function formatToCep(value: string): string {
2
- const cleaned = value.replace(/\D/g, "");
1
+ import type { FormatToCepFunction } from "@arkyn/types";
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
3
+
4
+ /**
5
+ * Formats a given string into a Brazilian postal code (CEP) format.
6
+ *
7
+ * The function removes all non-numeric characters from the input string
8
+ * and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
9
+ * If the input does not match the expected format, an error is thrown.
10
+ *
11
+ * @param value - The input string to be formatted as a CEP.
12
+ * @returns The formatted CEP string in the pattern `XXXXX-XXX`.
13
+ * @throws {Error} If the input does not match the expected CEP format.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { formatToCep } from "./formatToCep";
18
+ *
19
+ * const formattedCep = formatToCep("12345678");
20
+ * console.log(formattedCep); // Output: "12345-678"
21
+ *
22
+ * try {
23
+ * formatToCep("1234");
24
+ * } catch (error) {
25
+ * console.error(error.message); // Output: "Invalid CEP format"
26
+ * }
27
+ * ```
28
+ */
29
+
30
+ const formatToCep: FormatToCepFunction = (value) => {
31
+ const cleaned = removeNonNumeric(value);
3
32
  const match = cleaned.match(/^(\d{5})(\d{3})$/);
4
- if (match) {
5
- return `${match[1]}-${match[2]}`;
6
- }
7
- return value;
8
- }
33
+
34
+ if (match) return `${match[1]}-${match[2]}`;
35
+ throw new Error("Invalid CEP format");
36
+ };
9
37
 
10
38
  export { formatToCep };
@@ -0,0 +1,39 @@
1
+ import type { FormatToCnpjFunction } from "@arkyn/types";
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
3
+
4
+ /**
5
+ * Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
6
+ *
7
+ * The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
8
+ *
9
+ * @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
10
+ * Non-numeric characters will be removed before formatting.
11
+ *
12
+ * @returns A string formatted as a CNPJ.
13
+ *
14
+ * @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { formatToCnpj } from "./formatToCNPJ";
19
+ *
20
+ * const formattedCnpj = formatToCnpj("12345678000195");
21
+ * console.log(formattedCnpj); // Output: "12.345.678/0001-95"
22
+ *
23
+ * try {
24
+ * formatToCnpj("12345");
25
+ * } catch (error) {
26
+ * console.error(error.message); // Output: "Invalid CNPJ length"
27
+ * }
28
+ * ```
29
+ */
30
+
31
+ const formatToCnpj: FormatToCnpjFunction = (value) => {
32
+ const cleaned = removeNonNumeric(value);
33
+ const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
34
+ if (match)
35
+ return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
36
+ throw new Error("Invalid CNPJ length");
37
+ };
38
+
39
+ export { formatToCnpj };
@@ -0,0 +1,39 @@
1
+ import type { FormatToCpfFunction } from "@arkyn/types";
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
3
+
4
+ /**
5
+ * Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
6
+ *
7
+ * A CPF is a Brazilian individual taxpayer registry identification format.
8
+ * This function ensures the input is cleaned of non-numeric characters and
9
+ * then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
10
+ *
11
+ * @param value - The input string to be formatted as a CPF.
12
+ * @returns The formatted CPF string.
13
+ * @throws {Error} If the input string does not match the expected CPF format.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { formatToCpf } from "./formatToCPF";
18
+ *
19
+ * const formattedCpf = formatToCpf("12345678909");
20
+ * console.log(formattedCpf); // Output: "123.456.789-09"
21
+
22
+ * try {
23
+ * const formattedCpf = formatToCpf("12345");
24
+ * } catch (error) {
25
+ * console.error(error.message); // Output: "Invalid CPF format"
26
+ * }
27
+ *
28
+ * ```
29
+ */
30
+
31
+ const formatToCpf: FormatToCpfFunction = (value) => {
32
+ const cleaned = removeNonNumeric(value);
33
+ const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
34
+
35
+ if (match) return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
36
+ throw new Error("Invalid CPF format");
37
+ };
38
+
39
+ export { formatToCpf };
@@ -1,14 +1,38 @@
1
- import { formatToCNPJ } from "./formatToCNPJ";
2
- import { formatToCPF } from "./formatToCPF";
1
+ import type { FormatToCpfCnpjFunction } from "@arkyn/types";
3
2
 
4
- function formatToCpfCnpj(value: string): string {
3
+ import { formatToCnpj } from "./formatToCnpj";
4
+ import { formatToCpf } from "./formatToCpf";
5
+
6
+ /**
7
+ * Formats a given string value into either a CPF or CNPJ format based on its length.
8
+ *
9
+ * - If the input contains 11 numeric characters, it is formatted as a CPF.
10
+ * - If the input contains 14 numeric characters, it is formatted as a CNPJ.
11
+ * - Throws an error if the input length is neither 11 nor 14 after removing non-numeric characters.
12
+ *
13
+ * @param value - The string value to be formatted. It may contain non-numeric characters, which will be removed.
14
+ * @returns The formatted CPF or CNPJ string.
15
+ * @throws {Error} If the input does not have a valid CPF or CNPJ length.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * formatToCpfCnpj("123.456.789-09"); // Returns "123.456.789-09" (CPF format)
20
+ * formatToCpfCnpj("12.345.678/0001-95"); // Returns "12.345.678/0001-95" (CNPJ format)
21
+ * formatToCpfCnpj("12345678909"); // Returns "123.456.789-09" (CPF format)
22
+ * formatToCpfCnpj("12345678000195"); // Returns "12.345.678/0001-95" (CNPJ format)
23
+ * formatToCpfCnpj("123"); // Throws an error: "Invalid CPF or CNPJ length"
24
+ * ```
25
+ */
26
+
27
+ const formatToCpfCnpj: FormatToCpfCnpjFunction = (value) => {
5
28
  const cleaned = value.replace(/\D/g, "");
6
29
  if (cleaned.length === 11) {
7
- return formatToCPF(cleaned);
30
+ return formatToCpf(cleaned);
8
31
  } else if (cleaned.length === 14) {
9
- return formatToCNPJ(cleaned);
32
+ return formatToCnpj(cleaned);
10
33
  }
11
- return value;
12
- }
34
+
35
+ throw new Error("Invalid CPF or CNPJ length");
36
+ };
13
37
 
14
38
  export { formatToCpfCnpj };
@@ -0,0 +1,53 @@
1
+ import { countryCurrencies } from "@arkyn/templates";
2
+ import type { FormatToCurrency } from "@arkyn/types";
3
+
4
+ import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
5
+
6
+ /**
7
+ * Formats a numeric value into a currency string based on the specified currency and configuration.
8
+ *
9
+ * @param value - The numeric value to be formatted.
10
+ * @param currency - The currency code used to determine the formatting style.
11
+ * @param config - Optional configuration object.
12
+ * @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
13
+ *
14
+ * @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
19
+ * console.log(formatted); // "$1,234.56"
20
+ *
21
+ * const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
22
+ * console.log(withoutPrefix); // "1,234.56"
23
+ *
24
+ * const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
25
+ * console.log(formattedBRL); // "R$ 1.234,56"
26
+ *
27
+ * const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
28
+ * console.log(withoutPrefixBRL); // "1.234,56"
29
+ * ```
30
+ */
31
+
32
+ const formatToCurrency: FormatToCurrency = (
33
+ value,
34
+ currency,
35
+ config = { showPrefix: true }
36
+ ) => {
37
+ if (!countryCurrencies[currency]) {
38
+ throw new Error("Unsupported currency code");
39
+ }
40
+
41
+ const { countryCurrency, countryLanguage } = countryCurrencies[currency];
42
+
43
+ const format = new Intl.NumberFormat(countryLanguage, {
44
+ style: "currency",
45
+ currency: countryCurrency,
46
+ }).format(value);
47
+
48
+ return config.showPrefix
49
+ ? format.replace(/\s/g, " ")
50
+ : removeCurrencySymbols(format).replace(/\s/g, " ");
51
+ };
52
+
53
+ export { formatToCurrency };
@@ -1,5 +1,25 @@
1
- function formatToEllipsis(text: string, size = 40): string {
2
- return text.length > size ? `${text.substring(0, size)}...` : text;
3
- }
1
+ import type { FormatToEllipsisFunction } from "@arkyn/types";
2
+
3
+ /**
4
+ * Truncates a given text to a specified maximum length and appends an ellipsis ("...")
5
+ * if the text exceeds the maximum length.
6
+ *
7
+ * @param text - The input string to be truncated.
8
+ * @param maxLength - The maximum allowed length of the string before truncation.
9
+ * @returns The truncated string with an ellipsis if the input exceeds the maximum length,
10
+ * or the original string if it does not.
11
+ * @example
12
+ * const result = formatToEllipsis("Hello, world!", 5);
13
+ * console.log(result); // Output: "Hello..."
14
+ */
15
+
16
+ const formatToEllipsis: FormatToEllipsisFunction = (text, maxLength) => {
17
+ if (text.length > maxLength) {
18
+ let trimmedText = text.substring(0, maxLength).trimEnd();
19
+ trimmedText = trimmedText.replace(/[.,!?;:]$/, "");
20
+ return `${trimmedText}...`;
21
+ }
22
+ return text;
23
+ };
4
24
 
5
25
  export { formatToEllipsis };
@@ -1,12 +1,80 @@
1
- import type { FormatToHiddenDigitsOptions } from "@arkyn/types";
1
+ import type { FormatToHiddenDigitsFunction } from "@arkyn/types";
2
2
 
3
- import { parseToCharacters } from "../parsers/parseToCharacters";
4
- import { normalizeRange, within } from "../services/range";
3
+ const DIGIT = /^\d$/;
5
4
 
6
- const formatToHiddenDigits = (
7
- value: string,
8
- options: FormatToHiddenDigitsOptions = {}
9
- ): string => {
5
+ type DigitCharacterNode = {
6
+ kind: "digit";
7
+ digit: number;
8
+ character: string;
9
+ };
10
+
11
+ type OtherCharacterNode = {
12
+ kind: "other";
13
+ character: string;
14
+ };
15
+
16
+ type RootCharacterNode = {
17
+ kind: "root";
18
+ digits: number;
19
+ children: (DigitCharacterNode | OtherCharacterNode)[];
20
+ };
21
+
22
+ const parseToCharacters = (value: string): RootCharacterNode => {
23
+ let digits = 0;
24
+
25
+ const children = value
26
+ .split("")
27
+ .map((character: string): DigitCharacterNode | OtherCharacterNode => {
28
+ if (DIGIT.test(character))
29
+ return { character, kind: "digit", digit: ++digits };
30
+ return { character, kind: "other" };
31
+ });
32
+
33
+ return { digits, children, kind: "root" };
34
+ };
35
+
36
+ const normalizeRange = (
37
+ range: number | [number, number],
38
+ limit: number
39
+ ): [number, number] => {
40
+ if (Array.isArray(range)) return range;
41
+ if (range >= 0) return [0, range];
42
+ return [limit + 1 - Math.abs(range), limit];
43
+ };
44
+
45
+ const within = (range: [number, number], value: number): boolean =>
46
+ value >= range[0] && value <= range[1];
47
+
48
+ /**
49
+ * Formats a string by hiding specific digits within a given range.
50
+ *
51
+ * This function takes a string input and replaces digits within a specified range
52
+ * with a hiding character (e.g., "*"). Non-digit characters remain unchanged.
53
+ *
54
+ * @param value - The input string to be formatted.
55
+ * @param options - Configuration options for formatting.
56
+ * @param options.range - The range of digits to hide. It can be:
57
+ * - A single number (e.g., `3`), which hides the first `n` digits if positive,
58
+ * or the last `n` digits if negative.
59
+ * - A tuple `[start, end]` specifying the range of digits to hide (inclusive).
60
+ * - Defaults to `3`, hiding the first three digits.
61
+ * @param options.hider - The character used to hide digits. Defaults to `"*"`.
62
+ *
63
+ * @returns The formatted string with specified digits hidden.
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * import { formatToHiddenDigits } from "./formatToHiddenDigits";
68
+ *
69
+ * formatToHiddenDigits("123-456-7890", { range: 3 });
70
+ * // Output: "***-456-7890"
71
+ *
72
+ * formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
73
+ * // Output: "123-###-7890"
74
+ * ```
75
+ */
76
+
77
+ const formatToHiddenDigits: FormatToHiddenDigitsFunction = (value, options) => {
10
78
  const characters = parseToCharacters(value);
11
79
  const range = normalizeRange(options.range ?? 3, characters.digits);
12
80
  return characters.children
@@ -1,10 +1,160 @@
1
- function formatToPhone(value: string): string {
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 type { CountryType, FormatToPhoneFunction } from "@arkyn/types";
3
+
4
+ import { removeNonNumeric } from "../services/removeNonNumeric";
5
+
6
+ function getMask(value: string): "NINE" | "EIGTH" {
7
+ const mask = value.length > 10 ? "NINE" : "EIGTH";
8
+ return mask;
9
+ }
10
+
11
+ const TYPES = {
12
+ EIGTH: "(99) 9999-9999",
13
+ NINE: "(99) 99999-9999",
14
+ };
15
+
16
+ const MAX_LENGTH = removeNonNumeric(TYPES.NINE).length;
17
+
18
+ function applyMask(value: string, maskPattern: string) {
19
+ let result = "";
20
+ let digitIndex = 0;
21
+
22
+ for (let i = 0; i < maskPattern.length; i++) {
23
+ if (maskPattern[i] === "9") {
24
+ if (digitIndex < value.length) {
25
+ result += value[digitIndex];
26
+ digitIndex++;
27
+ } else {
28
+ break;
29
+ }
30
+ } else {
31
+ if (digitIndex < value.length) {
32
+ result += maskPattern[i];
33
+ } else {
34
+ break;
35
+ }
36
+ }
37
+ }
38
+
39
+ return result;
40
+ }
41
+
42
+ function formatPhoneNumber(phoneNumber: string, country: CountryType): string {
43
+ if (country.code === "+55") {
44
+ let value = removeNonNumeric(phoneNumber);
45
+ const mask = getMask(value);
46
+
47
+ let nextLength = value.length;
48
+ if (nextLength > MAX_LENGTH) return value;
49
+
50
+ value = applyMask(value, TYPES[mask] as "EIGTH" | "NINE");
51
+ return value;
52
+ }
53
+
54
+ const mask = country.mask;
55
+ let formattedNumber = mask;
56
+
57
+ if (country.prefix) {
58
+ const prefixRegex = /\$+/g;
59
+ formattedNumber = formattedNumber.replace(prefixRegex, country.prefix);
60
+ }
61
+
62
+ for (
63
+ let i = 0, j = 0;
64
+ i < formattedNumber.length && j < phoneNumber.length;
65
+ i++
66
+ ) {
67
+ if (formattedNumber[i] === "_") {
68
+ formattedNumber =
69
+ formattedNumber.substring(0, i) +
70
+ phoneNumber[j] +
71
+ formattedNumber.substring(i + 1);
72
+ j++;
73
+ }
74
+ }
75
+
76
+ return formattedNumber;
77
+ }
78
+
79
+ function getCountryWithPrefixCode(countryCode: string, prefix: string) {
80
+ const country = countries.find(
81
+ (country) => country.code === countryCode && country.prefix === prefix
82
+ );
83
+
84
+ if (!country) throw new Error("Invalid country code or prefix");
85
+
86
+ if (country.prefix !== prefix) {
87
+ throw new Error("Invalid country code or prefix");
6
88
  }
7
- return value;
89
+
90
+ if (!country.prefix) {
91
+ throw new Error("Invalid country code or prefix");
92
+ }
93
+ return country;
8
94
  }
9
95
 
96
+ function getCountryWithoutPrefixCode(countryCode: string) {
97
+ const country = countries.find((country) => country.code === countryCode);
98
+ if (!country) throw new Error("Invalid country code");
99
+ if (country.prefix) throw new Error("Invalid country code");
100
+ return country;
101
+ }
102
+
103
+ /**
104
+ * Formats a phone number string based on the provided country code and optional prefix.
105
+ *
106
+ * The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
107
+ * The function determines the appropriate formatting mask based on the country and applies it to the phone number.
108
+ *
109
+ * @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
110
+ * Example formats:
111
+ * - "+55-11 912345678"
112
+ * - "+1 1234567890"
113
+ *
114
+ * @returns The formatted phone number string based on the country's formatting rules.
115
+ *
116
+ * @throws {Error} If the input phone number does not match the expected format.
117
+ * @throws {Error} If the country code or phone number is missing from the input string.
118
+ * @throws {Error} If the provided country code and prefix combination is invalid.
119
+ * @throws {Error} If the provided country code is invalid.
120
+ * @throws {Error} If the provided country code has a prefix but none is supplied in the input.
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { formatToPhone } from "./formatToPhone";
125
+ *
126
+ * const formattedPhone1 = formatToPhone("+55 11912345678");
127
+ * console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
128
+ *
129
+ * const formattedPhone2 = formatToPhone("+1-123 4567890");
130
+ * console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
131
+ * ```
132
+ */
133
+
134
+ const formatToPhone: FormatToPhoneFunction = (prop) => {
135
+ const phoneRegex = /^\+\d{1,4}(-\d{1,4})? \d+$/;
136
+
137
+ if (!phoneRegex.test(prop)) {
138
+ throw new Error(
139
+ "Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
140
+ );
141
+ }
142
+
143
+ const countryCode = prop.split(" ")[0].split("-")[0];
144
+ const prefixCode = prop.split(" ")[0].split("-")[1];
145
+ const phoneNumber = prop.split(" ")[1];
146
+
147
+ if (!countryCode || !phoneNumber) {
148
+ throw new Error("Invalid phone number format");
149
+ }
150
+
151
+ if (prefixCode) {
152
+ const country = getCountryWithPrefixCode(countryCode, prefixCode);
153
+ return formatPhoneNumber(phoneNumber, country);
154
+ }
155
+
156
+ const country = getCountryWithoutPrefixCode(countryCode);
157
+ return formatPhoneNumber(phoneNumber, country);
158
+ };
159
+
10
160
  export { formatToPhone };