libphonenumber-js 1.9.50 → 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.
Files changed (45) hide show
  1. package/README.md +29 -3
  2. package/build/AsYouType.test.js +17 -2
  3. package/build/AsYouType.test.js.map +1 -1
  4. package/build/AsYouTypeFormatter.js +4 -1
  5. package/build/AsYouTypeFormatter.js.map +1 -1
  6. package/build/helpers/extractNationalNumber.js +53 -40
  7. package/build/helpers/extractNationalNumber.js.map +1 -1
  8. package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
  9. package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -1
  10. package/build/parsePhoneNumberFromString.test.js +7 -0
  11. package/build/parsePhoneNumberFromString.test.js.map +1 -1
  12. package/bundle/libphonenumber-js.min.js +1 -1
  13. package/bundle/libphonenumber-js.min.js.map +1 -1
  14. package/bundle/libphonenumber-max.js +1 -1
  15. package/bundle/libphonenumber-max.js.map +1 -1
  16. package/bundle/libphonenumber-min.js +1 -1
  17. package/bundle/libphonenumber-min.js.map +1 -1
  18. package/bundle/libphonenumber-mobile.js +1 -1
  19. package/bundle/libphonenumber-mobile.js.map +1 -1
  20. package/es6/AsYouType.test.js +17 -2
  21. package/es6/AsYouType.test.js.map +1 -1
  22. package/es6/AsYouTypeFormatter.js +4 -1
  23. package/es6/AsYouTypeFormatter.js.map +1 -1
  24. package/es6/helpers/extractNationalNumber.js +50 -37
  25. package/es6/helpers/extractNationalNumber.js.map +1 -1
  26. package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
  27. package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -1
  28. package/es6/parsePhoneNumberFromString.test.js +7 -0
  29. package/es6/parsePhoneNumberFromString.test.js.map +1 -1
  30. package/index.d.ts +2 -2
  31. package/metadata.full.json +1 -1
  32. package/metadata.full.json.js +1 -1
  33. package/metadata.max.json +1 -1
  34. package/metadata.max.json.js +1 -1
  35. package/metadata.min.json +1 -1
  36. package/metadata.min.json.js +1 -1
  37. package/metadata.mobile.json +1 -1
  38. package/metadata.mobile.json.js +1 -1
  39. package/package.json +1 -1
  40. package/source/AsYouType.test.js +18 -2
  41. package/source/AsYouTypeFormatter.js +6 -2
  42. package/source/helpers/extractNationalNumber.js +49 -35
  43. package/source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +1 -1
  44. package/source/parsePhoneNumberFromString.test.js +8 -0
  45. package/types.d.ts +1 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.9.50",
3
+ "version": "1.9.53",
4
4
  "description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",
5
5
  "main": "index.common.js",
6
6
  "module": "index.es6.js",
@@ -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 13 3123 4567')
764
- asYouType.getTemplate().should.equal('xxx xx xxxx xxxx')
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
- '(\\$\\d[' + VALID_PUNCTUATION + ']*)+' +
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
- nationalNumber,
26
- carrierCode
25
+ carrierCode,
26
+ nationalNumber
27
27
  } = extractNationalNumberFromPossiblyIncompleteNumber(
28
28
  number,
29
29
  metadata
30
30
  )
31
- if (!shouldExtractNationalPrefix(number, nationalNumber, metadata)) {
32
- // Don't strip the national prefix.
33
- return { nationalNumber: number }
34
- }
35
- // If a national prefix has been extracted, check to see
36
- // if the resultant number isn't too short.
37
- // Same code in Google's `libphonenumber`:
38
- // https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3291-L3302
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
- // "We require that the NSN remaining after stripping the national prefix and
49
- // carrier code be long enough to be a possible length for the region.
50
- // Otherwise, we don't do the stripping, since the original number could be
51
- // a valid short number."
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
- switch (checkNumberLength(nationalNumber, metadata)) {
54
- case 'TOO_SHORT':
55
- case 'INVALID_LENGTH':
56
- // case 'IS_POSSIBLE_LOCAL_ONLY':
57
- // Don't strip the national prefix.
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 shouldExtractNationalPrefix(number, nationalSignificantNumber, metadata) {
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(number, metadata.nationalNumberPattern()) &&
83
- !matchesEntirely(nationalSignificantNumber, metadata.nationalNumberPattern())) {
75
+ if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) &&
76
+ !matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
84
77
  return false
85
78
  }
86
- // Just "possible" number check would be more relaxed, so it's not used.
87
- // if (isPossibleNumber(number, metadata) &&
88
- // !isPossibleNumber(numberWithNationalPrefixExtracted, metadata)) {
89
- // return false
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
  })
package/types.d.ts CHANGED
@@ -39,8 +39,7 @@ export type Examples = {
39
39
  [country in CountryCode]: NationalNumber;
40
40
  };
41
41
 
42
- // 'National' and 'International' formats are deprecated.
43
- export type NumberFormat = 'NATIONAL' | 'National' | 'INTERNATIONAL' | 'International' | 'E.164' | 'RFC3966' | 'IDD';
42
+ export type NumberFormat = 'NATIONAL' | 'INTERNATIONAL' | 'E.164' | 'RFC3966' | 'IDD';
44
43
  export type NumberType = undefined | 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VOIP' | 'PERSONAL_NUMBER' | 'PAGER' | 'UAN' | 'VOICEMAIL' | 'FIXED_LINE_OR_MOBILE' | 'FIXED_LINE' | 'MOBILE';
45
44
 
46
45
  type Tagged<A, T> = A & { __tag?: T };