tele_number_utils 1.0.5 → 1.0.7
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.
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
type Separator = " " | "-" | "()";
|
|
2
|
-
/**
|
|
3
|
-
* Formats a number string into groups dynamically
|
|
4
|
-
* @param value The input string (digits)
|
|
5
|
-
* @param groupSizes Array of group sizes, e.g., [3,3,4]
|
|
6
|
-
* @param separator Separator between groups: " ", "-", "/", "()" (default: " ")
|
|
7
|
-
*/
|
|
8
2
|
export declare const formatter: (value?: string, groupSizes?: number[], separator?: Separator) => string;
|
|
9
3
|
export {};
|
|
10
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/formatter/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/formatter/index.ts"],"names":[],"mappings":"AAEA,KAAK,SAAS,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AAuBlC,eAAO,MAAM,SAAS,GACpB,QAAO,MAAW,EAClB,aAAY,MAAM,EAAc,EAChC,YAAW,SAAe,KACzB,MA2BF,CAAC"}
|
package/dist/formatter/index.js
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @param value The input string (digits)
|
|
4
|
-
* @param groupSizes Array of group sizes, e.g., [3,3,4]
|
|
5
|
-
* @param separator Separator between groups: " ", "-", "/", "()" (default: " ")
|
|
6
|
-
*/
|
|
7
|
-
export const formatter = (value = "", groupSizes = [3, 3, 4], separator = " ") => {
|
|
8
|
-
const digits = value.replace(/\D/g, ""); // remove non-digits
|
|
9
|
-
if (!digits)
|
|
10
|
-
return "";
|
|
11
|
-
// If length <= 6, return as-is
|
|
12
|
-
if (digits.length <= 6)
|
|
13
|
-
return digits;
|
|
1
|
+
import { parsePhoneNumber } from "../parsed/index.js";
|
|
2
|
+
const formatGroups = (number, groupSizes, separator) => {
|
|
14
3
|
const groups = [];
|
|
15
4
|
let start = 0;
|
|
16
|
-
for (
|
|
17
|
-
if (start >=
|
|
5
|
+
for (const size of groupSizes) {
|
|
6
|
+
if (start >= number.length)
|
|
18
7
|
break;
|
|
19
|
-
groups.push(
|
|
8
|
+
groups.push(number.slice(start, start + size));
|
|
20
9
|
start += size;
|
|
21
10
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
groups.push(digits.slice(start));
|
|
25
|
-
}
|
|
26
|
-
// Handle parentheses specially: wrap first group
|
|
27
|
-
if (separator === "()") {
|
|
28
|
-
const [first, ...rest] = groups;
|
|
29
|
-
return `(${first}) ${rest.join(" ")}`;
|
|
11
|
+
if (start < number.length) {
|
|
12
|
+
groups.push(number.slice(start));
|
|
30
13
|
}
|
|
31
14
|
return groups.join(separator);
|
|
32
15
|
};
|
|
16
|
+
export const formatter = (value = "", groupSizes = [3, 3, 4], separator = " ") => {
|
|
17
|
+
const trimmed = value.trim();
|
|
18
|
+
if (!trimmed)
|
|
19
|
+
return "";
|
|
20
|
+
const digits = trimmed.replace(/\D/g, "");
|
|
21
|
+
if (!digits)
|
|
22
|
+
return "";
|
|
23
|
+
try {
|
|
24
|
+
// Always parse full number
|
|
25
|
+
const parsed = parsePhoneNumber(trimmed.startsWith("+") ? trimmed : `+${digits}`);
|
|
26
|
+
const formattedNational = formatGroups(parsed.nationalNumber || "", groupSizes, separator === "()" ? " " : separator);
|
|
27
|
+
if (separator === "()") {
|
|
28
|
+
return `(+${parsed.callingCode}) ${formattedNational}`;
|
|
29
|
+
}
|
|
30
|
+
return `+${parsed.callingCode} ${formattedNational}`;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
};
|