@arkyn/shared 1.5.0 → 1.5.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 (179) hide show
  1. package/LICENSE.txt +24 -0
  2. package/README.md +113 -0
  3. package/dist/formats/formatDate.d.ts +43 -0
  4. package/dist/formats/formatDate.d.ts.map +1 -0
  5. package/dist/formats/formatDate.js +77 -0
  6. package/dist/formats/formatJsonObject.d.ts +31 -1
  7. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  8. package/dist/formats/formatJsonObject.js +67 -26
  9. package/dist/formats/formatJsonString.d.ts +37 -1
  10. package/dist/formats/formatJsonString.d.ts.map +1 -1
  11. package/dist/formats/formatJsonString.js +38 -4
  12. package/dist/formats/formatToCep.d.ts +27 -1
  13. package/dist/formats/formatToCep.d.ts.map +1 -1
  14. package/dist/formats/formatToCep.js +31 -6
  15. package/dist/formats/formatToCnpj.d.ts +30 -0
  16. package/dist/formats/formatToCnpj.d.ts.map +1 -0
  17. package/dist/formats/formatToCnpj.js +35 -0
  18. package/dist/formats/formatToCpf.d.ts +30 -0
  19. package/dist/formats/formatToCpf.d.ts.map +1 -0
  20. package/dist/formats/formatToCpf.js +35 -0
  21. package/dist/formats/formatToCpfCnpj.d.ts +22 -1
  22. package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
  23. package/dist/formats/formatToCpfCnpj.js +27 -7
  24. package/dist/formats/formatToCurrency.d.ts +29 -0
  25. package/dist/formats/formatToCurrency.d.ts.map +1 -0
  26. package/dist/formats/formatToCurrency.js +41 -0
  27. package/dist/formats/formatToEllipsis.d.ts +14 -1
  28. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  29. package/dist/formats/formatToEllipsis.js +20 -3
  30. package/dist/formats/formatToHiddenDigits.d.ts +30 -2
  31. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  32. package/dist/formats/formatToHiddenDigits.js +49 -3
  33. package/dist/formats/formatToPhone.d.ts +32 -1
  34. package/dist/formats/formatToPhone.d.ts.map +1 -1
  35. package/dist/formats/formatToPhone.js +128 -6
  36. package/dist/generators/generateColorByString.d.ts +13 -1
  37. package/dist/generators/generateColorByString.d.ts.map +1 -1
  38. package/dist/generators/generateColorByString.js +15 -4
  39. package/dist/generators/generateId.d.ts +26 -4
  40. package/dist/generators/generateId.d.ts.map +1 -1
  41. package/dist/generators/generateId.js +11 -5
  42. package/dist/generators/generateSlug.d.ts +15 -1
  43. package/dist/generators/generateSlug.d.ts.map +1 -1
  44. package/dist/generators/generateSlug.js +16 -2
  45. package/dist/index.d.ts +9 -10
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +10 -12
  48. package/dist/services/calculateCardInstallment.d.ts +31 -5
  49. package/dist/services/calculateCardInstallment.d.ts.map +1 -1
  50. package/dist/services/calculateCardInstallment.js +52 -11
  51. package/dist/services/maskSensitiveData.d.ts +24 -1
  52. package/dist/services/maskSensitiveData.d.ts.map +1 -1
  53. package/dist/services/maskSensitiveData.js +24 -2
  54. package/dist/services/removeCurrencySymbols.d.ts +20 -0
  55. package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
  56. package/dist/services/removeCurrencySymbols.js +23 -0
  57. package/dist/services/removeNonNumeric.d.ts +15 -0
  58. package/dist/services/removeNonNumeric.d.ts.map +1 -0
  59. package/dist/services/removeNonNumeric.js +16 -0
  60. package/dist/services/truncateLargeFields.d.ts +30 -1
  61. package/dist/services/truncateLargeFields.d.ts.map +1 -1
  62. package/dist/services/truncateLargeFields.js +34 -6
  63. package/dist/validations/validateCep.d.ts +24 -0
  64. package/dist/validations/validateCep.d.ts.map +1 -0
  65. package/dist/validations/validateCep.js +33 -0
  66. package/dist/validations/validateCnpj.d.ts +22 -0
  67. package/dist/validations/validateCnpj.d.ts.map +1 -0
  68. package/dist/validations/validateCnpj.js +52 -0
  69. package/dist/validations/validateCpf.d.ts +24 -0
  70. package/dist/validations/validateCpf.d.ts.map +1 -0
  71. package/dist/validations/validateCpf.js +54 -0
  72. package/dist/validations/validateDate.d.ts +27 -6
  73. package/dist/validations/validateDate.d.ts.map +1 -1
  74. package/dist/validations/validateDate.js +38 -12
  75. package/dist/validations/validatePassword.d.ts +21 -0
  76. package/dist/validations/validatePassword.d.ts.map +1 -0
  77. package/dist/validations/validatePassword.js +34 -0
  78. package/dist/validations/validatePhone.d.ts +27 -1
  79. package/dist/validations/validatePhone.d.ts.map +1 -1
  80. package/dist/validations/validatePhone.js +29 -4
  81. package/dist/validations/validateRg.d.ts +22 -0
  82. package/dist/validations/validateRg.d.ts.map +1 -0
  83. package/dist/validations/validateRg.js +31 -0
  84. package/package.json +7 -2
  85. package/src/formats/__test__/formatDate.spec.ts +88 -0
  86. package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
  87. package/src/formats/__test__/formatJsonString.spec.ts +83 -0
  88. package/src/formats/__test__/formatToCep.spec.ts +37 -0
  89. package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
  90. package/src/formats/__test__/formatToCpf.spec.ts +37 -0
  91. package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
  92. package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
  93. package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
  94. package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
  95. package/src/formats/__test__/formatToPhone.spec.ts +58 -0
  96. package/src/formats/formatDate.ts +97 -0
  97. package/src/formats/formatJsonObject.ts +68 -26
  98. package/src/formats/formatJsonString.ts +40 -4
  99. package/src/formats/formatToCep.ts +35 -7
  100. package/src/formats/formatToCnpj.ts +39 -0
  101. package/src/formats/formatToCpf.ts +39 -0
  102. package/src/formats/formatToCpfCnpj.ts +31 -7
  103. package/src/formats/formatToCurrency.ts +53 -0
  104. package/src/formats/formatToEllipsis.ts +23 -3
  105. package/src/formats/formatToHiddenDigits.ts +75 -7
  106. package/src/formats/formatToPhone.ts +156 -6
  107. package/src/generators/__test__/generateColorByString.spec.ts +37 -0
  108. package/src/generators/__test__/generateId.spec.ts +44 -0
  109. package/src/generators/__test__/generateSlug.spec.ts +47 -0
  110. package/src/generators/generateColorByString.ts +18 -4
  111. package/src/generators/generateId.ts +36 -5
  112. package/src/generators/generateSlug.ts +17 -2
  113. package/src/index.ts +10 -13
  114. package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
  115. package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
  116. package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
  117. package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
  118. package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
  119. package/src/services/calculateCardInstallment.ts +57 -12
  120. package/src/services/maskSensitiveData.ts +29 -4
  121. package/src/services/removeCurrencySymbols.ts +25 -0
  122. package/src/services/removeNonNumeric.ts +18 -0
  123. package/src/services/truncateLargeFields.ts +40 -6
  124. package/src/validations/__test__/validateCep.spec.ts +40 -0
  125. package/src/validations/__test__/validateCnpj.spec.ts +42 -0
  126. package/src/validations/__test__/validateCpf.spec.ts +38 -0
  127. package/src/validations/__test__/validateDate.spec.ts +81 -0
  128. package/src/validations/__test__/validatePassword.spec.ts +43 -0
  129. package/src/validations/__test__/validatePhone.spec.ts +39 -0
  130. package/src/validations/__test__/validateRg.spec.ts +48 -0
  131. package/src/validations/validateCep.ts +40 -0
  132. package/src/validations/validateCnpj.ts +64 -0
  133. package/src/validations/validateCpf.ts +61 -0
  134. package/src/validations/validateDate.ts +39 -17
  135. package/src/validations/validatePassword.ts +41 -0
  136. package/src/validations/validatePhone.ts +31 -4
  137. package/src/validations/validateRg.ts +37 -0
  138. package/tsconfig.json +2 -2
  139. package/vitest.config.ts +5 -0
  140. package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
  141. package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
  142. package/dist/formats/formatBrazilianDateHour.js +0 -12
  143. package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
  144. package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
  145. package/dist/formats/formatBrazilianDateToDate.js +0 -9
  146. package/dist/formats/formatDateHour.d.ts +0 -3
  147. package/dist/formats/formatDateHour.d.ts.map +0 -1
  148. package/dist/formats/formatDateHour.js +0 -11
  149. package/dist/formats/formatToBRL.d.ts +0 -3
  150. package/dist/formats/formatToBRL.d.ts.map +0 -1
  151. package/dist/formats/formatToBRL.js +0 -8
  152. package/dist/formats/formatToCNPJ.d.ts +0 -3
  153. package/dist/formats/formatToCNPJ.d.ts.map +0 -1
  154. package/dist/formats/formatToCNPJ.js +0 -9
  155. package/dist/formats/formatToCPF.d.ts +0 -3
  156. package/dist/formats/formatToCPF.d.ts.map +0 -1
  157. package/dist/formats/formatToCPF.js +0 -9
  158. package/dist/parsers/parseToCharacters.d.ts +0 -17
  159. package/dist/parsers/parseToCharacters.d.ts.map +0 -1
  160. package/dist/parsers/parseToCharacters.js +0 -13
  161. package/dist/services/range.d.ts +0 -4
  162. package/dist/services/range.d.ts.map +0 -1
  163. package/dist/services/range.js +0 -9
  164. package/dist/validations/regex.d.ts +0 -15
  165. package/dist/validations/regex.d.ts.map +0 -1
  166. package/dist/validations/regex.js +0 -25
  167. package/dist/validations/validateCPF.d.ts +0 -3
  168. package/dist/validations/validateCPF.d.ts.map +0 -1
  169. package/dist/validations/validateCPF.js +0 -36
  170. package/src/formats/formatBrazilianDateHour.ts +0 -17
  171. package/src/formats/formatBrazilianDateToDate.ts +0 -13
  172. package/src/formats/formatDateHour.ts +0 -15
  173. package/src/formats/formatToBRL.ts +0 -10
  174. package/src/formats/formatToCNPJ.ts +0 -10
  175. package/src/formats/formatToCPF.ts +0 -10
  176. package/src/parsers/parseToCharacters.ts +0 -34
  177. package/src/services/range.ts +0 -15
  178. package/src/validations/regex.ts +0 -41
  179. package/src/validations/validateCPF.ts +0 -38
@@ -0,0 +1,83 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import { formatJsonString } from "../formatJsonString";
3
+
4
+ describe("formatJsonString", () => {
5
+ it("should format a valid JSON string", () => {
6
+ const jsonString =
7
+ '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
8
+ const result = formatJsonString(jsonString);
9
+ expect(result).toBe(`{
10
+ "name": "John",
11
+ "age": 30,
12
+ "hobbies": [
13
+ "reading",
14
+ "gaming"
15
+ ]
16
+ }`);
17
+ });
18
+
19
+ it("should handle a nested JSON string", () => {
20
+ const jsonString =
21
+ '{"user":{"name":"John","details":{"age":30,"city":"New York"}}}';
22
+ const result = formatJsonString(jsonString);
23
+ expect(result).toBe(`{
24
+ "user": {
25
+ "name": "John",
26
+ "details": {
27
+ "age": 30,
28
+ "city": "New York"
29
+ }
30
+ }
31
+ }`);
32
+ });
33
+
34
+ it("should handle an empty JSON object", () => {
35
+ const jsonString = "{}";
36
+ const result = formatJsonString(jsonString);
37
+ expect(result).toBe(`{}`);
38
+ });
39
+
40
+ it("should handle an empty JSON array", () => {
41
+ const jsonString = "[]";
42
+ const result = formatJsonString(jsonString);
43
+ expect(result).toBe(`[]`);
44
+ });
45
+
46
+ it("should handle a JSON array", () => {
47
+ const jsonString = '["apple","banana","cherry"]';
48
+ const result = formatJsonString(jsonString);
49
+ expect(result).toBe(`[
50
+ "apple",
51
+ "banana",
52
+ "cherry"
53
+ ]`);
54
+ });
55
+
56
+ it("should return an empty string for invalid JSON", () => {
57
+ const invalidJsonString = '{"name":"John", "age":30,';
58
+ expect(() => formatJsonString(invalidJsonString)).toThrow();
59
+ });
60
+
61
+ it("should log an error for invalid JSON", () => {
62
+ const invalidJsonString = '{"name":"John", "age":30,';
63
+ expect(() => formatJsonString(invalidJsonString)).toThrow();
64
+ });
65
+
66
+ it("should handle a primitive JSON value", () => {
67
+ const jsonString = "42";
68
+ const result = formatJsonString(jsonString);
69
+ expect(result).toBe("42");
70
+ });
71
+
72
+ it("should handle a JSON string value", () => {
73
+ const jsonString = '"hello"';
74
+ const result = formatJsonString(jsonString);
75
+ expect(result).toBe('"hello"');
76
+ });
77
+
78
+ it("should handle null JSON value", () => {
79
+ const jsonString = "null";
80
+ const result = formatJsonString(jsonString);
81
+ expect(result).toBe("null");
82
+ });
83
+ });
@@ -0,0 +1,37 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToCep } from "../formatToCep";
3
+
4
+ describe("formatToCep", () => {
5
+ it("should format a valid CEP with only numeric characters", () => {
6
+ const result = formatToCep("12345678");
7
+ expect(result).toBe("12345-678");
8
+ });
9
+
10
+ it("should format a valid CEP with non-numeric characters", () => {
11
+ const result = formatToCep("12345-678");
12
+ expect(result).toBe("12345-678");
13
+ });
14
+
15
+ it("should throw an error for invalid CEP length", () => {
16
+ expect(() => formatToCep("1234")).toThrow("Invalid CEP format");
17
+ expect(() => formatToCep("123456789")).toThrow("Invalid CEP format");
18
+ });
19
+
20
+ it("should handle empty input and throw an error", () => {
21
+ expect(() => formatToCep("")).toThrow("Invalid CEP format");
22
+ });
23
+
24
+ it("should ignore non-numeric characters and format correctly", () => {
25
+ const result = formatToCep("12345-678abc");
26
+ expect(result).toBe("12345-678");
27
+ });
28
+
29
+ it("should format a CEP with extra spaces", () => {
30
+ const result = formatToCep(" 12345678 ");
31
+ expect(result).toBe("12345-678");
32
+ });
33
+
34
+ it("should throw an error for completely invalid input", () => {
35
+ expect(() => formatToCep("abcdefg")).toThrow("Invalid CEP format");
36
+ });
37
+ });
@@ -0,0 +1,35 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToCnpj } from "../formatToCnpj";
3
+
4
+ describe("formatToCnpj", () => {
5
+ it("should format a valid CNPJ with only numeric characters", () => {
6
+ const result = formatToCnpj("12345678000195");
7
+ expect(result).toBe("12.345.678/0001-95");
8
+ });
9
+
10
+ it("should format a valid CNPJ with non-numeric characters", () => {
11
+ const result = formatToCnpj("12.345.678/0001-95");
12
+ expect(result).toBe("12.345.678/0001-95");
13
+ });
14
+
15
+ it("should throw an error for invalid CNPJ length", () => {
16
+ expect(() => formatToCnpj("123")).toThrow("Invalid CNPJ length");
17
+ expect(() => formatToCnpj("123456789012345")).toThrow(
18
+ "Invalid CNPJ length"
19
+ );
20
+ });
21
+
22
+ it("should handle empty input and throw an error", () => {
23
+ expect(() => formatToCnpj("")).toThrow("Invalid CNPJ length");
24
+ });
25
+
26
+ it("should ignore non-numeric characters and format correctly", () => {
27
+ const result = formatToCnpj("12.345.678/0001-95abc");
28
+ expect(result).toBe("12.345.678/0001-95");
29
+ });
30
+
31
+ it("should format a CNPJ with extra spaces", () => {
32
+ const result = formatToCnpj(" 12345678000195 ");
33
+ expect(result).toBe("12.345.678/0001-95");
34
+ });
35
+ });
@@ -0,0 +1,37 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToCpf } from "../formatToCpf";
3
+
4
+ describe("formatToCpf", () => {
5
+ it("should format a valid CPF with only numeric characters", () => {
6
+ const result = formatToCpf("12345678909");
7
+ expect(result).toBe("123.456.789-09");
8
+ });
9
+
10
+ it("should format a valid CPF with non-numeric characters", () => {
11
+ const result = formatToCpf("123.456.789-09");
12
+ expect(result).toBe("123.456.789-09");
13
+ });
14
+
15
+ it("should throw an error for invalid CPF length", () => {
16
+ expect(() => formatToCpf("12345")).toThrow("Invalid CPF format");
17
+ expect(() => formatToCpf("123456789012")).toThrow("Invalid CPF format");
18
+ });
19
+
20
+ it("should handle empty input and throw an error", () => {
21
+ expect(() => formatToCpf("")).toThrow("Invalid CPF format");
22
+ });
23
+
24
+ it("should ignore non-numeric characters and format correctly", () => {
25
+ const result = formatToCpf("123.456.789-09abc");
26
+ expect(result).toBe("123.456.789-09");
27
+ });
28
+
29
+ it("should format a CPF with extra spaces", () => {
30
+ const result = formatToCpf(" 12345678909 ");
31
+ expect(result).toBe("123.456.789-09");
32
+ });
33
+
34
+ it("should throw an error for completely invalid input", () => {
35
+ expect(() => formatToCpf("abcdefg")).toThrow("Invalid CPF format");
36
+ });
37
+ });
@@ -0,0 +1,43 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToCpfCnpj } from "../formatToCpfCnpj";
3
+
4
+ describe("formatToCpfCnpj", () => {
5
+ it("should format a valid CPF with non-numeric characters", () => {
6
+ const result = formatToCpfCnpj("123.456.789-09");
7
+ expect(result).toBe("123.456.789-09");
8
+ });
9
+
10
+ it("should format a valid CPF with only numeric characters", () => {
11
+ const result = formatToCpfCnpj("12345678909");
12
+ expect(result).toBe("123.456.789-09");
13
+ });
14
+
15
+ it("should format a valid CNPJ with non-numeric characters", () => {
16
+ const result = formatToCpfCnpj("12.345.678/0001-95");
17
+ expect(result).toBe("12.345.678/0001-95");
18
+ });
19
+
20
+ it("should format a valid CNPJ with only numeric characters", () => {
21
+ const result = formatToCpfCnpj("12345678000195");
22
+ expect(result).toBe("12.345.678/0001-95");
23
+ });
24
+
25
+ it("should throw an error for invalid CPF or CNPJ length", () => {
26
+ expect(() => formatToCpfCnpj("123")).toThrow("Invalid CPF or CNPJ length");
27
+ expect(() => formatToCpfCnpj("123456")).toThrow(
28
+ "Invalid CPF or CNPJ length"
29
+ );
30
+ expect(() => formatToCpfCnpj("123456789012345")).toThrow(
31
+ "Invalid CPF or CNPJ length"
32
+ );
33
+ });
34
+
35
+ it("should handle empty input and throw an error", () => {
36
+ expect(() => formatToCpfCnpj("")).toThrow("Invalid CPF or CNPJ length");
37
+ });
38
+
39
+ it("should ignore non-numeric characters and format correctly", () => {
40
+ const result = formatToCpfCnpj("123.456.789-09abc");
41
+ expect(result).toBe("123.456.789-09");
42
+ });
43
+ });
@@ -0,0 +1,50 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToCurrency } from "../formatToCurrency";
3
+
4
+ describe("formatToCurrency", () => {
5
+ it("should format a number to USD currency with prefix", () => {
6
+ const result = formatToCurrency(1234.56, "USD", { showPrefix: true });
7
+ expect(result).toBe("$1,234.56");
8
+ });
9
+
10
+ it("should format a number to USD currency without prefix", () => {
11
+ const result = formatToCurrency(1234.56, "USD", { showPrefix: false });
12
+ expect(result).toBe("1,234.56");
13
+ });
14
+
15
+ it("should format a number to BRL currency with prefix", () => {
16
+ const result = formatToCurrency(1234.56, "BRL", { showPrefix: true });
17
+ expect(result).toBe("R$ 1.234,56");
18
+ });
19
+
20
+ it("should format a number to BRL currency without prefix", () => {
21
+ const result = formatToCurrency(1234.56, "BRL", { showPrefix: false });
22
+ expect(result).toBe("1.234,56");
23
+ });
24
+
25
+ it("should format a number to EUR currency with prefix", () => {
26
+ const result = formatToCurrency(1234.56, "EUR", { showPrefix: true });
27
+ expect(result).toEqual("1.234,56 €");
28
+ });
29
+
30
+ it("should format a number to EUR currency without prefix", () => {
31
+ const result = formatToCurrency(1234.56, "EUR", { showPrefix: false });
32
+ expect(result).toBe("1.234,56");
33
+ });
34
+
35
+ it("should handle zero value correctly", () => {
36
+ const result = formatToCurrency(0, "USD", { showPrefix: true });
37
+ expect(result).toBe("$0.00");
38
+ });
39
+
40
+ it("should handle negative values correctly", () => {
41
+ const result = formatToCurrency(-1234.56, "USD", { showPrefix: true });
42
+ expect(result).toBe("-$1,234.56");
43
+ });
44
+
45
+ it("should throw an error for unsupported currency codes", () => {
46
+ expect(() =>
47
+ formatToCurrency(1234.56, "XYZ" as any, { showPrefix: true })
48
+ ).toThrow("Unsupported currency code");
49
+ });
50
+ });
@@ -0,0 +1,44 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToEllipsis } from "../formatToEllipsis";
3
+
4
+ describe("formatToEllipsis", () => {
5
+ it("should truncate text and append ellipsis if it exceeds the maxLength", () => {
6
+ const result = formatToEllipsis("Hello, world!", 5);
7
+ expect(result).toBe("Hello...");
8
+ });
9
+
10
+ it("should return the original text if it does not exceed the maxLength", () => {
11
+ const result = formatToEllipsis("Hello", 10);
12
+ expect(result).toBe("Hello");
13
+ });
14
+
15
+ it("should handle empty strings", () => {
16
+ const result = formatToEllipsis("", 5);
17
+ expect(result).toBe("");
18
+ });
19
+
20
+ it("should handle maxLength of 0", () => {
21
+ const result = formatToEllipsis("Hello, world!", 0);
22
+ expect(result).toBe("...");
23
+ });
24
+
25
+ it("should handle maxLength greater than the text length", () => {
26
+ const result = formatToEllipsis("Short text", 20);
27
+ expect(result).toBe("Short text");
28
+ });
29
+
30
+ it("should handle text exactly equal to maxLength", () => {
31
+ const result = formatToEllipsis("Exact", 5);
32
+ expect(result).toBe("Exact");
33
+ });
34
+
35
+ it("should handle special characters in the text", () => {
36
+ const result = formatToEllipsis("Olá, mundo!", 4);
37
+ expect(result).toBe("Olá...");
38
+ });
39
+
40
+ it("should handle whitespace correctly", () => {
41
+ const result = formatToEllipsis("Hello world", 6);
42
+ expect(result).toBe("Hello...");
43
+ });
44
+ });
@@ -0,0 +1,58 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToHiddenDigits } from "../formatToHiddenDigits";
3
+
4
+ describe("formatToHiddenDigits", () => {
5
+ it("should hide the first 3 digits by default", () => {
6
+ const result = formatToHiddenDigits("123-456-7890", {});
7
+ expect(result).toBe("***-456-7890");
8
+ });
9
+
10
+ it("should hide the first n digits when range is a positive number", () => {
11
+ const result = formatToHiddenDigits("123-456-7890", { range: 5 });
12
+ expect(result).toBe("***-**6-7890");
13
+ });
14
+
15
+ it("should hide the last n digits when range is a negative number", () => {
16
+ const result = formatToHiddenDigits("123-456-7890", { range: -4 });
17
+ expect(result).toBe("123-456-****");
18
+ });
19
+
20
+ it("should hide digits within a specific range", () => {
21
+ const result = formatToHiddenDigits("123-456-7890", { range: [4, 6] });
22
+ expect(result).toBe("123-***-7890");
23
+ });
24
+
25
+ it("should use a custom hider character", () => {
26
+ const result = formatToHiddenDigits("123-456-7890", {
27
+ range: 3,
28
+ hider: "#",
29
+ });
30
+
31
+ expect(result).toBe("###-456-7890");
32
+ });
33
+
34
+ it("should handle strings with no digits", () => {
35
+ const result = formatToHiddenDigits("abc-def-ghij", { range: 3 });
36
+ expect(result).toBe("abc-def-ghij");
37
+ });
38
+
39
+ it("should handle empty strings", () => {
40
+ const result = formatToHiddenDigits("", { range: 3 });
41
+ expect(result).toBe("");
42
+ });
43
+
44
+ it("should handle ranges larger than the number of digits", () => {
45
+ const result = formatToHiddenDigits("123-456", { range: 10 });
46
+ expect(result).toBe("***-***");
47
+ });
48
+
49
+ it("should handle ranges that do not overlap with any digits", () => {
50
+ const result = formatToHiddenDigits("123-456-7890", { range: [11, 12] });
51
+ expect(result).toBe("123-456-7890");
52
+ });
53
+
54
+ it("should hide all digits when range covers all digits", () => {
55
+ const result = formatToHiddenDigits("123-456-7890", { range: [1, 10] });
56
+ expect(result).toBe("***-***-****");
57
+ });
58
+ });
@@ -0,0 +1,58 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { formatToPhone } from "../formatToPhone";
3
+
4
+ describe("formatToPhone", () => {
5
+ it("should format a Brazilian phone number with 9 digits", () => {
6
+ const result = formatToPhone("+55 11912345678");
7
+ expect(result).toBe("(11) 91234-5678");
8
+ });
9
+
10
+ it("should format a Brazilian phone number with 8 digits", () => {
11
+ const result = formatToPhone("+55 1123456789");
12
+ expect(result).toBe("(11) 2345-6789");
13
+ });
14
+
15
+ it("should format a US phone number with a prefix", () => {
16
+ const result = formatToPhone("+1-408 4567890");
17
+ expect(result).toBe("(408) 456-7890");
18
+ });
19
+
20
+ it("should format a US phone number without a prefix", () => {
21
+ expect(() => formatToPhone("+1 4567890")).toThrow("Invalid country code");
22
+ });
23
+
24
+ it("should throw an error for invalid phone number format", () => {
25
+ expect(() => formatToPhone("+55")).toThrow("Invalid phone number format");
26
+ expect(() => formatToPhone("+55-11")).toThrow(
27
+ "Invalid phone number format"
28
+ );
29
+ });
30
+
31
+ it("should throw an error for unrecognized country code", () => {
32
+ expect(() => formatToPhone("+99 123456789")).toThrow(
33
+ "Invalid country code"
34
+ );
35
+ });
36
+
37
+ it("should throw an error for unrecognized country code or prefix", () => {
38
+ expect(() => formatToPhone("+1-999 123456789")).toThrow(
39
+ "Invalid country code or prefix"
40
+ );
41
+ });
42
+
43
+ it("should handle empty input and throw an error", () => {
44
+ expect(() => formatToPhone("")).toThrow("Invalid phone number format");
45
+ });
46
+
47
+ it("should format a phone number with special characters removed", () => {
48
+ expect(() => formatToPhone("+55 11 91234-5678")).toThrow(
49
+ "Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
50
+ );
51
+ });
52
+
53
+ it("should format a phone number with spaces and special characters", () => {
54
+ expect(() => formatToPhone("+55 11 91234 5678")).toThrow(
55
+ "Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
56
+ );
57
+ });
58
+ });
@@ -0,0 +1,97 @@
1
+ import type { FormatDateFunction } from "@arkyn/types";
2
+
3
+ function formatDateString(date: Date, format: string): string {
4
+ const pad = (num: number) => num.toString().padStart(2, "0");
5
+
6
+ const replacements: Record<string, string> = {
7
+ YYYY: date.getFullYear().toString(),
8
+ YY: date.getFullYear().toString().slice(-2),
9
+ MM: pad(date.getMonth() + 1),
10
+ DD: pad(date.getDate()),
11
+ hh: pad(date.getHours()),
12
+ mm: pad(date.getMinutes()),
13
+ ss: pad(date.getSeconds()),
14
+ };
15
+
16
+ return format.replace(
17
+ /YYYY|YY|MM|DD|hh|mm|ss/g,
18
+ (match) => replacements[match]
19
+ );
20
+ }
21
+
22
+ /**
23
+ * Formats a date string from a given input format to a specified output format,
24
+ * with optional timezone adjustment.
25
+ *
26
+ * @param date - The date string to be formatted. It should match the specified `inputFormat`.
27
+ * @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
28
+ * @param inputFormat - The format of the input date string. Supported formats:
29
+ * - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
30
+ * - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
31
+ * - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
32
+ *
33
+ * @param outputFormat - The desired format for the output date string. Supported tokens:
34
+ * - `YYYY`: Full year (e.g., 2023)
35
+ * - `YY`: Last two digits of the year (e.g., 23)
36
+ * - `MM`: Month (zero-padded, e.g., 01)
37
+ * - `DD`: Day of the month (zero-padded, e.g., 09)
38
+ * - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
39
+ * - `mm`: Minutes (zero-padded, e.g., 05)
40
+ * - `ss`: Seconds (zero-padded, e.g., 07)
41
+ * @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
42
+ *
43
+ * @returns The formatted date string in the specified `outputFormat`.
44
+ *
45
+ * @throws Will throw an error if:
46
+ * - The `inputFormat` is invalid.
47
+ * - The `date` string does not match the expected `inputFormat`.
48
+ * - The `time` string is not in the format `hh:mm:ss`.
49
+ * - The resulting date is invalid.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * import { formatDate } from "./formatDate";
54
+ *
55
+ * const date = "25/12/2023";
56
+ * const time = "14:30:00";
57
+ * const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
58
+ * console.log(formatted); // Outputs: "2023-12-25 14:30:00"
59
+ * ```
60
+ */
61
+
62
+ const formatDate: FormatDateFunction = (
63
+ date,
64
+ time,
65
+ inputFormat,
66
+ outputFormat,
67
+ timezone = 0
68
+ ) => {
69
+ const dateParts = date.split(/[-/]/).map(Number);
70
+ const timeParts = time.split(":").map(Number);
71
+
72
+ let day, month, year;
73
+ const [hours = 0, minutes = 0, seconds = 0] = timeParts;
74
+
75
+ switch (inputFormat) {
76
+ case "brazilianDate":
77
+ [day, month, year] = dateParts;
78
+ break;
79
+ case "isoDate":
80
+ [year, month, day] = dateParts;
81
+ break;
82
+ case "timestamp":
83
+ [year, month, day] = dateParts.map(Number);
84
+ break;
85
+ default:
86
+ throw new Error("Invalid input format");
87
+ }
88
+
89
+ const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
90
+ if (isNaN(formattedDate.getTime())) throw new Error("Invalid date");
91
+
92
+ formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
93
+
94
+ return formatDateString(formattedDate, outputFormat);
95
+ };
96
+
97
+ export { formatDate };
@@ -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 };