@arkyn/shared 1.4.56 → 1.5.0
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/formatBrazilianDateHour.d.ts +3 -0
- package/dist/formats/formatBrazilianDateHour.d.ts.map +1 -0
- package/dist/formats/formatBrazilianDateHour.js +12 -0
- package/dist/formats/formatBrazilianDateToDate.d.ts +3 -0
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +1 -0
- package/dist/formats/formatBrazilianDateToDate.js +9 -0
- package/dist/formats/formatDateHour.d.ts +3 -0
- package/dist/formats/formatDateHour.d.ts.map +1 -0
- package/dist/formats/formatDateHour.js +11 -0
- package/dist/formats/formatJsonObject.d.ts +1 -31
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +26 -67
- package/dist/formats/formatJsonString.d.ts +1 -37
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +4 -38
- package/dist/formats/formatToBRL.d.ts +3 -0
- package/dist/formats/formatToBRL.d.ts.map +1 -0
- package/dist/formats/formatToBRL.js +8 -0
- package/dist/formats/formatToCNPJ.d.ts +3 -0
- package/dist/formats/formatToCNPJ.d.ts.map +1 -0
- package/dist/formats/formatToCNPJ.js +9 -0
- package/dist/formats/formatToCPF.d.ts +3 -0
- package/dist/formats/formatToCPF.d.ts.map +1 -0
- package/dist/formats/formatToCPF.js +9 -0
- package/dist/formats/formatToCep.d.ts +1 -27
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +6 -31
- package/dist/formats/formatToCpfCnpj.d.ts +1 -22
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +7 -27
- package/dist/formats/formatToEllipsis.d.ts +1 -14
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +3 -20
- package/dist/formats/formatToHiddenDigits.d.ts +2 -30
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +3 -49
- package/dist/formats/formatToPhone.d.ts +1 -32
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +6 -128
- package/dist/generators/generateColorByString.d.ts +1 -13
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +4 -15
- package/dist/generators/generateId.d.ts +4 -26
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +5 -11
- package/dist/generators/generateSlug.d.ts +1 -15
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +2 -16
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -7
- package/dist/parsers/parseToCharacters.d.ts +17 -0
- package/dist/parsers/parseToCharacters.d.ts.map +1 -0
- package/dist/parsers/parseToCharacters.js +13 -0
- package/dist/services/calculateCardInstallment.d.ts +5 -31
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +11 -52
- package/dist/services/maskSensitiveData.d.ts +1 -24
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +2 -24
- package/dist/services/range.d.ts +4 -0
- package/dist/services/range.d.ts.map +1 -0
- package/dist/services/range.js +9 -0
- package/dist/services/truncateLargeFields.d.ts +1 -30
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +6 -34
- package/dist/validations/regex.d.ts +15 -0
- package/dist/validations/regex.d.ts.map +1 -0
- package/dist/validations/regex.js +25 -0
- package/dist/validations/validateCPF.d.ts +3 -0
- package/dist/validations/validateCPF.d.ts.map +1 -0
- package/dist/validations/validateCPF.js +36 -0
- package/dist/validations/validateDate.d.ts +6 -27
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +6 -32
- package/dist/validations/validatePhone.d.ts +1 -27
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +2 -27
- package/package.json +2 -4
- package/src/formats/formatBrazilianDateHour.ts +17 -0
- package/src/formats/formatBrazilianDateToDate.ts +13 -0
- package/src/formats/formatDateHour.ts +15 -0
- package/src/formats/formatJsonObject.ts +26 -68
- package/src/formats/formatJsonString.ts +4 -40
- package/src/formats/formatToBRL.ts +10 -0
- package/src/formats/formatToCNPJ.ts +10 -0
- package/src/formats/formatToCPF.ts +10 -0
- package/src/formats/formatToCep.ts +7 -35
- package/src/formats/formatToCpfCnpj.ts +7 -31
- package/src/formats/formatToEllipsis.ts +3 -23
- package/src/formats/formatToHiddenDigits.ts +7 -75
- package/src/formats/formatToPhone.ts +6 -156
- package/src/generators/generateColorByString.ts +4 -18
- package/src/generators/generateId.ts +5 -36
- package/src/generators/generateSlug.ts +2 -17
- package/src/index.ts +15 -7
- package/src/parsers/parseToCharacters.ts +34 -0
- package/src/services/calculateCardInstallment.ts +12 -57
- package/src/services/maskSensitiveData.ts +4 -29
- package/src/services/range.ts +15 -0
- package/src/services/truncateLargeFields.ts +6 -40
- package/src/validations/regex.ts +41 -0
- package/src/validations/validateCPF.ts +38 -0
- package/src/validations/validateDate.ts +11 -33
- package/src/validations/validatePhone.ts +2 -29
- package/tsconfig.json +1 -1
- package/dist/formats/formatDate.d.ts +0 -43
- package/dist/formats/formatDate.d.ts.map +0 -1
- package/dist/formats/formatDate.js +0 -77
- package/dist/formats/formatToCnpj.d.ts +0 -30
- package/dist/formats/formatToCnpj.d.ts.map +0 -1
- package/dist/formats/formatToCnpj.js +0 -35
- package/dist/formats/formatToCpf.d.ts +0 -30
- package/dist/formats/formatToCpf.d.ts.map +0 -1
- package/dist/formats/formatToCpf.js +0 -35
- package/dist/formats/formatToCurrency.d.ts +0 -29
- package/dist/formats/formatToCurrency.d.ts.map +0 -1
- package/dist/formats/formatToCurrency.js +0 -41
- 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/dist/services/removeCurrencySymbols.d.ts +0 -20
- package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
- package/dist/services/removeCurrencySymbols.js +0 -23
- package/dist/services/removeNonNumeric.d.ts +0 -15
- package/dist/services/removeNonNumeric.d.ts.map +0 -1
- package/dist/services/removeNonNumeric.js +0 -16
- package/dist/validations/validateCpf.d.ts +0 -24
- package/dist/validations/validateCpf.d.ts.map +0 -1
- package/dist/validations/validateCpf.js +0 -56
- package/src/formats/formatDate.ts +0 -97
- package/src/formats/formatToCnpj.ts +0 -39
- package/src/formats/formatToCpf.ts +0 -39
- package/src/formats/formatToCurrency.ts +0 -53
- package/src/regex/index.ts +0 -8
- package/src/services/removeCurrencySymbols.ts +0 -25
- package/src/services/removeNonNumeric.ts +0 -18
- package/src/validations/validateCpf.ts +0 -64
- package/vitest.config.ts +0 -5
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
-
/**
|
|
3
|
-
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
4
|
-
*
|
|
5
|
-
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
6
|
-
* This function ensures the input is cleaned of non-numeric characters and
|
|
7
|
-
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
8
|
-
*
|
|
9
|
-
* @param value - The input string to be formatted as a CPF.
|
|
10
|
-
* @returns The formatted CPF string.
|
|
11
|
-
* @throws {Error} If the input string does not match the expected CPF format.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { formatToCpf } from "./formatToCPF";
|
|
16
|
-
*
|
|
17
|
-
* const formattedCpf = formatToCpf("12345678909");
|
|
18
|
-
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
19
|
-
|
|
20
|
-
* try {
|
|
21
|
-
* const formattedCpf = formatToCpf("12345");
|
|
22
|
-
* } catch (error) {
|
|
23
|
-
* console.error(error.message); // Output: "Invalid CPF format"
|
|
24
|
-
* }
|
|
25
|
-
*
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
const formatToCpf = (value) => {
|
|
29
|
-
const cleaned = removeNonNumeric(value);
|
|
30
|
-
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
31
|
-
if (match)
|
|
32
|
-
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
33
|
-
throw new Error("Invalid CPF format");
|
|
34
|
-
};
|
|
35
|
-
export { formatToCpf };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { FormatToCurrency } from "@arkyn/types";
|
|
2
|
-
/**
|
|
3
|
-
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
4
|
-
*
|
|
5
|
-
* @param value - The numeric value to be formatted.
|
|
6
|
-
* @param currency - The currency code used to determine the formatting style.
|
|
7
|
-
* @param config - Optional configuration object.
|
|
8
|
-
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
9
|
-
*
|
|
10
|
-
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
15
|
-
* console.log(formatted); // "$1,234.56"
|
|
16
|
-
*
|
|
17
|
-
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
18
|
-
* console.log(withoutPrefix); // "1,234.56"
|
|
19
|
-
*
|
|
20
|
-
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
21
|
-
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
22
|
-
*
|
|
23
|
-
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
24
|
-
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
declare const formatToCurrency: FormatToCurrency;
|
|
28
|
-
export { formatToCurrency };
|
|
29
|
-
//# sourceMappingURL=formatToCurrency.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatToCurrency.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCurrency.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIrD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,gBAmBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { countryCurrencies } from "@arkyn/templates";
|
|
2
|
-
import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
|
|
3
|
-
/**
|
|
4
|
-
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
5
|
-
*
|
|
6
|
-
* @param value - The numeric value to be formatted.
|
|
7
|
-
* @param currency - The currency code used to determine the formatting style.
|
|
8
|
-
* @param config - Optional configuration object.
|
|
9
|
-
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
10
|
-
*
|
|
11
|
-
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
16
|
-
* console.log(formatted); // "$1,234.56"
|
|
17
|
-
*
|
|
18
|
-
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
19
|
-
* console.log(withoutPrefix); // "1,234.56"
|
|
20
|
-
*
|
|
21
|
-
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
22
|
-
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
23
|
-
*
|
|
24
|
-
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
25
|
-
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
const formatToCurrency = (value, currency, config = { showPrefix: true }) => {
|
|
29
|
-
if (!countryCurrencies[currency]) {
|
|
30
|
-
throw new Error("Unsupported currency code");
|
|
31
|
-
}
|
|
32
|
-
const { countryCurrency, countryLanguage } = countryCurrencies[currency];
|
|
33
|
-
const format = new Intl.NumberFormat(countryLanguage, {
|
|
34
|
-
style: "currency",
|
|
35
|
-
currency: countryCurrency,
|
|
36
|
-
}).format(value);
|
|
37
|
-
return config.showPrefix
|
|
38
|
-
? format.replace(/\s/g, " ")
|
|
39
|
-
: removeCurrencySymbols(format).replace(/\s/g, " ");
|
|
40
|
-
};
|
|
41
|
-
export { formatToCurrency };
|
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
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes currency symbols from a given formatted string.
|
|
3
|
-
*
|
|
4
|
-
* This function takes a string that may contain currency symbols
|
|
5
|
-
* and removes them using a regular expression. The resulting string
|
|
6
|
-
* is also trimmed of any leading or trailing whitespace.
|
|
7
|
-
*
|
|
8
|
-
* @param formattedValue - The input string containing currency symbols.
|
|
9
|
-
* @returns A string with all currency symbols removed and trimmed of whitespace.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* removeCurrencySymbols("R$13,45"); // "13,45"
|
|
13
|
-
* removeCurrencySymbols("$123.45"); // "123.45"
|
|
14
|
-
* removeCurrencySymbols("€99.99"); // "99.99"
|
|
15
|
-
* removeCurrencySymbols("¥1,000"); // "1,000"
|
|
16
|
-
* removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
|
|
17
|
-
*/
|
|
18
|
-
declare const removeCurrencySymbols: (formattedValue: string) => string;
|
|
19
|
-
export { removeCurrencySymbols };
|
|
20
|
-
//# sourceMappingURL=removeCurrencySymbols.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"removeCurrencySymbols.d.ts","sourceRoot":"","sources":["../../src/services/removeCurrencySymbols.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,qBAAqB,GAAI,gBAAgB,MAAM,KAAG,MAIvD,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes currency symbols from a given formatted string.
|
|
3
|
-
*
|
|
4
|
-
* This function takes a string that may contain currency symbols
|
|
5
|
-
* and removes them using a regular expression. The resulting string
|
|
6
|
-
* is also trimmed of any leading or trailing whitespace.
|
|
7
|
-
*
|
|
8
|
-
* @param formattedValue - The input string containing currency symbols.
|
|
9
|
-
* @returns A string with all currency symbols removed and trimmed of whitespace.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* removeCurrencySymbols("R$13,45"); // "13,45"
|
|
13
|
-
* removeCurrencySymbols("$123.45"); // "123.45"
|
|
14
|
-
* removeCurrencySymbols("€99.99"); // "99.99"
|
|
15
|
-
* removeCurrencySymbols("¥1,000"); // "1,000"
|
|
16
|
-
* removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
|
|
17
|
-
*/
|
|
18
|
-
const removeCurrencySymbols = (formattedValue) => {
|
|
19
|
-
return formattedValue
|
|
20
|
-
.replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "") // Inclui "R$" e outros símbolos comuns
|
|
21
|
-
.trim();
|
|
22
|
-
};
|
|
23
|
-
export { removeCurrencySymbols };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes all non-numeric characters from a given string.
|
|
3
|
-
*
|
|
4
|
-
* @param prop - The input string from which non-numeric characters will be removed.
|
|
5
|
-
* @returns A new string containing only numeric characters from the input.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const result = removeNonNumeric("abc123def456");
|
|
10
|
-
* console.log(result); // Output: "123456"
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
declare function removeNonNumeric(prop: string): string;
|
|
14
|
-
export { removeNonNumeric };
|
|
15
|
-
//# sourceMappingURL=removeNonNumeric.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"removeNonNumeric.d.ts","sourceRoot":"","sources":["../../src/services/removeNonNumeric.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,iBAAS,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes all non-numeric characters from a given string.
|
|
3
|
-
*
|
|
4
|
-
* @param prop - The input string from which non-numeric characters will be removed.
|
|
5
|
-
* @returns A new string containing only numeric characters from the input.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const result = removeNonNumeric("abc123def456");
|
|
10
|
-
* console.log(result); // Output: "123456"
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
function removeNonNumeric(prop) {
|
|
14
|
-
return prop.replace(/[^0-9]/g, "");
|
|
15
|
-
}
|
|
16
|
-
export { removeNonNumeric };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { ValidateCpfFunction } from "@arkyn/types";
|
|
2
|
-
/**
|
|
3
|
-
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
4
|
-
*
|
|
5
|
-
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
6
|
-
* This function checks if the provided CPF is valid by performing the following steps:
|
|
7
|
-
* - Removes any non-digit characters from the input.
|
|
8
|
-
* - Verifies if the CPF has the correct length (11 digits).
|
|
9
|
-
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
10
|
-
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
11
|
-
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
12
|
-
*
|
|
13
|
-
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
14
|
-
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* validateCpf("123.456.789-09"); // false
|
|
19
|
-
* validateCpf("111.444.777-35"); // true
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
declare const validateCpf: ValidateCpfFunction;
|
|
23
|
-
export { validateCpf };
|
|
24
|
-
//# sourceMappingURL=validateCpf.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,56 +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
|
-
/**
|
|
25
|
-
* Validates a Brazilian CPF (Cadastro de Pessoas Físicas) number.
|
|
26
|
-
*
|
|
27
|
-
* The CPF is a unique identifier assigned to Brazilian citizens and residents.
|
|
28
|
-
* This function checks if the provided CPF is valid by performing the following steps:
|
|
29
|
-
* - Removes any non-digit characters from the input.
|
|
30
|
-
* - Verifies if the CPF has the correct length (11 digits).
|
|
31
|
-
* - Ensures that all digits are not the same (e.g., "111.111.111-11" is invalid).
|
|
32
|
-
* - Calculates the first and second verification digits using the CPF algorithm.
|
|
33
|
-
* - Compares the calculated verification digits with the ones provided in the CPF.
|
|
34
|
-
*
|
|
35
|
-
* @param rawCpf - The raw CPF string, which may include formatting characters (e.g., dots or dashes).
|
|
36
|
-
* @returns `true` if the CPF is valid, otherwise `false`.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```typescript
|
|
40
|
-
* validateCpf("123.456.789-09"); // false
|
|
41
|
-
* validateCpf("111.444.777-35"); // true
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
const validateCpf = (rawCpf) => {
|
|
45
|
-
if (!rawCpf)
|
|
46
|
-
return false;
|
|
47
|
-
const cpf = removeNonDigits(rawCpf);
|
|
48
|
-
if (isInvalidLength(cpf))
|
|
49
|
-
return false;
|
|
50
|
-
if (hasAllDigitsEqual(cpf))
|
|
51
|
-
return false;
|
|
52
|
-
const digit1 = calculateDigit(cpf, 10);
|
|
53
|
-
const digit2 = calculateDigit(cpf, 11);
|
|
54
|
-
return extractDigit(cpf) === `${digit1}${digit2}`;
|
|
55
|
-
};
|
|
56
|
-
export { validateCpf };
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import type { FormatDateFunction } from "@arkyn/types";
|
|
2
|
-
|
|
3
|
-
function formatDateString(date: Date, format: string): string {
|
|
4
|
-
const pad = (num: number) => num.toString().padStart(2, "0");
|
|
5
|
-
|
|
6
|
-
const replacements: Record<string, string> = {
|
|
7
|
-
YYYY: date.getFullYear().toString(),
|
|
8
|
-
YY: date.getFullYear().toString().slice(-2),
|
|
9
|
-
MM: pad(date.getMonth() + 1),
|
|
10
|
-
DD: pad(date.getDate()),
|
|
11
|
-
hh: pad(date.getHours()),
|
|
12
|
-
mm: pad(date.getMinutes()),
|
|
13
|
-
ss: pad(date.getSeconds()),
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
return format.replace(
|
|
17
|
-
/YYYY|YY|MM|DD|hh|mm|ss/g,
|
|
18
|
-
(match) => replacements[match]
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Formats a date string from a given input format to a specified output format,
|
|
24
|
-
* with optional timezone adjustment.
|
|
25
|
-
*
|
|
26
|
-
* @param date - The date string to be formatted. It should match the specified `inputFormat`.
|
|
27
|
-
* @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
|
|
28
|
-
* @param inputFormat - The format of the input date string. Supported formats:
|
|
29
|
-
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
30
|
-
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
31
|
-
* - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
|
|
32
|
-
*
|
|
33
|
-
* @param outputFormat - The desired format for the output date string. Supported tokens:
|
|
34
|
-
* - `YYYY`: Full year (e.g., 2023)
|
|
35
|
-
* - `YY`: Last two digits of the year (e.g., 23)
|
|
36
|
-
* - `MM`: Month (zero-padded, e.g., 01)
|
|
37
|
-
* - `DD`: Day of the month (zero-padded, e.g., 09)
|
|
38
|
-
* - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
|
|
39
|
-
* - `mm`: Minutes (zero-padded, e.g., 05)
|
|
40
|
-
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
41
|
-
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
42
|
-
*
|
|
43
|
-
* @returns The formatted date string in the specified `outputFormat`.
|
|
44
|
-
*
|
|
45
|
-
* @throws Will throw an error if:
|
|
46
|
-
* - The `inputFormat` is invalid.
|
|
47
|
-
* - The `date` string does not match the expected `inputFormat`.
|
|
48
|
-
* - The `time` string is not in the format `hh:mm:ss`.
|
|
49
|
-
* - The resulting date is invalid.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* import { formatDate } from "./formatDate";
|
|
54
|
-
*
|
|
55
|
-
* const date = "25/12/2023";
|
|
56
|
-
* const time = "14:30:00";
|
|
57
|
-
* const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
|
|
58
|
-
* console.log(formatted); // Outputs: "2023-12-25 14:30:00"
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
const formatDate: FormatDateFunction = (
|
|
63
|
-
date,
|
|
64
|
-
time,
|
|
65
|
-
inputFormat,
|
|
66
|
-
outputFormat,
|
|
67
|
-
timezone = 0
|
|
68
|
-
) => {
|
|
69
|
-
const dateParts = date.split(/[-/]/).map(Number);
|
|
70
|
-
const timeParts = time.split(":").map(Number);
|
|
71
|
-
|
|
72
|
-
let day, month, year;
|
|
73
|
-
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
74
|
-
|
|
75
|
-
switch (inputFormat) {
|
|
76
|
-
case "brazilianDate":
|
|
77
|
-
[day, month, year] = dateParts;
|
|
78
|
-
break;
|
|
79
|
-
case "isoDate":
|
|
80
|
-
[year, month, day] = dateParts;
|
|
81
|
-
break;
|
|
82
|
-
case "timestamp":
|
|
83
|
-
[year, month, day] = dateParts.map(Number);
|
|
84
|
-
break;
|
|
85
|
-
default:
|
|
86
|
-
throw new Error("Invalid input format");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
90
|
-
if (isNaN(formattedDate.getTime())) throw new Error("Invalid date");
|
|
91
|
-
|
|
92
|
-
formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
|
|
93
|
-
|
|
94
|
-
return formatDateString(formattedDate, outputFormat);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export { formatDate };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { FormatToCnpjFunction } from "@arkyn/types";
|
|
2
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
6
|
-
*
|
|
7
|
-
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
8
|
-
*
|
|
9
|
-
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
10
|
-
* Non-numeric characters will be removed before formatting.
|
|
11
|
-
*
|
|
12
|
-
* @returns A string formatted as a CNPJ.
|
|
13
|
-
*
|
|
14
|
-
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { formatToCnpj } from "./formatToCNPJ";
|
|
19
|
-
*
|
|
20
|
-
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
21
|
-
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
22
|
-
*
|
|
23
|
-
* try {
|
|
24
|
-
* formatToCnpj("12345");
|
|
25
|
-
* } catch (error) {
|
|
26
|
-
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const formatToCnpj: FormatToCnpjFunction = (value) => {
|
|
32
|
-
const cleaned = removeNonNumeric(value);
|
|
33
|
-
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
34
|
-
if (match)
|
|
35
|
-
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
36
|
-
throw new Error("Invalid CNPJ length");
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export { formatToCnpj };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { FormatToCpfFunction } from "@arkyn/types";
|
|
2
|
-
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Formats a given string into a CPF (Cadastro de Pessoas Físicas) format.
|
|
6
|
-
*
|
|
7
|
-
* A CPF is a Brazilian individual taxpayer registry identification format.
|
|
8
|
-
* This function ensures the input is cleaned of non-numeric characters and
|
|
9
|
-
* then formats it into the standard CPF format: `XXX.XXX.XXX-XX`.
|
|
10
|
-
*
|
|
11
|
-
* @param value - The input string to be formatted as a CPF.
|
|
12
|
-
* @returns The formatted CPF string.
|
|
13
|
-
* @throws {Error} If the input string does not match the expected CPF format.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { formatToCpf } from "./formatToCPF";
|
|
18
|
-
*
|
|
19
|
-
* const formattedCpf = formatToCpf("12345678909");
|
|
20
|
-
* console.log(formattedCpf); // Output: "123.456.789-09"
|
|
21
|
-
|
|
22
|
-
* try {
|
|
23
|
-
* const formattedCpf = formatToCpf("12345");
|
|
24
|
-
* } catch (error) {
|
|
25
|
-
* console.error(error.message); // Output: "Invalid CPF format"
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const formatToCpf: FormatToCpfFunction = (value) => {
|
|
32
|
-
const cleaned = removeNonNumeric(value);
|
|
33
|
-
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
|
|
34
|
-
|
|
35
|
-
if (match) return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
|
|
36
|
-
throw new Error("Invalid CPF format");
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export { formatToCpf };
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { countryCurrencies } from "@arkyn/templates";
|
|
2
|
-
import type { FormatToCurrency } from "@arkyn/types";
|
|
3
|
-
|
|
4
|
-
import { removeCurrencySymbols } from "../services/removeCurrencySymbols";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Formats a numeric value into a currency string based on the specified currency and configuration.
|
|
8
|
-
*
|
|
9
|
-
* @param value - The numeric value to be formatted.
|
|
10
|
-
* @param currency - The currency code used to determine the formatting style.
|
|
11
|
-
* @param config - Optional configuration object.
|
|
12
|
-
* @param config.showPrefix - Determines whether the currency symbol/prefix should be included in the formatted string. Defaults to `true`.
|
|
13
|
-
*
|
|
14
|
-
* @returns A formatted currency string. If `config.showPrefix` is `false`, the currency symbol is removed.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
19
|
-
* console.log(formatted); // "$1,234.56"
|
|
20
|
-
*
|
|
21
|
-
* const withoutPrefix = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
22
|
-
* console.log(withoutPrefix); // "1,234.56"
|
|
23
|
-
*
|
|
24
|
-
* const formattedBRL = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
25
|
-
* console.log(formattedBRL); // "R$ 1.234,56"
|
|
26
|
-
*
|
|
27
|
-
* const withoutPrefixBRL = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
28
|
-
* console.log(withoutPrefixBRL); // "1.234,56"
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const formatToCurrency: FormatToCurrency = (
|
|
33
|
-
value,
|
|
34
|
-
currency,
|
|
35
|
-
config = { showPrefix: true }
|
|
36
|
-
) => {
|
|
37
|
-
if (!countryCurrencies[currency]) {
|
|
38
|
-
throw new Error("Unsupported currency code");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const { countryCurrency, countryLanguage } = countryCurrencies[currency];
|
|
42
|
-
|
|
43
|
-
const format = new Intl.NumberFormat(countryLanguage, {
|
|
44
|
-
style: "currency",
|
|
45
|
-
currency: countryCurrency,
|
|
46
|
-
}).format(value);
|
|
47
|
-
|
|
48
|
-
return config.showPrefix
|
|
49
|
-
? format.replace(/\s/g, " ")
|
|
50
|
-
: removeCurrencySymbols(format).replace(/\s/g, " ");
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export { formatToCurrency };
|
package/src/regex/index.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes currency symbols from a given formatted string.
|
|
3
|
-
*
|
|
4
|
-
* This function takes a string that may contain currency symbols
|
|
5
|
-
* and removes them using a regular expression. The resulting string
|
|
6
|
-
* is also trimmed of any leading or trailing whitespace.
|
|
7
|
-
*
|
|
8
|
-
* @param formattedValue - The input string containing currency symbols.
|
|
9
|
-
* @returns A string with all currency symbols removed and trimmed of whitespace.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* removeCurrencySymbols("R$13,45"); // "13,45"
|
|
13
|
-
* removeCurrencySymbols("$123.45"); // "123.45"
|
|
14
|
-
* removeCurrencySymbols("€99.99"); // "99.99"
|
|
15
|
-
* removeCurrencySymbols("¥1,000"); // "1,000"
|
|
16
|
-
* removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const removeCurrencySymbols = (formattedValue: string): string => {
|
|
20
|
-
return formattedValue
|
|
21
|
-
.replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "") // Inclui "R$" e outros símbolos comuns
|
|
22
|
-
.trim();
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export { removeCurrencySymbols };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes all non-numeric characters from a given string.
|
|
3
|
-
*
|
|
4
|
-
* @param prop - The input string from which non-numeric characters will be removed.
|
|
5
|
-
* @returns A new string containing only numeric characters from the input.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const result = removeNonNumeric("abc123def456");
|
|
10
|
-
* console.log(result); // Output: "123456"
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
function removeNonNumeric(prop: string): string {
|
|
15
|
-
return prop.replace(/[^0-9]/g, "");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { removeNonNumeric };
|
|
@@ -1,64 +0,0 @@
|
|
|
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 };
|