@arkyn/shared 1.4.57 → 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.
- package/LICENSE.txt +24 -0
- package/README.md +113 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts.map +1 -1
- package/dist/validations/validateCpf.js +2 -4
- package/dist/validations/validateDate.d.ts +1 -1
- package/dist/validations/validateDate.js +8 -8
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +1 -1
- package/dist/validations/validatePhone.js +4 -4
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +8 -5
- package/src/formats/__test__/formatDate.spec.ts +88 -0
- package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
- package/src/formats/__test__/formatJsonString.spec.ts +83 -0
- package/src/formats/__test__/formatToCep.spec.ts +37 -0
- package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
- package/src/formats/__test__/formatToCpf.spec.ts +37 -0
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
- package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
- package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
- package/src/formats/__test__/formatToPhone.spec.ts +58 -0
- package/src/generators/__test__/generateColorByString.spec.ts +37 -0
- package/src/generators/__test__/generateId.spec.ts +44 -0
- package/src/generators/__test__/generateSlug.spec.ts +47 -0
- package/src/index.ts +7 -2
- package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
- package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
- package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
- package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
- package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
- package/src/validations/__test__/validateCep.spec.ts +40 -0
- package/src/validations/__test__/validateCnpj.spec.ts +42 -0
- package/src/validations/__test__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePassword.spec.ts +43 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/__test__/validateRg.spec.ts +48 -0
- package/src/validations/validateCep.ts +40 -0
- package/src/validations/validateCnpj.ts +64 -0
- package/src/validations/validateCpf.ts +2 -5
- package/src/validations/validateDate.ts +8 -8
- package/src/validations/validatePassword.ts +41 -0
- package/src/validations/validatePhone.ts +4 -4
- package/src/validations/validateRg.ts +37 -0
- package/tsconfig.json +1 -1
- package/dist/regex/index.d.ts +0 -6
- package/dist/regex/index.d.ts.map +0 -1
- package/dist/regex/index.js +0 -4
- package/src/regex/index.ts +0 -8
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validatePassword } from "../validatePassword";
|
|
3
|
+
|
|
4
|
+
describe("validatePassword", () => {
|
|
5
|
+
it("should return true for a valid password", () => {
|
|
6
|
+
const validPassword = "StrongP@ssw0rd!";
|
|
7
|
+
expect(validatePassword(validPassword)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for a password shorter than the minimum length", () => {
|
|
11
|
+
const shortPassword = "Short1!";
|
|
12
|
+
expect(validatePassword(shortPassword)).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a password without an uppercase letter", () => {
|
|
16
|
+
const noUppercase = "weakp@ssw0rd!";
|
|
17
|
+
expect(validatePassword(noUppercase)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a password without a lowercase letter", () => {
|
|
21
|
+
const noLowercase = "WEAKP@SSW0RD!";
|
|
22
|
+
expect(validatePassword(noLowercase)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for a password without a number", () => {
|
|
26
|
+
const noNumber = "NoNumber@!";
|
|
27
|
+
expect(validatePassword(noNumber)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for a password without a special character", () => {
|
|
31
|
+
const noSpecialChar = "NoSpecialChar1";
|
|
32
|
+
expect(validatePassword(noSpecialChar)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should return false for an empty password", () => {
|
|
36
|
+
expect(validatePassword("")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should return false for a null or undefined password", () => {
|
|
40
|
+
expect(validatePassword(null as unknown as string)).toBe(false);
|
|
41
|
+
expect(validatePassword(undefined as unknown as string)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validatePhone } from "../validatePhone";
|
|
3
|
+
|
|
4
|
+
describe("validatePhone", () => {
|
|
5
|
+
it("should return true for valid Brazilian phone numbers", () => {
|
|
6
|
+
expect(validatePhone("+55 32912345678")).toBe(true); // Valid with 9th digit
|
|
7
|
+
expect(validatePhone("+55 3212345678")).toBe(true); // Valid without 9th digit
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for invalid Brazilian phone numbers", () => {
|
|
11
|
+
expect(validatePhone("+5532912345678")).toBe(false); // Missing space
|
|
12
|
+
expect(validatePhone("+55 123456789")).toBe(false); // Invalid format
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return true for valid American Samoa phone numbers", () => {
|
|
16
|
+
expect(validatePhone("+1-684 1234567")).toBe(true); // Valid format
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return false for invalid phone numbers", () => {
|
|
20
|
+
expect(validatePhone("+1-684 12345")).toBe(false); // Too few digits
|
|
21
|
+
expect(validatePhone("+99 1234567890")).toBe(false); // Non-existent country code
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for valid phone numbers from other countries", () => {
|
|
25
|
+
expect(validatePhone("+44 1234567890")).toBe(true); // Example for UK
|
|
26
|
+
expect(validatePhone("+91 9876543210")).toBe(true); // Example for India
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return false for phone numbers with incorrect prefixes", () => {
|
|
30
|
+
expect(validatePhone("+44-20 12345678")).toBe(false); // Invalid prefix for UK
|
|
31
|
+
expect(validatePhone("+91-80 12345678")).toBe(false); // Invalid prefix for India
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle edge cases gracefully", () => {
|
|
35
|
+
expect(validatePhone("")).toBe(false); // Empty string
|
|
36
|
+
expect(validatePhone("+55")).toBe(false); // Only country code
|
|
37
|
+
expect(validatePhone("+55 329123456789")).toBe(false); // Too many digits
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateRg } from "../validateRg";
|
|
3
|
+
|
|
4
|
+
describe("validateRg", () => {
|
|
5
|
+
it("should return true for a valid RG with formatting", () => {
|
|
6
|
+
const validRg = "12.345.678-9";
|
|
7
|
+
expect(validateRg(validRg)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return true for a valid RG without formatting", () => {
|
|
11
|
+
const validRg = "123456789";
|
|
12
|
+
expect(validateRg(validRg)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return true for a valid RG with a verifier letter", () => {
|
|
16
|
+
const validRg = "12345678X";
|
|
17
|
+
expect(validateRg(validRg)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for an RG with invalid length (too short)", () => {
|
|
21
|
+
const invalidRg = "123456";
|
|
22
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for an RG with invalid length (too long)", () => {
|
|
26
|
+
const invalidRg = "1234567890";
|
|
27
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for an RG with special characters", () => {
|
|
31
|
+
const invalidRg = "12.345.678@9";
|
|
32
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should return false for an RG with letters in the middle", () => {
|
|
36
|
+
const invalidRg = "12345A678";
|
|
37
|
+
expect(validateRg(invalidRg)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return false for an empty RG", () => {
|
|
41
|
+
expect(validateRg("")).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should return false for a null or undefined RG", () => {
|
|
45
|
+
expect(validateRg(null as unknown as string)).toBe(false);
|
|
46
|
+
expect(validateRg(undefined as unknown as string)).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -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 =
|
|
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
|
|
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 = (
|
|
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(
|
|
39
|
-
[, day, month, year] =
|
|
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(
|
|
43
|
-
[, month, day, year] =
|
|
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(
|
|
47
|
-
[, year, month, day] =
|
|
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
|
|
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 = (
|
|
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(
|
|
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(
|
|
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 };
|
package/tsconfig.json
CHANGED
package/dist/regex/index.d.ts
DELETED
|
@@ -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"}
|
package/dist/regex/index.js
DELETED
package/src/regex/index.ts
DELETED