@webority-technologies/mobile-core 0.0.3 → 0.0.4
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.
|
@@ -8,31 +8,37 @@ exports.formatPhone = formatPhone;
|
|
|
8
8
|
Object.defineProperty(exports, "isValidPhoneNumber", {
|
|
9
9
|
enumerable: true,
|
|
10
10
|
get: function () {
|
|
11
|
-
return
|
|
11
|
+
return _max.isValidPhoneNumber;
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
14
|
exports.normalizePhone = void 0;
|
|
15
15
|
Object.defineProperty(exports, "parsePhoneNumber", {
|
|
16
16
|
enumerable: true,
|
|
17
17
|
get: function () {
|
|
18
|
-
return
|
|
18
|
+
return _max.parsePhoneNumber;
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
Object.defineProperty(exports, "parsePhoneNumberFromString", {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
get: function () {
|
|
24
|
-
return
|
|
24
|
+
return _max.parsePhoneNumberFromString;
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
|
-
var
|
|
27
|
+
var _max = require("libphonenumber-js/max");
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
* The bare 'libphonenumber-js' entry point actually resolves to the library's
|
|
30
|
+
* OWN slimmed-down min/ metadata internally, not its full data - './max' is
|
|
31
|
+
* the variant with complete per-country metadata. Since the reason this
|
|
32
|
+
* module exists is validation and formatting accuracy, the more accurate
|
|
33
|
+
* variant wins throughout this file, not just for validation.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Re-exported under our own name rather than importing CountryCode directly
|
|
38
|
+
* everywhere, so a future need to diverge (or re-narrow) has one place to do
|
|
39
|
+
* it. Every ISO 3166-1 alpha-2 code libphonenumber-js recognises - formatPhone
|
|
40
|
+
* and detectPhoneCountry below are genuinely correct for all of them, not a
|
|
41
|
+
* curated subset.
|
|
36
42
|
*/
|
|
37
43
|
|
|
38
44
|
/**
|
|
@@ -47,82 +53,32 @@ const normalizePhone = input => {
|
|
|
47
53
|
const digits = trimmed.replace(/\D/g, '');
|
|
48
54
|
return hasPlus ? `+${digits}` : digits;
|
|
49
55
|
};
|
|
50
|
-
exports.normalizePhone = normalizePhone;
|
|
51
|
-
const groupIndianMobile = digits => {
|
|
52
|
-
// Indian mobile is 10 digits: format as `XXXXX XXXXX`.
|
|
53
|
-
if (digits.length !== 10) {
|
|
54
|
-
return digits;
|
|
55
|
-
}
|
|
56
|
-
return `${digits.slice(0, 5)} ${digits.slice(5)}`;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// Longest calling code first so a 3-digit code is never shadowed by a shorter one.
|
|
60
|
-
const COUNTRY_RULES = [{
|
|
61
|
-
country: 'AE',
|
|
62
|
-
callingCode: '971',
|
|
63
|
-
nationalLength: 9,
|
|
64
|
-
group: n => `${n.slice(0, 2)} ${n.slice(2, 5)} ${n.slice(5)}`
|
|
65
|
-
}, {
|
|
66
|
-
country: 'IN',
|
|
67
|
-
callingCode: '91',
|
|
68
|
-
nationalLength: 10,
|
|
69
|
-
group: groupIndianMobile
|
|
70
|
-
}, {
|
|
71
|
-
country: 'GB',
|
|
72
|
-
callingCode: '44',
|
|
73
|
-
nationalLength: 10,
|
|
74
|
-
group: n => `${n.slice(0, 4)} ${n.slice(4, 7)} ${n.slice(7)}`
|
|
75
|
-
}, {
|
|
76
|
-
country: 'AU',
|
|
77
|
-
callingCode: '61',
|
|
78
|
-
nationalLength: 9,
|
|
79
|
-
group: n => `${n.slice(0, 3)} ${n.slice(3, 6)} ${n.slice(6)}`
|
|
80
|
-
}, {
|
|
81
|
-
country: 'SG',
|
|
82
|
-
callingCode: '65',
|
|
83
|
-
nationalLength: 8,
|
|
84
|
-
group: n => `${n.slice(0, 4)} ${n.slice(4)}`
|
|
85
|
-
}, {
|
|
86
|
-
country: 'US',
|
|
87
|
-
callingCode: '1',
|
|
88
|
-
nationalLength: 10,
|
|
89
|
-
group: n => `(${n.slice(0, 3)}) ${n.slice(3, 6)}-${n.slice(6)}`
|
|
90
|
-
},
|
|
91
|
-
// US and CA share a calling code and are indistinguishable from it alone;
|
|
92
|
-
// this row only serves an explicit `country: 'CA'` override, never detection
|
|
93
|
-
// (it sits after the US row, and findRuleByCallingCode returns the first match).
|
|
94
|
-
{
|
|
95
|
-
country: 'CA',
|
|
96
|
-
callingCode: '1',
|
|
97
|
-
nationalLength: 10,
|
|
98
|
-
group: n => `(${n.slice(0, 3)}) ${n.slice(3, 6)}-${n.slice(6)}`
|
|
99
|
-
}];
|
|
100
|
-
const findRuleByCallingCode = rest => COUNTRY_RULES.find(rule => rest.startsWith(rule.callingCode));
|
|
101
|
-
const findRuleByCountry = country => COUNTRY_RULES.find(rule => rule.country === country);
|
|
102
56
|
|
|
103
57
|
/**
|
|
104
58
|
* Detect the country of a phone number: from its own `+` country code when
|
|
105
59
|
* present, otherwise from `defaultCountryCode`. Returns null when neither
|
|
106
|
-
*
|
|
60
|
+
* parses to a recognised country.
|
|
107
61
|
*/
|
|
62
|
+
exports.normalizePhone = normalizePhone;
|
|
108
63
|
const detectPhoneCountry = (input, defaultCountryCode = '+91') => {
|
|
109
64
|
const normalized = normalizePhone(input);
|
|
110
65
|
if (!normalized) {
|
|
111
66
|
return null;
|
|
112
67
|
}
|
|
113
68
|
if (normalized.startsWith('+')) {
|
|
114
|
-
return
|
|
69
|
+
return (0, _max.parsePhoneNumberFromString)(normalized)?.country ?? null;
|
|
115
70
|
}
|
|
116
71
|
const normalizedDefault = normalizePhone(defaultCountryCode);
|
|
117
72
|
if (!normalizedDefault.startsWith('+')) {
|
|
118
73
|
return null;
|
|
119
74
|
}
|
|
120
|
-
return
|
|
75
|
+
return (0, _max.parsePhoneNumberFromString)(normalizedDefault + normalized)?.country ?? null;
|
|
121
76
|
};
|
|
122
77
|
exports.detectPhoneCountry = detectPhoneCountry;
|
|
123
78
|
const bestEffortGroup = rest => {
|
|
124
|
-
//
|
|
125
|
-
// the country code (first 1-3
|
|
79
|
+
// Not a parseable number at all (garbage input, or a real number that's an
|
|
80
|
+
// unsupported length for its country): split the country code (first 1-3
|
|
81
|
+
// digits) and group the rest in 4s, rather than returning nothing.
|
|
126
82
|
const ccLen = rest.length > 11 ? 3 : rest.length > 10 ? 2 : 1;
|
|
127
83
|
const cc = rest.slice(0, ccLen);
|
|
128
84
|
const number = rest.slice(ccLen);
|
|
@@ -131,13 +87,18 @@ const bestEffortGroup = rest => {
|
|
|
131
87
|
};
|
|
132
88
|
|
|
133
89
|
/**
|
|
134
|
-
* Format a phone number for human display
|
|
90
|
+
* Format a phone number for human display, via libphonenumber-js's own
|
|
91
|
+
* per-country formatting rather than a hand-rolled grouping table - correct
|
|
92
|
+
* for every country it recognises, not a curated subset.
|
|
135
93
|
*
|
|
136
|
-
* -
|
|
137
|
-
* -
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
94
|
+
* - A number that already carries a `+` country code formats for that country.
|
|
95
|
+
* - A bare national number formats for `options.country` when given, else for
|
|
96
|
+
* the country implied by `defaultCountryCode` (default `+91`).
|
|
97
|
+
* - `includeCountryCode` controls international vs national format; defaults
|
|
98
|
+
* to international whenever the country was detected rather than passed in
|
|
99
|
+
* explicitly as `options.country`, and to national when it was.
|
|
100
|
+
* - Input libphonenumber-js can't parse into a valid number for the resolved
|
|
101
|
+
* country falls back to a best-effort `+CC NNNN NNNN…` digit grouping.
|
|
141
102
|
*/
|
|
142
103
|
|
|
143
104
|
function formatPhone(input, arg) {
|
|
@@ -150,35 +111,40 @@ function formatPhone(input, arg) {
|
|
|
150
111
|
}
|
|
151
112
|
const options = typeof arg === 'string' ? {} : arg ?? {};
|
|
152
113
|
const defaultCountryCode = typeof arg === 'string' ? arg : options.defaultCountryCode ?? '+91';
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const body = activeRule.group(national);
|
|
162
|
-
return includeCc ? `+${activeRule.callingCode} ${body}` : body;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return bestEffortGroup(rest);
|
|
166
|
-
}
|
|
114
|
+
const hasOwnCountryCode = normalized.startsWith('+');
|
|
115
|
+
let parsed;
|
|
116
|
+
// What to best-effort group if parsing fails to produce a valid number.
|
|
117
|
+
// Defaults to the plain input; the defaultCountryCode branch below points
|
|
118
|
+
// it at the combined digits instead, so a bare number that turns out
|
|
119
|
+
// invalid for its detected country still shows that country's code rather
|
|
120
|
+
// than silently discarding it.
|
|
121
|
+
let fallbackRest = normalized;
|
|
167
122
|
if (options.country) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
123
|
+
// parsePhoneNumberFromString ignores this hint when normalized already
|
|
124
|
+
// carries its own +, so a real E.164 number is never overridden by a
|
|
125
|
+
// wrong or stale options.country.
|
|
126
|
+
parsed = (0, _max.parsePhoneNumberFromString)(normalized, options.country);
|
|
127
|
+
} else if (hasOwnCountryCode) {
|
|
128
|
+
parsed = (0, _max.parsePhoneNumberFromString)(normalized);
|
|
129
|
+
} else {
|
|
130
|
+
const normalizedDefault = normalizePhone(defaultCountryCode);
|
|
131
|
+
if (normalizedDefault.startsWith('+')) {
|
|
132
|
+
const combined = normalizedDefault + normalized;
|
|
133
|
+
parsed = (0, _max.parsePhoneNumberFromString)(combined);
|
|
134
|
+
fallbackRest = combined;
|
|
135
|
+
} else {
|
|
136
|
+
parsed = undefined;
|
|
173
137
|
}
|
|
174
|
-
return normalized.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
175
138
|
}
|
|
176
139
|
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
140
|
+
// International by default whenever the country came from the number
|
|
141
|
+
// itself (or from defaultCountryCode); national by default only when a
|
|
142
|
+
// bare number relied on an explicit options.country to resolve at all.
|
|
143
|
+
const includeCcDefault = hasOwnCountryCode || !options.country;
|
|
144
|
+
if (parsed?.isValid()) {
|
|
145
|
+
const includeCc = options.includeCountryCode ?? includeCcDefault;
|
|
146
|
+
return includeCc ? parsed.formatInternational() : parsed.formatNational();
|
|
180
147
|
}
|
|
181
|
-
|
|
182
|
-
return normalized.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
148
|
+
return fallbackRest.startsWith('+') ? bestEffortGroup(fallbackRest.slice(1)) : fallbackRest.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
183
149
|
}
|
|
184
150
|
//# sourceMappingURL=phone.js.map
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
* The bare 'libphonenumber-js' entry point actually resolves to the library's
|
|
5
|
+
* OWN slimmed-down min/ metadata internally, not its full data - './max' is
|
|
6
|
+
* the variant with complete per-country metadata. Since the reason this
|
|
7
|
+
* module exists is validation and formatting accuracy, the more accurate
|
|
8
|
+
* variant wins throughout this file, not just for validation.
|
|
9
|
+
*/
|
|
10
|
+
import { parsePhoneNumberFromString } from 'libphonenumber-js/max';
|
|
11
|
+
export { isValidPhoneNumber, parsePhoneNumber, parsePhoneNumberFromString } from 'libphonenumber-js/max';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Re-exported under our own name rather than importing CountryCode directly
|
|
15
|
+
* everywhere, so a future need to diverge (or re-narrow) has one place to do
|
|
16
|
+
* it. Every ISO 3166-1 alpha-2 code libphonenumber-js recognises - formatPhone
|
|
17
|
+
* and detectPhoneCountry below are genuinely correct for all of them, not a
|
|
18
|
+
* curated subset.
|
|
11
19
|
*/
|
|
12
20
|
|
|
13
|
-
export { isValidPhoneNumber, parsePhoneNumber, parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
14
21
|
/**
|
|
15
22
|
* Strip everything except digits and a leading `+` (preserved if present).
|
|
16
23
|
*/
|
|
@@ -23,62 +30,11 @@ export const normalizePhone = input => {
|
|
|
23
30
|
const digits = trimmed.replace(/\D/g, '');
|
|
24
31
|
return hasPlus ? `+${digits}` : digits;
|
|
25
32
|
};
|
|
26
|
-
const groupIndianMobile = digits => {
|
|
27
|
-
// Indian mobile is 10 digits: format as `XXXXX XXXXX`.
|
|
28
|
-
if (digits.length !== 10) {
|
|
29
|
-
return digits;
|
|
30
|
-
}
|
|
31
|
-
return `${digits.slice(0, 5)} ${digits.slice(5)}`;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// Longest calling code first so a 3-digit code is never shadowed by a shorter one.
|
|
35
|
-
const COUNTRY_RULES = [{
|
|
36
|
-
country: 'AE',
|
|
37
|
-
callingCode: '971',
|
|
38
|
-
nationalLength: 9,
|
|
39
|
-
group: n => `${n.slice(0, 2)} ${n.slice(2, 5)} ${n.slice(5)}`
|
|
40
|
-
}, {
|
|
41
|
-
country: 'IN',
|
|
42
|
-
callingCode: '91',
|
|
43
|
-
nationalLength: 10,
|
|
44
|
-
group: groupIndianMobile
|
|
45
|
-
}, {
|
|
46
|
-
country: 'GB',
|
|
47
|
-
callingCode: '44',
|
|
48
|
-
nationalLength: 10,
|
|
49
|
-
group: n => `${n.slice(0, 4)} ${n.slice(4, 7)} ${n.slice(7)}`
|
|
50
|
-
}, {
|
|
51
|
-
country: 'AU',
|
|
52
|
-
callingCode: '61',
|
|
53
|
-
nationalLength: 9,
|
|
54
|
-
group: n => `${n.slice(0, 3)} ${n.slice(3, 6)} ${n.slice(6)}`
|
|
55
|
-
}, {
|
|
56
|
-
country: 'SG',
|
|
57
|
-
callingCode: '65',
|
|
58
|
-
nationalLength: 8,
|
|
59
|
-
group: n => `${n.slice(0, 4)} ${n.slice(4)}`
|
|
60
|
-
}, {
|
|
61
|
-
country: 'US',
|
|
62
|
-
callingCode: '1',
|
|
63
|
-
nationalLength: 10,
|
|
64
|
-
group: n => `(${n.slice(0, 3)}) ${n.slice(3, 6)}-${n.slice(6)}`
|
|
65
|
-
},
|
|
66
|
-
// US and CA share a calling code and are indistinguishable from it alone;
|
|
67
|
-
// this row only serves an explicit `country: 'CA'` override, never detection
|
|
68
|
-
// (it sits after the US row, and findRuleByCallingCode returns the first match).
|
|
69
|
-
{
|
|
70
|
-
country: 'CA',
|
|
71
|
-
callingCode: '1',
|
|
72
|
-
nationalLength: 10,
|
|
73
|
-
group: n => `(${n.slice(0, 3)}) ${n.slice(3, 6)}-${n.slice(6)}`
|
|
74
|
-
}];
|
|
75
|
-
const findRuleByCallingCode = rest => COUNTRY_RULES.find(rule => rest.startsWith(rule.callingCode));
|
|
76
|
-
const findRuleByCountry = country => COUNTRY_RULES.find(rule => rule.country === country);
|
|
77
33
|
|
|
78
34
|
/**
|
|
79
35
|
* Detect the country of a phone number: from its own `+` country code when
|
|
80
36
|
* present, otherwise from `defaultCountryCode`. Returns null when neither
|
|
81
|
-
*
|
|
37
|
+
* parses to a recognised country.
|
|
82
38
|
*/
|
|
83
39
|
export const detectPhoneCountry = (input, defaultCountryCode = '+91') => {
|
|
84
40
|
const normalized = normalizePhone(input);
|
|
@@ -86,17 +42,18 @@ export const detectPhoneCountry = (input, defaultCountryCode = '+91') => {
|
|
|
86
42
|
return null;
|
|
87
43
|
}
|
|
88
44
|
if (normalized.startsWith('+')) {
|
|
89
|
-
return
|
|
45
|
+
return parsePhoneNumberFromString(normalized)?.country ?? null;
|
|
90
46
|
}
|
|
91
47
|
const normalizedDefault = normalizePhone(defaultCountryCode);
|
|
92
48
|
if (!normalizedDefault.startsWith('+')) {
|
|
93
49
|
return null;
|
|
94
50
|
}
|
|
95
|
-
return
|
|
51
|
+
return parsePhoneNumberFromString(normalizedDefault + normalized)?.country ?? null;
|
|
96
52
|
};
|
|
97
53
|
const bestEffortGroup = rest => {
|
|
98
|
-
//
|
|
99
|
-
// the country code (first 1-3
|
|
54
|
+
// Not a parseable number at all (garbage input, or a real number that's an
|
|
55
|
+
// unsupported length for its country): split the country code (first 1-3
|
|
56
|
+
// digits) and group the rest in 4s, rather than returning nothing.
|
|
100
57
|
const ccLen = rest.length > 11 ? 3 : rest.length > 10 ? 2 : 1;
|
|
101
58
|
const cc = rest.slice(0, ccLen);
|
|
102
59
|
const number = rest.slice(ccLen);
|
|
@@ -105,13 +62,18 @@ const bestEffortGroup = rest => {
|
|
|
105
62
|
};
|
|
106
63
|
|
|
107
64
|
/**
|
|
108
|
-
* Format a phone number for human display
|
|
65
|
+
* Format a phone number for human display, via libphonenumber-js's own
|
|
66
|
+
* per-country formatting rather than a hand-rolled grouping table - correct
|
|
67
|
+
* for every country it recognises, not a curated subset.
|
|
109
68
|
*
|
|
110
|
-
* -
|
|
111
|
-
* -
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
69
|
+
* - A number that already carries a `+` country code formats for that country.
|
|
70
|
+
* - A bare national number formats for `options.country` when given, else for
|
|
71
|
+
* the country implied by `defaultCountryCode` (default `+91`).
|
|
72
|
+
* - `includeCountryCode` controls international vs national format; defaults
|
|
73
|
+
* to international whenever the country was detected rather than passed in
|
|
74
|
+
* explicitly as `options.country`, and to national when it was.
|
|
75
|
+
* - Input libphonenumber-js can't parse into a valid number for the resolved
|
|
76
|
+
* country falls back to a best-effort `+CC NNNN NNNN…` digit grouping.
|
|
115
77
|
*/
|
|
116
78
|
|
|
117
79
|
export function formatPhone(input, arg) {
|
|
@@ -124,35 +86,40 @@ export function formatPhone(input, arg) {
|
|
|
124
86
|
}
|
|
125
87
|
const options = typeof arg === 'string' ? {} : arg ?? {};
|
|
126
88
|
const defaultCountryCode = typeof arg === 'string' ? arg : options.defaultCountryCode ?? '+91';
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const body = activeRule.group(national);
|
|
136
|
-
return includeCc ? `+${activeRule.callingCode} ${body}` : body;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return bestEffortGroup(rest);
|
|
140
|
-
}
|
|
89
|
+
const hasOwnCountryCode = normalized.startsWith('+');
|
|
90
|
+
let parsed;
|
|
91
|
+
// What to best-effort group if parsing fails to produce a valid number.
|
|
92
|
+
// Defaults to the plain input; the defaultCountryCode branch below points
|
|
93
|
+
// it at the combined digits instead, so a bare number that turns out
|
|
94
|
+
// invalid for its detected country still shows that country's code rather
|
|
95
|
+
// than silently discarding it.
|
|
96
|
+
let fallbackRest = normalized;
|
|
141
97
|
if (options.country) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
98
|
+
// parsePhoneNumberFromString ignores this hint when normalized already
|
|
99
|
+
// carries its own +, so a real E.164 number is never overridden by a
|
|
100
|
+
// wrong or stale options.country.
|
|
101
|
+
parsed = parsePhoneNumberFromString(normalized, options.country);
|
|
102
|
+
} else if (hasOwnCountryCode) {
|
|
103
|
+
parsed = parsePhoneNumberFromString(normalized);
|
|
104
|
+
} else {
|
|
105
|
+
const normalizedDefault = normalizePhone(defaultCountryCode);
|
|
106
|
+
if (normalizedDefault.startsWith('+')) {
|
|
107
|
+
const combined = normalizedDefault + normalized;
|
|
108
|
+
parsed = parsePhoneNumberFromString(combined);
|
|
109
|
+
fallbackRest = combined;
|
|
110
|
+
} else {
|
|
111
|
+
parsed = undefined;
|
|
147
112
|
}
|
|
148
|
-
return normalized.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
149
113
|
}
|
|
150
114
|
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
115
|
+
// International by default whenever the country came from the number
|
|
116
|
+
// itself (or from defaultCountryCode); national by default only when a
|
|
117
|
+
// bare number relied on an explicit options.country to resolve at all.
|
|
118
|
+
const includeCcDefault = hasOwnCountryCode || !options.country;
|
|
119
|
+
if (parsed?.isValid()) {
|
|
120
|
+
const includeCc = options.includeCountryCode ?? includeCcDefault;
|
|
121
|
+
return includeCc ? parsed.formatInternational() : parsed.formatNational();
|
|
154
122
|
}
|
|
155
|
-
|
|
156
|
-
return normalized.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
123
|
+
return fallbackRest.startsWith('+') ? bestEffortGroup(fallbackRest.slice(1)) : fallbackRest.replace(/(.{4})(?=.)/g, '$1 ').trim();
|
|
157
124
|
}
|
|
158
125
|
//# sourceMappingURL=phone.js.map
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* hand-rolling that risks silently-wrong validation for a country nobody
|
|
8
|
-
* tests against. Both live here so consumers get one phone module, not two.
|
|
2
|
+
* The bare 'libphonenumber-js' entry point actually resolves to the library's
|
|
3
|
+
* OWN slimmed-down min/ metadata internally, not its full data - './max' is
|
|
4
|
+
* the variant with complete per-country metadata. Since the reason this
|
|
5
|
+
* module exists is validation and formatting accuracy, the more accurate
|
|
6
|
+
* variant wins throughout this file, not just for validation.
|
|
9
7
|
*/
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
export
|
|
8
|
+
import { type CountryCode } from 'libphonenumber-js/max';
|
|
9
|
+
export type { PhoneNumber } from 'libphonenumber-js/max';
|
|
10
|
+
export { isValidPhoneNumber, parsePhoneNumber, parsePhoneNumberFromString } from 'libphonenumber-js/max';
|
|
11
|
+
/**
|
|
12
|
+
* Re-exported under our own name rather than importing CountryCode directly
|
|
13
|
+
* everywhere, so a future need to diverge (or re-narrow) has one place to do
|
|
14
|
+
* it. Every ISO 3166-1 alpha-2 code libphonenumber-js recognises - formatPhone
|
|
15
|
+
* and detectPhoneCountry below are genuinely correct for all of them, not a
|
|
16
|
+
* curated subset.
|
|
17
|
+
*/
|
|
18
|
+
export type PhoneCountry = CountryCode;
|
|
13
19
|
export interface FormatPhoneOptions {
|
|
14
20
|
defaultCountryCode?: string;
|
|
15
21
|
country?: PhoneCountry;
|
|
@@ -22,17 +28,22 @@ export declare const normalizePhone: (input: string) => string;
|
|
|
22
28
|
/**
|
|
23
29
|
* Detect the country of a phone number: from its own `+` country code when
|
|
24
30
|
* present, otherwise from `defaultCountryCode`. Returns null when neither
|
|
25
|
-
*
|
|
31
|
+
* parses to a recognised country.
|
|
26
32
|
*/
|
|
27
33
|
export declare const detectPhoneCountry: (input: string, defaultCountryCode?: string) => PhoneCountry | null;
|
|
28
34
|
/**
|
|
29
|
-
* Format a phone number for human display
|
|
35
|
+
* Format a phone number for human display, via libphonenumber-js's own
|
|
36
|
+
* per-country formatting rather than a hand-rolled grouping table - correct
|
|
37
|
+
* for every country it recognises, not a curated subset.
|
|
30
38
|
*
|
|
31
|
-
* -
|
|
32
|
-
* -
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
39
|
+
* - A number that already carries a `+` country code formats for that country.
|
|
40
|
+
* - A bare national number formats for `options.country` when given, else for
|
|
41
|
+
* the country implied by `defaultCountryCode` (default `+91`).
|
|
42
|
+
* - `includeCountryCode` controls international vs national format; defaults
|
|
43
|
+
* to international whenever the country was detected rather than passed in
|
|
44
|
+
* explicitly as `options.country`, and to national when it was.
|
|
45
|
+
* - Input libphonenumber-js can't parse into a valid number for the resolved
|
|
46
|
+
* country falls back to a best-effort `+CC NNNN NNNN…` digit grouping.
|
|
36
47
|
*/
|
|
37
48
|
export declare function formatPhone(input: string, defaultCountryCode?: string): string;
|
|
38
49
|
export declare function formatPhone(input: string, options?: FormatPhoneOptions): string;
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* hand-rolling that risks silently-wrong validation for a country nobody
|
|
8
|
-
* tests against. Both live here so consumers get one phone module, not two.
|
|
2
|
+
* The bare 'libphonenumber-js' entry point actually resolves to the library's
|
|
3
|
+
* OWN slimmed-down min/ metadata internally, not its full data - './max' is
|
|
4
|
+
* the variant with complete per-country metadata. Since the reason this
|
|
5
|
+
* module exists is validation and formatting accuracy, the more accurate
|
|
6
|
+
* variant wins throughout this file, not just for validation.
|
|
9
7
|
*/
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
export
|
|
8
|
+
import { type CountryCode } from 'libphonenumber-js/max';
|
|
9
|
+
export type { PhoneNumber } from 'libphonenumber-js/max';
|
|
10
|
+
export { isValidPhoneNumber, parsePhoneNumber, parsePhoneNumberFromString } from 'libphonenumber-js/max';
|
|
11
|
+
/**
|
|
12
|
+
* Re-exported under our own name rather than importing CountryCode directly
|
|
13
|
+
* everywhere, so a future need to diverge (or re-narrow) has one place to do
|
|
14
|
+
* it. Every ISO 3166-1 alpha-2 code libphonenumber-js recognises - formatPhone
|
|
15
|
+
* and detectPhoneCountry below are genuinely correct for all of them, not a
|
|
16
|
+
* curated subset.
|
|
17
|
+
*/
|
|
18
|
+
export type PhoneCountry = CountryCode;
|
|
13
19
|
export interface FormatPhoneOptions {
|
|
14
20
|
defaultCountryCode?: string;
|
|
15
21
|
country?: PhoneCountry;
|
|
@@ -22,17 +28,22 @@ export declare const normalizePhone: (input: string) => string;
|
|
|
22
28
|
/**
|
|
23
29
|
* Detect the country of a phone number: from its own `+` country code when
|
|
24
30
|
* present, otherwise from `defaultCountryCode`. Returns null when neither
|
|
25
|
-
*
|
|
31
|
+
* parses to a recognised country.
|
|
26
32
|
*/
|
|
27
33
|
export declare const detectPhoneCountry: (input: string, defaultCountryCode?: string) => PhoneCountry | null;
|
|
28
34
|
/**
|
|
29
|
-
* Format a phone number for human display
|
|
35
|
+
* Format a phone number for human display, via libphonenumber-js's own
|
|
36
|
+
* per-country formatting rather than a hand-rolled grouping table - correct
|
|
37
|
+
* for every country it recognises, not a curated subset.
|
|
30
38
|
*
|
|
31
|
-
* -
|
|
32
|
-
* -
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
39
|
+
* - A number that already carries a `+` country code formats for that country.
|
|
40
|
+
* - A bare national number formats for `options.country` when given, else for
|
|
41
|
+
* the country implied by `defaultCountryCode` (default `+91`).
|
|
42
|
+
* - `includeCountryCode` controls international vs national format; defaults
|
|
43
|
+
* to international whenever the country was detected rather than passed in
|
|
44
|
+
* explicitly as `options.country`, and to national when it was.
|
|
45
|
+
* - Input libphonenumber-js can't parse into a valid number for the resolved
|
|
46
|
+
* country falls back to a best-effort `+CC NNNN NNNN…` digit grouping.
|
|
36
47
|
*/
|
|
37
48
|
export declare function formatPhone(input: string, defaultCountryCode?: string): string;
|
|
38
49
|
export declare function formatPhone(input: string, options?: FormatPhoneOptions): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority-technologies/mobile-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Platform layer for Webority React Native apps: HTTP clients, auth/token storage, config, logging, formatters, validators, network status, permissions, storage and version checks. No UI.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|