@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeCurrencySymbols.d.ts","sourceRoot":"","sources":["../../src/services/removeCurrencySymbols.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,qBAAqB,GAAI,gBAAgB,MAAM,KAAG,MAIvD,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Removes currency symbols from a given formatted string.
3
+ *
4
+ * This function takes a string that may contain currency symbols
5
+ * and removes them using a regular expression. The resulting string
6
+ * is also trimmed of any leading or trailing whitespace.
7
+ *
8
+ * @param formattedValue - The input string containing currency symbols.
9
+ * @returns A string with all currency symbols removed and trimmed of whitespace.
10
+ *
11
+ * @example
12
+ * removeCurrencySymbols("R$13,45"); // "13,45"
13
+ * removeCurrencySymbols("$123.45"); // "123.45"
14
+ * removeCurrencySymbols("€99.99"); // "99.99"
15
+ * removeCurrencySymbols("¥1,000"); // "1,000"
16
+ * removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
17
+ */
18
+ const removeCurrencySymbols = (formattedValue) => {
19
+ return formattedValue
20
+ .replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "") // Inclui "R$" e outros símbolos comuns
21
+ .trim();
22
+ };
23
+ export { removeCurrencySymbols };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Removes all non-numeric characters from a given string.
3
+ *
4
+ * @param prop - The input string from which non-numeric characters will be removed.
5
+ * @returns A new string containing only numeric characters from the input.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const result = removeNonNumeric("abc123def456");
10
+ * console.log(result); // Output: "123456"
11
+ * ```
12
+ */
13
+ declare function removeNonNumeric(prop: string): string;
14
+ export { removeNonNumeric };
15
+ //# sourceMappingURL=removeNonNumeric.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeNonNumeric.d.ts","sourceRoot":"","sources":["../../src/services/removeNonNumeric.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,iBAAS,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Removes all non-numeric characters from a given string.
3
+ *
4
+ * @param prop - The input string from which non-numeric characters will be removed.
5
+ * @returns A new string containing only numeric characters from the input.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const result = removeNonNumeric("abc123def456");
10
+ * console.log(result); // Output: "123456"
11
+ * ```
12
+ */
13
+ function removeNonNumeric(prop) {
14
+ return prop.replace(/[^0-9]/g, "");
15
+ }
16
+ export { removeNonNumeric };
@@ -1,3 +1,32 @@
1
- declare function truncateLargeFields(jsonString: string, maxLength?: number): string;
1
+ import type { TruncateLargeFieldsFunction } from "@arkyn/types";
2
+ /**
3
+ * Truncates large string fields in a JSON string to a specified maximum length.
4
+ *
5
+ * This function parses a JSON string, traverses its structure recursively, and truncates
6
+ * any string fields that exceed the specified maximum length. If a string field is truncated,
7
+ * it is replaced with a message indicating the original length of the field.
8
+ *
9
+ * @param jsonString - The JSON string to process.
10
+ * @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
11
+ * @returns A JSON string with large string fields truncated.
12
+ *
13
+ * @throws {Error} Throws an error if the input is not a valid JSON string.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const json = JSON.stringify({
18
+ * name: "John",
19
+ * description: "A very long description that exceeds the maximum length...",
20
+ * nested: {
21
+ * details: "Another long string that needs truncation."
22
+ * }
23
+ * });
24
+ *
25
+ * const result = truncateLargeFields(json, 50);
26
+ * console.log(result);
27
+ * // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
28
+ * ```
29
+ */
30
+ declare const truncateLargeFields: TruncateLargeFieldsFunction;
2
31
  export { truncateLargeFields };
3
32
  //# sourceMappingURL=truncateLargeFields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"truncateLargeFields.d.ts","sourceRoot":"","sources":["../../src/services/truncateLargeFields.ts"],"names":[],"mappings":"AAAA,iBAAS,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,SAAO,GAAG,MAAM,CA6BzE;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
1
+ {"version":3,"file":"truncateLargeFields.d.ts","sourceRoot":"","sources":["../../src/services/truncateLargeFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,QAAA,MAAM,mBAAmB,EAAE,2BAgC1B,CAAC;AAEF,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
@@ -1,5 +1,33 @@
1
- function truncateLargeFields(jsonString, maxLength = 1000) {
2
- function truncateValue(key, value) {
1
+ /**
2
+ * Truncates large string fields in a JSON string to a specified maximum length.
3
+ *
4
+ * This function parses a JSON string, traverses its structure recursively, and truncates
5
+ * any string fields that exceed the specified maximum length. If a string field is truncated,
6
+ * it is replaced with a message indicating the original length of the field.
7
+ *
8
+ * @param jsonString - The JSON string to process.
9
+ * @param maxLength - The maximum allowed length for string fields. Defaults to 1000.
10
+ * @returns A JSON string with large string fields truncated.
11
+ *
12
+ * @throws {Error} Throws an error if the input is not a valid JSON string.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const json = JSON.stringify({
17
+ * name: "John",
18
+ * description: "A very long description that exceeds the maximum length...",
19
+ * nested: {
20
+ * details: "Another long string that needs truncation."
21
+ * }
22
+ * });
23
+ *
24
+ * const result = truncateLargeFields(json, 50);
25
+ * console.log(result);
26
+ * // Output: '{"name":"John","description":"To large information: field as 57 characters","nested":{"details":"To large information: field as 43 characters"}}'
27
+ * ```
28
+ */
29
+ const truncateLargeFields = (jsonString, maxLength = 1000) => {
30
+ function truncateValue(value) {
3
31
  if (typeof value === "string" && value.length > maxLength) {
4
32
  return `To large information: field as ${value.length} characters`;
5
33
  }
@@ -7,15 +35,15 @@ function truncateLargeFields(jsonString, maxLength = 1000) {
7
35
  }
8
36
  function recursiveTruncate(obj) {
9
37
  if (Array.isArray(obj)) {
10
- return obj.map(recursiveTruncate);
38
+ return obj.map((item) => recursiveTruncate(item)); // Corrigido para processar elementos do array
11
39
  }
12
40
  else if (obj !== null && typeof obj === "object") {
13
41
  return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
14
42
  key,
15
- truncateValue(key, recursiveTruncate(value)),
43
+ recursiveTruncate(value), // Corrigido para aplicar recursão corretamente
16
44
  ]));
17
45
  }
18
- return obj;
46
+ return truncateValue(obj); // Corrigido para truncar valores diretamente
19
47
  }
20
48
  try {
21
49
  const parsedJson = JSON.parse(jsonString);
@@ -25,5 +53,5 @@ function truncateLargeFields(jsonString, maxLength = 1000) {
25
53
  catch (error) {
26
54
  throw new Error("Invalid JSON string");
27
55
  }
28
- }
56
+ };
29
57
  export { truncateLargeFields };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validateCpf.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateCpf.spec.d.ts","sourceRoot":"","sources":["../../../src/validations/__test__/validateCpf.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,31 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { validateCpf } from "../validateCpf";
3
+ describe("validateCpf", () => {
4
+ it("should return false for an empty CPF", () => {
5
+ expect(validateCpf("")).toBe(false);
6
+ });
7
+ it("should return false for a CPF with invalid length", () => {
8
+ expect(validateCpf("123.456")).toBe(false);
9
+ expect(validateCpf("123.456.789.123")).toBe(false);
10
+ });
11
+ it("should return false for a CPF with all digits equal", () => {
12
+ expect(validateCpf("111.111.111-11")).toBe(false);
13
+ expect(validateCpf("222.222.222-22")).toBe(false);
14
+ });
15
+ it("should return false for an invalid CPF", () => {
16
+ expect(validateCpf("123.456.789-19")).toBe(false);
17
+ expect(validateCpf("987.654.321-01")).toBe(false);
18
+ });
19
+ it("should return true for a valid CPF", () => {
20
+ expect(validateCpf("111.444.777-35")).toBe(true);
21
+ expect(validateCpf("935.411.347-80")).toBe(true);
22
+ });
23
+ it("should ignore formatting characters and validate correctly", () => {
24
+ expect(validateCpf("11144477735")).toBe(true);
25
+ expect(validateCpf("935.411.347-80")).toBe(true);
26
+ });
27
+ it("should return false for a CPF with non-digit characters", () => {
28
+ expect(validateCpf("111.444.777-3a")).toBe(false);
29
+ expect(validateCpf("935.411.347-8x")).toBe(false);
30
+ });
31
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validateDate.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateDate.spec.d.ts","sourceRoot":"","sources":["../../../src/validations/__test__/validateDate.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,44 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { validateDate } from "../validateDate";
3
+ describe("validateDate", () => {
4
+ it("should return true for valid dates in DD/MM/YYYY format", () => {
5
+ expect(validateDate("31/12/2023")).toBe(true);
6
+ expect(validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" })).toBe(true); // Leap year
7
+ });
8
+ it("should return false for invalid dates in DD/MM/YYYY format", () => {
9
+ expect(validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" })).toBe(false); // April has 30 days
10
+ expect(validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" })).toBe(false); // Not a leap year
11
+ expect(validateDate("32/01/2023", { inputFormat: "DD/MM/YYYY" })).toBe(false); // Invalid day
12
+ });
13
+ it("should return true for valid dates in MM-DD-YYYY format", () => {
14
+ expect(validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(true);
15
+ });
16
+ it("should return false for invalid dates in MM-DD-YYYY format", () => {
17
+ expect(validateDate("04-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(false); // April has 30 days
18
+ expect(validateDate("02-29-2023", { inputFormat: "MM-DD-YYYY" })).toBe(false); // Not a leap year
19
+ expect(validateDate("13-01-2023", { inputFormat: "MM-DD-YYYY" })).toBe(false); // Invalid month
20
+ });
21
+ it("should return true for valid dates in YYYY-MM-DD format", () => {
22
+ expect(validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD" })).toBe(true);
23
+ expect(validateDate("2024-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(true); // Leap year
24
+ });
25
+ it("should return false for invalid dates in YYYY-MM-DD format", () => {
26
+ expect(validateDate("2023-04-31", { inputFormat: "YYYY-MM-DD" })).toBe(false); // April has 30 days
27
+ expect(validateDate("2023-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(false); // Not a leap year
28
+ expect(validateDate("2023-13-01", { inputFormat: "YYYY-MM-DD" })).toBe(false); // Invalid month
29
+ });
30
+ it("should respect minYear and maxYear configuration", () => {
31
+ expect(validateDate("01/01/1899", { minYear: 1900 })).toBe(false); // Below minYear
32
+ expect(validateDate("01/01/1900", { minYear: 1900 })).toBe(true); // Equal to minYear
33
+ expect(validateDate("31/12/3001", { maxYear: 3000 })).toBe(false); // Above maxYear
34
+ expect(validateDate("31/12/3000", { maxYear: 3000 })).toBe(true); // Equal to maxYear
35
+ });
36
+ it("should throw an error for unsupported date formats", () => {
37
+ expect(() => validateDate("2023.12.31", { inputFormat: "YYYY.MM.DD" })).toThrow("Invalid date format");
38
+ });
39
+ it("should return false for completely invalid date strings", () => {
40
+ expect(validateDate("invalid-date")).toBe(false);
41
+ expect(validateDate("12345678")).toBe(false);
42
+ expect(validateDate("")).toBe(false);
43
+ });
44
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validatePhone.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validatePhone.spec.d.ts","sourceRoot":"","sources":["../../../src/validations/__test__/validatePhone.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,32 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { validatePhone } from "../validatePhone";
3
+ describe("validatePhone", () => {
4
+ it("should return true for valid Brazilian phone numbers", () => {
5
+ expect(validatePhone("+55 32912345678")).toBe(true); // Valid with 9th digit
6
+ expect(validatePhone("+55 3212345678")).toBe(true); // Valid without 9th digit
7
+ });
8
+ it("should return false for invalid Brazilian phone numbers", () => {
9
+ expect(validatePhone("+5532912345678")).toBe(false); // Missing space
10
+ expect(validatePhone("+55 123456789")).toBe(false); // Invalid format
11
+ });
12
+ it("should return true for valid American Samoa phone numbers", () => {
13
+ expect(validatePhone("+1-684 1234567")).toBe(true); // Valid format
14
+ });
15
+ it("should return false for invalid phone numbers", () => {
16
+ expect(validatePhone("+1-684 12345")).toBe(false); // Too few digits
17
+ expect(validatePhone("+99 1234567890")).toBe(false); // Non-existent country code
18
+ });
19
+ it("should return true for valid phone numbers from other countries", () => {
20
+ expect(validatePhone("+44 1234567890")).toBe(true); // Example for UK
21
+ expect(validatePhone("+91 9876543210")).toBe(true); // Example for India
22
+ });
23
+ it("should return false for phone numbers with incorrect prefixes", () => {
24
+ expect(validatePhone("+44-20 12345678")).toBe(false); // Invalid prefix for UK
25
+ expect(validatePhone("+91-80 12345678")).toBe(false); // Invalid prefix for India
26
+ });
27
+ it("should handle edge cases gracefully", () => {
28
+ expect(validatePhone("")).toBe(false); // Empty string
29
+ expect(validatePhone("+55")).toBe(false); // Only country code
30
+ expect(validatePhone("+55 329123456789")).toBe(false); // Too many digits
31
+ });
32
+ });
@@ -0,0 +1,24 @@
1
+ import type { ValidateCpfFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
4
+ *
5
+ * The CPF is a unique identifier assigned to Brazilian citizens and residents.
6
+ * This function checks if the provided CPF is valid by performing the following steps:
7
+ * - Removes any non-digit characters from the input.
8
+ * - Verifies if the CPF has the correct length (11 digits).
9
+ * - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
10
+ * - Calculates the first and second verification digits using the CPF algorithm.
11
+ * - Compares the calculated verification digits with the ones provided in the CPF.
12
+ *
13
+ * @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
14
+ * @returns `true` if the CPF is valid, otherwise `false`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * validateCpf("123.456.789-09"); // false
19
+ * validateCpf("111.444.777-35"); // true
20
+ * ```
21
+ */
22
+ declare const validateCpf: ValidateCpfFunction;
23
+ export { validateCpf };
24
+ //# sourceMappingURL=validateCpf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateCpf.d.ts","sourceRoot":"","sources":["../../src/validations/validateCpf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AA6BxD;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAWlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,56 @@
1
+ function removeNonDigits(cpf) {
2
+ return cpf.replace(/\D/g, "");
3
+ }
4
+ function isInvalidLength(cpf) {
5
+ const CPF_LENGTH = 11;
6
+ return cpf.length !== CPF_LENGTH;
7
+ }
8
+ function hasAllDigitsEqual(cpf) {
9
+ const [firstCpfDigit] = cpf;
10
+ return [...cpf].every((digit) => digit === firstCpfDigit);
11
+ }
12
+ function calculateDigit(cpf, factor) {
13
+ let total = 0;
14
+ for (const digit of cpf) {
15
+ if (factor > 1)
16
+ total += parseInt(digit) * factor--;
17
+ }
18
+ const rest = total % 11;
19
+ return rest < 2 ? 0 : 11 - rest;
20
+ }
21
+ function extractDigit(cpf) {
22
+ return cpf.slice(9);
23
+ }
24
+ /**
25
+ * Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
26
+ *
27
+ * The CPF is a unique identifier assigned to Brazilian citizens and residents.
28
+ * This function checks if the provided CPF is valid by performing the following steps:
29
+ * - Removes any non-digit characters from the input.
30
+ * - Verifies if the CPF has the correct length (11 digits).
31
+ * - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
32
+ * - Calculates the first and second verification digits using the CPF algorithm.
33
+ * - Compares the calculated verification digits with the ones provided in the CPF.
34
+ *
35
+ * @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
36
+ * @returns `true` if the CPF is valid, otherwise `false`.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * validateCpf("123.456.789-09"); // false
41
+ * validateCpf("111.444.777-35"); // true
42
+ * ```
43
+ */
44
+ const validateCpf = (rawCpf) => {
45
+ if (!rawCpf)
46
+ return false;
47
+ const cpf = removeNonDigits(rawCpf);
48
+ if (isInvalidLength(cpf))
49
+ return false;
50
+ if (hasAllDigitsEqual(cpf))
51
+ return false;
52
+ const digit1 = calculateDigit(cpf, 10);
53
+ const digit2 = calculateDigit(cpf, 11);
54
+ return extractDigit(cpf) === `${digit1}${digit2}`;
55
+ };
56
+ export { validateCpf };
@@ -1,8 +1,29 @@
1
- type Format = "DD/MM/YYYY" | "MM-DD-YYYY" | "YYYY-MM-DD";
2
- type Config = {
3
- minYear?: number;
4
- maxYear?: number;
5
- };
6
- declare function validateDate(date: string, format: Format, config?: Config): boolean;
1
+ import type { ValidateDateFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a date string based on the provided format and configuration.
4
+ *
5
+ * @param date - The date string to validate.
6
+ * @param config - Optional configuration object to customize validation.
7
+ * @param config.inputFormat - The expected format of the input date.
8
+ * Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
9
+ * Defaults to "DD/MM/YYYY".
10
+ * @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
11
+ * @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
12
+ *
13
+ * @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
14
+ *
15
+ * @throws {Error} If an invalid date format is provided in the configuration.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * validateDate("31/12/2023"); // true
20
+ * validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
21
+ * validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
22
+ * validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
23
+ * validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
24
+ * validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
25
+ * ```
26
+ */
27
+ declare const validateDate: ValidateDateFunction;
7
28
  export { validateDate };
8
29
  //# sourceMappingURL=validateDate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validateDate.d.ts","sourceRoot":"","sources":["../../src/validations/validateDate.ts"],"names":[],"mappings":"AAAA,KAAK,MAAM,GAAG,YAAY,GAAG,YAAY,GAAG,YAAY,CAAC;AAEzD,KAAK,MAAM,GAAG;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,iBAAS,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CA6C5E;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
1
+ {"version":3,"file":"validateDate.d.ts","sourceRoot":"","sources":["../../src/validations/validateDate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBA8CnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -1,20 +1,46 @@
1
- function validateDate(date, format, config) {
1
+ /**
2
+ * Validates a date string based on the provided format and configuration.
3
+ *
4
+ * @param date - The date string to validate.
5
+ * @param config - Optional configuration object to customize validation.
6
+ * @param config.inputFormat - The expected format of the input date.
7
+ * Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
8
+ * Defaults to "DD/MM/YYYY".
9
+ * @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
10
+ * @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
11
+ *
12
+ * @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
13
+ *
14
+ * @throws {Error} If an invalid date format is provided in the configuration.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * validateDate("31/12/2023"); // true
19
+ * validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
20
+ * validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
21
+ * validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
22
+ * validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
23
+ * validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
24
+ * ```
25
+ */
26
+ const validateDate = (date, config) => {
2
27
  let day, month, year;
3
- const minYear = config?.minYear || 0;
28
+ const inputFormat = config?.inputFormat || "DD/MM/YYYY";
29
+ const minYear = config?.minYear || 1900;
4
30
  const maxYear = config?.maxYear || 3000;
5
- if (format === "DD/MM/YYYY") {
31
+ if (inputFormat === "DD/MM/YYYY") {
6
32
  const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
7
33
  if (!dateRegex.test(date))
8
34
  return false;
9
35
  [, day, month, year] = date.match(dateRegex) || [];
10
36
  }
11
- else if (format === "MM-DD-YYYY") {
37
+ else if (inputFormat === "MM-DD-YYYY") {
12
38
  const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
13
39
  if (!dateRegex.test(date))
14
40
  return false;
15
41
  [, month, day, year] = date.match(dateRegex) || [];
16
42
  }
17
- else if (format === "YYYY-MM-DD") {
43
+ else if (inputFormat === "YYYY-MM-DD") {
18
44
  const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
19
45
  if (!dateRegex.test(date))
20
46
  return false;
@@ -43,5 +69,5 @@ function validateDate(date, format, config) {
43
69
  return false;
44
70
  const isValidDate = new Date(yearNum, monthNum - 1, dayNum).getDate() === dayNum;
45
71
  return isValidDate;
46
- }
72
+ };
47
73
  export { validateDate };
@@ -1,3 +1,29 @@
1
- declare function validatePhone(phone: string): boolean;
1
+ import type { ValidatePhoneFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a phone number against a list of country-specific formats.
4
+ *
5
+ * The function iterates through a predefined list of countries and checks if the
6
+ * provided phone number matches the format for any of the countries. It uses
7
+ * regular expressions to validate the phone number based on the country's code,
8
+ * prefix, and mask.
9
+ *
10
+ * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
+ * allows for an optional ninth digit.
12
+ *
13
+ * @param phone - The phone number to validate as a string.
14
+ * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { validatePhone } from "./validatePhone";
19
+ *
20
+ * validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
21
+ * validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
22
+ * validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
23
+ * validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
24
+ * validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
25
+ * ```
26
+ */
27
+ declare const validatePhone: ValidatePhoneFunction;
2
28
  export { validatePhone };
3
29
  //# sourceMappingURL=validatePhone.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"AAEA,iBAAS,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAiB7C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
1
+ {"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAiBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -1,5 +1,30 @@
1
1
  import { countries } from "@arkyn/templates";
2
- function validatePhone(phone) {
2
+ /**
3
+ * Validates a phone number against a list of country-specific formats.
4
+ *
5
+ * The function iterates through a predefined list of countries and checks if the
6
+ * provided phone number matches the format for any of the countries. It uses
7
+ * regular expressions to validate the phone number based on the country's code,
8
+ * prefix, and mask.
9
+ *
10
+ * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
+ * allows for an optional ninth digit.
12
+ *
13
+ * @param phone - The phone number to validate as a string.
14
+ * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { validatePhone } from "./validatePhone";
19
+ *
20
+ * validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
21
+ * validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
22
+ * validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
23
+ * validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
24
+ * validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
25
+ * ```
26
+ */
27
+ const validatePhone = (phone) => {
3
28
  for (const country of countries) {
4
29
  const countryCode = country.code;
5
30
  const prefix = country.prefix ? `-${country.prefix}` : "";
@@ -15,5 +40,5 @@ function validatePhone(phone) {
15
40
  return true;
16
41
  }
17
42
  return false;
18
- }
43
+ };
19
44
  export { validatePhone };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/shared",
3
- "version": "1.4.52",
3
+ "version": "2.0.1-beta.1",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
@@ -8,10 +8,12 @@
8
8
  "scripts": {
9
9
  "clean": "rm -rf dist",
10
10
  "build": "bun run clean && bunx tsc --project tsconfig.json",
11
+ "test": "vitest --config ./vitest.config.ts",
11
12
  "typecheck": "bunx tsc --project tsconfig.json --noEmit"
12
13
  },
13
14
  "dependencies": {
14
- "uuid": "^10.0.0"
15
+ "uuid": "^10.0.0",
16
+ "vitest": "^3.1.1"
15
17
  },
16
18
  "devDependencies": {
17
19
  "@types/uuid": "^10.0.0",