@arkyn/shared 3.0.1-beta.155 → 3.0.1-beta.157

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/index.js +371 -30
  2. package/dist/modules/formats/formatDate.js +42 -0
  3. package/dist/modules/formats/formatJsonObject.js +32 -0
  4. package/dist/modules/formats/formatJsonString.js +13 -0
  5. package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js +9 -0
  6. package/dist/modules/formats/formatToCep.js +9 -0
  7. package/dist/modules/formats/formatToCnpj.js +9 -0
  8. package/dist/modules/formats/formatToCpf.js +9 -0
  9. package/dist/modules/formats/formatToCurrency.js +15 -0
  10. package/dist/modules/formats/formatToEllipsis.js +11 -0
  11. package/dist/modules/formats/formatToHiddenDigits.js +12 -0
  12. package/dist/modules/formats/formatToPhone.js +17 -0
  13. package/dist/modules/generators/generateColorByString.js +9 -0
  14. package/dist/modules/generators/generateId.js +26 -0
  15. package/dist/modules/generators/generateSlug.js +7 -0
  16. package/dist/modules/index.js +52 -0
  17. package/dist/modules/parsers/parseLargeFields.js +22 -0
  18. package/dist/modules/parsers/parseSensitiveData.js +26 -0
  19. package/dist/modules/parsers/parseToDate.js +27 -0
  20. package/dist/modules/services/validateDateService.js +59 -0
  21. package/dist/modules/utilities/calculateCardInstallment.js +21 -0
  22. package/dist/modules/utilities/ensureQuotes.js +7 -0
  23. package/dist/modules/utilities/findCountryMask.js +25 -0
  24. package/dist/modules/utilities/isHtml.js +6 -0
  25. package/dist/modules/utilities/removeCurrencySymbols.js +6 -0
  26. package/dist/modules/utilities/removeNonNumeric.js +6 -0
  27. package/dist/modules/utilities/stripHtmlTags.js +6 -0
  28. package/package.json +100 -74
  29. package/dist/bundle.js +0 -3719
  30. package/dist/bundle.umd.cjs +0 -10
  31. package/dist/formats/formatDate.js +0 -65
  32. package/dist/formats/formatJsonObject.js +0 -89
  33. package/dist/formats/formatJsonString.js +0 -30
  34. package/dist/formats/formatToCapitalizeFirstWordLetter.js +0 -23
  35. package/dist/formats/formatToCep.js +0 -28
  36. package/dist/formats/formatToCnpj.js +0 -26
  37. package/dist/formats/formatToCpf.js +0 -28
  38. package/dist/formats/formatToCurrency.js +0 -31
  39. package/dist/formats/formatToEllipsis.js +0 -32
  40. package/dist/formats/formatToHiddenDigits.js +0 -49
  41. package/dist/formats/formatToPhone.js +0 -43
  42. package/dist/generators/generateColorByString.js +0 -28
  43. package/dist/generators/generateId.js +0 -29
  44. package/dist/generators/generateSlug.js +0 -31
  45. package/dist/parsers/parseLargeFields.js +0 -55
  46. package/dist/parsers/parseSensitiveData.js +0 -57
  47. package/dist/parsers/parseToDate.js +0 -51
  48. package/dist/services/validateDateService.js +0 -81
  49. package/dist/utilities/calculateCardInstallment.js +0 -39
  50. package/dist/utilities/ensureQuotes.js +0 -26
  51. package/dist/utilities/findCountryMask.js +0 -49
  52. package/dist/utilities/isHtml.js +0 -23
  53. package/dist/utilities/removeCurrencySymbols.js +0 -23
  54. package/dist/utilities/removeNonNumeric.js +0 -16
  55. package/dist/utilities/stripHtmlTags.js +0 -23
@@ -1,81 +0,0 @@
1
- /**
2
- * Validates date components and input format strings for date-parsing utilities.
3
- * Enforces 4-digit years, month/day ranges, month-specific day counts, and leap year rules.
4
- *
5
- * @example
6
- * ```typescript
7
- * const service = new ValidateDateService();
8
- * service.validateDateParts(2024, 2, 29); // OK — leap year
9
- * service.validateDateParts(2023, 2, 29); // throws — not a leap year
10
- * service.validateInputFormat("brazilianDate"); // OK
11
- * service.validateInputFormat("custom"); // throws
12
- * ```
13
- */
14
- class ValidateDateService {
15
- isLeapYear(year) {
16
- return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
17
- }
18
- getDaysInMonth(month, year) {
19
- const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
20
- if (month === 2 && this.isLeapYear(year))
21
- return 29;
22
- return daysInMonth[month - 1];
23
- }
24
- validateDayInMonth(day, month, year) {
25
- const maxDays = this.getDaysInMonth(month, year);
26
- if (day > maxDays) {
27
- const monthNames = [
28
- "January",
29
- "February",
30
- "March",
31
- "April",
32
- "May",
33
- "June",
34
- "July",
35
- "August",
36
- "September",
37
- "October",
38
- "November",
39
- "December",
40
- ];
41
- const errorMessage = `Day ${day} is not valid for ${monthNames[month - 1]}`;
42
- const leapYearMessage = `Day ${day} is not valid for February ${year} (non-leap year)`;
43
- if (month === 2 && day === 29)
44
- throw new Error(leapYearMessage);
45
- throw new Error(errorMessage);
46
- }
47
- }
48
- /**
49
- * Throws if year, month, or day are out of valid range or inconsistent with the calendar.
50
- *
51
- * @param year - 4-digit year (1000–9999).
52
- * @param month - Month number (1–12).
53
- * @param day - Day number (1–31, validated against the specific month).
54
- */
55
- validateDateParts(year, month, day) {
56
- const messageErrors = {
57
- year: "Year should be four digits",
58
- month: "Month should be between 1 and 12",
59
- day: "Day should be between 1 and 31",
60
- };
61
- if (`${year}`.length !== 4)
62
- throw new Error(messageErrors.year);
63
- if (month < 1 || month > 12)
64
- throw new Error(messageErrors.month);
65
- if (day < 1 || day > 31)
66
- throw new Error(messageErrors.day);
67
- this.validateDayInMonth(day, month, year);
68
- }
69
- /**
70
- * Throws if `format` is not one of `"brazilianDate"`, `"isoDate"`, or `"timestamp"`.
71
- *
72
- * @param format - The format string to check.
73
- */
74
- validateInputFormat(format) {
75
- const validFormats = ["brazilianDate", "isoDate", "timestamp"];
76
- if (!validFormats.includes(format)) {
77
- throw new Error(`Invalid input format: ${format}`);
78
- }
79
- }
80
- }
81
- export { ValidateDateService };
@@ -1,39 +0,0 @@
1
- /**
2
- * Calculates the per-installment and total price for a card payment plan with compound interest.
3
- * No interest is applied when `fees` is `0` or `numberInstallments` is `1`.
4
- *
5
- * @param props.cashPrice - The base price of the item.
6
- * @param props.numberInstallments - Number of installments (must be > 0).
7
- * @param props.fees - Monthly interest rate (defaults to `0.0349`).
8
- * @returns `{ totalPrice, installmentPrice }` — both rounded to 2 decimal places.
9
- *
10
- * @example
11
- * ```typescript
12
- * calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
13
- * // { totalPrice: 1124.62, installmentPrice: 93.72 }
14
- * ```
15
- */
16
- function calculateCardInstallment(props) {
17
- const { cashPrice, numberInstallments, fees = 0.0349 } = props;
18
- if (fees === 0 || numberInstallments === 1) {
19
- return {
20
- totalPrice: cashPrice,
21
- installmentPrice: cashPrice / numberInstallments,
22
- };
23
- }
24
- if (numberInstallments <= 0) {
25
- throw new Error("Number of installments must be greater than 0");
26
- }
27
- if (fees < 0) {
28
- throw new Error("Fees must be greater than or equal to 0");
29
- }
30
- let numerator = Math.pow(1 + fees, numberInstallments) * fees;
31
- let denominator = Math.pow(1 + fees, numberInstallments) - 1;
32
- const installmentPrice = +(cashPrice * (numerator / denominator)).toFixed(2);
33
- const totalPrice = +(installmentPrice * numberInstallments).toFixed(2);
34
- return {
35
- totalPrice: +totalPrice.toFixed(2),
36
- installmentPrice: +installmentPrice.toFixed(2),
37
- };
38
- }
39
- export { calculateCardInstallment };
@@ -1,26 +0,0 @@
1
- /**
2
- * Ensures that a given rawValue string is enclosed in quotes.
3
- *
4
- * This function checks if the input string is already enclosed in either single
5
- * quotes (`'`) or double quotes (`"`). If the string is already quoted, it is
6
- * returned as-is. Otherwise, the function wraps the string in double quotes.
7
- *
8
- * @param rawValue - The string to be checked and potentially quoted.
9
- * @returns The original string if already quoted, otherwise the string wrapped in double quotes.
10
- *
11
- * @example
12
- * ```typescript
13
- * ensureQuotes('example'); // Returns: '"example"'
14
- * ensureQuotes('"already quoted"'); // Returns: '"already quoted"'
15
- * ensureQuotes("'single quoted'"); // Returns: "'single quoted'"
16
- * ```
17
- */
18
- function ensureQuotes(rawValue) {
19
- const hasSingleQuotes = rawValue.startsWith("'") && rawValue.endsWith("'");
20
- const hasDoubleQuotes = rawValue.startsWith('"') && rawValue.endsWith('"');
21
- if (hasSingleQuotes || hasDoubleQuotes) {
22
- return rawValue;
23
- }
24
- return `"${rawValue}"`;
25
- }
26
- export { ensureQuotes };
@@ -1,49 +0,0 @@
1
- import { countries } from "@arkyn/templates";
2
- import { parsePhoneNumberWithError } from "libphonenumber-js";
3
- import { removeNonNumeric } from "./removeNonNumeric";
4
- /**
5
- * Resolves the matching phone mask and country metadata for a given phone number.
6
- * Masks use `"_"` as digit placeholders. For countries with multiple mask lengths
7
- * (e.g. Brazil with and without the ninth digit), the mask matching the number's
8
- * digit count is returned.
9
- *
10
- * @param phoneNumber - Phone number in E.164 format (e.g. `"+5511999999999"`).
11
- * @returns A tuple of `[maskString, CountryType]`.
12
- * @throws If the number is invalid or no mask is found for the parsed country.
13
- *
14
- * @example
15
- * ```typescript
16
- * const [mask, country] = findCountryMask("+5511999999999");
17
- * // mask: "(__) _____-____"
18
- * // country.name: "Brazil"
19
- * ```
20
- */
21
- function findCountryMask(phoneNumber) {
22
- try {
23
- const parsedPhone = parsePhoneNumberWithError(phoneNumber);
24
- const countryCode = parsedPhone?.country;
25
- if (!countryCode)
26
- throw new Error("Invalid phone number");
27
- const country = countries.find((c) => c.iso === countryCode);
28
- if (!country)
29
- throw new Error("Phone number country not supported");
30
- if (typeof country.mask === "string")
31
- return [country.mask, country];
32
- const maskForLength = country.mask.find((mask) => {
33
- const maskDigits = mask.replace(/[^_]/g, "");
34
- const phoneDigits = removeNonNumeric(parsedPhone.nationalNumber);
35
- const maskDigitsCount = maskDigits.length;
36
- const phoneDigitsCount = phoneDigits.length;
37
- return maskDigitsCount === phoneDigitsCount;
38
- });
39
- if (!maskForLength) {
40
- throw new Error("No mask found for the given phone number length");
41
- }
42
- return [maskForLength, country];
43
- }
44
- catch (rawError) {
45
- const error = rawError;
46
- throw new Error(error.message);
47
- }
48
- }
49
- export { findCountryMask };
@@ -1,23 +0,0 @@
1
- /**
2
- * Check if a string contains HTML markup.
3
- *
4
- * This function uses a regular expression to detect the presence of HTML tags
5
- * in a string. The check is case-insensitive and detects both opening
6
- * and closing tags.
7
- *
8
- * @param rawString - The string to check.
9
- * @returns `true` if the string contains HTML markup, `false` otherwise.
10
- *
11
- * @example
12
- * ```typescript
13
- * isHtml('<p>Hello world</p>'); // true
14
- * isHtml('<div>Content</div>'); // true
15
- * isHtml('Plain text'); // false
16
- * isHtml(''); // false
17
- * ```
18
- */
19
- function isHtml(rawString) {
20
- const htmlRegex = /<\/?[a-z][\s\S]*>/i;
21
- return htmlRegex.test(rawString);
22
- }
23
- export { isHtml };
@@ -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 rawString - The formatted string that may contain currency symbols.
9
- * @returns The string with all currency symbols removed and trimmed of whitespace.
10
- *
11
- * @example
12
- * ```typescript
13
- * removeCurrencySymbols("R$13,45"); // "13,45"
14
- * removeCurrencySymbols("$123.45"); // "123.45"
15
- * removeCurrencySymbols("€99.99"); // "99.99"
16
- * removeCurrencySymbols("¥1,000"); // "1,000"
17
- * removeCurrencySymbols("123.45"); // "123.45" (no symbols to remove)
18
- * ```
19
- */
20
- function removeCurrencySymbols(rawString) {
21
- return rawString.replace(/(?:R\$|\p{Sc}|[$€¥£])/gu, "").trim();
22
- }
23
- export { removeCurrencySymbols };
@@ -1,16 +0,0 @@
1
- /**
2
- * Removes all non-numeric characters from a given string.
3
- *
4
- * @param rawString - The input string to strip non-numeric characters from.
5
- * @returns A new string containing only the 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(rawString) {
14
- return rawString.replace(/[^0-9]/g, "");
15
- }
16
- export { removeNonNumeric };
@@ -1,23 +0,0 @@
1
- /**
2
- * Strips HTML tags from a string.
3
- *
4
- * This function removes all HTML tags from the provided string by replacing any content
5
- * that matches the HTML tag pattern with an empty string.
6
- *
7
- * @param rawHtml - The HTML string to strip tags from.
8
- * @returns The plain text with all HTML tags (including `<script>`, `<style>`, and comments) removed.
9
- *
10
- * @example
11
- * ```typescript
12
- * const strippedHtml = stripHtmlTags("<p>Hello <strong>World</strong></p>");
13
- * console.log(strippedHtml); // "Hello World"
14
- * ```
15
- */
16
- function stripHtmlTags(rawHtml) {
17
- return rawHtml
18
- .replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
19
- .replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "")
20
- .replace(/<!--[\s\S]*?-->/g, "")
21
- .replace(/<\/?[a-z][a-z0-9]*[^>]*>/gi, "");
22
- }
23
- export { stripHtmlTags };