@arkyn/shared 3.0.1-beta.121 β†’ 3.0.1-beta.123

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.
@@ -5,7 +5,7 @@
5
5
  * **Note:** This function works with UTC+0 by default. The returned formatted string is not automatically converted to the machine's local timezone.
6
6
  * To adjust the timezone, you must manually specify the `timezone` parameter (e.g., -3 for UTC-3).
7
7
  *
8
- * @param {[string, string]} dateTime - An array containing the date and optional time.
8
+ * @param {string[]} dateTime - An array containing the date and optional time.
9
9
  * - The first element is the date string.
10
10
  * - The second element is the time string (default is "00:00:00").
11
11
  * @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
@@ -57,6 +57,6 @@
57
57
  * console.log(formattedDate); // Output: "12-25-2023 15:30:00"
58
58
  * ```
59
59
  */
60
- declare function formatDate([date, time]: [string, string?], inputFormat: "brazilianDate" | "isoDate" | "timestamp", outputFormat: string, timezone?: number): string;
60
+ declare function formatDate([date, time]: string[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", outputFormat: string, timezone?: number): string;
61
61
  export { formatDate };
62
62
  //# sourceMappingURL=formatDate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,iBAAS,UAAU,CACjB,CAAC,IAAI,EAAE,IAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAC5C,WAAW,EAAE,eAAe,GAAG,SAAS,GAAG,WAAW,EACtD,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,MAAU,GACnB,MAAM,CAkCR;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
1
+ {"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,iBAAS,UAAU,CACjB,CAAC,IAAI,EAAE,IAAiB,CAAC,EAAE,MAAM,EAAE,EACnC,WAAW,EAAE,eAAe,GAAG,SAAS,GAAG,WAAW,EACtD,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,MAAU,GACnB,MAAM,CAkCR;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -19,7 +19,7 @@ function formatDateString(date, format) {
19
19
  * **Note:** This function works with UTC+0 by default. The returned formatted string is not automatically converted to the machine's local timezone.
20
20
  * To adjust the timezone, you must manually specify the `timezone` parameter (e.g., -3 for UTC-3).
21
21
  *
22
- * @param {[string, string]} dateTime - An array containing the date and optional time.
22
+ * @param {string[]} dateTime - An array containing the date and optional time.
23
23
  * - The first element is the date string.
24
24
  * - The second element is the time string (default is "00:00:00").
25
25
  * @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
@@ -1,34 +1,23 @@
1
- type FormatToPhoneFunction = (rawString: string) => string;
2
1
  /**
3
- * Formats a phone number string based on the provided country code and optional prefix.
2
+ * Formats a phone number string according to the country mask defined in `@arkyn/templates`.
4
3
  *
5
- * The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
6
- * The function determines the appropriate formatting mask based on the country and applies it to the phone number.
4
+ * The function parses the input using libphonenumber-js to determine the country and
5
+ * national number, then applies the corresponding country's mask (underscore `_` used
6
+ * as digit placeholder) replacing placeholders with actual digits.
7
7
  *
8
- * @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
9
- * Example formats:
10
- * - "+55 32912345678"
11
- * - "+1 1234567890"
8
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
12
9
  *
13
- * @returns The formatted phone number string based on the country's formatting rules.
10
+ * @returns {string} The phone number formatted following the country's mask.
14
11
  *
15
- * @throws {Error} If the input phone number does not match the expected format.
16
- * @throws {Error} If the country code or phone number is missing from the input string.
17
- * @throws {Error} If the provided country code and prefix combination is invalid.
18
- * @throws {Error} If the provided country code is invalid.
19
- * @throws {Error} If the provided country code has a prefix but none is supplied in the input.
12
+ * @throws {Error} If the phone number is invalid or if no country mask is found for the parsed country.
20
13
  *
21
14
  * @example
22
15
  * ```typescript
23
- * import { formatToPhone } from "./formatToPhone";
24
- *
25
- * const formattedPhone1 = formatToPhone("+55 11912345678");
26
- * console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
27
- *
28
- * const formattedPhone2 = formatToPhone("+1-123 4567890");
29
- * console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
16
+ * console.log(formatToPhone("+5534920524282")); // Output: "(34) 92052-4282" (Brazilian format)
17
+ * console.log(formatToPhone("+553420524282")); // Output: "(34) 2052-4282" (Brazilian format with optional ninth digit)
18
+ * console.log(formatToPhone("+12125550199")); // Output: "(212) 555-0199" (American Samoa format)
30
19
  * ```
31
20
  */
32
- declare const formatToPhone: FormatToPhoneFunction;
21
+ declare function formatToPhone(phoneNumber: string): string;
33
22
  export { formatToPhone };
34
23
  //# sourceMappingURL=formatToPhone.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"AAaA,KAAK,qBAAqB,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC;AAmG3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAwBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
1
+ {"version":3,"file":"formatToPhone.d.ts","sourceRoot":"","sources":["../../src/formats/formatToPhone.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,iBAAS,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CA0BlD;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -1,131 +1,44 @@
1
- import { countries } from "@arkyn/templates";
2
- import { removeNonNumeric } from "../utilities/removeNonNumeric";
3
- function getMask(value) {
4
- const mask = value.length > 10 ? "NINE" : "EIGHT";
5
- return mask;
6
- }
7
- const TYPES = {
8
- EIGHT: "(99) 9999-9999",
9
- NINE: "(99) 99999-9999",
10
- };
11
- const MAX_LENGTH = removeNonNumeric(TYPES.NINE).length;
12
- function applyMask(value, maskPattern) {
13
- let result = "";
14
- let digitIndex = 0;
15
- for (let i = 0; i < maskPattern.length; i++) {
16
- if (maskPattern[i] === "9") {
17
- if (digitIndex < value.length) {
18
- result += value[digitIndex];
19
- digitIndex++;
20
- }
21
- else {
22
- break;
23
- }
24
- }
25
- else {
26
- if (digitIndex < value.length) {
27
- result += maskPattern[i];
28
- }
29
- else {
30
- break;
31
- }
32
- }
33
- }
34
- return result;
35
- }
36
- function formatPhoneNumber(phoneNumber, country) {
37
- if (country.code === "+55") {
38
- let value = removeNonNumeric(phoneNumber);
39
- const mask = getMask(value);
40
- let nextLength = value.length;
41
- if (nextLength > MAX_LENGTH)
42
- return value;
43
- value = applyMask(value, TYPES[mask]);
44
- return value;
45
- }
46
- const mask = country.mask;
47
- let formattedNumber = mask;
48
- if (country.prefix) {
49
- const prefixRegex = /\$+/g;
50
- formattedNumber = formattedNumber.replace(prefixRegex, country.prefix);
51
- }
52
- for (let i = 0, j = 0; i < formattedNumber.length && j < phoneNumber.length; i++) {
53
- if (formattedNumber[i] === "_") {
54
- formattedNumber =
55
- formattedNumber.substring(0, i) +
56
- phoneNumber[j] +
57
- formattedNumber.substring(i + 1);
58
- j++;
59
- }
60
- }
61
- return formattedNumber;
62
- }
63
- function getCountryWithPrefixCode(countryCode, prefix) {
64
- const country = countries.find((country) => country.code === countryCode && country.prefix === prefix);
65
- if (!country)
66
- throw new Error("Invalid country code or prefix");
67
- if (country.prefix !== prefix) {
68
- throw new Error("Invalid country code or prefix");
69
- }
70
- if (!country.prefix) {
71
- throw new Error("Invalid country code or prefix");
72
- }
73
- return country;
74
- }
75
- function getCountryWithoutPrefixCode(countryCode) {
76
- const country = countries.find((country) => country.code === countryCode);
77
- if (!country)
78
- throw new Error("Invalid country code");
79
- if (country.prefix)
80
- throw new Error("Invalid country code");
81
- return country;
82
- }
1
+ import { parsePhoneNumberWithError } from "libphonenumber-js";
2
+ import { findCountryMask } from "../utilities/findCountryMask";
83
3
  /**
84
- * Formats a phone number string based on the provided country code and optional prefix.
4
+ * Formats a phone number string according to the country mask defined in `@arkyn/templates`.
85
5
  *
86
- * The input string should follow the format: `"<countryCode>-<prefix> <phoneNumber>"` or `"<countryCode> <phoneNumber>"`.
87
- * The function determines the appropriate formatting mask based on the country and applies it to the phone number.
6
+ * The function parses the input using libphonenumber-js to determine the country and
7
+ * national number, then applies the corresponding country's mask (underscore `_` used
8
+ * as digit placeholder) replacing placeholders with actual digits.
88
9
  *
89
- * @param prop - The phone number string to be formatted. It must include the country code and optionally a prefix.
90
- * Example formats:
91
- * - "+55 32912345678"
92
- * - "+1 1234567890"
10
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
93
11
  *
94
- * @returns The formatted phone number string based on the country's formatting rules.
12
+ * @returns {string} The phone number formatted following the country's mask.
95
13
  *
96
- * @throws {Error} If the input phone number does not match the expected format.
97
- * @throws {Error} If the country code or phone number is missing from the input string.
98
- * @throws {Error} If the provided country code and prefix combination is invalid.
99
- * @throws {Error} If the provided country code is invalid.
100
- * @throws {Error} If the provided country code has a prefix but none is supplied in the input.
14
+ * @throws {Error} If the phone number is invalid or if no country mask is found for the parsed country.
101
15
  *
102
16
  * @example
103
17
  * ```typescript
104
- * import { formatToPhone } from "./formatToPhone";
105
- *
106
- * const formattedPhone1 = formatToPhone("+55 11912345678");
107
- * console.log(formattedPhone1); // Output: "(11) 91234-5678" (brazilian phone number format)
108
- *
109
- * const formattedPhone2 = formatToPhone("+1-123 4567890");
110
- * console.log(formattedPhone2); // Output: "(123) 456-7890" (us phone number format)
18
+ * console.log(formatToPhone("+5534920524282")); // Output: "(34) 92052-4282" (Brazilian format)
19
+ * console.log(formatToPhone("+553420524282")); // Output: "(34) 2052-4282" (Brazilian format with optional ninth digit)
20
+ * console.log(formatToPhone("+12125550199")); // Output: "(212) 555-0199" (American Samoa format)
111
21
  * ```
112
22
  */
113
- const formatToPhone = (prop) => {
114
- const phoneRegex = /^\+\d{1,4}(-\d{1,4})? \d+$/;
115
- if (!phoneRegex.test(prop)) {
116
- throw new Error("Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>");
117
- }
118
- const countryCode = prop.split(" ")[0].split("-")[0];
119
- const prefixCode = prop.split(" ")[0].split("-")[1];
120
- const phoneNumber = prop.split(" ")[1];
121
- if (!countryCode || !phoneNumber) {
122
- throw new Error("Invalid phone number format");
23
+ function formatToPhone(phoneNumber) {
24
+ try {
25
+ const parsedPhone = parsePhoneNumberWithError(phoneNumber);
26
+ const phoneNumberDigits = parsedPhone.nationalNumber.toString();
27
+ let formattedNumber = findCountryMask(phoneNumber)[0];
28
+ for (let i = 0, j = 0; i < formattedNumber.length && j < phoneNumberDigits.length; i++) {
29
+ if (formattedNumber[i] === "_") {
30
+ formattedNumber =
31
+ formattedNumber.substring(0, i) +
32
+ phoneNumberDigits[j] +
33
+ formattedNumber.substring(i + 1);
34
+ j++;
35
+ }
36
+ }
37
+ return formattedNumber;
123
38
  }
124
- if (prefixCode) {
125
- const country = getCountryWithPrefixCode(countryCode, prefixCode);
126
- return formatPhoneNumber(phoneNumber, country);
39
+ catch (rawError) {
40
+ const error = rawError;
41
+ throw new Error(error.message);
127
42
  }
128
- const country = getCountryWithoutPrefixCode(countryCode);
129
- return formatPhoneNumber(phoneNumber, country);
130
- };
43
+ }
131
44
  export { formatToPhone };
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export { parseToDate } from "./parsers/parseToDate";
18
18
  export { ValidateDateService } from "./services/validateDateService";
19
19
  export { calculateCardInstallment } from "./utilities/calculateCardInstallment";
20
20
  export { ensureQuotes } from "./utilities/ensureQuotes";
21
+ export { findCountryMask } from "./utilities/findCountryMask";
21
22
  export { isHtml } from "./utilities/isHtml";
22
23
  export { removeCurrencySymbols } from "./utilities/removeCurrencySymbols";
23
24
  export { removeNonNumeric } from "./utilities/removeNonNumeric";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,iCAAiC,EAAE,MAAM,6CAA6C,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,iCAAiC,EAAE,MAAM,6CAA6C,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC"}
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ export { ValidateDateService } from "./services/validateDateService";
23
23
  // utilities
24
24
  export { calculateCardInstallment } from "./utilities/calculateCardInstallment";
25
25
  export { ensureQuotes } from "./utilities/ensureQuotes";
26
+ export { findCountryMask } from "./utilities/findCountryMask";
26
27
  export { isHtml } from "./utilities/isHtml";
27
28
  export { removeCurrencySymbols } from "./utilities/removeCurrencySymbols";
28
29
  export { removeNonNumeric } from "./utilities/removeNonNumeric";
@@ -5,7 +5,7 @@
5
5
  * **Note:** This function works with UTC+0 by default. The returned Date object is not automatically converted to the machine's local timezone.
6
6
  * To adjust the timezone, you must manually specify the `timezone` parameter (e.g., -3 for UTC-3).
7
7
  *
8
- * @param {[string, string]} dateTime - An array containing the date and optional time.
8
+ * @param {string[]} dateTime - An array containing the date and optional time.
9
9
  * - The first element is the date string.
10
10
  * - The second element is the time string (default is "00:00:00")
11
11
  * @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
@@ -36,6 +36,6 @@
36
36
  * console.log(date); // Outputs a Date object for "2023-12-25T15:30:00.000Z" (UTC)
37
37
  * ```
38
38
  */
39
- declare function parseToDate([date, time]: [string, string?], inputFormat: "brazilianDate" | "isoDate" | "timestamp", timezone?: number): Date;
39
+ declare function parseToDate([date, time]: string[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", timezone?: number): Date;
40
40
  export { parseToDate };
41
41
  //# sourceMappingURL=parseToDate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parseToDate.d.ts","sourceRoot":"","sources":["../../src/parsers/parseToDate.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iBAAS,WAAW,CAClB,CAAC,IAAI,EAAE,IAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAC5C,WAAW,EAAE,eAAe,GAAG,SAAS,GAAG,WAAW,EACtD,QAAQ,GAAE,MAAU,GACnB,IAAI,CAkCN;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"parseToDate.d.ts","sourceRoot":"","sources":["../../src/parsers/parseToDate.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iBAAS,WAAW,CAClB,CAAC,IAAI,EAAE,IAAiB,CAAC,EAAE,MAAM,EAAE,EACnC,WAAW,EAAE,eAAe,GAAG,SAAS,GAAG,WAAW,EACtD,QAAQ,GAAE,MAAU,GACnB,IAAI,CAkCN;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -6,7 +6,7 @@ import { ValidateDateService } from "../services/validateDateService";
6
6
  * **Note:** This function works with UTC+0 by default. The returned Date object is not automatically converted to the machine's local timezone.
7
7
  * To adjust the timezone, you must manually specify the `timezone` parameter (e.g., -3 for UTC-3).
8
8
  *
9
- * @param {[string, string]} dateTime - An array containing the date and optional time.
9
+ * @param {string[]} dateTime - An array containing the date and optional time.
10
10
  * - The first element is the date string.
11
11
  * - The second element is the time string (default is "00:00:00")
12
12
  * @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
@@ -0,0 +1,45 @@
1
+ import { type CountryType } from "@arkyn/templates";
2
+ /**
3
+ * Finds and returns the phone mask for a given phone number based on the project's
4
+ * country templates (masks use "_" as digit placeholders).
5
+ *
6
+ * The function typically:
7
+ * - Parses the provided phone number to determine the country (using libphonenumber-js).
8
+ * - Looks up the corresponding country mask in the `@arkyn/templates` list.
9
+ * - Returns the mask string (e.g. "(__) _____-____") to be used for formatting.
10
+ *
11
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
12
+ * @returns {string} The country mask string containing "_" placeholders for digits.
13
+ *
14
+ * @throws {Error} If the phone number is invalid or if no mask is found for the parsed country.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * console.log(findCountryMask("+5511999999999"));
19
+ * // output:
20
+ * [
21
+ * ["(__) _____-____", ["(__) ____-____"]],
22
+ * {
23
+ * name: "Brazil",
24
+ * code: "+55",
25
+ * iso: "BR",
26
+ * flag: "πŸ‡§πŸ‡·",
27
+ * mask: ["(__) _____-____", ["(__) ____-____"]] }
28
+ * ]
29
+ *
30
+ * console.log(findCountryMask("+19700000000"));
31
+ * // output:
32
+ * [
33
+ * "(___) ___-____",
34
+ * {
35
+ * name: "United States",
36
+ * code: "+1",
37
+ * iso: "US",
38
+ * flag: "πŸ‡ΊπŸ‡Έ",
39
+ * mask: "(___) ___-____" }
40
+ * ]
41
+ * ```
42
+ */
43
+ declare function findCountryMask(phoneNumber: string): [string, CountryType];
44
+ export { findCountryMask };
45
+ //# sourceMappingURL=findCountryMask.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findCountryMask.d.ts","sourceRoot":"","sources":["../../src/utilities/findCountryMask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAI/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,iBAAS,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CA+BnE;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,73 @@
1
+ import { countries } from "@arkyn/templates";
2
+ import { parsePhoneNumberWithError } from "libphonenumber-js";
3
+ import { removeNonNumeric } from "./removeNonNumeric";
4
+ /**
5
+ * Finds and returns the phone mask for a given phone number based on the project's
6
+ * country templates (masks use "_" as digit placeholders).
7
+ *
8
+ * The function typically:
9
+ * - Parses the provided phone number to determine the country (using libphonenumber-js).
10
+ * - Looks up the corresponding country mask in the `@arkyn/templates` list.
11
+ * - Returns the mask string (e.g. "(__) _____-____") to be used for formatting.
12
+ *
13
+ * @param {string} phoneNumber - The input phone number (can include country code or be in national format).
14
+ * @returns {string} The country mask string containing "_" placeholders for digits.
15
+ *
16
+ * @throws {Error} If the phone number is invalid or if no mask is found for the parsed country.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * console.log(findCountryMask("+5511999999999"));
21
+ * // output:
22
+ * [
23
+ * ["(__) _____-____", ["(__) ____-____"]],
24
+ * {
25
+ * name: "Brazil",
26
+ * code: "+55",
27
+ * iso: "BR",
28
+ * flag: "πŸ‡§πŸ‡·",
29
+ * mask: ["(__) _____-____", ["(__) ____-____"]] }
30
+ * ]
31
+ *
32
+ * console.log(findCountryMask("+19700000000"));
33
+ * // output:
34
+ * [
35
+ * "(___) ___-____",
36
+ * {
37
+ * name: "United States",
38
+ * code: "+1",
39
+ * iso: "US",
40
+ * flag: "πŸ‡ΊπŸ‡Έ",
41
+ * mask: "(___) ___-____" }
42
+ * ]
43
+ * ```
44
+ */
45
+ function findCountryMask(phoneNumber) {
46
+ try {
47
+ const parsedPhone = parsePhoneNumberWithError(phoneNumber);
48
+ const countryCode = parsedPhone?.country;
49
+ if (!countryCode)
50
+ throw new Error("Invalid phone number");
51
+ const country = countries.find((c) => c.iso === countryCode);
52
+ if (!country)
53
+ throw new Error("Phone number country not supported");
54
+ if (typeof country.mask === "string")
55
+ return [country.mask, country];
56
+ const maskForLength = country.mask.find((mask) => {
57
+ const maskDigits = mask.replace(/[^_]/g, "");
58
+ const phoneDigits = removeNonNumeric(parsedPhone.nationalNumber);
59
+ const maskDigitsCount = maskDigits.length;
60
+ const phoneDigitsCount = phoneDigits.length;
61
+ return maskDigitsCount === phoneDigitsCount;
62
+ });
63
+ if (!maskForLength) {
64
+ throw new Error("No mask found for the given phone number length");
65
+ }
66
+ return [maskForLength, country];
67
+ }
68
+ catch (rawError) {
69
+ const error = rawError;
70
+ throw new Error(error.message);
71
+ }
72
+ }
73
+ export { findCountryMask };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/shared",
3
- "version": "3.0.1-beta.121",
3
+ "version": "3.0.1-beta.123",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./dist/bundle.js",
6
6
  "type": "module",
@@ -31,6 +31,7 @@
31
31
  "typecheck": "bunx tsc --project tsconfig.json --noEmit"
32
32
  },
33
33
  "dependencies": {
34
+ "libphonenumber-js": "^1.12.37",
34
35
  "uuid": "^10.0.0"
35
36
  },
36
37
  "devDependencies": {