@arkyn/shared 1.4.52 → 1.4.54
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/dist/formats/formatDate.d.ts +43 -0
- package/dist/formats/formatDate.d.ts.map +1 -0
- package/dist/formats/formatDate.js +77 -0
- package/dist/formats/formatJsonObject.d.ts +31 -1
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +67 -26
- package/dist/formats/formatJsonString.d.ts +37 -1
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +38 -4
- package/dist/formats/formatToCep.d.ts +27 -1
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +31 -6
- package/dist/formats/formatToCnpj.d.ts +30 -0
- package/dist/formats/formatToCnpj.d.ts.map +1 -0
- package/dist/formats/formatToCnpj.js +35 -0
- package/dist/formats/formatToCpf.d.ts +30 -0
- package/dist/formats/formatToCpf.d.ts.map +1 -0
- package/dist/formats/formatToCpf.js +35 -0
- package/dist/formats/formatToCpfCnpj.d.ts +22 -1
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +27 -7
- package/dist/formats/formatToCurrency.d.ts +29 -0
- package/dist/formats/formatToCurrency.d.ts.map +1 -0
- package/dist/formats/formatToCurrency.js +41 -0
- package/dist/formats/formatToEllipsis.d.ts +14 -1
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +20 -3
- package/dist/formats/formatToHiddenDigits.d.ts +30 -2
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +49 -3
- package/dist/formats/formatToPhone.d.ts +32 -1
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +128 -6
- package/dist/generators/generateColorByString.d.ts +13 -1
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +15 -4
- package/dist/generators/generateId.d.ts +26 -4
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +11 -5
- package/dist/generators/generateSlug.d.ts +15 -1
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +16 -2
- package/dist/index.d.ts +7 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -15
- package/dist/regex/index.d.ts +6 -0
- package/dist/regex/index.d.ts.map +1 -0
- package/dist/regex/index.js +4 -0
- package/dist/services/calculateCardInstallment.d.ts +31 -5
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +52 -11
- package/dist/services/maskSensitiveData.d.ts +24 -1
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +24 -2
- package/dist/services/removeCurrencySymbols.d.ts +20 -0
- package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
- package/dist/services/removeCurrencySymbols.js +23 -0
- package/dist/services/removeNonNumeric.d.ts +15 -0
- package/dist/services/removeNonNumeric.d.ts.map +1 -0
- package/dist/services/removeNonNumeric.js +16 -0
- package/dist/services/truncateLargeFields.d.ts +30 -1
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +34 -6
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +56 -0
- package/dist/validations/validateDate.d.ts +27 -6
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +32 -6
- package/dist/validations/validatePhone.d.ts +27 -1
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +27 -2
- package/package.json +4 -2
- package/src/formats/formatDate.ts +97 -0
- package/src/formats/formatJsonObject.ts +68 -26
- package/src/formats/formatJsonString.ts +40 -4
- package/src/formats/formatToCep.ts +35 -7
- package/src/formats/formatToCnpj.ts +39 -0
- package/src/formats/formatToCpf.ts +39 -0
- package/src/formats/formatToCpfCnpj.ts +31 -7
- package/src/formats/formatToCurrency.ts +53 -0
- package/src/formats/formatToEllipsis.ts +23 -3
- package/src/formats/formatToHiddenDigits.ts +75 -7
- package/src/formats/formatToPhone.ts +156 -6
- package/src/generators/generateColorByString.ts +18 -4
- package/src/generators/generateId.ts +36 -5
- package/src/generators/generateSlug.ts +17 -2
- package/src/index.ts +7 -15
- package/src/regex/index.ts +8 -0
- package/src/services/calculateCardInstallment.ts +57 -12
- package/src/services/maskSensitiveData.ts +29 -4
- package/src/services/removeCurrencySymbols.ts +25 -0
- package/src/services/removeNonNumeric.ts +18 -0
- package/src/services/truncateLargeFields.ts +40 -6
- package/src/validations/validateCpf.ts +64 -0
- package/src/validations/validateDate.ts +33 -11
- package/src/validations/validatePhone.ts +29 -2
- package/tsconfig.json +1 -1
- package/vitest.config.ts +5 -0
- package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
- package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateHour.js +0 -12
- package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateToDate.js +0 -9
- package/dist/formats/formatDateHour.d.ts +0 -3
- package/dist/formats/formatDateHour.d.ts.map +0 -1
- package/dist/formats/formatDateHour.js +0 -11
- package/dist/formats/formatToBRL.d.ts +0 -3
- package/dist/formats/formatToBRL.d.ts.map +0 -1
- package/dist/formats/formatToBRL.js +0 -8
- package/dist/formats/formatToCNPJ.d.ts +0 -3
- package/dist/formats/formatToCNPJ.d.ts.map +0 -1
- package/dist/formats/formatToCNPJ.js +0 -9
- package/dist/formats/formatToCPF.d.ts +0 -3
- package/dist/formats/formatToCPF.d.ts.map +0 -1
- package/dist/formats/formatToCPF.js +0 -9
- package/dist/parsers/parseToCharacters.d.ts +0 -17
- package/dist/parsers/parseToCharacters.d.ts.map +0 -1
- package/dist/parsers/parseToCharacters.js +0 -13
- package/dist/services/range.d.ts +0 -4
- package/dist/services/range.d.ts.map +0 -1
- package/dist/services/range.js +0 -9
- package/dist/validations/regex.d.ts +0 -15
- package/dist/validations/regex.d.ts.map +0 -1
- package/dist/validations/regex.js +0 -25
- package/dist/validations/validateCPF.d.ts +0 -3
- package/dist/validations/validateCPF.d.ts.map +0 -1
- package/dist/validations/validateCPF.js +0 -36
- package/src/formats/formatBrazilianDateHour.ts +0 -17
- package/src/formats/formatBrazilianDateToDate.ts +0 -13
- package/src/formats/formatDateHour.ts +0 -15
- package/src/formats/formatToBRL.ts +0 -10
- package/src/formats/formatToCNPJ.ts +0 -10
- package/src/formats/formatToCPF.ts +0 -10
- package/src/parsers/parseToCharacters.ts +0 -34
- package/src/services/range.ts +0 -15
- package/src/validations/regex.ts +0 -41
- package/src/validations/validateCPF.ts +0 -38
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ValidateCpfFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
function removeNonDigits(cpf: string) {
|
|
4
|
+
return cpf.replace(/\D/g, "");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isInvalidLength(cpf: string) {
|
|
8
|
+
const CPF_LENGTH = 11;
|
|
9
|
+
return cpf.length !== CPF_LENGTH;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function hasAllDigitsEqual(cpf: string) {
|
|
13
|
+
const [firstCpfDigit] = cpf;
|
|
14
|
+
return [...cpf].every((digit) => digit === firstCpfDigit);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function calculateDigit(cpf: string, factor: number) {
|
|
18
|
+
let total = 0;
|
|
19
|
+
for (const digit of cpf) {
|
|
20
|
+
if (factor > 1) total += parseInt(digit) * factor--;
|
|
21
|
+
}
|
|
22
|
+
const rest = total % 11;
|
|
23
|
+
return rest < 2 ? 0 : 11 - rest;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function extractDigit(cpf: string) {
|
|
27
|
+
return cpf.slice(9);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
32
|
+
*
|
|
33
|
+
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
34
|
+
* This function checks if the provided CPF is valid by performing the following steps:
|
|
35
|
+
* - Removes any non-digit characters from the input.
|
|
36
|
+
* - Verifies if the CPF has the correct length (11 digits).
|
|
37
|
+
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
38
|
+
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
39
|
+
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
40
|
+
*
|
|
41
|
+
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
42
|
+
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* validateCpf("123.456.789-09"); // false
|
|
47
|
+
* validateCpf("111.444.777-35"); // true
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
const validateCpf: ValidateCpfFunction = (rawCpf) => {
|
|
52
|
+
if (!rawCpf) return false;
|
|
53
|
+
const cpf = removeNonDigits(rawCpf);
|
|
54
|
+
|
|
55
|
+
if (isInvalidLength(cpf)) return false;
|
|
56
|
+
if (hasAllDigitsEqual(cpf)) return false;
|
|
57
|
+
|
|
58
|
+
const digit1 = calculateDigit(cpf, 10);
|
|
59
|
+
const digit2 = calculateDigit(cpf, 11);
|
|
60
|
+
|
|
61
|
+
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { validateCpf };
|
|
@@ -1,25 +1,47 @@
|
|
|
1
|
-
type
|
|
1
|
+
import type { ValidateDateFunction } from "@arkyn/types";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Validates a date string based on the provided format and configuration.
|
|
5
|
+
*
|
|
6
|
+
* @param date - The date string to validate.
|
|
7
|
+
* @param config - Optional configuration object to customize validation.
|
|
8
|
+
* @param config.inputFormat - The expected format of the input date.
|
|
9
|
+
* Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
|
|
10
|
+
* Defaults to "DD/MM/YYYY".
|
|
11
|
+
* @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
|
|
12
|
+
* @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
|
|
13
|
+
*
|
|
14
|
+
* @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
|
|
15
|
+
*
|
|
16
|
+
* @throws {Error} If an invalid date format is provided in the configuration.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* validateDate("31/12/2023"); // true
|
|
21
|
+
* validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
|
|
22
|
+
* validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
|
|
23
|
+
* validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
|
|
24
|
+
* validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
|
|
25
|
+
* validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
7
28
|
|
|
8
|
-
|
|
29
|
+
const validateDate: ValidateDateFunction = (date, config) => {
|
|
9
30
|
let day: string, month: string, year: string;
|
|
10
31
|
|
|
11
|
-
const
|
|
32
|
+
const inputFormat = config?.inputFormat || "DD/MM/YYYY";
|
|
33
|
+
const minYear = config?.minYear || 1900;
|
|
12
34
|
const maxYear = config?.maxYear || 3000;
|
|
13
35
|
|
|
14
|
-
if (
|
|
36
|
+
if (inputFormat === "DD/MM/YYYY") {
|
|
15
37
|
const dateRegex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
|
|
16
38
|
if (!dateRegex.test(date)) return false;
|
|
17
39
|
[, day, month, year] = date.match(dateRegex) || [];
|
|
18
|
-
} else if (
|
|
40
|
+
} else if (inputFormat === "MM-DD-YYYY") {
|
|
19
41
|
const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
20
42
|
if (!dateRegex.test(date)) return false;
|
|
21
43
|
[, month, day, year] = date.match(dateRegex) || [];
|
|
22
|
-
} else if (
|
|
44
|
+
} else if (inputFormat === "YYYY-MM-DD") {
|
|
23
45
|
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
24
46
|
if (!dateRegex.test(date)) return false;
|
|
25
47
|
[, year, month, day] = date.match(dateRegex) || [];
|
|
@@ -50,6 +72,6 @@ function validateDate(date: string, format: Format, config?: Config): boolean {
|
|
|
50
72
|
new Date(yearNum, monthNum - 1, dayNum).getDate() === dayNum;
|
|
51
73
|
|
|
52
74
|
return isValidDate;
|
|
53
|
-
}
|
|
75
|
+
};
|
|
54
76
|
|
|
55
77
|
export { validateDate };
|
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
import { countries } from "@arkyn/templates";
|
|
2
|
+
import type { ValidatePhoneFunction } from "@arkyn/types";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Validates a phone number against a list of country-specific formats.
|
|
6
|
+
*
|
|
7
|
+
* The function iterates through a predefined list of countries and checks if the
|
|
8
|
+
* provided phone number matches the format for any of the countries. It uses
|
|
9
|
+
* regular expressions to validate the phone number based on the country's code,
|
|
10
|
+
* prefix, and mask.
|
|
11
|
+
*
|
|
12
|
+
* Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
|
|
13
|
+
* allows for an optional ninth digit.
|
|
14
|
+
*
|
|
15
|
+
* @param phone - The phone number to validate as a string.
|
|
16
|
+
* @returns `true` if the phone number matches any country's format, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { validatePhone } from "./validatePhone";
|
|
21
|
+
*
|
|
22
|
+
* validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
|
|
23
|
+
* validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
|
|
24
|
+
* validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
|
|
25
|
+
* validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
|
|
26
|
+
* validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const validatePhone: ValidatePhoneFunction = (phone) => {
|
|
4
31
|
for (const country of countries) {
|
|
5
32
|
const countryCode = country.code;
|
|
6
33
|
const prefix = country.prefix ? `-${country.prefix}` : "";
|
|
@@ -17,6 +44,6 @@ function validatePhone(phone: string): boolean {
|
|
|
17
44
|
}
|
|
18
45
|
|
|
19
46
|
return false;
|
|
20
|
-
}
|
|
47
|
+
};
|
|
21
48
|
|
|
22
49
|
export { validatePhone };
|
package/tsconfig.json
CHANGED
package/vitest.config.ts
ADDED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatBrazilianDateHour.d.ts","sourceRoot":"","sources":["../../src/formats/formatBrazilianDateHour.ts"],"names":[],"mappings":"AAEA,iBAAS,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAY1D;AAED,OAAO,EAAE,uBAAuB,EAAE,CAAC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
function formatBrazilianDateHour(date, hour) {
|
|
3
|
-
if (regex.HOUR.test(hour) === false)
|
|
4
|
-
throw new Error("Invalid hour format");
|
|
5
|
-
const [day, month, year] = date.split("/").map(Number);
|
|
6
|
-
const dateHour = new Date(`${year}-${month}-${day}`);
|
|
7
|
-
const [hours, minutes] = hour.split(":").map(Number);
|
|
8
|
-
dateHour.setUTCHours(hours);
|
|
9
|
-
dateHour.setUTCMinutes(minutes);
|
|
10
|
-
return dateHour;
|
|
11
|
-
}
|
|
12
|
-
export { formatBrazilianDateHour };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatBrazilianDateToDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatBrazilianDateToDate.ts"],"names":[],"mappings":"AAEA,iBAAS,yBAAyB,CAAC,aAAa,EAAE,MAAM,QAQvD;AAED,OAAO,EAAE,yBAAyB,EAAE,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
function formatBrazilianDateToDate(brazilianData) {
|
|
3
|
-
if (regex.DATE.test(brazilianData) === false)
|
|
4
|
-
throw new Error("Invalid date format");
|
|
5
|
-
const [day, month, year] = brazilianData.split("/").map(Number);
|
|
6
|
-
const formattedDate = `${year}-${month}-${day}`;
|
|
7
|
-
return new Date(formattedDate);
|
|
8
|
-
}
|
|
9
|
-
export { formatBrazilianDateToDate };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatDateHour.d.ts","sourceRoot":"","sources":["../../src/formats/formatDateHour.ts"],"names":[],"mappings":"AAEA,iBAAS,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAUjD;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
function formatDateHour(date, hour) {
|
|
3
|
-
if (regex.HOUR.test(hour) === false)
|
|
4
|
-
throw new Error("Invalid hour format");
|
|
5
|
-
const dateHour = new Date(date);
|
|
6
|
-
const [hours, minutes] = hour.split(":").map(Number);
|
|
7
|
-
dateHour.setUTCHours(hours);
|
|
8
|
-
dateHour.setUTCMinutes(minutes);
|
|
9
|
-
return dateHour;
|
|
10
|
-
}
|
|
11
|
-
export { formatDateHour };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatToBRL.d.ts","sourceRoot":"","sources":["../../src/formats/formatToBRL.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,UAAO,UAOpD;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatToCNPJ.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCNPJ.ts"],"names":[],"mappings":"AAAA,iBAAS,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
function formatToCNPJ(value) {
|
|
2
|
-
const cleaned = value.replace(/\D/g, "");
|
|
3
|
-
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
4
|
-
if (match) {
|
|
5
|
-
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
6
|
-
}
|
|
7
|
-
return value;
|
|
8
|
-
}
|
|
9
|
-
export { formatToCNPJ };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatToCPF.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCPF.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO1C;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
type DigitCharacterNode = {
|
|
2
|
-
kind: "digit";
|
|
3
|
-
digit: number;
|
|
4
|
-
character: string;
|
|
5
|
-
};
|
|
6
|
-
type OtherCharacterNode = {
|
|
7
|
-
kind: "other";
|
|
8
|
-
character: string;
|
|
9
|
-
};
|
|
10
|
-
type RootCharacterNode = {
|
|
11
|
-
kind: "root";
|
|
12
|
-
digits: number;
|
|
13
|
-
children: (DigitCharacterNode | OtherCharacterNode)[];
|
|
14
|
-
};
|
|
15
|
-
declare const parseToCharacters: (value: string) => RootCharacterNode;
|
|
16
|
-
export { parseToCharacters };
|
|
17
|
-
//# sourceMappingURL=parseToCharacters.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parseToCharacters.d.ts","sourceRoot":"","sources":["../../src/parsers/parseToCharacters.ts"],"names":[],"mappings":"AAEA,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,CAAC,kBAAkB,GAAG,kBAAkB,CAAC,EAAE,CAAC;CACvD,CAAC;AAEF,QAAA,MAAM,iBAAiB,GAAI,OAAO,MAAM,KAAG,iBAY1C,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const DIGIT = /^\d$/;
|
|
2
|
-
const parseToCharacters = (value) => {
|
|
3
|
-
let digits = 0;
|
|
4
|
-
const children = value
|
|
5
|
-
.split("")
|
|
6
|
-
.map((character) => {
|
|
7
|
-
if (DIGIT.test(character))
|
|
8
|
-
return { character, kind: "digit", digit: ++digits };
|
|
9
|
-
return { character, kind: "other" };
|
|
10
|
-
});
|
|
11
|
-
return { digits, children, kind: "root" };
|
|
12
|
-
};
|
|
13
|
-
export { parseToCharacters };
|
package/dist/services/range.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/services/range.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,cAAc,GAClB,OAAO,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,OAAO,MAAM,KACZ,CAAC,MAAM,EAAE,MAAM,CAMjB,CAAC;AAEF,QAAA,MAAM,MAAM,GAAI,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,MAAM,KAAG,OACjB,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/services/range.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
const normalizeRange = (range, limit) => {
|
|
2
|
-
if (Array.isArray(range))
|
|
3
|
-
return range;
|
|
4
|
-
if (range >= 0)
|
|
5
|
-
return [0, range];
|
|
6
|
-
return [limit + 1 - Math.abs(range), limit];
|
|
7
|
-
};
|
|
8
|
-
const within = (range, value) => value >= range[0] && value <= range[1];
|
|
9
|
-
export { normalizeRange, within };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
declare const regex: {
|
|
2
|
-
URL: RegExp;
|
|
3
|
-
HOUR: RegExp;
|
|
4
|
-
RG: RegExp;
|
|
5
|
-
PHONE: RegExp;
|
|
6
|
-
PASSWORD: RegExp;
|
|
7
|
-
DATEHOUR: RegExp;
|
|
8
|
-
CPF: RegExp;
|
|
9
|
-
CNPJ: RegExp;
|
|
10
|
-
CEP: RegExp;
|
|
11
|
-
CPFCNPJ: RegExp;
|
|
12
|
-
DATE: RegExp;
|
|
13
|
-
};
|
|
14
|
-
export { regex };
|
|
15
|
-
//# sourceMappingURL=regex.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"regex.d.ts","sourceRoot":"","sources":["../../src/validations/regex.ts"],"names":[],"mappings":"AA0BA,QAAA,MAAM,KAAK;;;;;;;;;;;;CAYV,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const CEP = /^(\d{8}|\d{2}\.?\d{3}\-\d{3})$/;
|
|
2
|
-
const CNPJ = /(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/;
|
|
3
|
-
const CPF = /(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)/;
|
|
4
|
-
const DATE = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/;
|
|
5
|
-
const DATEHOUR = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
6
|
-
const HOUR = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
7
|
-
const PASSWORD = /(?=^.{8,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*/;
|
|
8
|
-
const PHONE = /([0-9]{2})?(\s|-)?(9?[0-9]{4})-?([0-9]{4}$)/;
|
|
9
|
-
const RG = /(^\d{1,2}).?(\d{3}).?(\d{3})-?(\d{1}|X|x$)/;
|
|
10
|
-
const URL = /^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$/;
|
|
11
|
-
const CPFCNPJ = /^(?:(\d{3}\.\d{3}\.\d{3}-\d{2})|(\d{11})|(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})|(\d{14}))$/;
|
|
12
|
-
const regex = {
|
|
13
|
-
URL,
|
|
14
|
-
HOUR,
|
|
15
|
-
RG,
|
|
16
|
-
PHONE,
|
|
17
|
-
PASSWORD,
|
|
18
|
-
DATEHOUR,
|
|
19
|
-
CPF,
|
|
20
|
-
CNPJ,
|
|
21
|
-
CEP,
|
|
22
|
-
CPFCNPJ,
|
|
23
|
-
DATE,
|
|
24
|
-
};
|
|
25
|
-
export { regex };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validateCPF.d.ts","sourceRoot":"","sources":["../../src/validations/validateCPF.ts"],"names":[],"mappings":"AA2BA,iBAAS,WAAW,CAAC,MAAM,EAAE,MAAM,WAQlC;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
function validateCPF(rawCpf) {
|
|
25
|
-
if (!rawCpf)
|
|
26
|
-
return false;
|
|
27
|
-
const cpf = removeNonDigits(rawCpf);
|
|
28
|
-
if (isInvalidLength(cpf))
|
|
29
|
-
return false;
|
|
30
|
-
if (hasAllDigitsEqual(cpf))
|
|
31
|
-
return false;
|
|
32
|
-
const digit1 = calculateDigit(cpf, 10);
|
|
33
|
-
const digit2 = calculateDigit(cpf, 11);
|
|
34
|
-
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
35
|
-
}
|
|
36
|
-
export { validateCPF };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
|
|
3
|
-
function formatBrazilianDateHour(date: string, hour: string) {
|
|
4
|
-
if (regex.HOUR.test(hour) === false) throw new Error("Invalid hour format");
|
|
5
|
-
|
|
6
|
-
const [day, month, year] = date.split("/").map(Number);
|
|
7
|
-
|
|
8
|
-
const dateHour = new Date(`${year}-${month}-${day}`);
|
|
9
|
-
const [hours, minutes] = hour.split(":").map(Number);
|
|
10
|
-
|
|
11
|
-
dateHour.setUTCHours(hours);
|
|
12
|
-
dateHour.setUTCMinutes(minutes);
|
|
13
|
-
|
|
14
|
-
return dateHour;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { formatBrazilianDateHour };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
|
|
3
|
-
function formatBrazilianDateToDate(brazilianData: string) {
|
|
4
|
-
if (regex.DATE.test(brazilianData) === false)
|
|
5
|
-
throw new Error("Invalid date format");
|
|
6
|
-
|
|
7
|
-
const [day, month, year] = brazilianData.split("/").map(Number);
|
|
8
|
-
const formattedDate = `${year}-${month}-${day}`;
|
|
9
|
-
|
|
10
|
-
return new Date(formattedDate);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export { formatBrazilianDateToDate };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { regex } from "../validations/regex";
|
|
2
|
-
|
|
3
|
-
function formatDateHour(date: string, hour: string) {
|
|
4
|
-
if (regex.HOUR.test(hour) === false) throw new Error("Invalid hour format");
|
|
5
|
-
|
|
6
|
-
const dateHour = new Date(date);
|
|
7
|
-
const [hours, minutes] = hour.split(":").map(Number);
|
|
8
|
-
|
|
9
|
-
dateHour.setUTCHours(hours);
|
|
10
|
-
dateHour.setUTCMinutes(minutes);
|
|
11
|
-
|
|
12
|
-
return dateHour;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export { formatDateHour };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
function formatToBRL(value: number, showPrefix = true) {
|
|
2
|
-
const format = new Intl.NumberFormat("pt-BR", {
|
|
3
|
-
style: "currency",
|
|
4
|
-
currency: "BRL",
|
|
5
|
-
}).format(value);
|
|
6
|
-
|
|
7
|
-
return showPrefix ? format : format.replace("R$", "").trim();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { formatToBRL };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
function formatToCNPJ(value: string): string {
|
|
2
|
-
const cleaned = value.replace(/\D/g, "");
|
|
3
|
-
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
4
|
-
if (match) {
|
|
5
|
-
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
6
|
-
}
|
|
7
|
-
return value;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { formatToCNPJ };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
function formatToCPF(value: string): string {
|
|
2
|
-
const cleaned = value.replace(/\D/g, "");
|
|
3
|
-
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
4
|
-
if (match) {
|
|
5
|
-
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
6
|
-
}
|
|
7
|
-
return value;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { formatToCPF };
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const DIGIT = /^\d$/;
|
|
2
|
-
|
|
3
|
-
type DigitCharacterNode = {
|
|
4
|
-
kind: "digit";
|
|
5
|
-
digit: number;
|
|
6
|
-
character: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
type OtherCharacterNode = {
|
|
10
|
-
kind: "other";
|
|
11
|
-
character: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
type RootCharacterNode = {
|
|
15
|
-
kind: "root";
|
|
16
|
-
digits: number;
|
|
17
|
-
children: (DigitCharacterNode | OtherCharacterNode)[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const parseToCharacters = (value: string): RootCharacterNode => {
|
|
21
|
-
let digits = 0;
|
|
22
|
-
|
|
23
|
-
const children = value
|
|
24
|
-
.split("")
|
|
25
|
-
.map((character: string): DigitCharacterNode | OtherCharacterNode => {
|
|
26
|
-
if (DIGIT.test(character))
|
|
27
|
-
return { character, kind: "digit", digit: ++digits };
|
|
28
|
-
return { character, kind: "other" };
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return { digits, children, kind: "root" };
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export { parseToCharacters };
|
package/src/services/range.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const normalizeRange = (
|
|
2
|
-
range: number | [number, number],
|
|
3
|
-
limit: number
|
|
4
|
-
): [number, number] => {
|
|
5
|
-
if (Array.isArray(range)) return range;
|
|
6
|
-
|
|
7
|
-
if (range >= 0) return [0, range];
|
|
8
|
-
|
|
9
|
-
return [limit + 1 - Math.abs(range), limit];
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const within = (range: [number, number], value: number): boolean =>
|
|
13
|
-
value >= range[0] && value <= range[1];
|
|
14
|
-
|
|
15
|
-
export { normalizeRange, within };
|
package/src/validations/regex.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const CEP = /^(\d{8}|\d{2}\.?\d{3}\-\d{3})$/;
|
|
2
|
-
|
|
3
|
-
const CNPJ = /(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/;
|
|
4
|
-
|
|
5
|
-
const CPF = /(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)/;
|
|
6
|
-
|
|
7
|
-
const DATE = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/;
|
|
8
|
-
|
|
9
|
-
const DATEHOUR =
|
|
10
|
-
/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
11
|
-
|
|
12
|
-
const HOUR = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
13
|
-
|
|
14
|
-
const PASSWORD =
|
|
15
|
-
/(?=^.{8,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*/;
|
|
16
|
-
|
|
17
|
-
const PHONE = /([0-9]{2})?(\s|-)?(9?[0-9]{4})-?([0-9]{4}$)/;
|
|
18
|
-
|
|
19
|
-
const RG = /(^\d{1,2}).?(\d{3}).?(\d{3})-?(\d{1}|X|x$)/;
|
|
20
|
-
|
|
21
|
-
const URL =
|
|
22
|
-
/^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$/;
|
|
23
|
-
|
|
24
|
-
const CPFCNPJ =
|
|
25
|
-
/^(?:(\d{3}\.\d{3}\.\d{3}-\d{2})|(\d{11})|(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})|(\d{14}))$/;
|
|
26
|
-
|
|
27
|
-
const regex = {
|
|
28
|
-
URL,
|
|
29
|
-
HOUR,
|
|
30
|
-
RG,
|
|
31
|
-
PHONE,
|
|
32
|
-
PASSWORD,
|
|
33
|
-
DATEHOUR,
|
|
34
|
-
CPF,
|
|
35
|
-
CNPJ,
|
|
36
|
-
CEP,
|
|
37
|
-
CPFCNPJ,
|
|
38
|
-
DATE,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export { regex };
|