tele_number_utils 1.0.5 → 1.0.6
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;AAElC,eAAO,MAAM,SAAS,GACpB,QAAO,MAAW,EAClB,aAAY,MAAM,EAAc,EAChC,YAAW,SAAe,KACzB,MA6DF,CAAC"}
|
package/dist/formatter/index.js
CHANGED
|
@@ -1,32 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
* Formats a number string into groups dynamically
|
|
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
|
-
*/
|
|
1
|
+
import { parsePhoneNumber } from "../parsed/index.js";
|
|
7
2
|
export const formatter = (value = "", groupSizes = [3, 3, 4], separator = " ") => {
|
|
8
|
-
const
|
|
3
|
+
const trimmed = value.trim();
|
|
4
|
+
if (!trimmed)
|
|
5
|
+
return "";
|
|
6
|
+
const hasPlus = trimmed.startsWith("+");
|
|
7
|
+
const digits = trimmed.replace(/\D/g, "");
|
|
9
8
|
if (!digits)
|
|
10
9
|
return "";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
if (hasPlus) {
|
|
11
|
+
try {
|
|
12
|
+
const parsed = parsePhoneNumber(trimmed);
|
|
13
|
+
if (separator === "()") {
|
|
14
|
+
return `(+${parsed.callingCode}) ${parsed.nationalNumber}`;
|
|
15
|
+
}
|
|
16
|
+
return parsed.formatted || "";
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
let callingCode = "";
|
|
23
|
+
let nationalNumber = digits;
|
|
24
|
+
for (let i = 1; i <= 3; i++) {
|
|
25
|
+
const code = digits.slice(0, i);
|
|
26
|
+
try {
|
|
27
|
+
parsePhoneNumber(`+${code}`);
|
|
28
|
+
callingCode = code;
|
|
29
|
+
nationalNumber = digits.slice(i);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (!callingCode)
|
|
37
|
+
return value;
|
|
38
|
+
// Format national number
|
|
14
39
|
const groups = [];
|
|
15
40
|
let start = 0;
|
|
16
|
-
for (
|
|
17
|
-
if (start >=
|
|
41
|
+
for (const size of groupSizes) {
|
|
42
|
+
if (start >= nationalNumber.length)
|
|
18
43
|
break;
|
|
19
|
-
groups.push(
|
|
44
|
+
groups.push(nationalNumber.slice(start, start + size));
|
|
20
45
|
start += size;
|
|
21
46
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
groups.push(digits.slice(start));
|
|
47
|
+
if (start < nationalNumber.length) {
|
|
48
|
+
groups.push(nationalNumber.slice(start));
|
|
25
49
|
}
|
|
26
|
-
|
|
50
|
+
const formattedNational = groups.join(" ");
|
|
51
|
+
// ✅ Parentheses wrap ONLY country code
|
|
27
52
|
if (separator === "()") {
|
|
28
|
-
|
|
29
|
-
return `(${first}) ${rest.join(" ")}`;
|
|
53
|
+
return `(+${callingCode}) ${formattedNational}`;
|
|
30
54
|
}
|
|
31
|
-
return groups.join(separator)
|
|
55
|
+
return `+${callingCode} ${groups.join(separator)}`;
|
|
32
56
|
};
|