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