@arkyn/shared 2.0.1-beta.12 → 2.0.1-beta.13

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 (34) hide show
  1. package/dist/index.d.ts +3 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +7 -1
  4. package/dist/validations/validateCep.d.ts +24 -0
  5. package/dist/validations/validateCep.d.ts.map +1 -0
  6. package/dist/validations/validateCep.js +33 -0
  7. package/dist/validations/validateCnpj.d.ts +22 -0
  8. package/dist/validations/validateCnpj.d.ts.map +1 -0
  9. package/dist/validations/validateCnpj.js +52 -0
  10. package/dist/validations/validateCpf.d.ts.map +1 -1
  11. package/dist/validations/validateCpf.js +2 -4
  12. package/dist/validations/validateDate.d.ts +1 -1
  13. package/dist/validations/validateDate.js +8 -8
  14. package/dist/validations/validatePassword.d.ts +21 -0
  15. package/dist/validations/validatePassword.d.ts.map +1 -0
  16. package/dist/validations/validatePassword.js +34 -0
  17. package/dist/validations/validatePhone.d.ts +1 -1
  18. package/dist/validations/validatePhone.js +4 -4
  19. package/dist/validations/validateRg.d.ts +22 -0
  20. package/dist/validations/validateRg.d.ts.map +1 -0
  21. package/dist/validations/validateRg.js +31 -0
  22. package/package.json +1 -1
  23. package/src/index.ts +7 -2
  24. package/src/validations/validateCep.ts +40 -0
  25. package/src/validations/validateCnpj.ts +64 -0
  26. package/src/validations/validateCpf.ts +2 -5
  27. package/src/validations/validateDate.ts +8 -8
  28. package/src/validations/validatePassword.ts +41 -0
  29. package/src/validations/validatePhone.ts +4 -4
  30. package/src/validations/validateRg.ts +37 -0
  31. package/dist/regex/index.d.ts +0 -6
  32. package/dist/regex/index.d.ts.map +0 -1
  33. package/dist/regex/index.js +0 -4
  34. package/src/regex/index.ts +0 -8
package/dist/index.d.ts CHANGED
@@ -12,12 +12,14 @@ export { formatToPhone } from "./formats/formatToPhone";
12
12
  export { generateColorByString } from "./generators/generateColorByString";
13
13
  export { generateId } from "./generators/generateId";
14
14
  export { generateSlug } from "./generators/generateSlug";
15
- export { regex } from "./regex";
16
15
  export { calculateCardInstallment } from "./services/calculateCardInstallment";
17
16
  export { maskSensitiveData } from "./services/maskSensitiveData";
18
17
  export { removeNonNumeric } from "./services/removeNonNumeric";
19
18
  export { truncateLargeFields } from "./services/truncateLargeFields";
19
+ export { validateCep } from "./validations/validateCep";
20
+ export { validateCnpj } from "./validations/validateCnpj";
20
21
  export { validateCpf } from "./validations/validateCpf";
21
22
  export { validateDate } from "./validations/validateDate";
22
23
  export { validatePhone } from "./validations/validatePhone";
24
+ export { validateRg } from "./validations/validateRg";
23
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAExD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAErE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ // formats
1
2
  export { formatDate } from "./formats/formatDate";
2
3
  export { formatJsonObject } from "./formats/formatJsonObject";
3
4
  export { formatJsonString } from "./formats/formatJsonString";
@@ -9,14 +10,19 @@ export { formatToCurrency } from "./formats/formatToCurrency";
9
10
  export { formatToEllipsis } from "./formats/formatToEllipsis";
10
11
  export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
11
12
  export { formatToPhone } from "./formats/formatToPhone";
13
+ // generators
12
14
  export { generateColorByString } from "./generators/generateColorByString";
13
15
  export { generateId } from "./generators/generateId";
14
16
  export { generateSlug } from "./generators/generateSlug";
15
- export { regex } from "./regex";
17
+ // services
16
18
  export { calculateCardInstallment } from "./services/calculateCardInstallment";
17
19
  export { maskSensitiveData } from "./services/maskSensitiveData";
18
20
  export { removeNonNumeric } from "./services/removeNonNumeric";
19
21
  export { truncateLargeFields } from "./services/truncateLargeFields";
22
+ // utils
23
+ export { validateCep } from "./validations/validateCep";
24
+ export { validateCnpj } from "./validations/validateCnpj";
20
25
  export { validateCpf } from "./validations/validateCpf";
21
26
  export { validateDate } from "./validations/validateDate";
22
27
  export { validatePhone } from "./validations/validatePhone";
28
+ export { validateRg } from "./validations/validateRg";
@@ -0,0 +1,24 @@
1
+ import type { ValidateCepFunction } from "@arkyn/types";
2
+ /**
3
+ * Removes all non-digit characters from the CEP.
4
+ * @param cep - Raw CEP string.
5
+ * @returns Only numeric characters.
6
+ */
7
+ /**
8
+ * Validates a Brazilian CEP (Código de Endereçamento Postal).
9
+ *
10
+ * A valid CEP has 8 numeric digits.
11
+ *
12
+ * @param rawCep - CEP string, may include formatting (e.g., "12345-678").
13
+ * @returns `true` if the CEP is valid, otherwise `false`.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * validateCep("12345-678"); // true
18
+ * validateCep("12345678"); // true
19
+ * validateCep("ABCDE-123"); // false
20
+ * ```
21
+ */
22
+ declare const validateCep: ValidateCepFunction;
23
+ export { validateCep };
24
+ //# sourceMappingURL=validateCep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateCep.d.ts","sourceRoot":"","sources":["../../src/validations/validateCep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;GAIG;AAEH;;;;;;;;;;;;;;GAcG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAYlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { removeNonNumeric } from "../services/removeNonNumeric";
2
+ /**
3
+ * Removes all non-digit characters from the CEP.
4
+ * @param cep - Raw CEP string.
5
+ * @returns Only numeric characters.
6
+ */
7
+ /**
8
+ * Validates a Brazilian CEP (Código de Endereçamento Postal).
9
+ *
10
+ * A valid CEP has 8 numeric digits.
11
+ *
12
+ * @param rawCep - CEP string, may include formatting (e.g., "12345-678").
13
+ * @returns `true` if the CEP is valid, otherwise `false`.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * validateCep("12345-678"); // true
18
+ * validateCep("12345678"); // true
19
+ * validateCep("ABCDE-123"); // false
20
+ * ```
21
+ */
22
+ const validateCep = (rawCep) => {
23
+ if (!rawCep)
24
+ return false;
25
+ const validFormat = /^[0-9-]+$/.test(rawCep);
26
+ if (!validFormat)
27
+ return false;
28
+ const cep = removeNonNumeric(rawCep);
29
+ const CEP_LENGTH = 8;
30
+ const isOnlyDigits = /^\d{8}$/.test(cep);
31
+ return cep.length === CEP_LENGTH && isOnlyDigits;
32
+ };
33
+ export { validateCep };
@@ -0,0 +1,22 @@
1
+ import type { ValidateCnpjFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
4
+ *
5
+ * This function performs:
6
+ * - Sanitization (removes non-digit characters).
7
+ * - Length check (must be 14 digits).
8
+ * - Repeating digits check (invalid if all digits are the same).
9
+ * - Verifies the two check digits with the proper weights.
10
+ *
11
+ * @param rawCnpj - CNPJ string, possibly formatted.
12
+ * @returns `true` if valid, otherwise `false`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * validateCnpj("12.345.678/0001-95"); // false
17
+ * validateCnpj("11.444.777/0001-61"); // true
18
+ * ```
19
+ */
20
+ declare const validateCnpj: ValidateCnpjFunction;
21
+ export { validateCnpj };
22
+ //# sourceMappingURL=validateCnpj.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateCnpj.d.ts","sourceRoot":"","sources":["../../src/validations/validateCnpj.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AA0BzD;;;;;;;;;;;;;;;;;GAiBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBAgBnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,52 @@
1
+ import { removeNonNumeric } from "../services/removeNonNumeric";
2
+ function isInvalidLength(cnpj) {
3
+ const CNPJ_LENGTH = 14;
4
+ return cnpj.length !== CNPJ_LENGTH;
5
+ }
6
+ function hasAllDigitsEqual(cnpj) {
7
+ const [firstDigit] = cnpj;
8
+ return [...cnpj].every((digit) => digit === firstDigit);
9
+ }
10
+ function calculateDigit(cnpj, multipliers) {
11
+ let total = 0;
12
+ for (let i = 0; i < multipliers.length; i++) {
13
+ total += parseInt(cnpj[i]) * multipliers[i];
14
+ }
15
+ const rest = total % 11;
16
+ return rest < 2 ? 0 : 11 - rest;
17
+ }
18
+ function extractDigit(cnpj) {
19
+ return cnpj.slice(12);
20
+ }
21
+ /**
22
+ * Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
23
+ *
24
+ * This function performs:
25
+ * - Sanitization (removes non-digit characters).
26
+ * - Length check (must be 14 digits).
27
+ * - Repeating digits check (invalid if all digits are the same).
28
+ * - Verifies the two check digits with the proper weights.
29
+ *
30
+ * @param rawCnpj - CNPJ string, possibly formatted.
31
+ * @returns `true` if valid, otherwise `false`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * validateCnpj("12.345.678/0001-95"); // false
36
+ * validateCnpj("11.444.777/0001-61"); // true
37
+ * ```
38
+ */
39
+ const validateCnpj = (rawCnpj) => {
40
+ if (!rawCnpj)
41
+ return false;
42
+ const cnpj = removeNonNumeric(rawCnpj);
43
+ if (isInvalidLength(cnpj))
44
+ return false;
45
+ if (hasAllDigitsEqual(cnpj))
46
+ return false;
47
+ const base = cnpj.slice(0, 12);
48
+ const digit1 = calculateDigit(base, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
49
+ const digit2 = calculateDigit(base + digit1, [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
50
+ return extractDigit(cnpj) === `${digit1}${digit2}`;
51
+ };
52
+ export { validateCnpj };
@@ -1 +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"}
1
+ {"version":3,"file":"validateCpf.d.ts","sourceRoot":"","sources":["../../src/validations/validateCpf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AA0BxD;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAWlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -1,6 +1,4 @@
1
- function removeNonDigits(cpf) {
2
- return cpf.replace(/\D/g, "");
3
- }
1
+ import { removeNonNumeric } from "../services/removeNonNumeric";
4
2
  function isInvalidLength(cpf) {
5
3
  const CPF_LENGTH = 11;
6
4
  return cpf.length !== CPF_LENGTH;
@@ -44,7 +42,7 @@ function extractDigit(cpf) {
44
42
  const validateCpf = (rawCpf) => {
45
43
  if (!rawCpf)
46
44
  return false;
47
- const cpf = removeNonDigits(rawCpf);
45
+ const cpf = removeNonNumeric(rawCpf);
48
46
  if (isInvalidLength(cpf))
49
47
  return false;
50
48
  if (hasAllDigitsEqual(cpf))
@@ -2,7 +2,7 @@ import type { ValidateDateFunction } from "@arkyn/types";
2
2
  /**
3
3
  * Validates a date string based on the provided format and configuration.
4
4
  *
5
- * @param date - The date string to validate.
5
+ * @param rawDate - The date string to validate.
6
6
  * @param config - Optional configuration object to customize validation.
7
7
  * @param config.inputFormat - The expected format of the input date.
8
8
  * Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Validates a date string based on the provided format and configuration.
3
3
  *
4
- * @param date - The date string to validate.
4
+ * @param rawDate - The date string to validate.
5
5
  * @param config - Optional configuration object to customize validation.
6
6
  * @param config.inputFormat - The expected format of the input date.
7
7
  * Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
@@ -23,28 +23,28 @@
23
23
  * validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
24
24
  * ```
25
25
  */
26
- const validateDate = (date, config) => {
26
+ const validateDate = (rawDate, config) => {
27
27
  let day, month, year;
28
28
  const inputFormat = config?.inputFormat || "DD/MM/YYYY";
29
29
  const minYear = config?.minYear || 1900;
30
30
  const maxYear = config?.maxYear || 3000;
31
31
  if (inputFormat === "DD/MM/YYYY") {
32
32
  const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
33
- if (!dateRegex.test(date))
33
+ if (!dateRegex.test(rawDate))
34
34
  return false;
35
- [, day, month, year] = date.match(dateRegex) || [];
35
+ [, day, month, year] = rawDate.match(dateRegex) || [];
36
36
  }
37
37
  else if (inputFormat === "MM-DD-YYYY") {
38
38
  const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
39
- if (!dateRegex.test(date))
39
+ if (!dateRegex.test(rawDate))
40
40
  return false;
41
- [, month, day, year] = date.match(dateRegex) || [];
41
+ [, month, day, year] = rawDate.match(dateRegex) || [];
42
42
  }
43
43
  else if (inputFormat === "YYYY-MM-DD") {
44
44
  const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
45
- if (!dateRegex.test(date))
45
+ if (!dateRegex.test(rawDate))
46
46
  return false;
47
- [, year, month, day] = date.match(dateRegex) || [];
47
+ [, year, month, day] = rawDate.match(dateRegex) || [];
48
48
  }
49
49
  else {
50
50
  throw new Error("Invalid date format");
@@ -0,0 +1,21 @@
1
+ import type { ValidatePasswordFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a password based on the following rules:
4
+ * - At least 8 characters
5
+ * - At least 1 uppercase letter
6
+ * - At least 1 letter (any case)
7
+ * - At least 1 number
8
+ * - At least 1 special character
9
+ *
10
+ * @param rawPassword - The raw password string.
11
+ * @returns `true` if password is valid, otherwise `false`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * validatePassword("Senha@123"); // true
16
+ * validatePassword("senha123"); // false (no uppercase, no special char)
17
+ * ```
18
+ */
19
+ declare const validatePassword: ValidatePasswordFunction;
20
+ export { validatePassword };
21
+ //# sourceMappingURL=validatePassword.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validatePassword.d.ts","sourceRoot":"","sources":["../../src/validations/validatePassword.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAkBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Validates a password based on the following rules:
3
+ * - At least 8 characters
4
+ * - At least 1 uppercase letter
5
+ * - At least 1 letter (any case)
6
+ * - At least 1 number
7
+ * - At least 1 special character
8
+ *
9
+ * @param rawPassword - The raw password string.
10
+ * @returns `true` if password is valid, otherwise `false`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * validatePassword("Senha@123"); // true
15
+ * validatePassword("senha123"); // false (no uppercase, no special char)
16
+ * ```
17
+ */
18
+ const validatePassword = (rawPassword) => {
19
+ if (!rawPassword)
20
+ return false;
21
+ const hasMinLength = rawPassword.length >= 8;
22
+ const hasUppercase = /[A-Z]/.test(rawPassword);
23
+ const hasLetter = /[a-z]/.test(rawPassword);
24
+ const hasNumber = /\d/.test(rawPassword);
25
+ const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>_\-+=~`[\]\\\/]/.test(rawPassword);
26
+ return [
27
+ hasMinLength,
28
+ hasUppercase,
29
+ hasLetter,
30
+ hasNumber,
31
+ hasSpecialChar,
32
+ ].every((condition) => condition);
33
+ };
34
+ export { validatePassword };
@@ -10,7 +10,7 @@ import type { ValidatePhoneFunction } from "@arkyn/types";
10
10
  * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
11
  * allows for an optional ninth digit.
12
12
  *
13
- * @param phone - The phone number to validate as a string.
13
+ * @param rawPhone - The phone number to validate as a string.
14
14
  * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
15
  *
16
16
  * @example
@@ -10,7 +10,7 @@ import { countries } from "@arkyn/templates";
10
10
  * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
11
  * allows for an optional ninth digit.
12
12
  *
13
- * @param phone - The phone number to validate as a string.
13
+ * @param rawPhone - The phone number to validate as a string.
14
14
  * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
15
  *
16
16
  * @example
@@ -24,19 +24,19 @@ import { countries } from "@arkyn/templates";
24
24
  * validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
25
25
  * ```
26
26
  */
27
- const validatePhone = (phone) => {
27
+ const validatePhone = (rawPhone) => {
28
28
  for (const country of countries) {
29
29
  const countryCode = country.code;
30
30
  const prefix = country.prefix ? `-${country.prefix}` : "";
31
31
  const digitCount = country.mask.replace(/[^_]/g, "").length;
32
32
  if (country.iso === "BR") {
33
33
  const brazilRegex = new RegExp(`^\\${countryCode} \\d{2}9?\\d{8}$`);
34
- if (brazilRegex.test(phone))
34
+ if (brazilRegex.test(rawPhone))
35
35
  return true;
36
36
  continue;
37
37
  }
38
38
  const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
39
- if (regex.test(phone))
39
+ if (regex.test(rawPhone))
40
40
  return true;
41
41
  }
42
42
  return false;
@@ -0,0 +1,22 @@
1
+ import type { ValidateRgFunction } from "@arkyn/types";
2
+ /**
3
+ * Validates a Brazilian RG (Registro Geral) in a generic way.
4
+ *
5
+ * This function does a basic structure validation:
6
+ * - Removes non-alphanumeric characters.
7
+ * - Ensures length is reasonable (7–9 digits).
8
+ * - Optionally allows for a final letter (verifier).
9
+ *
10
+ * @param rawRg - RG string, possibly formatted.
11
+ * @returns `true` if format seems valid, otherwise `false`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * validateRg("12.345.678-9"); // true
16
+ * validateRg("MG-12.345.678"); // false (not supported)
17
+ * validateRg("12345678X"); // true
18
+ * ```
19
+ */
20
+ declare const validateRg: ValidateRgFunction;
21
+ export { validateRg };
22
+ //# sourceMappingURL=validateRg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateRg.d.ts","sourceRoot":"","sources":["../../src/validations/validateRg.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;;;;;;GAiBG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAajB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Validates a Brazilian RG (Registro Geral) in a generic way.
3
+ *
4
+ * This function does a basic structure validation:
5
+ * - Removes non-alphanumeric characters.
6
+ * - Ensures length is reasonable (7–9 digits).
7
+ * - Optionally allows for a final letter (verifier).
8
+ *
9
+ * @param rawRg - RG string, possibly formatted.
10
+ * @returns `true` if format seems valid, otherwise `false`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * validateRg("12.345.678-9"); // true
15
+ * validateRg("MG-12.345.678"); // false (not supported)
16
+ * validateRg("12345678X"); // true
17
+ * ```
18
+ */
19
+ const validateRg = (rawRg) => {
20
+ if (!rawRg)
21
+ return false;
22
+ const validFormat = /^[0-9a-zA-Z.-]+$/.test(rawRg);
23
+ if (!validFormat)
24
+ return false;
25
+ const rg = rawRg.replace(/[^a-zA-Z0-9]/g, "");
26
+ if (rg.length < 7 || rg.length > 9)
27
+ return false;
28
+ const isValidFormat = /^[0-9]{7,8}[0-9Xx]?$/.test(rg);
29
+ return isValidFormat;
30
+ };
31
+ export { validateRg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/shared",
3
- "version": "2.0.1-beta.12",
3
+ "version": "2.0.1-beta.13",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ // formats
1
2
  export { formatDate } from "./formats/formatDate";
2
3
  export { formatJsonObject } from "./formats/formatJsonObject";
3
4
  export { formatJsonString } from "./formats/formatJsonString";
@@ -10,17 +11,21 @@ export { formatToEllipsis } from "./formats/formatToEllipsis";
10
11
  export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
11
12
  export { formatToPhone } from "./formats/formatToPhone";
12
13
 
14
+ // generators
13
15
  export { generateColorByString } from "./generators/generateColorByString";
14
16
  export { generateId } from "./generators/generateId";
15
17
  export { generateSlug } from "./generators/generateSlug";
16
18
 
17
- export { regex } from "./regex";
18
-
19
+ // services
19
20
  export { calculateCardInstallment } from "./services/calculateCardInstallment";
20
21
  export { maskSensitiveData } from "./services/maskSensitiveData";
21
22
  export { removeNonNumeric } from "./services/removeNonNumeric";
22
23
  export { truncateLargeFields } from "./services/truncateLargeFields";
23
24
 
25
+ // utils
26
+ export { validateCep } from "./validations/validateCep";
27
+ export { validateCnpj } from "./validations/validateCnpj";
24
28
  export { validateCpf } from "./validations/validateCpf";
25
29
  export { validateDate } from "./validations/validateDate";
26
30
  export { validatePhone } from "./validations/validatePhone";
31
+ export { validateRg } from "./validations/validateRg";
@@ -0,0 +1,40 @@
1
+ import type { ValidateCepFunction } from "@arkyn/types";
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
3
+
4
+ /**
5
+ * Removes all non-digit characters from the CEP.
6
+ * @param cep - Raw CEP string.
7
+ * @returns Only numeric characters.
8
+ */
9
+
10
+ /**
11
+ * Validates a Brazilian CEP (Código de Endereçamento Postal).
12
+ *
13
+ * A valid CEP has 8 numeric digits.
14
+ *
15
+ * @param rawCep - CEP string, may include formatting (e.g., "12345-678").
16
+ * @returns `true` if the CEP is valid, otherwise `false`.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * validateCep("12345-678"); // true
21
+ * validateCep("12345678"); // true
22
+ * validateCep("ABCDE-123"); // false
23
+ * ```
24
+ */
25
+
26
+ const validateCep: ValidateCepFunction = (rawCep) => {
27
+ if (!rawCep) return false;
28
+
29
+ const validFormat = /^[0-9-]+$/.test(rawCep);
30
+ if (!validFormat) return false;
31
+
32
+ const cep = removeNonNumeric(rawCep);
33
+
34
+ const CEP_LENGTH = 8;
35
+ const isOnlyDigits = /^\d{8}$/.test(cep);
36
+
37
+ return cep.length === CEP_LENGTH && isOnlyDigits;
38
+ };
39
+
40
+ export { validateCep };
@@ -0,0 +1,64 @@
1
+ import type { ValidateCnpjFunction } from "@arkyn/types";
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
3
+
4
+ function isInvalidLength(cnpj: string) {
5
+ const CNPJ_LENGTH = 14;
6
+ return cnpj.length !== CNPJ_LENGTH;
7
+ }
8
+
9
+ function hasAllDigitsEqual(cnpj: string) {
10
+ const [firstDigit] = cnpj;
11
+ return [...cnpj].every((digit) => digit === firstDigit);
12
+ }
13
+
14
+ function calculateDigit(cnpj: string, multipliers: number[]) {
15
+ let total = 0;
16
+ for (let i = 0; i < multipliers.length; i++) {
17
+ total += parseInt(cnpj[i]) * multipliers[i];
18
+ }
19
+ const rest = total % 11;
20
+ return rest < 2 ? 0 : 11 - rest;
21
+ }
22
+
23
+ function extractDigit(cnpj: string) {
24
+ return cnpj.slice(12);
25
+ }
26
+
27
+ /**
28
+ * Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
29
+ *
30
+ * This function performs:
31
+ * - Sanitization (removes non-digit characters).
32
+ * - Length check (must be 14 digits).
33
+ * - Repeating digits check (invalid if all digits are the same).
34
+ * - Verifies the two check digits with the proper weights.
35
+ *
36
+ * @param rawCnpj - CNPJ string, possibly formatted.
37
+ * @returns `true` if valid, otherwise `false`.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * validateCnpj("12.345.678/0001-95"); // false
42
+ * validateCnpj("11.444.777/0001-61"); // true
43
+ * ```
44
+ */
45
+
46
+ const validateCnpj: ValidateCnpjFunction = (rawCnpj) => {
47
+ if (!rawCnpj) return false;
48
+
49
+ const cnpj = removeNonNumeric(rawCnpj);
50
+
51
+ if (isInvalidLength(cnpj)) return false;
52
+ if (hasAllDigitsEqual(cnpj)) return false;
53
+
54
+ const base = cnpj.slice(0, 12);
55
+ const digit1 = calculateDigit(base, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
56
+ const digit2 = calculateDigit(
57
+ base + digit1,
58
+ [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
59
+ );
60
+
61
+ return extractDigit(cnpj) === `${digit1}${digit2}`;
62
+ };
63
+
64
+ export { validateCnpj };
@@ -1,8 +1,5 @@
1
1
  import type { ValidateCpfFunction } from "@arkyn/types";
2
-
3
- function removeNonDigits(cpf: string) {
4
- return cpf.replace(/\D/g, "");
5
- }
2
+ import { removeNonNumeric } from "../services/removeNonNumeric";
6
3
 
7
4
  function isInvalidLength(cpf: string) {
8
5
  const CPF_LENGTH = 11;
@@ -50,7 +47,7 @@ function extractDigit(cpf: string) {
50
47
 
51
48
  const validateCpf: ValidateCpfFunction = (rawCpf) => {
52
49
  if (!rawCpf) return false;
53
- const cpf = removeNonDigits(rawCpf);
50
+ const cpf = removeNonNumeric(rawCpf);
54
51
 
55
52
  if (isInvalidLength(cpf)) return false;
56
53
  if (hasAllDigitsEqual(cpf)) return false;
@@ -3,7 +3,7 @@ import type { ValidateDateFunction } from "@arkyn/types";
3
3
  /**
4
4
  * Validates a date string based on the provided format and configuration.
5
5
  *
6
- * @param date - The date string to validate.
6
+ * @param rawDate - The date string to validate.
7
7
  * @param config - Optional configuration object to customize validation.
8
8
  * @param config.inputFormat - The expected format of the input date.
9
9
  * Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
@@ -26,7 +26,7 @@ import type { ValidateDateFunction } from "@arkyn/types";
26
26
  * ```
27
27
  */
28
28
 
29
- const validateDate: ValidateDateFunction = (date, config) => {
29
+ const validateDate: ValidateDateFunction = (rawDate, config) => {
30
30
  let day: string, month: string, year: string;
31
31
 
32
32
  const inputFormat = config?.inputFormat || "DD/MM/YYYY";
@@ -35,16 +35,16 @@ const validateDate: ValidateDateFunction = (date, config) => {
35
35
 
36
36
  if (inputFormat === "DD/MM/YYYY") {
37
37
  const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
38
- if (!dateRegex.test(date)) return false;
39
- [, day, month, year] = date.match(dateRegex) || [];
38
+ if (!dateRegex.test(rawDate)) return false;
39
+ [, day, month, year] = rawDate.match(dateRegex) || [];
40
40
  } else if (inputFormat === "MM-DD-YYYY") {
41
41
  const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
42
- if (!dateRegex.test(date)) return false;
43
- [, month, day, year] = date.match(dateRegex) || [];
42
+ if (!dateRegex.test(rawDate)) return false;
43
+ [, month, day, year] = rawDate.match(dateRegex) || [];
44
44
  } else if (inputFormat === "YYYY-MM-DD") {
45
45
  const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
46
- if (!dateRegex.test(date)) return false;
47
- [, year, month, day] = date.match(dateRegex) || [];
46
+ if (!dateRegex.test(rawDate)) return false;
47
+ [, year, month, day] = rawDate.match(dateRegex) || [];
48
48
  } else {
49
49
  throw new Error("Invalid date format");
50
50
  }
@@ -0,0 +1,41 @@
1
+ import type { ValidatePasswordFunction } from "@arkyn/types";
2
+
3
+ /**
4
+ * Validates a password based on the following rules:
5
+ * - At least 8 characters
6
+ * - At least 1 uppercase letter
7
+ * - At least 1 letter (any case)
8
+ * - At least 1 number
9
+ * - At least 1 special character
10
+ *
11
+ * @param rawPassword - The raw password string.
12
+ * @returns `true` if password is valid, otherwise `false`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * validatePassword("Senha@123"); // true
17
+ * validatePassword("senha123"); // false (no uppercase, no special char)
18
+ * ```
19
+ */
20
+
21
+ const validatePassword: ValidatePasswordFunction = (rawPassword) => {
22
+ if (!rawPassword) return false;
23
+
24
+ const hasMinLength = rawPassword.length >= 8;
25
+ const hasUppercase = /[A-Z]/.test(rawPassword);
26
+ const hasLetter = /[a-z]/.test(rawPassword);
27
+ const hasNumber = /\d/.test(rawPassword);
28
+ const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>_\-+=~`[\]\\\/]/.test(
29
+ rawPassword
30
+ );
31
+
32
+ return [
33
+ hasMinLength,
34
+ hasUppercase,
35
+ hasLetter,
36
+ hasNumber,
37
+ hasSpecialChar,
38
+ ].every((condition) => condition);
39
+ };
40
+
41
+ export { validatePassword };
@@ -12,7 +12,7 @@ import type { ValidatePhoneFunction } from "@arkyn/types";
12
12
  * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
13
13
  * allows for an optional ninth digit.
14
14
  *
15
- * @param phone - The phone number to validate as a string.
15
+ * @param rawPhone - The phone number to validate as a string.
16
16
  * @returns `true` if the phone number matches any country's format, otherwise `false`.
17
17
  *
18
18
  * @example
@@ -27,7 +27,7 @@ import type { ValidatePhoneFunction } from "@arkyn/types";
27
27
  * ```
28
28
  */
29
29
 
30
- const validatePhone: ValidatePhoneFunction = (phone) => {
30
+ const validatePhone: ValidatePhoneFunction = (rawPhone) => {
31
31
  for (const country of countries) {
32
32
  const countryCode = country.code;
33
33
  const prefix = country.prefix ? `-${country.prefix}` : "";
@@ -35,12 +35,12 @@ const validatePhone: ValidatePhoneFunction = (phone) => {
35
35
 
36
36
  if (country.iso === "BR") {
37
37
  const brazilRegex = new RegExp(`^\\${countryCode} \\d{2}9?\\d{8}$`);
38
- if (brazilRegex.test(phone)) return true;
38
+ if (brazilRegex.test(rawPhone)) return true;
39
39
  continue;
40
40
  }
41
41
 
42
42
  const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
43
- if (regex.test(phone)) return true;
43
+ if (regex.test(rawPhone)) return true;
44
44
  }
45
45
 
46
46
  return false;
@@ -0,0 +1,37 @@
1
+ import type { ValidateRgFunction } from "@arkyn/types";
2
+
3
+ /**
4
+ * Validates a Brazilian RG (Registro Geral) in a generic way.
5
+ *
6
+ * This function does a basic structure validation:
7
+ * - Removes non-alphanumeric characters.
8
+ * - Ensures length is reasonable (7–9 digits).
9
+ * - Optionally allows for a final letter (verifier).
10
+ *
11
+ * @param rawRg - RG string, possibly formatted.
12
+ * @returns `true` if format seems valid, otherwise `false`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * validateRg("12.345.678-9"); // true
17
+ * validateRg("MG-12.345.678"); // false (not supported)
18
+ * validateRg("12345678X"); // true
19
+ * ```
20
+ */
21
+
22
+ const validateRg: ValidateRgFunction = (rawRg) => {
23
+ if (!rawRg) return false;
24
+
25
+ const validFormat = /^[0-9a-zA-Z.-]+$/.test(rawRg);
26
+ if (!validFormat) return false;
27
+
28
+ const rg = rawRg.replace(/[^a-zA-Z0-9]/g, "");
29
+
30
+ if (rg.length < 7 || rg.length > 9) return false;
31
+
32
+ const isValidFormat = /^[0-9]{7,8}[0-9Xx]?$/.test(rg);
33
+
34
+ return isValidFormat;
35
+ };
36
+
37
+ export { validateRg };
@@ -1,6 +0,0 @@
1
- declare const regex: {
2
- RG: RegExp;
3
- PASSWORD: RegExp;
4
- };
5
- export { regex };
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/regex/index.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,KAAK;;;CAAmB,CAAC;AAE/B,OAAO,EAAE,KAAK,EAAE,CAAC"}
@@ -1,4 +0,0 @@
1
- const RG = /(^\d{1,2}).?(\d{3}).?(\d{3})-?(\d{1}|X|x$)/;
2
- const PASSWORD = /(?=^.{8,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*/;
3
- const regex = { RG, PASSWORD };
4
- export { regex };
@@ -1,8 +0,0 @@
1
- const RG = /(^\d{1,2}).?(\d{3}).?(\d{3})-?(\d{1}|X|x$)/;
2
-
3
- const PASSWORD =
4
- /(?=^.{8,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*/;
5
-
6
- const regex = { RG, PASSWORD };
7
-
8
- export { regex };