@arkyn/shared 3.0.1-beta.9 → 3.0.1-beta.91
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/README.md +452 -67
- package/dist/bundle.js +2337 -0
- package/dist/bundle.umd.cjs +6 -0
- package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts +35 -0
- package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts.map +1 -0
- package/dist/formats/formatToCapitalizeFirstWordLetter.js +42 -0
- package/dist/index.d.ts +2 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -8
- package/dist/services/isHtml.d.ts +22 -0
- package/dist/services/isHtml.d.ts.map +1 -0
- package/dist/services/isHtml.js +24 -0
- package/package.json +26 -6
- package/dist/validations/validateCep.d.ts +0 -24
- package/dist/validations/validateCep.d.ts.map +0 -1
- package/dist/validations/validateCep.js +0 -33
- package/dist/validations/validateCnpj.d.ts +0 -22
- package/dist/validations/validateCnpj.d.ts.map +0 -1
- package/dist/validations/validateCnpj.js +0 -52
- package/dist/validations/validateCpf.d.ts +0 -24
- package/dist/validations/validateCpf.d.ts.map +0 -1
- package/dist/validations/validateCpf.js +0 -54
- package/dist/validations/validateDate.d.ts +0 -34
- package/dist/validations/validateDate.d.ts.map +0 -1
- package/dist/validations/validateDate.js +0 -73
- package/dist/validations/validatePassword.d.ts +0 -21
- package/dist/validations/validatePassword.d.ts.map +0 -1
- package/dist/validations/validatePassword.js +0 -34
- package/dist/validations/validatePhone.d.ts +0 -29
- package/dist/validations/validatePhone.d.ts.map +0 -1
- package/dist/validations/validatePhone.js +0 -44
- package/dist/validations/validateRg.d.ts +0 -22
- package/dist/validations/validateRg.d.ts.map +0 -1
- package/dist/validations/validateRg.js +0 -31
- package/src/formats/formatDate.ts +0 -92
- package/src/formats/formatJsonObject.ts +0 -90
- package/src/formats/formatJsonString.ts +0 -50
- package/src/formats/formatToCep.ts +0 -39
- package/src/formats/formatToCnpj.ts +0 -40
- package/src/formats/formatToCpf.ts +0 -40
- package/src/formats/formatToCpfCnpj.ts +0 -38
- package/src/formats/formatToCurrency.ts +0 -63
- package/src/formats/formatToDate.ts +0 -70
- package/src/formats/formatToEllipsis.ts +0 -25
- package/src/formats/formatToHiddenDigits.ts +0 -92
- package/src/formats/formatToPhone.ts +0 -170
- package/src/generators/generateColorByString.ts +0 -33
- package/src/generators/generateId.ts +0 -61
- package/src/generators/generateSlug.ts +0 -31
- package/src/index.ts +0 -36
- package/src/services/calculateCardInstallment.ts +0 -73
- package/src/services/ensureQuotes.ts +0 -25
- package/src/services/maskSensitiveData.ts +0 -68
- package/src/services/removeCurrencySymbols.ts +0 -29
- package/src/services/removeNonNumeric.ts +0 -20
- package/src/services/stripHtmlTags.ts +0 -20
- package/src/services/truncateLargeFields.ts +0 -69
- package/src/validations/validateCep.ts +0 -41
- package/src/validations/validateCnpj.ts +0 -65
- package/src/validations/validateCpf.ts +0 -62
- package/src/validations/validateDate.ts +0 -86
- package/src/validations/validatePassword.ts +0 -41
- package/src/validations/validatePhone.ts +0 -50
- package/src/validations/validateRg.ts +0 -37
- package/tsconfig.json +0 -20
- package/vitest.config.ts +0 -5
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
type ValidateDateConfig = {
|
|
2
|
-
inputFormat?: "DD/MM/YYYY" | "MM-DD-YYYY" | "YYYY-MM-DD";
|
|
3
|
-
minYear?: number;
|
|
4
|
-
maxYear?: number;
|
|
5
|
-
};
|
|
6
|
-
type ValidateDateFunction = (rawDate: string, config?: ValidateDateConfig) => boolean;
|
|
7
|
-
/**
|
|
8
|
-
* Validates a date string based on the provided format and configuration.
|
|
9
|
-
*
|
|
10
|
-
* @param rawDate - The date string to validate.
|
|
11
|
-
* @param config - Optional configuration object to customize validation.
|
|
12
|
-
* @param config.inputFormat - The expected format of the input date.
|
|
13
|
-
* Supported formats are "DD/MM/YYYY", "MM-DD-YYYY", and "YYYY-MM-DD".
|
|
14
|
-
* Defaults to "DD/MM/YYYY".
|
|
15
|
-
* @param config.minYear - The minimum allowed year for the date. Defaults to 1900.
|
|
16
|
-
* @param config.maxYear - The maximum allowed year for the date. Defaults to 3000.
|
|
17
|
-
*
|
|
18
|
-
* @returns `true` if the date is valid according to the specified format and configuration, otherwise `false`.
|
|
19
|
-
*
|
|
20
|
-
* @throws {Error} If an invalid date format is provided in the configuration.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```typescript
|
|
24
|
-
* validateDate("31/12/2023"); // true
|
|
25
|
-
* validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" }); // true
|
|
26
|
-
* validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD", minYear: 2000, maxYear: 2100 }); // true
|
|
27
|
-
* validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" }); // true (leap year)
|
|
28
|
-
* validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" }); // false (not a leap year)
|
|
29
|
-
* validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" }); // false (April has 30 days)
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
declare const validateDate: ValidateDateFunction;
|
|
33
|
-
export { validateDate };
|
|
34
|
-
//# sourceMappingURL=validateDate.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validateDate.d.ts","sourceRoot":"","sources":["../../src/validations/validateDate.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG;IACxB,WAAW,CAAC,EAAE,YAAY,GAAG,YAAY,GAAG,YAAY,CAAC;IACzD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,oBAAoB,GAAG,CAC1B,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,kBAAkB,KACxB,OAAO,CAAC;AAEb;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBA8CnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validates a date string based on the provided format and configuration.
|
|
3
|
-
*
|
|
4
|
-
* @param rawDate - 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 = (rawDate, 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(rawDate))
|
|
34
|
-
return false;
|
|
35
|
-
[, day, month, year] = rawDate.match(dateRegex) || [];
|
|
36
|
-
}
|
|
37
|
-
else if (inputFormat === "MM-DD-YYYY") {
|
|
38
|
-
const dateRegex = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
39
|
-
if (!dateRegex.test(rawDate))
|
|
40
|
-
return false;
|
|
41
|
-
[, month, day, year] = rawDate.match(dateRegex) || [];
|
|
42
|
-
}
|
|
43
|
-
else if (inputFormat === "YYYY-MM-DD") {
|
|
44
|
-
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
45
|
-
if (!dateRegex.test(rawDate))
|
|
46
|
-
return false;
|
|
47
|
-
[, year, month, day] = rawDate.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 };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type ValidatePasswordFunction = (rawPassword: string) => boolean;
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validatePassword.d.ts","sourceRoot":"","sources":["../../src/validations/validatePassword.ts"],"names":[],"mappings":"AAAA,KAAK,wBAAwB,GAAG,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC;AAEjE;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAkBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,34 +0,0 @@
|
|
|
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 };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
type ValidatePhoneFunction = (rawPhone: string) => boolean;
|
|
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 rawPhone - 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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"AAEA,KAAK,qBAAqB,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAiBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -1,44 +0,0 @@
|
|
|
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 rawPhone - 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 = (rawPhone) => {
|
|
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(rawPhone))
|
|
35
|
-
return true;
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
|
|
39
|
-
if (regex.test(rawPhone))
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
};
|
|
44
|
-
export { validatePhone };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
type ValidateRgFunction = (rawRg: string) => boolean;
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validateRg.d.ts","sourceRoot":"","sources":["../../src/validations/validateRg.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;AAErD;;;;;;;;;;;;;;;;;GAiBG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAajB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
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 };
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
type InputFormatTypes = "brazilianDate" | "isoDate" | "timestamp";
|
|
2
|
-
|
|
3
|
-
type FormatDateFunction = (
|
|
4
|
-
date: string[], // [date: string, time?: string]
|
|
5
|
-
inputFormat: InputFormatTypes,
|
|
6
|
-
outputFormat: string,
|
|
7
|
-
timezone?: number
|
|
8
|
-
) => string;
|
|
9
|
-
|
|
10
|
-
function formatDateString(date: Date, format: string): string {
|
|
11
|
-
const pad = (num: number) => num.toString().padStart(2, "0");
|
|
12
|
-
|
|
13
|
-
const replacements: Record<string, string> = {
|
|
14
|
-
YYYY: date.getFullYear().toString(),
|
|
15
|
-
YY: date.getFullYear().toString().slice(-2),
|
|
16
|
-
MM: pad(date.getMonth() + 1),
|
|
17
|
-
DD: pad(date.getDate()),
|
|
18
|
-
hh: pad(date.getHours()),
|
|
19
|
-
mm: pad(date.getMinutes()),
|
|
20
|
-
ss: pad(date.getSeconds()),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
return format.replace(
|
|
24
|
-
/YYYY|YY|MM|DD|hh|mm|ss/g,
|
|
25
|
-
(match) => replacements[match]
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Formats a date and time string based on the provided input and output formats.
|
|
31
|
-
*
|
|
32
|
-
* @param {[string, string]} dateTime - An array containing the date and optional time.
|
|
33
|
-
* - The first element is the date string.
|
|
34
|
-
* - The second element is the time string (default is "00:00:00").
|
|
35
|
-
* @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
|
|
36
|
-
* - "brazilianDate": Expects the date in "DD/MM/YYYY" format.
|
|
37
|
-
* - "isoDate": Expects the date in "YYYY-MM-DD" format.
|
|
38
|
-
* - "timestamp": Expects the date in "YYYY/MM/DD" format.
|
|
39
|
-
* @param {string} outputFormat - The desired output format for the date.
|
|
40
|
-
* - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
|
|
41
|
-
* @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
|
|
42
|
-
* - Defaults to 0 (UTC).
|
|
43
|
-
* @returns {string} The formatted date string based on the output format.
|
|
44
|
-
* @throws {Error} If the input format is invalid.
|
|
45
|
-
* @throws {Error} If the date is invalid.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* // Format a Brazilian date to ISO format
|
|
49
|
-
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
|
|
50
|
-
* // Returns: "2023-12-25 15:30:00"
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* // Format an ISO date to a custom format with timezone adjustment
|
|
54
|
-
* formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
|
|
55
|
-
* // Returns: "25/12/2023 12:30:00"
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
const formatDate: FormatDateFunction = (
|
|
59
|
-
[date, time = "00:00:00"],
|
|
60
|
-
inputFormat,
|
|
61
|
-
outputFormat,
|
|
62
|
-
timezone = 0
|
|
63
|
-
) => {
|
|
64
|
-
const dateParts = date.split(/[-/]/).map(Number);
|
|
65
|
-
const timeParts = time.split(".")[0].split(":").map(Number);
|
|
66
|
-
|
|
67
|
-
let day, month, year;
|
|
68
|
-
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
69
|
-
|
|
70
|
-
switch (inputFormat) {
|
|
71
|
-
case "brazilianDate":
|
|
72
|
-
[day, month, year] = dateParts;
|
|
73
|
-
break;
|
|
74
|
-
case "isoDate":
|
|
75
|
-
[year, month, day] = dateParts;
|
|
76
|
-
break;
|
|
77
|
-
case "timestamp":
|
|
78
|
-
[year, month, day] = dateParts.map(Number);
|
|
79
|
-
break;
|
|
80
|
-
default:
|
|
81
|
-
throw new Error("Invalid input format");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
85
|
-
if (isNaN(formattedDate.getTime())) throw new Error("Invalid date");
|
|
86
|
-
|
|
87
|
-
formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
|
|
88
|
-
|
|
89
|
-
return formatDateString(formattedDate, outputFormat);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export { formatDate };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
type FormatJsonObjectFunction = (jsonString: any, identLevel: number) => string;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Formats a JSON object into a human-readable string with proper indentation.
|
|
5
|
-
*
|
|
6
|
-
* @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
|
|
7
|
-
* @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
|
|
8
|
-
* @returns A formatted string representation of the JSON object.
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
12
|
-
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
13
|
-
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
14
|
-
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
19
|
-
* const formatted = formatJsonObject(obj, 0);
|
|
20
|
-
* console.log(formatted);
|
|
21
|
-
* // Output:
|
|
22
|
-
* // {
|
|
23
|
-
* // "name": "John",
|
|
24
|
-
* // "age": 30,
|
|
25
|
-
* // "hobbies": [
|
|
26
|
-
* // "reading",
|
|
27
|
-
* // "gaming"
|
|
28
|
-
* // ]
|
|
29
|
-
* // }
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const formatJsonObject: FormatJsonObjectFunction = (obj, indentLevel) => {
|
|
34
|
-
const indent = " ".repeat(indentLevel);
|
|
35
|
-
let formattedString = "";
|
|
36
|
-
|
|
37
|
-
if (typeof obj === "object" && obj !== null) {
|
|
38
|
-
if (Array.isArray(obj)) {
|
|
39
|
-
if (obj.length === 0) {
|
|
40
|
-
// Caso especial para arrays vazios
|
|
41
|
-
formattedString += "[]";
|
|
42
|
-
} else {
|
|
43
|
-
formattedString += "[\n";
|
|
44
|
-
obj.forEach((item, index) => {
|
|
45
|
-
formattedString +=
|
|
46
|
-
indent + " " + formatJsonObject(item, indentLevel + 1);
|
|
47
|
-
if (index < obj.length - 1) {
|
|
48
|
-
formattedString += ",";
|
|
49
|
-
}
|
|
50
|
-
formattedString += "\n";
|
|
51
|
-
});
|
|
52
|
-
formattedString += indent + "]";
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
const keys = Object.keys(obj);
|
|
56
|
-
if (keys.length === 0) {
|
|
57
|
-
// Caso especial para objetos vazios
|
|
58
|
-
formattedString += "{}";
|
|
59
|
-
} else {
|
|
60
|
-
formattedString += "{\n";
|
|
61
|
-
keys.forEach((key, index) => {
|
|
62
|
-
formattedString +=
|
|
63
|
-
indent +
|
|
64
|
-
' "' +
|
|
65
|
-
key +
|
|
66
|
-
'": ' +
|
|
67
|
-
formatJsonObject(obj[key], indentLevel + 1);
|
|
68
|
-
if (index < keys.length - 1) {
|
|
69
|
-
formattedString += ",";
|
|
70
|
-
}
|
|
71
|
-
formattedString += "\n";
|
|
72
|
-
});
|
|
73
|
-
formattedString += indent + "}";
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
} else if (typeof obj === "string") {
|
|
77
|
-
try {
|
|
78
|
-
const parsedObj = JSON.parse(obj);
|
|
79
|
-
formattedString += formatJsonObject(parsedObj, indentLevel);
|
|
80
|
-
} catch {
|
|
81
|
-
formattedString += '"' + obj + '"';
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
formattedString += obj;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return formattedString;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export { formatJsonObject };
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { formatJsonObject } from "./formatJsonObject";
|
|
2
|
-
|
|
3
|
-
type FormatJsonStringFunction = (jsonString: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a JSON string into a more readable format.
|
|
7
|
-
*
|
|
8
|
-
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
9
|
-
* and then formats it using the `formatJsonObject` function. If the input string
|
|
10
|
-
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
11
|
-
*
|
|
12
|
-
* @param jsonString - The JSON string to be formatted.
|
|
13
|
-
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
14
|
-
*
|
|
15
|
-
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
20
|
-
* const formatted = formatJsonString(jsonString);
|
|
21
|
-
* console.log(formatted);
|
|
22
|
-
* // Output:
|
|
23
|
-
* // {
|
|
24
|
-
* // "name": "John",
|
|
25
|
-
* // "age": 30,
|
|
26
|
-
* // "hobbies": [
|
|
27
|
-
* // "reading",
|
|
28
|
-
* // "gaming"
|
|
29
|
-
* // ]
|
|
30
|
-
* // }
|
|
31
|
-
|
|
32
|
-
* const invalidJsonString = '{"name":"John", "age":30,';
|
|
33
|
-
* const formatted = formatJsonString(invalidJsonString);
|
|
34
|
-
* console.log(formatted);
|
|
35
|
-
* // Output:
|
|
36
|
-
* // (Logs "Invalid JSON string: ..." to the console)
|
|
37
|
-
* // ""
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
const formatJsonString: FormatJsonStringFunction = (jsonString) => {
|
|
42
|
-
try {
|
|
43
|
-
const jsonObject = JSON.parse(jsonString);
|
|
44
|
-
return formatJsonObject(jsonObject, 0);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
throw new Error(`Invalid JSON string \n ${error}`);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export { formatJsonString };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCepFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
7
|
-
*
|
|
8
|
-
* The function removes all non-numeric characters from the input string
|
|
9
|
-
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
10
|
-
* If the input does not match the expected format, an error is thrown.
|
|
11
|
-
*
|
|
12
|
-
* @param value - The input string to be formatted as a CEP.
|
|
13
|
-
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
14
|
-
* @throws {Error} If the input does not match the expected CEP format.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { formatToCep } from "./formatToCep";
|
|
19
|
-
*
|
|
20
|
-
* const formattedCep = formatToCep("12345678");
|
|
21
|
-
* console.log(formattedCep); // Output: "12345-678"
|
|
22
|
-
*
|
|
23
|
-
* try {
|
|
24
|
-
* formatToCep("1234");
|
|
25
|
-
* } catch (error) {
|
|
26
|
-
* console.error(error.message); // Output: "Invalid CEP format"
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const formatToCep: FormatToCepFunction = (value) => {
|
|
32
|
-
const cleaned = removeNonNumeric(value);
|
|
33
|
-
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
34
|
-
|
|
35
|
-
if (match) return `${match[1]}-${match[2]}`;
|
|
36
|
-
throw new Error("Invalid CEP format");
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export { formatToCep };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCnpjFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
7
|
-
*
|
|
8
|
-
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
9
|
-
*
|
|
10
|
-
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
11
|
-
* Non-numeric characters will be removed before formatting.
|
|
12
|
-
*
|
|
13
|
-
* @returns A string formatted as a CNPJ.
|
|
14
|
-
*
|
|
15
|
-
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import { formatToCnpj } from "./formatToCNPJ";
|
|
20
|
-
*
|
|
21
|
-
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
22
|
-
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
23
|
-
*
|
|
24
|
-
* try {
|
|
25
|
-
* formatToCnpj("12345");
|
|
26
|
-
* } catch (error) {
|
|
27
|
-
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
28
|
-
* }
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const formatToCnpj: FormatToCnpjFunction = (value) => {
|
|
33
|
-
const cleaned = removeNonNumeric(value);
|
|
34
|
-
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
35
|
-
if (match)
|
|
36
|
-
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
37
|
-
throw new Error("Invalid CNPJ length");
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export { formatToCnpj };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
|
|
3
|
-
type FormatToCpfFunction = (value: string) => string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
7
|
-
*
|
|
8
|
-
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
9
|
-
* This function ensures the input is cleaned of non-numeric characters and
|
|
10
|
-
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
11
|
-
*
|
|
12
|
-
* @param value - The input string to be formatted as a CPF.
|
|
13
|
-
* @returns The formatted CPF string.
|
|
14
|
-
* @throws {Error} If the input string does not match the expected CPF format.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { formatToCpf } from "./formatToCPF";
|
|
19
|
-
*
|
|
20
|
-
* const formattedCpf = formatToCpf("12345678909");
|
|
21
|
-
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
22
|
-
|
|
23
|
-
* try {
|
|
24
|
-
* const formattedCpf = formatToCpf("12345");
|
|
25
|
-
* } catch (error) {
|
|
26
|
-
* console.error(error.message); // Output: "Invalid CPF format"
|
|
27
|
-
* }
|
|
28
|
-
*
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const formatToCpf: FormatToCpfFunction = (value) => {
|
|
33
|
-
const cleaned = removeNonNumeric(value);
|
|
34
|
-
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
35
|
-
|
|
36
|
-
if (match) return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
37
|
-
throw new Error("Invalid CPF format");
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export { formatToCpf };
|