libphonenumber-js 1.10.30 → 1.10.32
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/build/AsYouType.js +5 -1
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js +6 -0
- package/build/AsYouType.test.js.map +1 -1
- package/build/PhoneNumber.js +15 -9
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumberMatcher.js +6 -1
- package/build/PhoneNumberMatcher.js.map +1 -1
- package/build/findNumbers/Leniency.js +38 -9
- package/build/findNumbers/Leniency.js.map +1 -1
- package/build/helpers/getCountryByCallingCode.js +10 -2
- package/build/helpers/getCountryByCallingCode.js.map +1 -1
- package/build/helpers/getCountryByNationalNumber.js +23 -3
- package/build/helpers/getCountryByNationalNumber.js.map +1 -1
- package/build/parse.js +5 -1
- package/build/parse.js.map +1 -1
- package/build/parse.test.js +8 -0
- package/build/parse.test.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/es6/AsYouType.js +5 -1
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js +6 -0
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/PhoneNumber.js +15 -9
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumberMatcher.js +6 -1
- package/es6/PhoneNumberMatcher.js.map +1 -1
- package/es6/findNumbers/Leniency.js +38 -9
- package/es6/findNumbers/Leniency.js.map +1 -1
- package/es6/helpers/getCountryByCallingCode.js +10 -2
- package/es6/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6/helpers/getCountryByNationalNumber.js +23 -3
- package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6/parse.js +5 -1
- package/es6/parse.js.map +1 -1
- package/es6/parse.test.js +8 -0
- package/es6/parse.test.js.map +1 -1
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.js +1 -1
- package/package.json +1 -1
- package/source/AsYouType.js +5 -2
- package/source/AsYouType.test.js +7 -0
- package/source/PhoneNumber.js +12 -8
- package/source/PhoneNumberMatcher.js +6 -1
- package/source/findNumbers/Leniency.js +13 -9
- package/source/helpers/getCountryByCallingCode.js +10 -2
- package/source/helpers/getCountryByNationalNumber.js +21 -3
- package/source/parse.js +5 -1
- package/source/parse.test.js +6 -0
package/package.json
CHANGED
package/source/AsYouType.js
CHANGED
|
@@ -273,8 +273,11 @@ export default class AsYouType {
|
|
|
273
273
|
determineTheCountry() {
|
|
274
274
|
this.state.setCountry(getCountryByCallingCode(
|
|
275
275
|
this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
|
|
276
|
-
|
|
277
|
-
|
|
276
|
+
{
|
|
277
|
+
nationalNumber: this.state.nationalSignificantNumber,
|
|
278
|
+
defaultCountry: this.defaultCountry,
|
|
279
|
+
metadata: this.metadata
|
|
280
|
+
}
|
|
278
281
|
))
|
|
279
282
|
}
|
|
280
283
|
|
package/source/AsYouType.test.js
CHANGED
|
@@ -1363,6 +1363,13 @@ describe('AsYouType.getNumberValue()', () => {
|
|
|
1363
1363
|
formatter.input('421901222333').should.equal('421 901 222 333')
|
|
1364
1364
|
formatter.getTemplate().should.equal('xxx xxx xxx xxx')
|
|
1365
1365
|
})
|
|
1366
|
+
|
|
1367
|
+
it('should choose `defaultCountry` (non-"main" one) when multiple countries match the number', function() {
|
|
1368
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103
|
|
1369
|
+
const formatter = new AsYouType('CA')
|
|
1370
|
+
formatter.input('8004001000')
|
|
1371
|
+
formatter.getNumber().country.should.equal('CA')
|
|
1372
|
+
})
|
|
1366
1373
|
})
|
|
1367
1374
|
|
|
1368
1375
|
function type(something) {
|
package/source/PhoneNumber.js
CHANGED
|
@@ -33,7 +33,11 @@ export default class PhoneNumber {
|
|
|
33
33
|
this.countryCallingCode = countryCallingCode
|
|
34
34
|
this.nationalNumber = nationalNumber
|
|
35
35
|
this.number = '+' + this.countryCallingCode + this.nationalNumber
|
|
36
|
-
|
|
36
|
+
// Exclude `metadata` property output from `PhoneNumber.toString()`
|
|
37
|
+
// so that it doesn't clutter the console output of Node.js.
|
|
38
|
+
// Previously, when Node.js did `console.log(new PhoneNumber(...))`,
|
|
39
|
+
// it would output the whole internal structure of the `metadata` object.
|
|
40
|
+
this.getMetadata = () => metadata
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
setExt(ext) {
|
|
@@ -47,20 +51,20 @@ export default class PhoneNumber {
|
|
|
47
51
|
return getPossibleCountriesForNumber(
|
|
48
52
|
this.countryCallingCode,
|
|
49
53
|
this.nationalNumber,
|
|
50
|
-
this.
|
|
54
|
+
this.getMetadata()
|
|
51
55
|
)
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
isPossible() {
|
|
55
|
-
return isPossibleNumber(this, { v2: true }, this.
|
|
59
|
+
return isPossibleNumber(this, { v2: true }, this.getMetadata())
|
|
56
60
|
}
|
|
57
61
|
|
|
58
62
|
isValid() {
|
|
59
|
-
return isValidNumber(this, { v2: true }, this.
|
|
63
|
+
return isValidNumber(this, { v2: true }, this.getMetadata())
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
isNonGeographic() {
|
|
63
|
-
const metadata = new Metadata(this.
|
|
67
|
+
const metadata = new Metadata(this.getMetadata())
|
|
64
68
|
return metadata.isNonGeographicCallingCode(this.countryCallingCode)
|
|
65
69
|
}
|
|
66
70
|
|
|
@@ -75,7 +79,7 @@ export default class PhoneNumber {
|
|
|
75
79
|
// and just left the `validatePhoneNumberLength()` function, even though that one would require
|
|
76
80
|
// and additional step to also validate the actual country / calling code of the phone number.
|
|
77
81
|
// validateLength() {
|
|
78
|
-
// const metadata = new Metadata(this.
|
|
82
|
+
// const metadata = new Metadata(this.getMetadata())
|
|
79
83
|
// metadata.selectNumberingPlan(this.countryCallingCode)
|
|
80
84
|
// const result = checkNumberLength(this.nationalNumber, metadata)
|
|
81
85
|
// if (result !== 'IS_POSSIBLE') {
|
|
@@ -84,7 +88,7 @@ export default class PhoneNumber {
|
|
|
84
88
|
// }
|
|
85
89
|
|
|
86
90
|
getType() {
|
|
87
|
-
return getNumberType(this, { v2: true }, this.
|
|
91
|
+
return getNumberType(this, { v2: true }, this.getMetadata())
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
format(format, options) {
|
|
@@ -92,7 +96,7 @@ export default class PhoneNumber {
|
|
|
92
96
|
this,
|
|
93
97
|
format,
|
|
94
98
|
options ? { ...options, v2: true } : { v2: true },
|
|
95
|
-
this.
|
|
99
|
+
this.getMetadata()
|
|
96
100
|
)
|
|
97
101
|
}
|
|
98
102
|
|
|
@@ -345,7 +345,12 @@ export default class PhoneNumberMatcher
|
|
|
345
345
|
return
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
-
if (this.leniency(phoneNumber,
|
|
348
|
+
if (this.leniency(phoneNumber, {
|
|
349
|
+
candidate,
|
|
350
|
+
defaultCountry: this.options.defaultCountry,
|
|
351
|
+
metadata: this.metadata,
|
|
352
|
+
regExpCache: this.regExpCache
|
|
353
|
+
})) {
|
|
349
354
|
return {
|
|
350
355
|
startsAt: offset,
|
|
351
356
|
endsAt: offset + candidate.length,
|
|
@@ -19,7 +19,7 @@ export default
|
|
|
19
19
|
/**
|
|
20
20
|
* Phone numbers accepted are "possible", but not necessarily "valid".
|
|
21
21
|
*/
|
|
22
|
-
POSSIBLE(phoneNumber, candidate, metadata)
|
|
22
|
+
POSSIBLE(phoneNumber, { candidate, metadata })
|
|
23
23
|
{
|
|
24
24
|
return true
|
|
25
25
|
},
|
|
@@ -29,7 +29,7 @@ export default
|
|
|
29
29
|
* Numbers written in national format must have their national-prefix
|
|
30
30
|
* present if it is usually written for a number of this type.
|
|
31
31
|
*/
|
|
32
|
-
VALID(phoneNumber, candidate, metadata)
|
|
32
|
+
VALID(phoneNumber, { candidate, defaultCountry, metadata })
|
|
33
33
|
{
|
|
34
34
|
if (
|
|
35
35
|
!phoneNumber.isValid() ||
|
|
@@ -40,7 +40,7 @@ export default
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// Skipped for simplicity.
|
|
43
|
-
// return isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
43
|
+
// return isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
|
|
44
44
|
return true
|
|
45
45
|
},
|
|
46
46
|
|
|
@@ -56,13 +56,13 @@ export default
|
|
|
56
56
|
* country code "+1". If you are not sure about which level to use,
|
|
57
57
|
* email the discussion group libphonenumber-discuss@googlegroups.com.
|
|
58
58
|
*/
|
|
59
|
-
STRICT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
|
|
59
|
+
STRICT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
|
|
60
60
|
{
|
|
61
61
|
if (
|
|
62
62
|
!phoneNumber.isValid() ||
|
|
63
63
|
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
|
|
64
64
|
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
|
|
65
|
-
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
65
|
+
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
|
|
66
66
|
)
|
|
67
67
|
{
|
|
68
68
|
return false
|
|
@@ -89,13 +89,13 @@ export default
|
|
|
89
89
|
* country code "+1". If you are not sure about which level to use, email the discussion group
|
|
90
90
|
* libphonenumber-discuss@googlegroups.com.
|
|
91
91
|
*/
|
|
92
|
-
EXACT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
|
|
92
|
+
EXACT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
|
|
93
93
|
{
|
|
94
94
|
if (
|
|
95
95
|
!phoneNumber.isValid() ||
|
|
96
96
|
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
|
|
97
97
|
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
|
|
98
|
-
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
|
|
98
|
+
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
|
|
99
99
|
)
|
|
100
100
|
{
|
|
101
101
|
return false
|
|
@@ -157,7 +157,7 @@ function containsOnlyValidXChars(phoneNumber, candidate, metadata)
|
|
|
157
157
|
return true
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
function isNationalPrefixPresentIfRequired(phoneNumber, _metadata)
|
|
160
|
+
function isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata: _metadata })
|
|
161
161
|
{
|
|
162
162
|
// First, check how we deduced the country code. If it was written in international format, then
|
|
163
163
|
// the national prefix is not required.
|
|
@@ -169,7 +169,11 @@ function isNationalPrefixPresentIfRequired(phoneNumber, _metadata)
|
|
|
169
169
|
const metadata = new Metadata(_metadata)
|
|
170
170
|
metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
|
|
171
171
|
|
|
172
|
-
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode,
|
|
172
|
+
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, {
|
|
173
|
+
nationalNumber: phoneNumber.nationalNumber,
|
|
174
|
+
defaultCountry,
|
|
175
|
+
metadata
|
|
176
|
+
})
|
|
173
177
|
|
|
174
178
|
// Check if a national prefix should be present when formatting this number.
|
|
175
179
|
const nationalNumber = phoneNumber.nationalNumber
|
|
@@ -2,7 +2,11 @@ import getCountryByNationalNumber from './getCountryByNationalNumber.js'
|
|
|
2
2
|
|
|
3
3
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
4
4
|
|
|
5
|
-
export default function getCountryByCallingCode(callingCode,
|
|
5
|
+
export default function getCountryByCallingCode(callingCode, {
|
|
6
|
+
nationalNumber: nationalPhoneNumber,
|
|
7
|
+
defaultCountry,
|
|
8
|
+
metadata
|
|
9
|
+
}) {
|
|
6
10
|
/* istanbul ignore if */
|
|
7
11
|
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
|
8
12
|
if (metadata.isNonGeographicCallingCode(callingCode)) {
|
|
@@ -18,5 +22,9 @@ export default function getCountryByCallingCode(callingCode, nationalPhoneNumber
|
|
|
18
22
|
if (possibleCountries.length === 1) {
|
|
19
23
|
return possibleCountries[0]
|
|
20
24
|
}
|
|
21
|
-
return getCountryByNationalNumber(
|
|
25
|
+
return getCountryByNationalNumber(nationalPhoneNumber, {
|
|
26
|
+
countries: possibleCountries,
|
|
27
|
+
defaultCountry,
|
|
28
|
+
metadata: metadata.metadata
|
|
29
|
+
})
|
|
22
30
|
}
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import Metadata from '../metadata.js'
|
|
2
2
|
import getNumberType from './getNumberType.js'
|
|
3
3
|
|
|
4
|
-
export default function getCountryByNationalNumber(
|
|
4
|
+
export default function getCountryByNationalNumber(nationalPhoneNumber, { countries, defaultCountry, metadata }) {
|
|
5
5
|
// Re-create `metadata` because it will be selecting a `country`.
|
|
6
6
|
metadata = new Metadata(metadata)
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
const matchingCountries = []
|
|
9
|
+
|
|
10
|
+
for (const country of countries) {
|
|
8
11
|
metadata.country(country)
|
|
9
12
|
// "Leading digits" patterns are only defined for about 20% of all countries.
|
|
10
13
|
// By definition, matching "leading digits" is a sufficient but not a necessary
|
|
11
14
|
// condition for a phone number to belong to a country.
|
|
12
15
|
// The point of "leading digits" check is that it's the fastest one to get a match.
|
|
13
16
|
// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits
|
|
17
|
+
// I'd suppose that "leading digits" patterns are mutually exclusive for different countries
|
|
18
|
+
// because of the intended use of that feature.
|
|
14
19
|
if (metadata.leadingDigits()) {
|
|
15
20
|
if (nationalPhoneNumber &&
|
|
16
21
|
nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
|
|
@@ -20,7 +25,20 @@ export default function getCountryByNationalNumber(possibleCountries, nationalPh
|
|
|
20
25
|
// Else perform full validation with all of those
|
|
21
26
|
// fixed-line/mobile/etc regular expressions.
|
|
22
27
|
else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
|
|
23
|
-
return
|
|
28
|
+
// If the `defaultCountry` is among the `matchingCountries` then return it.
|
|
29
|
+
if (defaultCountry) {
|
|
30
|
+
if (country === defaultCountry) {
|
|
31
|
+
return country
|
|
32
|
+
}
|
|
33
|
+
matchingCountries.push(country)
|
|
34
|
+
} else {
|
|
35
|
+
return country
|
|
36
|
+
}
|
|
24
37
|
}
|
|
25
38
|
}
|
|
39
|
+
|
|
40
|
+
// Return the first ("main") one of the `matchingCountries`.
|
|
41
|
+
if (matchingCountries.length > 0) {
|
|
42
|
+
return matchingCountries[0]
|
|
43
|
+
}
|
|
26
44
|
}
|
package/source/parse.js
CHANGED
|
@@ -343,7 +343,11 @@ function parsePhoneNumber(
|
|
|
343
343
|
// get their countries populated with the full set of
|
|
344
344
|
// "phone number type" regular expressions.
|
|
345
345
|
//
|
|
346
|
-
const exactCountry = getCountryByCallingCode(countryCallingCode,
|
|
346
|
+
const exactCountry = getCountryByCallingCode(countryCallingCode, {
|
|
347
|
+
nationalNumber,
|
|
348
|
+
defaultCountry,
|
|
349
|
+
metadata
|
|
350
|
+
})
|
|
347
351
|
if (exactCountry) {
|
|
348
352
|
country = exactCountry
|
|
349
353
|
/* istanbul ignore if */
|
package/source/parse.test.js
CHANGED
|
@@ -536,4 +536,10 @@ describe('parse', () => {
|
|
|
536
536
|
// Extension too long.
|
|
537
537
|
expect(() => parseNumber('+1123-456-7890 7777777#', { v2: true })).to.throw('NOT_A_NUMBER')
|
|
538
538
|
})
|
|
539
|
+
|
|
540
|
+
it('should choose `defaultCountry` (non-"main" one) when multiple countries match the number', () => {
|
|
541
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103
|
|
542
|
+
const phoneNumber = parseNumber('8004001000', { defaultCountry: 'CA', v2: true })
|
|
543
|
+
phoneNumber.country.should.equal('CA')
|
|
544
|
+
})
|
|
539
545
|
})
|