libphonenumber-js 1.10.24 → 1.10.26
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.
- package/CODE_OF_CONDUCT.md +78 -0
- package/README.md +16 -6
- package/build/PhoneNumber.js +6 -0
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumberMatcher.js +53 -62
- package/build/PhoneNumberMatcher.js.map +1 -1
- package/build/PhoneNumberMatcher.test.js +8 -0
- package/build/PhoneNumberMatcher.test.js.map +1 -1
- package/build/findNumbers/Leniency.js +60 -50
- package/build/findNumbers/Leniency.js.map +1 -1
- package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +75 -0
- package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
- package/build/format.js +1 -0
- package/build/format.js.map +1 -1
- package/build/helpers/RFC3966.test.js +3 -1
- package/build/helpers/RFC3966.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.js +44 -4
- package/build/helpers/extractCountryCallingCode.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.test.js +2 -0
- package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +86 -0
- package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
- package/build/helpers/extractPhoneContext.js +97 -0
- package/build/helpers/extractPhoneContext.js.map +1 -0
- package/build/helpers/extractPhoneContext.test.js +72 -0
- package/build/helpers/extractPhoneContext.test.js.map +1 -0
- package/build/parse.js +19 -9
- package/build/parse.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.d.ts +3 -3
- package/custom.d.ts +4 -4
- package/es6/PhoneNumber.js +6 -0
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumberMatcher.js +53 -62
- package/es6/PhoneNumberMatcher.js.map +1 -1
- package/es6/PhoneNumberMatcher.test.js +8 -0
- package/es6/PhoneNumberMatcher.test.js.map +1 -1
- package/es6/findNumbers/Leniency.js +56 -50
- package/es6/findNumbers/Leniency.js.map +1 -1
- package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +66 -0
- package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
- package/es6/format.js +1 -1
- package/es6/format.js.map +1 -1
- package/es6/helpers/RFC3966.test.js +3 -1
- package/es6/helpers/RFC3966.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.js +44 -4
- package/es6/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.test.js +2 -0
- package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +70 -0
- package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
- package/es6/helpers/extractPhoneContext.js +82 -0
- package/es6/helpers/extractPhoneContext.js.map +1 -0
- package/es6/helpers/extractPhoneContext.test.js +66 -0
- package/es6/helpers/extractPhoneContext.test.js.map +1 -0
- package/es6/parse.js +20 -10
- package/es6/parse.js.map +1 -1
- package/examples.mobile.json.d.ts +1 -1
- package/index.d.ts +3 -3
- package/metadata.full.json +1 -1
- package/metadata.full.json.d.ts +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.d.ts +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.d.ts +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.d.ts +1 -1
- package/metadata.mobile.json.js +1 -1
- package/min/index.d.ts +3 -3
- package/package.json +4 -4
- package/source/AsYouTypeFormatter.PatternMatcher.d.ts +1 -1
- package/source/PhoneNumber.js +6 -0
- package/source/PhoneNumberMatcher.js +55 -58
- package/source/PhoneNumberMatcher.test.js +4 -0
- package/source/findNumbers/Leniency.js +281 -271
- package/source/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +64 -0
- package/source/format.js +1 -1
- package/source/helpers/RFC3966.test.js +3 -0
- package/source/helpers/extractCountryCallingCode.js +48 -3
- package/source/helpers/extractCountryCallingCode.test.js +2 -0
- package/source/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +74 -0
- package/source/helpers/extractPhoneContext.js +103 -0
- package/source/helpers/extractPhoneContext.test.js +104 -0
- package/source/parse.js +18 -8
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import isValidNumber from '../isValid.js'
|
|
2
2
|
import parseDigits from '../helpers/parseDigits.js'
|
|
3
|
+
import matchPhoneNumberStringAgainstPhoneNumber from './matchPhoneNumberStringAgainstPhoneNumber.js'
|
|
4
|
+
import Metadata from '../metadata.js'
|
|
5
|
+
import getCountryByCallingCode from '../helpers/getCountryByCallingCode.js'
|
|
6
|
+
import { chooseFormatForNumber } from '../format.js'
|
|
3
7
|
|
|
4
8
|
import {
|
|
5
9
|
startsWith,
|
|
@@ -15,7 +19,7 @@ export default
|
|
|
15
19
|
/**
|
|
16
20
|
* Phone numbers accepted are "possible", but not necessarily "valid".
|
|
17
21
|
*/
|
|
18
|
-
POSSIBLE(
|
|
22
|
+
POSSIBLE(phoneNumber, candidate, metadata)
|
|
19
23
|
{
|
|
20
24
|
return true
|
|
21
25
|
},
|
|
@@ -25,18 +29,20 @@ export default
|
|
|
25
29
|
* Numbers written in national format must have their national-prefix
|
|
26
30
|
* present if it is usually written for a number of this type.
|
|
27
31
|
*/
|
|
28
|
-
VALID(
|
|
32
|
+
VALID(phoneNumber, candidate, metadata)
|
|
29
33
|
{
|
|
30
|
-
if (
|
|
31
|
-
!
|
|
34
|
+
if (
|
|
35
|
+
!phoneNumber.isValid() ||
|
|
36
|
+
!containsOnlyValidXChars(phoneNumber, candidate, metadata)
|
|
37
|
+
)
|
|
32
38
|
{
|
|
33
39
|
return false
|
|
34
40
|
}
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
// return isNationalPrefixPresentIfRequired(
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
// Skipped for simplicity.
|
|
43
|
+
// return isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
44
|
+
return true
|
|
45
|
+
},
|
|
40
46
|
|
|
41
47
|
/**
|
|
42
48
|
* Phone numbers accepted are "valid" and
|
|
@@ -50,54 +56,54 @@ export default
|
|
|
50
56
|
* country code "+1". If you are not sure about which level to use,
|
|
51
57
|
* email the discussion group libphonenumber-discuss@googlegroups.com.
|
|
52
58
|
*/
|
|
53
|
-
STRICT_GROUPING(
|
|
59
|
+
STRICT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
|
|
54
60
|
{
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
if (
|
|
62
|
+
!phoneNumber.isValid() ||
|
|
63
|
+
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
|
|
64
|
+
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
|
|
65
|
+
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
66
|
+
)
|
|
61
67
|
{
|
|
62
68
|
return false
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
return checkNumberGroupingIsValid
|
|
66
72
|
(
|
|
67
|
-
|
|
73
|
+
phoneNumber,
|
|
68
74
|
candidate,
|
|
69
75
|
metadata,
|
|
70
76
|
allNumberGroupsRemainGrouped,
|
|
71
77
|
regExpCache
|
|
72
78
|
)
|
|
73
|
-
|
|
79
|
+
},
|
|
74
80
|
|
|
75
81
|
/**
|
|
76
|
-
* Phone numbers accepted are
|
|
77
|
-
*
|
|
78
|
-
* example, a US number written as "650 2530000" is not accepted
|
|
79
|
-
* "650 253 0000" or "6502530000" are.
|
|
82
|
+
* Phone numbers accepted are "valid" and are grouped in the same way
|
|
83
|
+
* that we would have formatted it, or as a single block.
|
|
84
|
+
* For example, a US number written as "650 2530000" is not accepted
|
|
85
|
+
* at this leniency level, whereas "650 253 0000" or "6502530000" are.
|
|
80
86
|
* Numbers with more than one '/' symbol are also dropped at this level.
|
|
81
|
-
*
|
|
82
|
-
* Warning: This level might result in lower coverage especially for regions outside of
|
|
83
|
-
* code "+1". If you are not sure about which level to use, email the discussion group
|
|
87
|
+
*
|
|
88
|
+
* Warning: This level might result in lower coverage especially for regions outside of
|
|
89
|
+
* country code "+1". If you are not sure about which level to use, email the discussion group
|
|
84
90
|
* libphonenumber-discuss@googlegroups.com.
|
|
85
91
|
*/
|
|
86
|
-
EXACT_GROUPING(
|
|
92
|
+
EXACT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
|
|
87
93
|
{
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
if (
|
|
95
|
+
!phoneNumber.isValid() ||
|
|
96
|
+
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
|
|
97
|
+
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
|
|
98
|
+
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
99
|
+
)
|
|
94
100
|
{
|
|
95
101
|
return false
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
return checkNumberGroupingIsValid
|
|
99
105
|
(
|
|
100
|
-
|
|
106
|
+
phoneNumber,
|
|
101
107
|
candidate,
|
|
102
108
|
metadata,
|
|
103
109
|
allNumberGroupsAreExactlyPresent,
|
|
@@ -106,161 +112,159 @@ export default
|
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
114
|
|
|
109
|
-
function containsOnlyValidXChars(
|
|
115
|
+
function containsOnlyValidXChars(phoneNumber, candidate, metadata)
|
|
110
116
|
{
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
117
|
+
// The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
|
|
118
|
+
// national significant number or (2) an extension sign, in which case they always precede the
|
|
119
|
+
// extension number. We assume a carrier code is more than 1 digit, so the first case has to
|
|
120
|
+
// have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
|
|
121
|
+
// or 'X'. We ignore the character if it appears as the last character of the string.
|
|
122
|
+
for (let index = 0; index < candidate.length - 1; index++)
|
|
123
|
+
{
|
|
124
|
+
const charAtIndex = candidate.charAt(index)
|
|
125
|
+
|
|
126
|
+
if (charAtIndex === 'x' || charAtIndex === 'X')
|
|
127
|
+
{
|
|
128
|
+
const charAtNextIndex = candidate.charAt(index + 1)
|
|
129
|
+
|
|
130
|
+
if (charAtNextIndex === 'x' || charAtNextIndex === 'X')
|
|
131
|
+
{
|
|
132
|
+
// This is the carrier code case, in which the 'X's always precede the national
|
|
133
|
+
// significant number.
|
|
134
|
+
index++
|
|
135
|
+
if (matchPhoneNumberStringAgainstPhoneNumber(candidate.substring(index), phoneNumber, metadata) !== 'NSN_MATCH')
|
|
136
|
+
{
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
// This is the extension sign case, in which the 'x' or 'X' should always precede the
|
|
140
|
+
// extension number.
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const ext = parseDigits(candidate.substring(index))
|
|
144
|
+
if (ext) {
|
|
145
|
+
if (phoneNumber.ext !== ext) {
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
if (phoneNumber.ext) {
|
|
150
|
+
return false
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return true
|
|
144
158
|
}
|
|
145
159
|
|
|
146
|
-
function isNationalPrefixPresentIfRequired(
|
|
160
|
+
function isNationalPrefixPresentIfRequired(phoneNumber, _metadata)
|
|
147
161
|
{
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
// Normalize the remainder.
|
|
186
|
-
const rawInputCopy = PhoneNumberUtil.normalizeDigitsOnly(number.getRawInput())
|
|
187
|
-
|
|
188
|
-
// Check if we found a national prefix and/or carrier code at the start of the raw input, and
|
|
189
|
-
// return the result.
|
|
190
|
-
return util.maybeStripNationalPrefixAndCarrierCode(rawInputCopy, metadata, null)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return true
|
|
162
|
+
// First, check how we deduced the country code. If it was written in international format, then
|
|
163
|
+
// the national prefix is not required.
|
|
164
|
+
if (phoneNumber.__countryCallingCodeSource !== 'FROM_DEFAULT_COUNTRY')
|
|
165
|
+
{
|
|
166
|
+
return true
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const metadata = new Metadata(_metadata)
|
|
170
|
+
metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
|
|
171
|
+
|
|
172
|
+
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, phoneNumber.nationalNumber, metadata)
|
|
173
|
+
|
|
174
|
+
// Check if a national prefix should be present when formatting this number.
|
|
175
|
+
const nationalNumber = phoneNumber.nationalNumber
|
|
176
|
+
const format = chooseFormatForNumber(metadata.numberingPlan.formats(), nationalNumber)
|
|
177
|
+
|
|
178
|
+
// To do this, we check that a national prefix formatting rule was present
|
|
179
|
+
// and that it wasn't just the first-group symbol ($1) with punctuation.
|
|
180
|
+
if (format.nationalPrefixFormattingRule())
|
|
181
|
+
{
|
|
182
|
+
if (metadata.numberingPlan.nationalPrefixIsOptionalWhenFormattingInNationalFormat())
|
|
183
|
+
{
|
|
184
|
+
// The national-prefix is optional in these cases, so we don't need to check if it was present.
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!format.usesNationalPrefix())
|
|
189
|
+
{
|
|
190
|
+
// National Prefix not needed for this number.
|
|
191
|
+
return true
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return Boolean(phoneNumber.nationalPrefix)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return true
|
|
194
198
|
}
|
|
195
199
|
|
|
196
|
-
export function containsMoreThanOneSlashInNationalNumber(
|
|
200
|
+
export function containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate)
|
|
197
201
|
{
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
return true
|
|
202
|
+
const firstSlashInBodyIndex = candidate.indexOf('/')
|
|
203
|
+
if (firstSlashInBodyIndex < 0)
|
|
204
|
+
{
|
|
205
|
+
// No slashes, this is okay.
|
|
206
|
+
return false
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Now look for a second one.
|
|
210
|
+
const secondSlashInBodyIndex = candidate.indexOf('/', firstSlashInBodyIndex + 1)
|
|
211
|
+
if (secondSlashInBodyIndex < 0)
|
|
212
|
+
{
|
|
213
|
+
// Only one slash, this is okay.
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// If the first slash is after the country calling code, this is permitted.
|
|
218
|
+
const candidateHasCountryCode =
|
|
219
|
+
phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITH_PLUS_SIGN' ||
|
|
220
|
+
phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITHOUT_PLUS_SIGN'
|
|
221
|
+
|
|
222
|
+
if (candidateHasCountryCode && parseDigits(candidate.substring(0, firstSlashInBodyIndex)) === phoneNumber.countryCallingCode)
|
|
223
|
+
{
|
|
224
|
+
// Any more slashes and this is illegal.
|
|
225
|
+
return candidate.slice(secondSlashInBodyIndex + 1).indexOf('/') >= 0
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return true
|
|
227
229
|
}
|
|
228
230
|
|
|
229
231
|
function checkNumberGroupingIsValid(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
number,
|
|
233
|
+
candidate,
|
|
234
|
+
metadata,
|
|
235
|
+
checkGroups,
|
|
236
|
+
regExpCache
|
|
235
237
|
) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
238
|
+
throw new Error('This part of code hasn\'t been ported')
|
|
239
|
+
|
|
240
|
+
const normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */)
|
|
241
|
+
let formattedNumberGroups = getNationalNumberGroups(metadata, number, null)
|
|
242
|
+
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
|
|
243
|
+
return true
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// If this didn't pass, see if there are any alternate formats that match, and try them instead.
|
|
247
|
+
const alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode())
|
|
248
|
+
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
|
|
249
|
+
|
|
250
|
+
if (alternateFormats) {
|
|
251
|
+
for (const alternateFormat of alternateFormats.numberFormats()) {
|
|
252
|
+
if (alternateFormat.leadingDigitsPatterns().length > 0) {
|
|
253
|
+
// There is only one leading digits pattern for alternate formats.
|
|
254
|
+
const leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0])
|
|
255
|
+
if (!leadingDigitsRegExp.test(nationalSignificantNumber)) {
|
|
256
|
+
// Leading digits don't match; try another one.
|
|
257
|
+
continue
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat)
|
|
261
|
+
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
|
|
262
|
+
return true
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return false
|
|
264
268
|
}
|
|
265
269
|
|
|
266
270
|
/**
|
|
@@ -273,26 +277,28 @@ function getNationalNumberGroups(
|
|
|
273
277
|
number,
|
|
274
278
|
formattingPattern
|
|
275
279
|
) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
280
|
+
throw new Error('This part of code hasn\'t been ported')
|
|
281
|
+
|
|
282
|
+
if (formattingPattern) {
|
|
283
|
+
// We format the NSN only, and split that according to the separator.
|
|
284
|
+
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
|
|
285
|
+
return util.formatNsnUsingPattern(nationalSignificantNumber,
|
|
286
|
+
formattingPattern, 'RFC3966', metadata).split('-')
|
|
281
287
|
}
|
|
282
288
|
|
|
283
|
-
|
|
284
|
-
|
|
289
|
+
// This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits.
|
|
290
|
+
const rfc3966Format = formatNumber(number, 'RFC3966', metadata)
|
|
285
291
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
+
// We remove the extension part from the formatted string before splitting it into different
|
|
293
|
+
// groups.
|
|
294
|
+
let endIndex = rfc3966Format.indexOf(';')
|
|
295
|
+
if (endIndex < 0) {
|
|
296
|
+
endIndex = rfc3966Format.length
|
|
297
|
+
}
|
|
292
298
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
299
|
+
// The country-code will have a '-' following it.
|
|
300
|
+
const startIndex = rfc3966Format.indexOf('-') + 1
|
|
301
|
+
return rfc3966Format.slice(startIndex, endIndex).split('-')
|
|
296
302
|
}
|
|
297
303
|
|
|
298
304
|
function allNumberGroupsAreExactlyPresent
|
|
@@ -303,39 +309,41 @@ function allNumberGroupsAreExactlyPresent
|
|
|
303
309
|
formattedNumberGroups
|
|
304
310
|
)
|
|
305
311
|
{
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
312
|
+
throw new Error('This part of code hasn\'t been ported')
|
|
313
|
+
|
|
314
|
+
const candidateGroups = normalizedCandidate.split(NON_DIGITS_PATTERN)
|
|
315
|
+
|
|
316
|
+
// Set this to the last group, skipping it if the number has an extension.
|
|
317
|
+
let candidateNumberGroupIndex =
|
|
318
|
+
number.hasExtension() ? candidateGroups.length - 2 : candidateGroups.length - 1
|
|
319
|
+
|
|
320
|
+
// First we check if the national significant number is formatted as a block.
|
|
321
|
+
// We use contains and not equals, since the national significant number may be present with
|
|
322
|
+
// a prefix such as a national number prefix, or the country code itself.
|
|
323
|
+
if (candidateGroups.length == 1
|
|
324
|
+
|| candidateGroups[candidateNumberGroupIndex].contains(
|
|
325
|
+
util.getNationalSignificantNumber(number)))
|
|
326
|
+
{
|
|
327
|
+
return true
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Starting from the end, go through in reverse, excluding the first group, and check the
|
|
331
|
+
// candidate and number groups are the same.
|
|
332
|
+
let formattedNumberGroupIndex = (formattedNumberGroups.length - 1)
|
|
333
|
+
while (formattedNumberGroupIndex > 0 && candidateNumberGroupIndex >= 0)
|
|
334
|
+
{
|
|
335
|
+
if (candidateGroups[candidateNumberGroupIndex] !== formattedNumberGroups[formattedNumberGroupIndex])
|
|
336
|
+
{
|
|
337
|
+
return false
|
|
338
|
+
}
|
|
339
|
+
formattedNumberGroupIndex--
|
|
340
|
+
candidateNumberGroupIndex--
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Now check the first group. There may be a national prefix at the start, so we only check
|
|
344
|
+
// that the candidate group ends with the formatted number group.
|
|
345
|
+
return (candidateNumberGroupIndex >= 0
|
|
346
|
+
&& endsWith(candidateGroups[candidateNumberGroupIndex], formattedNumberGroups[0]))
|
|
339
347
|
}
|
|
340
348
|
|
|
341
349
|
|
|
@@ -347,50 +355,52 @@ function allNumberGroupsRemainGrouped
|
|
|
347
355
|
formattedNumberGroups
|
|
348
356
|
)
|
|
349
357
|
{
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
358
|
+
throw new Error('This part of code hasn\'t been ported')
|
|
359
|
+
|
|
360
|
+
let fromIndex = 0
|
|
361
|
+
if (number.getCountryCodeSource() !== CountryCodeSource.FROM_DEFAULT_COUNTRY)
|
|
362
|
+
{
|
|
363
|
+
// First skip the country code if the normalized candidate contained it.
|
|
364
|
+
const countryCode = String(number.getCountryCode())
|
|
365
|
+
fromIndex = normalizedCandidate.indexOf(countryCode) + countryCode.length()
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Check each group of consecutive digits are not broken into separate groupings in the
|
|
369
|
+
// {@code normalizedCandidate} string.
|
|
370
|
+
for (let i = 0; i < formattedNumberGroups.length; i++)
|
|
371
|
+
{
|
|
372
|
+
// Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex}
|
|
373
|
+
// doesn't contain the consecutive digits in formattedNumberGroups[i].
|
|
374
|
+
fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex)
|
|
375
|
+
if (fromIndex < 0) {
|
|
376
|
+
return false
|
|
377
|
+
}
|
|
378
|
+
// Moves {@code fromIndex} forward.
|
|
379
|
+
fromIndex += formattedNumberGroups[i].length()
|
|
380
|
+
if (i == 0 && fromIndex < normalizedCandidate.length())
|
|
381
|
+
{
|
|
382
|
+
// We are at the position right after the NDC. We get the region used for formatting
|
|
383
|
+
// information based on the country code in the phone number, rather than the number itself,
|
|
384
|
+
// as we do not need to distinguish between different countries with the same country
|
|
385
|
+
// calling code and this is faster.
|
|
386
|
+
const region = util.getRegionCodeForCountryCode(number.getCountryCode())
|
|
387
|
+
if (util.getNddPrefixForRegion(region, true) != null
|
|
388
|
+
&& Character.isDigit(normalizedCandidate.charAt(fromIndex))) {
|
|
389
|
+
// This means there is no formatting symbol after the NDC. In this case, we only
|
|
390
|
+
// accept the number if there is no formatting symbol at all in the number, except
|
|
391
|
+
// for extensions. This is only important for countries with national prefixes.
|
|
392
|
+
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
|
|
393
|
+
return startsWith
|
|
394
|
+
(
|
|
395
|
+
normalizedCandidate.slice(fromIndex - formattedNumberGroups[i].length),
|
|
396
|
+
nationalSignificantNumber
|
|
397
|
+
)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// The check here makes sure that we haven't mistakenly already used the extension to
|
|
403
|
+
// match the last group of the subscriber number. Note the extension cannot have
|
|
404
|
+
// formatting in-between digits.
|
|
405
|
+
return normalizedCandidate.slice(fromIndex).contains(number.getExtension())
|
|
396
406
|
}
|