libphonenumber-js 1.9.22 → 1.9.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 (69) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +104 -5
  3. package/build/AsYouType.test.js +8 -7
  4. package/build/AsYouType.test.js.map +1 -1
  5. package/build/helpers/isViablePhoneNumber.js +22 -1
  6. package/build/helpers/isViablePhoneNumber.js.map +1 -1
  7. package/build/parse.test.js +3 -3
  8. package/build/parse.test.js.map +1 -1
  9. package/build/parsePhoneNumber.test.js +14 -4
  10. package/build/parsePhoneNumber.test.js.map +1 -1
  11. package/build/parse_.js +20 -3
  12. package/build/parse_.js.map +1 -1
  13. package/build/validatePhoneNumberLength.js +53 -0
  14. package/build/validatePhoneNumberLength.js.map +1 -0
  15. package/build/validatePhoneNumberLength.test.js +37 -0
  16. package/build/validatePhoneNumberLength.test.js.map +1 -0
  17. package/bundle/libphonenumber-js.min.js +1 -1
  18. package/bundle/libphonenumber-js.min.js.map +1 -1
  19. package/bundle/libphonenumber-max.js +1 -1
  20. package/bundle/libphonenumber-max.js.map +1 -1
  21. package/bundle/libphonenumber-min.js +1 -1
  22. package/bundle/libphonenumber-min.js.map +1 -1
  23. package/bundle/libphonenumber-mobile.js +1 -1
  24. package/bundle/libphonenumber-mobile.js.map +1 -1
  25. package/core/index.commonjs.js +1 -0
  26. package/core/index.d.ts +7 -2
  27. package/core/index.js +1 -0
  28. package/es6/AsYouType.test.js +8 -7
  29. package/es6/AsYouType.test.js.map +1 -1
  30. package/es6/helpers/isViablePhoneNumber.js +20 -1
  31. package/es6/helpers/isViablePhoneNumber.js.map +1 -1
  32. package/es6/parse.test.js +3 -3
  33. package/es6/parse.test.js.map +1 -1
  34. package/es6/parsePhoneNumber.test.js +14 -4
  35. package/es6/parsePhoneNumber.test.js.map +1 -1
  36. package/es6/parse_.js +18 -3
  37. package/es6/parse_.js.map +1 -1
  38. package/es6/validatePhoneNumberLength.js +39 -0
  39. package/es6/validatePhoneNumberLength.js.map +1 -0
  40. package/es6/validatePhoneNumberLength.test.js +32 -0
  41. package/es6/validatePhoneNumberLength.test.js.map +1 -0
  42. package/index.common.js +4 -0
  43. package/index.d.ts +5 -2
  44. package/index.es6.js +1 -0
  45. package/max/exports/validatePhoneNumberLength.js +6 -0
  46. package/max/index.commonjs.js +4 -0
  47. package/max/index.js +1 -0
  48. package/metadata.full.json +1 -1
  49. package/metadata.full.json.js +1 -1
  50. package/metadata.min.json +1 -1
  51. package/metadata.min.json.js +1 -1
  52. package/metadata.mobile.json +1 -1
  53. package/metadata.mobile.json.js +1 -1
  54. package/min/exports/validatePhoneNumberLength.js +6 -0
  55. package/min/index.commonjs.js +4 -0
  56. package/min/index.d.ts +5 -2
  57. package/min/index.js +1 -0
  58. package/mobile/exports/validatePhoneNumberLength.js +6 -0
  59. package/mobile/index.commonjs.js +4 -0
  60. package/mobile/index.js +1 -0
  61. package/package.json +1 -1
  62. package/source/AsYouType.test.js +8 -6
  63. package/source/helpers/isViablePhoneNumber.js +29 -0
  64. package/source/parse.test.js +3 -3
  65. package/source/parsePhoneNumber.test.js +5 -3
  66. package/source/parse_.js +12 -3
  67. package/source/validatePhoneNumberLength.js +31 -0
  68. package/source/validatePhoneNumberLength.test.js +30 -0
  69. package/types.d.ts +4 -0
package/CHANGELOG.md CHANGED
@@ -73,6 +73,22 @@ And edit the README:
73
73
 
74
74
  This changelog [only](https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/16#note_594165443) mentions the changes in the code. See [Google's changelog](https://github.com/google/libphonenumber/blob/master/release_notes.txt) for metadata changes.
75
75
 
76
+ 1.9.26 / 05.09.2021
77
+ ==================
78
+
79
+ * [Added](https://github.com/catamphetamine/libphonenumber-js/issues/406) `validatePhoneNumberLength()` function: same as `isPossiblePhoneNumber()` but tells the actual reason why a phone number is not possible: `TOO_SHORT`, `TOO_LONG`, `INVALID_LENGTH`, etc.
80
+
81
+ ```js
82
+ validatePhoneNumberLength('abcde') === 'NOT_A_NUMBER'
83
+ validatePhoneNumberLength('444 1 44') === 'INVALID_COUNTRY'
84
+ validatePhoneNumberLength('444 1 44', 'TR') === 'TOO_SHORT'
85
+ validatePhoneNumberLength('444 1 444', 'TR') === undefined
86
+ validatePhoneNumberLength('444 1 4444', 'TR') === 'INVALID_LENGTH'
87
+ validatePhoneNumberLength('444 1 44444', 'TR') === 'INVALID_LENGTH'
88
+ validatePhoneNumberLength('444 1 444444', 'TR') === undefined
89
+ validatePhoneNumberLength('444 1 4444444444', 'TR') === 'TOO_LONG'
90
+ ```
91
+
76
92
  1.9.20 / 07.06.2021
77
93
  ==================
78
94
 
package/README.md CHANGED
@@ -90,6 +90,55 @@ If you're not using a bundler then use a [standalone version from a CDN](https:/
90
90
 
91
91
  ## Use
92
92
 
93
+ <!--
94
+ ### Importing
95
+
96
+ When using [Babel](https://babeljs.io/) or [`esm`](npmjs.com/package/esm) or [rollup.js](https://rollupjs.org/) or any other `import` transpiler:
97
+
98
+ ```js
99
+ import parsePhoneNumber, {
100
+ isPossiblePhoneNumber,
101
+ isValidPhoneNumber,
102
+ AsYouType,
103
+ findPhoneNumbersInText
104
+ } from 'libphonenumber-js'
105
+ ```
106
+
107
+ When using Node.js 14 and not using any of the above:
108
+
109
+ ```js
110
+ import parsePhoneNumber from 'libphonenumber-js'
111
+
112
+ // or
113
+
114
+ import libphonenumber from 'libphonenumber-js'
115
+
116
+ const {
117
+ default: parsePhoneNumber,
118
+ isPossiblePhoneNumber,
119
+ isValidPhoneNumber,
120
+ AsYouType,
121
+ findPhoneNumbersInText
122
+ } = libphonenumber
123
+ ```
124
+
125
+ When using Node.js < 14 and not using any of the above:
126
+
127
+ ```js
128
+ const libphonenumber = require('libphonenumber-js')
129
+
130
+ const {
131
+ default: parsePhoneNumber,
132
+ isPossiblePhoneNumber,
133
+ isValidPhoneNumber,
134
+ AsYouType,
135
+ findPhoneNumbersInText
136
+ } = libphonenumber
137
+ ```
138
+
139
+ https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/42
140
+ -->
141
+
93
142
  ### Parse phone number
94
143
 
95
144
  <!--
@@ -125,14 +174,23 @@ parseNumber('Phone: 8 (800) 555 35 35.', 'RU')
125
174
  ### Validate phone number
126
175
 
127
176
  ```js
128
- import { isPossiblePhoneNumber, isValidPhoneNumber } from 'libphonenumber-js'
177
+ import {
178
+ isPossiblePhoneNumber,
179
+ isValidPhoneNumber,
180
+ validatePhoneNumberLength
181
+ } from 'libphonenumber-js'
129
182
 
130
183
  isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
131
184
  isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
185
+
186
+ validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
187
+ validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined
132
188
  ```
133
189
 
134
190
  `isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and the actual phone number digits.
135
191
 
192
+ `validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, it returns the actual reason: `TOO_SHORT`, `TOO_LONG`, etc.
193
+
136
194
  ### Format phone number
137
195
 
138
196
  <!--
@@ -330,13 +388,17 @@ try {
330
388
  <details>
331
389
  <summary>Possible errors</summary>
332
390
 
333
- * `INVALID_COUNTRY` — When `defaultCountry` doesn't exist (`parsePhoneNumber('(111) 222-3333', 'XX')`) or isn't supported by this library, or when parsing non-international number without a `defaultCountry` (`parsePhoneNumber('(111) 222-3333')`), or when international number country calling code doesn't exist (`parsePhoneNumber('+9991112223333')`).
391
+ * `NOT_A_NUMBER` — When the supplied string is not a phone number. For example, when there are no digits: `"abcde"`, `"+"`.
334
392
 
335
- * `NOT_A_NUMBER` — When no phone number was found. For example, when there are no digits (`"abcde"`) or when there's not enough digits (`parsePhoneNumber('2', 'US')`, `parsePhoneNumber('+1')`).
393
+ * `INVALID_COUNTRY`
336
394
 
337
- * `TOO_LONG` — When national (significant) number is too long (17 digits max) or when the string being parsed is too long (250 characters max).
395
+ * When `defaultCountry` doesn't exist (or isn't supported by this library yet): `parsePhoneNumber('(111) 222-3333', 'XX')`.
396
+ * When parsing a non-international number without a `defaultCountry`: `parsePhoneNumber('(111) 222-3333')`.
397
+ * When an international number's country calling code doesn't exist: `parsePhoneNumber('+9991112223333')`.
338
398
 
339
- * `TOO_SHORT` — When national (significant) number is too short (for ex. 1 digit).
399
+ * `TOO_SHORT` — When the number is too short. For example, just 1 or 2 digits: `"1"`, `"+12"`.
400
+
401
+ * `TOO_LONG` — When the national (significant) number is too long (17 digits max) or when the string being parsed is too long (250 characters max).
340
402
  </details>
341
403
 
342
404
  #### Strictness
@@ -546,6 +608,43 @@ This function is just a shortcut for a two-step process of ["strictly"](#strictn
546
608
 
547
609
  See ["Using phone number validation feature"](#using-phone-number-validation-feature) for choosing between `isPossible()` and `isValid()`.
548
610
 
611
+ ### `validatePhoneNumberLength(input: string): string?`
612
+
613
+ Checks if `input` phone number length is valid. If it is, then nothing is returned. Otherwise, a rejection reason is returned.
614
+
615
+ <details>
616
+ <summary>Possible rejection reasons</summary>
617
+
618
+ * `NOT_A_NUMBER` — When the supplied string is not a phone number. For example, when there are no digits: `"abcde"`, `"+"`.
619
+
620
+ * `INVALID_COUNTRY`
621
+
622
+ * When `defaultCountry` doesn't exist (or isn't supported by this library yet): `parsePhoneNumber('(111) 222-3333', 'XX')`.
623
+ * When parsing a non-international number without a `defaultCountry`: `parsePhoneNumber('(111) 222-3333')`.
624
+ * When an international number's country calling code doesn't exist: `parsePhoneNumber('+9991112223333')`.
625
+
626
+ * `TOO_SHORT` — When the number is too short. For example, just 1 or 2 digits: `"1"`, `"+12"`.
627
+
628
+ * `TOO_LONG` — When the national (significant) number is too long (17 digits max) or when the string being parsed is too long (250 characters max).
629
+
630
+ * `INVALID_LENGTH` — When the national (significant) number is neither too short, nor too long, but somewhere in between and its length is still invalid.
631
+ </details>
632
+
633
+ ######
634
+
635
+ ```js
636
+ validatePhoneNumberLength('abcde') === 'NOT_A_NUMBER'
637
+ validatePhoneNumberLength('444 1 44') === 'INVALID_COUNTRY'
638
+ validatePhoneNumberLength('444 1 44', 'TR') === 'TOO_SHORT'
639
+ validatePhoneNumberLength('444 1 444', 'TR') === undefined
640
+ validatePhoneNumberLength('444 1 4444', 'TR') === 'INVALID_LENGTH'
641
+ validatePhoneNumberLength('444 1 44444', 'TR') === 'INVALID_LENGTH'
642
+ validatePhoneNumberLength('444 1 444444', 'TR') === undefined
643
+ validatePhoneNumberLength('444 1 4444444444', 'TR') === 'TOO_LONG'
644
+ ```
645
+
646
+ This function is just a more detailed version of `isPossiblePhoneNumber()` for those who've [asked](https://github.com/catamphetamine/libphonenumber-js/issues/406) for a more specific rejection reason.
647
+
549
648
  ### `class` AsYouType([options or defaultCountry])
550
649
 
551
650
  Creates a formatter for a partially entered phone number.
@@ -319,11 +319,12 @@ describe('AsYouType', function () {
319
319
  formatter = new AsYouType();
320
320
  formatter.input('+1 1 877 215 5230').should.equal('+1 1 877 215 5230'); // formatter.input('+1 1 877 215 5230').should.equal('+1 1 8772155230')
321
321
 
322
- formatter.getNationalNumber().should.equal('8772155230');
323
- formatter = new AsYouType();
324
- formatter.input('+78800555353').should.equal('+7 880 055 53 53');
325
- formatter.input('5').should.equal('+7 8 800 555 35 35');
326
- formatter.getNationalNumber().should.equal('8005553535');
322
+ formatter.getNationalNumber().should.equal('8772155230'); // They've added another number format that has `8` leading digit
323
+ // and 14 digits. Maybe it's something related to Kazakhstan.
324
+ // formatter = new AsYouType()
325
+ // formatter.input('+78800555353').should.equal('+7 880 055 53 53')
326
+ // formatter.input('5').should.equal('+7 8 800 555 35 35')
327
+ // formatter.getNationalNumber().should.equal('8005553535')
327
328
  });
328
329
  it('should return a partial template for current value', function () {
329
330
  var asYouType = new AsYouType('US');
@@ -815,10 +816,10 @@ describe('AsYouType', function () {
815
816
  });
816
817
  it('shouldn\'t choose a format when there\'re too many digits for any of them', function () {
817
818
  var formatter = new AsYouType('RU');
818
- formatter.input('88005553535');
819
+ formatter.input('89991112233');
819
820
  formatter.formatter.chosenFormat.format().should.equal('$1 $2-$3-$4');
820
821
  formatter.reset();
821
- formatter.input('880055535355');
822
+ formatter.input('899911122334');
822
823
  expect(formatter.formatter.chosenFormat).to.be.undefined;
823
824
  });
824
825
  it('should get separator after national prefix', function () {