libphonenumber-js 1.10.15 → 1.10.17
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/CHANGELOG.md +5 -0
- package/METADATA.md +17 -9
- package/README.md +17 -11
- package/build/AsYouType.js +4 -1
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouTypeState.js +4 -2
- package/build/AsYouTypeState.js.map +1 -1
- package/build/PhoneNumber.js +11 -0
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumber.test.js +44 -0
- package/build/PhoneNumber.test.js.map +1 -1
- package/build/helpers/getCountryByCallingCode.js +3 -38
- package/build/helpers/getCountryByCallingCode.js.map +1 -1
- package/build/helpers/getCountryByNationalNumber.js +46 -0
- package/build/helpers/getCountryByNationalNumber.js.map +1 -0
- package/build/helpers/getPossibleCountriesForNumber.js +44 -0
- package/build/helpers/getPossibleCountriesForNumber.js.map +1 -0
- 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 +4 -1
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouTypeState.js +4 -2
- package/es6/AsYouTypeState.js.map +1 -1
- package/es6/PhoneNumber.js +10 -0
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumber.test.js +44 -0
- package/es6/PhoneNumber.test.js.map +1 -1
- package/es6/helpers/getCountryByCallingCode.js +3 -37
- package/es6/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6/helpers/getCountryByNationalNumber.js +35 -0
- package/es6/helpers/getCountryByNationalNumber.js.map +1 -0
- package/es6/helpers/getPossibleCountriesForNumber.js +35 -0
- package/es6/helpers/getPossibleCountriesForNumber.js.map +1 -0
- 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 +4 -1
- package/source/AsYouTypeState.js +2 -2
- package/source/PhoneNumber.js +12 -0
- package/source/PhoneNumber.test.js +54 -0
- package/source/helpers/getCountryByCallingCode.js +2 -27
- package/source/helpers/getCountryByNationalNumber.js +26 -0
- package/source/helpers/getPossibleCountriesForNumber.js +28 -0
- package/types.d.ts +4 -0
package/package.json
CHANGED
package/source/AsYouType.js
CHANGED
|
@@ -117,7 +117,10 @@ export default class AsYouType {
|
|
|
117
117
|
this.formatter.reset(this.metadata.numberingPlan, this.state)
|
|
118
118
|
}
|
|
119
119
|
})
|
|
120
|
-
this.state.reset(
|
|
120
|
+
this.state.reset({
|
|
121
|
+
country: this.defaultCountry,
|
|
122
|
+
callingCode: this.defaultCallingCode
|
|
123
|
+
})
|
|
121
124
|
this.formattedOutput = ''
|
|
122
125
|
return this
|
|
123
126
|
}
|
package/source/AsYouTypeState.js
CHANGED
|
@@ -4,14 +4,14 @@ export default class AsYouTypeState {
|
|
|
4
4
|
this.onCallingCodeChange = onCallingCodeChange
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
reset(
|
|
7
|
+
reset({ country, callingCode }) {
|
|
8
8
|
this.international = false
|
|
9
9
|
this.IDDPrefix = undefined
|
|
10
10
|
this.missingPlus = undefined
|
|
11
11
|
this.callingCode = undefined
|
|
12
12
|
this.digits = ''
|
|
13
13
|
this.resetNationalSignificantNumber()
|
|
14
|
-
this.initCountryAndCallingCode(
|
|
14
|
+
this.initCountryAndCallingCode(country, callingCode)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
resetNationalSignificantNumber() {
|
package/source/PhoneNumber.js
CHANGED
|
@@ -3,6 +3,7 @@ import isPossibleNumber from './isPossibleNumber_.js'
|
|
|
3
3
|
import isValidNumber from './validate_.js'
|
|
4
4
|
import isValidNumberForRegion from './isValidNumberForRegion_.js'
|
|
5
5
|
import getNumberType from './helpers/getNumberType.js'
|
|
6
|
+
import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
|
|
6
7
|
import formatNumber from './format_.js'
|
|
7
8
|
|
|
8
9
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
@@ -43,6 +44,17 @@ export default class PhoneNumber {
|
|
|
43
44
|
this.ext = ext
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
getPossibleCountries() {
|
|
48
|
+
if (this.country) {
|
|
49
|
+
return [this.country]
|
|
50
|
+
}
|
|
51
|
+
return getPossibleCountriesForNumber(
|
|
52
|
+
this.countryCallingCode,
|
|
53
|
+
this.nationalNumber,
|
|
54
|
+
this.metadata
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
46
58
|
isPossible() {
|
|
47
59
|
return isPossibleNumber(this, { v2: true }, this.metadata)
|
|
48
60
|
}
|
|
@@ -41,4 +41,58 @@ describe('PhoneNumber', () => {
|
|
|
41
41
|
phoneNumber.ext.should.equal('1234')
|
|
42
42
|
phoneNumber.formatNational().should.equal('(213) 373-4253 ext. 1234')
|
|
43
43
|
})
|
|
44
|
+
|
|
45
|
+
it('should return possible countries', () => {
|
|
46
|
+
// "599": [
|
|
47
|
+
// "CW", // "possible_lengths": [7, 8]
|
|
48
|
+
// "BQ" // "possible_lengths": [7]
|
|
49
|
+
// ]
|
|
50
|
+
|
|
51
|
+
let phoneNumber = new PhoneNumber('599', '123456', metadata)
|
|
52
|
+
expect(phoneNumber.country).to.be.undefined
|
|
53
|
+
phoneNumber.getPossibleCountries().should.deep.equal([])
|
|
54
|
+
|
|
55
|
+
phoneNumber = new PhoneNumber('599', '1234567', metadata)
|
|
56
|
+
expect(phoneNumber.country).to.be.undefined
|
|
57
|
+
phoneNumber.getPossibleCountries().should.deep.equal(['CW', 'BQ'])
|
|
58
|
+
|
|
59
|
+
phoneNumber = new PhoneNumber('599', '12345678', metadata)
|
|
60
|
+
expect(phoneNumber.country).to.be.undefined
|
|
61
|
+
phoneNumber.getPossibleCountries().should.deep.equal(['CW'])
|
|
62
|
+
|
|
63
|
+
phoneNumber = new PhoneNumber('599', '123456789', metadata)
|
|
64
|
+
expect(phoneNumber.country).to.be.undefined
|
|
65
|
+
phoneNumber.getPossibleCountries().should.deep.equal([])
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should return possible countries in case of ambiguity', () => {
|
|
69
|
+
const phoneNumber = new PhoneNumber('1', '2223334444', metadata)
|
|
70
|
+
expect(phoneNumber.country).to.be.undefined
|
|
71
|
+
phoneNumber.getPossibleCountries().indexOf('US').should.equal(0)
|
|
72
|
+
phoneNumber.getPossibleCountries().length.should.equal(25)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// it('should return empty possible countries when no national number has been input', () => {
|
|
76
|
+
// const phoneNumber = new PhoneNumber('1', '', metadata)
|
|
77
|
+
// expect(phoneNumber.country).to.be.undefined
|
|
78
|
+
// phoneNumber.getPossibleCountries().should.deep.equal([])
|
|
79
|
+
// })
|
|
80
|
+
|
|
81
|
+
it('should return empty possible countries when not enough national number digits have been input', () => {
|
|
82
|
+
const phoneNumber = new PhoneNumber('1', '222', metadata)
|
|
83
|
+
expect(phoneNumber.country).to.be.undefined
|
|
84
|
+
phoneNumber.getPossibleCountries().should.deep.equal([])
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should return possible countries in case of no ambiguity', () => {
|
|
88
|
+
const phoneNumber = new PhoneNumber('US', '2133734253', metadata)
|
|
89
|
+
phoneNumber.country.should.equal('US')
|
|
90
|
+
phoneNumber.getPossibleCountries().should.deep.equal(['US'])
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should return empty possible countries in case of an unknown calling code', () => {
|
|
94
|
+
const phoneNumber = new PhoneNumber('777', '123', metadata)
|
|
95
|
+
expect(phoneNumber.country).to.be.undefined
|
|
96
|
+
phoneNumber.getPossibleCountries().should.deep.equal([])
|
|
97
|
+
})
|
|
44
98
|
})
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import getNumberType from './getNumberType.js'
|
|
1
|
+
import getCountryByNationalNumber from './getCountryByNationalNumber.js'
|
|
3
2
|
|
|
4
3
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
5
4
|
|
|
@@ -10,7 +9,6 @@ export default function getCountryByCallingCode(callingCode, nationalPhoneNumber
|
|
|
10
9
|
return '001'
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
|
-
// Is always non-empty, because `callingCode` is always valid
|
|
14
12
|
const possibleCountries = metadata.getCountryCodesForCallingCode(callingCode)
|
|
15
13
|
if (!possibleCountries) {
|
|
16
14
|
return
|
|
@@ -20,28 +18,5 @@ export default function getCountryByCallingCode(callingCode, nationalPhoneNumber
|
|
|
20
18
|
if (possibleCountries.length === 1) {
|
|
21
19
|
return possibleCountries[0]
|
|
22
20
|
}
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function selectCountryFromList(possibleCountries, nationalPhoneNumber, metadata) {
|
|
27
|
-
// Re-create `metadata` because it will be selecting a `country`.
|
|
28
|
-
metadata = new Metadata(metadata)
|
|
29
|
-
for (const country of possibleCountries) {
|
|
30
|
-
metadata.country(country)
|
|
31
|
-
// Leading digits check would be the simplest and fastest one.
|
|
32
|
-
// Leading digits patterns are only defined for about 20% of all countries.
|
|
33
|
-
// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits
|
|
34
|
-
// Matching "leading digits" is a sufficient but not necessary condition.
|
|
35
|
-
if (metadata.leadingDigits()) {
|
|
36
|
-
if (nationalPhoneNumber &&
|
|
37
|
-
nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
|
|
38
|
-
return country
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Else perform full validation with all of those
|
|
42
|
-
// fixed-line/mobile/etc regular expressions.
|
|
43
|
-
else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
|
|
44
|
-
return country
|
|
45
|
-
}
|
|
46
|
-
}
|
|
21
|
+
return getCountryByNationalNumber(possibleCountries, nationalPhoneNumber, metadata.metadata)
|
|
47
22
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Metadata from '../metadata.js'
|
|
2
|
+
import getNumberType from './getNumberType.js'
|
|
3
|
+
|
|
4
|
+
export default function getCountryByNationalNumber(possibleCountries, nationalPhoneNumber, metadata) {
|
|
5
|
+
// Re-create `metadata` because it will be selecting a `country`.
|
|
6
|
+
metadata = new Metadata(metadata)
|
|
7
|
+
for (const country of possibleCountries) {
|
|
8
|
+
metadata.country(country)
|
|
9
|
+
// "Leading digits" patterns are only defined for about 20% of all countries.
|
|
10
|
+
// By definition, matching "leading digits" is a sufficient but not a necessary
|
|
11
|
+
// condition for a phone number to belong to a country.
|
|
12
|
+
// The point of "leading digits" check is that it's the fastest one to get a match.
|
|
13
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits
|
|
14
|
+
if (metadata.leadingDigits()) {
|
|
15
|
+
if (nationalPhoneNumber &&
|
|
16
|
+
nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
|
|
17
|
+
return country
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Else perform full validation with all of those
|
|
21
|
+
// fixed-line/mobile/etc regular expressions.
|
|
22
|
+
else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
|
|
23
|
+
return country
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Metadata from '../metadata.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a list of countries that the phone number could potentially belong to.
|
|
5
|
+
* @param {string} callingCode — Calling code.
|
|
6
|
+
* @param {string} nationalNumber — National (significant) number.
|
|
7
|
+
* @param {object} metadata — Metadata.
|
|
8
|
+
* @return {string[]} A list of possible countries.
|
|
9
|
+
*/
|
|
10
|
+
export default function getPossibleCountriesForNumber(callingCode, nationalNumber, metadata) {
|
|
11
|
+
const _metadata = new Metadata(metadata)
|
|
12
|
+
let possibleCountries = _metadata.getCountryCodesForCallingCode(callingCode)
|
|
13
|
+
if (!possibleCountries) {
|
|
14
|
+
return []
|
|
15
|
+
}
|
|
16
|
+
return possibleCountries.filter((country) => {
|
|
17
|
+
return couldNationalNumberBelongToCountry(nationalNumber, country, metadata)
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function couldNationalNumberBelongToCountry(nationalNumber, country, metadata) {
|
|
22
|
+
const _metadata = new Metadata(metadata)
|
|
23
|
+
_metadata.selectNumberingPlan(country)
|
|
24
|
+
if (_metadata.numberingPlan.possibleLengths().indexOf(nationalNumber.length) >= 0) {
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
return false
|
|
28
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -65,6 +65,9 @@ type FormatNumberOptions = {
|
|
|
65
65
|
formatExtension?: FormatExtension;
|
|
66
66
|
};
|
|
67
67
|
|
|
68
|
+
// // https://stackoverflow.com/a/67026991
|
|
69
|
+
// type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
|
|
70
|
+
|
|
68
71
|
export class PhoneNumber {
|
|
69
72
|
constructor(countryCallingCodeOrCountry: CountryCallingCode | CountryCode, nationalNumber: NationalNumber, metadata: MetadataJson);
|
|
70
73
|
countryCallingCode: CountryCallingCode;
|
|
@@ -74,6 +77,7 @@ export class PhoneNumber {
|
|
|
74
77
|
carrierCode?: CarrierCode;
|
|
75
78
|
ext?: Extension;
|
|
76
79
|
setExt(ext: Extension): void;
|
|
80
|
+
getPossibleCountries(): CountryCode[];
|
|
77
81
|
isPossible(): boolean;
|
|
78
82
|
isValid(): boolean;
|
|
79
83
|
getType(): NumberType;
|