@arkyn/shared 2.0.1-beta.0 → 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 (136) 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 +33 -0
  35. package/dist/formats/formatJsonObject.d.ts.map +1 -0
  36. package/dist/formats/formatJsonObject.js +90 -0
  37. package/dist/formats/formatJsonString.d.ts +39 -0
  38. package/dist/formats/formatJsonString.d.ts.map +1 -0
  39. package/dist/formats/formatJsonString.js +46 -0
  40. package/dist/formats/formatToCep.d.ts +29 -0
  41. package/dist/formats/formatToCep.d.ts.map +1 -0
  42. package/dist/formats/formatToCep.js +34 -0
  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 +24 -0
  50. package/dist/formats/formatToCpfCnpj.d.ts.map +1 -0
  51. package/dist/formats/formatToCpfCnpj.js +33 -0
  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 +16 -0
  56. package/dist/formats/formatToEllipsis.d.ts.map +1 -0
  57. package/dist/formats/formatToEllipsis.js +21 -0
  58. package/dist/formats/formatToHiddenDigits.d.ts +32 -0
  59. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -0
  60. package/dist/formats/formatToHiddenDigits.js +60 -0
  61. package/dist/formats/formatToPhone.d.ts +34 -0
  62. package/dist/formats/formatToPhone.d.ts.map +1 -0
  63. package/dist/formats/formatToPhone.js +131 -0
  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 +15 -0
  74. package/dist/generators/generateColorByString.d.ts.map +1 -0
  75. package/dist/generators/generateColorByString.js +25 -0
  76. package/dist/generators/generateId.d.ts +28 -0
  77. package/dist/generators/generateId.d.ts.map +1 -0
  78. package/dist/generators/generateId.js +29 -0
  79. package/dist/generators/generateSlug.d.ts +17 -0
  80. package/dist/generators/generateSlug.d.ts.map +1 -0
  81. package/dist/generators/generateSlug.js +25 -0
  82. package/dist/index.d.ts +23 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/index.js +22 -0
  85. package/dist/regex/index.d.ts +15 -0
  86. package/dist/regex/index.d.ts.map +1 -0
  87. package/dist/regex/index.js +25 -0
  88. package/dist/services/__test__/calculateCardInstallment.spec.d.ts +2 -0
  89. package/dist/services/__test__/calculateCardInstallment.spec.d.ts.map +1 -0
  90. package/dist/services/__test__/calculateCardInstallment.spec.js +71 -0
  91. package/dist/services/__test__/maskSensitiveData.spec.d.ts +2 -0
  92. package/dist/services/__test__/maskSensitiveData.spec.d.ts.map +1 -0
  93. package/dist/services/__test__/maskSensitiveData.spec.js +79 -0
  94. package/dist/services/__test__/removeCurrencySymbols.spec.d.ts +2 -0
  95. package/dist/services/__test__/removeCurrencySymbols.spec.d.ts.map +1 -0
  96. package/dist/services/__test__/removeCurrencySymbols.spec.js +34 -0
  97. package/dist/services/__test__/removeNonNumeric.spec.d.ts +2 -0
  98. package/dist/services/__test__/removeNonNumeric.spec.d.ts.map +1 -0
  99. package/dist/services/__test__/removeNonNumeric.spec.js +27 -0
  100. package/dist/services/__test__/truncateLargeFields.spec.d.ts +2 -0
  101. package/dist/services/__test__/truncateLargeFields.spec.d.ts.map +1 -0
  102. package/dist/services/__test__/truncateLargeFields.spec.js +65 -0
  103. package/dist/services/calculateCardInstallment.d.ts +33 -0
  104. package/dist/services/calculateCardInstallment.d.ts.map +1 -0
  105. package/dist/services/calculateCardInstallment.js +55 -0
  106. package/dist/services/maskSensitiveData.d.ts +26 -0
  107. package/dist/services/maskSensitiveData.d.ts.map +1 -0
  108. package/dist/services/maskSensitiveData.js +60 -0
  109. package/dist/services/removeCurrencySymbols.d.ts +20 -0
  110. package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
  111. package/dist/services/removeCurrencySymbols.js +23 -0
  112. package/dist/services/removeNonNumeric.d.ts +15 -0
  113. package/dist/services/removeNonNumeric.d.ts.map +1 -0
  114. package/dist/services/removeNonNumeric.js +16 -0
  115. package/dist/services/truncateLargeFields.d.ts +32 -0
  116. package/dist/services/truncateLargeFields.d.ts.map +1 -0
  117. package/dist/services/truncateLargeFields.js +57 -0
  118. package/dist/validations/__test__/validateCpf.spec.d.ts +2 -0
  119. package/dist/validations/__test__/validateCpf.spec.d.ts.map +1 -0
  120. package/dist/validations/__test__/validateCpf.spec.js +31 -0
  121. package/dist/validations/__test__/validateDate.spec.d.ts +2 -0
  122. package/dist/validations/__test__/validateDate.spec.d.ts.map +1 -0
  123. package/dist/validations/__test__/validateDate.spec.js +44 -0
  124. package/dist/validations/__test__/validatePhone.spec.d.ts +2 -0
  125. package/dist/validations/__test__/validatePhone.spec.d.ts.map +1 -0
  126. package/dist/validations/__test__/validatePhone.spec.js +32 -0
  127. package/dist/validations/validateCpf.d.ts +24 -0
  128. package/dist/validations/validateCpf.d.ts.map +1 -0
  129. package/dist/validations/validateCpf.js +56 -0
  130. package/dist/validations/validateDate.d.ts +29 -0
  131. package/dist/validations/validateDate.d.ts.map +1 -0
  132. package/dist/validations/validateDate.js +73 -0
  133. package/dist/validations/validatePhone.d.ts +29 -0
  134. package/dist/validations/validatePhone.d.ts.map +1 -0
  135. package/dist/validations/validatePhone.js +44 -0
  136. package/package.json +1 -1
@@ -0,0 +1,57 @@
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) {
31
+ if (typeof value === "string" && value.length > maxLength) {
32
+ return `To large information: field as ${value.length} characters`;
33
+ }
34
+ return value;
35
+ }
36
+ function recursiveTruncate(obj) {
37
+ if (Array.isArray(obj)) {
38
+ return obj.map((item) => recursiveTruncate(item)); // Corrigido para processar elementos do array
39
+ }
40
+ else if (obj !== null && typeof obj === "object") {
41
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
42
+ key,
43
+ recursiveTruncate(value), // Corrigido para aplicar recursão corretamente
44
+ ]));
45
+ }
46
+ return truncateValue(obj); // Corrigido para truncar valores diretamente
47
+ }
48
+ try {
49
+ const parsedJson = JSON.parse(jsonString);
50
+ const truncatedJson = recursiveTruncate(parsedJson);
51
+ return JSON.stringify(truncatedJson);
52
+ }
53
+ catch (error) {
54
+ throw new Error("Invalid JSON string");
55
+ }
56
+ };
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 };
@@ -0,0 +1,29 @@
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;
28
+ export { validateDate };
29
+ //# sourceMappingURL=validateDate.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,73 @@
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) => {
27
+ let day, month, year;
28
+ const inputFormat = config?.inputFormat || "DD/MM/YYYY";
29
+ const minYear = config?.minYear || 1900;
30
+ const maxYear = config?.maxYear || 3000;
31
+ if (inputFormat === "DD/MM/YYYY") {
32
+ const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
33
+ if (!dateRegex.test(date))
34
+ return false;
35
+ [, day, month, year] = date.match(dateRegex) || [];
36
+ }
37
+ else if (inputFormat === "MM-DD-YYYY") {
38
+ const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
39
+ if (!dateRegex.test(date))
40
+ return false;
41
+ [, month, day, year] = date.match(dateRegex) || [];
42
+ }
43
+ else if (inputFormat === "YYYY-MM-DD") {
44
+ const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
45
+ if (!dateRegex.test(date))
46
+ return false;
47
+ [, year, month, day] = date.match(dateRegex) || [];
48
+ }
49
+ else {
50
+ throw new Error("Invalid date format");
51
+ }
52
+ const dayNum = parseInt(day, 10);
53
+ const monthNum = parseInt(month, 10);
54
+ const yearNum = parseInt(year, 10);
55
+ if (dayNum < 1 || dayNum > 31)
56
+ return false;
57
+ if (monthNum < 1 || monthNum > 12)
58
+ return false;
59
+ const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
60
+ if (monthNum === 2) {
61
+ const isLeapYear = (yearNum % 4 === 0 && yearNum % 100 !== 0) || yearNum % 400 === 0;
62
+ if (dayNum > (isLeapYear ? 29 : 28))
63
+ return false;
64
+ }
65
+ else if (dayNum > daysInMonth[monthNum - 1]) {
66
+ return false;
67
+ }
68
+ if (yearNum < minYear || yearNum > maxYear)
69
+ return false;
70
+ const isValidDate = new Date(yearNum, monthNum - 1, dayNum).getDate() === dayNum;
71
+ return isValidDate;
72
+ };
73
+ export { validateDate };
@@ -0,0 +1,29 @@
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;
28
+ export { validatePhone };
29
+ //# sourceMappingURL=validatePhone.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,44 @@
1
+ import { countries } from "@arkyn/templates";
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) => {
28
+ for (const country of countries) {
29
+ const countryCode = country.code;
30
+ const prefix = country.prefix ? `-${country.prefix}` : "";
31
+ const digitCount = country.mask.replace(/[^_]/g, "").length;
32
+ if (country.iso === "BR") {
33
+ const brazilRegex = new RegExp(`^\\${countryCode} \\d{2}9?\\d{8}$`);
34
+ if (brazilRegex.test(phone))
35
+ return true;
36
+ continue;
37
+ }
38
+ const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
39
+ if (regex.test(phone))
40
+ return true;
41
+ }
42
+ return false;
43
+ };
44
+ export { validatePhone };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/shared",
3
- "version": "2.0.1-beta.0",
3
+ "version": "2.0.1-beta.1",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",