libphonenumber-js 1.9.52 → 1.9.53
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/README.md +29 -3
- package/build/AsYouType.test.js +17 -2
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.js +4 -1
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/helpers/extractNationalNumber.js +53 -40
- package/build/helpers/extractNationalNumber.js.map +1 -1
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -1
- package/build/parsePhoneNumberFromString.test.js +7 -0
- package/build/parsePhoneNumberFromString.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.test.js +17 -2
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +4 -1
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/helpers/extractNationalNumber.js +50 -37
- package/es6/helpers/extractNationalNumber.js.map +1 -1
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -1
- package/es6/parsePhoneNumberFromString.test.js +7 -0
- package/es6/parsePhoneNumberFromString.test.js.map +1 -1
- package/index.d.ts +2 -2
- 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.test.js +18 -2
- package/source/AsYouTypeFormatter.js +6 -2
- package/source/helpers/extractNationalNumber.js +49 -35
- package/source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
- package/source/parsePhoneNumberFromString.test.js +8 -0
package/package.json
CHANGED
package/source/AsYouType.test.js
CHANGED
|
@@ -760,8 +760,8 @@ describe('AsYouType', () => {
|
|
|
760
760
|
// Mobile.
|
|
761
761
|
// `1` is prepended before area code to mobile numbers in international format.
|
|
762
762
|
asYouType.reset()
|
|
763
|
-
asYouType.input('+521331234567').should.equal('+52
|
|
764
|
-
asYouType.getTemplate().should.equal('xxx
|
|
763
|
+
asYouType.input('+521331234567').should.equal('+52 133 1234 567')
|
|
764
|
+
asYouType.getTemplate().should.equal('xxx xxx xxxx xxx')
|
|
765
765
|
// Google's `libphonenumber` seems to not able to format this type of number.
|
|
766
766
|
// https://issuetracker.google.com/issues/147938979
|
|
767
767
|
asYouType.input('8').should.equal('+52 133 1234 5678')
|
|
@@ -1314,6 +1314,22 @@ describe('AsYouType.getNumberValue()', () => {
|
|
|
1314
1314
|
formatter.input('4')
|
|
1315
1315
|
expect(formatter.getNumberValue()).to.be.undefined
|
|
1316
1316
|
})
|
|
1317
|
+
|
|
1318
|
+
it('should not drop any input digits', () => {
|
|
1319
|
+
// Test "+529011234567" number, proactively ensuring that no formatting is applied,
|
|
1320
|
+
// where a format is chosen that would otherwise have led to some digits being dropped.
|
|
1321
|
+
const formatter = new AsYouType('MX')
|
|
1322
|
+
formatter.input('9').should.equal('9')
|
|
1323
|
+
formatter.input('0').should.equal('90')
|
|
1324
|
+
formatter.input('1').should.equal('901')
|
|
1325
|
+
formatter.input('1').should.equal('901 1')
|
|
1326
|
+
formatter.input('2').should.equal('901 12')
|
|
1327
|
+
formatter.input('3').should.equal('901 123')
|
|
1328
|
+
formatter.input('4').should.equal('901 123 4')
|
|
1329
|
+
formatter.input('5').should.equal('901 123 45')
|
|
1330
|
+
formatter.input('6').should.equal('901 123 456')
|
|
1331
|
+
formatter.input('7').should.equal('901 123 4567')
|
|
1332
|
+
})
|
|
1317
1333
|
})
|
|
1318
1334
|
|
|
1319
1335
|
function type(something) {
|
|
@@ -87,9 +87,13 @@ const CREATE_STANDALONE_DIGIT_PATTERN = SUPPORT_LEGACY_FORMATTING_PATTERNS && ((
|
|
|
87
87
|
// `libphonenumber` code.
|
|
88
88
|
//
|
|
89
89
|
const NON_ALTERING_FORMAT_REG_EXP = new RegExp(
|
|
90
|
-
'^' +
|
|
91
90
|
'[' + VALID_PUNCTUATION + ']*' +
|
|
92
|
-
|
|
91
|
+
// Google developers say:
|
|
92
|
+
// "We require that the first matching group is present in the
|
|
93
|
+
// output pattern to ensure no data is lost while formatting."
|
|
94
|
+
'\\$1' +
|
|
95
|
+
'[' + VALID_PUNCTUATION + ']*' +
|
|
96
|
+
'(\\$\\d[' + VALID_PUNCTUATION + ']*)*' +
|
|
93
97
|
'$'
|
|
94
98
|
)
|
|
95
99
|
|
|
@@ -22,43 +22,36 @@ export default function extractNationalNumber(number, metadata) {
|
|
|
22
22
|
// and so does this library, because it has been requested:
|
|
23
23
|
// https://github.com/catamphetamine/libphonenumber-js/issues/127
|
|
24
24
|
const {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
carrierCode,
|
|
26
|
+
nationalNumber
|
|
27
27
|
} = extractNationalNumberFromPossiblyIncompleteNumber(
|
|
28
28
|
number,
|
|
29
29
|
metadata
|
|
30
30
|
)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// For some reason, they do this check right after the `national_number_pattern` check
|
|
40
|
-
// this library does in `shouldExtractNationalPrefix()` function.
|
|
41
|
-
// Why is there a second "resultant" number validity check?
|
|
42
|
-
// They don't provide an explanation.
|
|
43
|
-
// This library just copies the behavior.
|
|
44
|
-
if (number.length !== nationalNumber.length + (carrierCode ? carrierCode.length : 0)) {
|
|
45
|
-
// If not using legacy generated metadata (before version `1.0.18`)
|
|
46
|
-
// then it has "possible lengths", so use those to validate the number length.
|
|
31
|
+
|
|
32
|
+
if (nationalNumber !== number) {
|
|
33
|
+
if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
|
|
34
|
+
// Don't strip the national prefix.
|
|
35
|
+
return { nationalNumber: number }
|
|
36
|
+
}
|
|
37
|
+
// Check the national (significant) number length after extracting national prefix and carrier code.
|
|
38
|
+
// Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
|
|
47
39
|
if (metadata.possibleLengths()) {
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
// Otherwise,
|
|
51
|
-
// a valid
|
|
40
|
+
// The number remaining after stripping the national prefix and carrier code
|
|
41
|
+
// should be long enough to have a possible length for the country.
|
|
42
|
+
// Otherwise, don't strip the national prefix and carrier code,
|
|
43
|
+
// since the original number could be a valid number.
|
|
44
|
+
// This check has been copy-pasted "as is" from Google's original library:
|
|
52
45
|
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
return { nationalNumber: number }
|
|
46
|
+
// It doesn't check for the "possibility" of the original `number`.
|
|
47
|
+
// I guess it's fine not checking that one. It works as is anyway.
|
|
48
|
+
if (!isPossibleIncompleteNationalNumber(nationalNumber, metadata)) {
|
|
49
|
+
// Don't strip the national prefix.
|
|
50
|
+
return { nationalNumber: number }
|
|
59
51
|
}
|
|
60
52
|
}
|
|
61
53
|
}
|
|
54
|
+
|
|
62
55
|
return { nationalNumber, carrierCode }
|
|
63
56
|
}
|
|
64
57
|
|
|
@@ -76,17 +69,38 @@ export default function extractNationalNumber(number, metadata) {
|
|
|
76
69
|
// is not extracted when the original number is "viable" and the
|
|
77
70
|
// resultant number is not, a "viable" national number being the one
|
|
78
71
|
// that matches `national_number_pattern`.
|
|
79
|
-
function
|
|
72
|
+
function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
|
|
80
73
|
// The equivalent in Google's code is:
|
|
81
74
|
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
|
|
82
|
-
if (matchesEntirely(
|
|
83
|
-
!matchesEntirely(
|
|
75
|
+
if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) &&
|
|
76
|
+
!matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
|
|
84
77
|
return false
|
|
85
78
|
}
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
//
|
|
79
|
+
// This "is possible" national number (length) check has been commented out
|
|
80
|
+
// because it's superceded by the (effectively) same check done in the
|
|
81
|
+
// `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
|
|
82
|
+
// In other words, why run the same check twice if it could only be run once.
|
|
83
|
+
// // Check the national (significant) number length after extracting national prefix and carrier code.
|
|
84
|
+
// // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
|
|
85
|
+
// // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
|
|
86
|
+
// if (metadata.possibleLengths()) {
|
|
87
|
+
// if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
|
|
88
|
+
// !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
|
|
89
|
+
// return false
|
|
90
|
+
// }
|
|
90
91
|
// }
|
|
91
92
|
return true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isPossibleIncompleteNationalNumber(nationalNumber, metadata) {
|
|
96
|
+
switch (checkNumberLength(nationalNumber, metadata)) {
|
|
97
|
+
case 'TOO_SHORT':
|
|
98
|
+
case 'INVALID_LENGTH':
|
|
99
|
+
// This library ignores "local-only" phone numbers (for simplicity).
|
|
100
|
+
// See the readme for more info on what are "local-only" phone numbers.
|
|
101
|
+
// case 'IS_POSSIBLE_LOCAL_ONLY':
|
|
102
|
+
return false
|
|
103
|
+
default:
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
92
106
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* it will return `{ number: "3402345678" }`, because `340` area code is prepended.
|
|
10
10
|
* @param {string} number — National number digits.
|
|
11
11
|
* @param {object} metadata — Metadata with country selected.
|
|
12
|
-
* @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`.
|
|
12
|
+
* @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`. Even if a national prefix was extracted, it's not necessarily present in the returned object, so don't rely on its presence in the returned object in order to find out whether a national prefix has been extracted or not.
|
|
13
13
|
*/
|
|
14
14
|
export default function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {
|
|
15
15
|
if (number && metadata.numberingPlan.nationalPrefixForParsing()) {
|
|
@@ -69,4 +69,12 @@ describe('parsePhoneNumberFromString', () => {
|
|
|
69
69
|
testCorrectness('+1 (213) 373-4253', true)
|
|
70
70
|
testCorrectness(' +1 (213) 373-4253', false)
|
|
71
71
|
})
|
|
72
|
+
|
|
73
|
+
it('should not prematurely strip a possible national prefix from Chinese numbers', () => {
|
|
74
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
|
|
75
|
+
const phoneNumber = parsePhoneNumberFromString('+86123456789')
|
|
76
|
+
phoneNumber.isPossible().should.equal(true)
|
|
77
|
+
phoneNumber.isValid().should.equal(false)
|
|
78
|
+
phoneNumber.nationalNumber.should.equal('123456789')
|
|
79
|
+
})
|
|
72
80
|
})
|