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
@@ -0,0 +1,6 @@
1
+ import { withMetadata } from '../metadata'
2
+ import { validatePhoneNumberLength as _validatePhoneNumberLength } from '../../core/index'
3
+
4
+ export function validatePhoneNumberLength() {
5
+ return withMetadata(_validatePhoneNumberLength, arguments)
6
+ }
@@ -42,6 +42,10 @@ exports.isPossiblePhoneNumber = function isPossiblePhoneNumber() {
42
42
  return call(core.isPossiblePhoneNumber, arguments)
43
43
  }
44
44
 
45
+ exports.validatePhoneNumberLength = function validatePhoneNumberLength() {
46
+ return call(core.validatePhoneNumberLength, arguments)
47
+ }
48
+
45
49
  exports.findNumbers = function findNumbers() {
46
50
  return call(core.findNumbers, arguments)
47
51
  }
package/min/index.d.ts CHANGED
@@ -11,7 +11,8 @@ import {
11
11
  NumberFoundLegacy,
12
12
  NumberFound,
13
13
  NumberType,
14
- NumberFormat
14
+ NumberFormat,
15
+ ValidatePhoneNumberLengthResult
15
16
  } from '../types';
16
17
 
17
18
  // They say this re-export is required.
@@ -29,7 +30,8 @@ export {
29
30
  NumberFoundLegacy,
30
31
  NumberFound,
31
32
  NumberType,
32
- NumberFormat
33
+ NumberFormat,
34
+ ValidatePhoneNumberLengthResult
33
35
  };
34
36
 
35
37
  // `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
@@ -45,6 +47,7 @@ export default parsePhoneNumberFromString;
45
47
 
46
48
  export function isValidPhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): boolean;
47
49
  export function isPossiblePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): boolean;
50
+ export function validatePhoneNumberLength(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): ValidatePhoneNumberLengthResult | undefined;
48
51
 
49
52
  export function findNumbers(text: string, options?: CountryCode): NumberFoundLegacy[];
50
53
  export function searchNumbers(text: string, options?: CountryCode): IterableIterator<NumberFoundLegacy>;
package/min/index.js CHANGED
@@ -6,6 +6,7 @@ export { parsePhoneNumberFromString, parsePhoneNumberFromString as default } fro
6
6
 
7
7
  export { isValidPhoneNumber } from './exports/isValidPhoneNumber'
8
8
  export { isPossiblePhoneNumber } from './exports/isPossiblePhoneNumber'
9
+ export { validatePhoneNumberLength } from './exports/validatePhoneNumberLength'
9
10
 
10
11
  // Deprecated.
11
12
  export { findNumbers } from './exports/findNumbers'
@@ -0,0 +1,6 @@
1
+ import { withMetadata } from '../metadata'
2
+ import { validatePhoneNumberLength as _validatePhoneNumberLength } from '../../core/index'
3
+
4
+ export function validatePhoneNumberLength() {
5
+ return withMetadata(_validatePhoneNumberLength, arguments)
6
+ }
@@ -42,6 +42,10 @@ exports.isPossiblePhoneNumber = function isPossiblePhoneNumber() {
42
42
  return call(core.isPossiblePhoneNumber, arguments)
43
43
  }
44
44
 
45
+ exports.validatePhoneNumberLength = function validatePhoneNumberLength() {
46
+ return call(core.validatePhoneNumberLength, arguments)
47
+ }
48
+
45
49
  exports.findNumbers = function findNumbers() {
46
50
  return call(core.findNumbers, arguments)
47
51
  }
package/mobile/index.js CHANGED
@@ -6,6 +6,7 @@ export { parsePhoneNumberFromString, parsePhoneNumberFromString as default } fro
6
6
 
7
7
  export { isValidPhoneNumber } from './exports/isValidPhoneNumber'
8
8
  export { isPossiblePhoneNumber } from './exports/isPossiblePhoneNumber'
9
+ export { validatePhoneNumberLength } from './exports/validatePhoneNumberLength'
9
10
 
10
11
  // Deprecated.
11
12
  export { findNumbers } from './exports/findNumbers'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.9.22",
3
+ "version": "1.9.26",
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",
@@ -362,10 +362,12 @@ describe('AsYouType', () => {
362
362
  // formatter.input('+1 1 877 215 5230').should.equal('+1 1 8772155230')
363
363
  formatter.getNationalNumber().should.equal('8772155230')
364
364
 
365
- formatter = new AsYouType()
366
- formatter.input('+78800555353').should.equal('+7 880 055 53 53')
367
- formatter.input('5').should.equal('+7 8 800 555 35 35')
368
- formatter.getNationalNumber().should.equal('8005553535')
365
+ // They've added another number format that has `8` leading digit
366
+ // and 14 digits. Maybe it's something related to Kazakhstan.
367
+ // formatter = new AsYouType()
368
+ // formatter.input('+78800555353').should.equal('+7 880 055 53 53')
369
+ // formatter.input('5').should.equal('+7 8 800 555 35 35')
370
+ // formatter.getNationalNumber().should.equal('8005553535')
369
371
  })
370
372
 
371
373
  it('should return a partial template for current value', () => {
@@ -923,10 +925,10 @@ describe('AsYouType', () => {
923
925
 
924
926
  it('shouldn\'t choose a format when there\'re too many digits for any of them', () => {
925
927
  const formatter = new AsYouType('RU')
926
- formatter.input('88005553535')
928
+ formatter.input('89991112233')
927
929
  formatter.formatter.chosenFormat.format().should.equal('$1 $2-$3-$4')
928
930
  formatter.reset()
929
- formatter.input('880055535355')
931
+ formatter.input('899911122334')
930
932
  expect(formatter.formatter.chosenFormat).to.be.undefined
931
933
  })
932
934
 
@@ -47,6 +47,25 @@ export const VALID_PHONE_NUMBER =
47
47
  VALID_DIGITS +
48
48
  ']*'
49
49
 
50
+ // This regular expression isn't present in Google's `libphonenumber`
51
+ // and is only used to determine whether the phone number being input
52
+ // is too short for it to even consider it a "valid" number.
53
+ // This is just a way to differentiate between a really invalid phone
54
+ // number like "abcde" and a valid phone number that a user has just
55
+ // started inputting, like "+1" or "1": both these cases would be
56
+ // considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
57
+ // library can provide a more detailed error message — whether it's
58
+ // really "not a number", or is it just a start of a valid phone number.
59
+ const VALID_PHONE_NUMBER_START_REG_EXP = new RegExp(
60
+ '^' +
61
+ '[' + PLUS_CHARS + ']{0,1}' +
62
+ '(?:' +
63
+ '[' + VALID_PUNCTUATION + ']*' +
64
+ '[' + VALID_DIGITS + ']' +
65
+ '){1,2}' +
66
+ '$'
67
+ , 'i')
68
+
50
69
  export const VALID_PHONE_NUMBER_WITH_EXTENSION =
51
70
  VALID_PHONE_NUMBER +
52
71
  // Phone number extensions
@@ -76,4 +95,14 @@ const VALID_PHONE_NUMBER_PATTERN = new RegExp(
76
95
  export default function isViablePhoneNumber(number) {
77
96
  return number.length >= MIN_LENGTH_FOR_NSN &&
78
97
  VALID_PHONE_NUMBER_PATTERN.test(number)
98
+ }
99
+
100
+ // This is just a way to differentiate between a really invalid phone
101
+ // number like "abcde" and a valid phone number that a user has just
102
+ // started inputting, like "+1" or "1": both these cases would be
103
+ // considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
104
+ // library can provide a more detailed error message — whether it's
105
+ // really "not a number", or is it just a start of a valid phone number.
106
+ export function isViablePhoneNumberStart(number) {
107
+ return VALID_PHONE_NUMBER_START_REG_EXP.test(number)
79
108
  }
@@ -119,10 +119,10 @@ describe('parse', () => {
119
119
  })
120
120
 
121
121
  // Too long.
122
- parseNumber('+7 (800) 55-35-35-555', { extended: true }).should.deep.equal({
122
+ parseNumber('+1 213 37342530', { extended: true }).should.deep.equal({
123
123
  country : undefined,
124
- countryCallingCode : '7',
125
- phone : '00553535555',
124
+ countryCallingCode : '1',
125
+ phone : '21337342530',
126
126
  carrierCode : undefined,
127
127
  ext : undefined,
128
128
  valid : false,
@@ -67,9 +67,11 @@ describe('parsePhoneNumber', () => {
67
67
 
68
68
  it('should throw parse errors', () => {
69
69
  expect(() => parsePhoneNumber('8005553535', 'XX')).to.throw('INVALID_COUNTRY')
70
- expect(() => parsePhoneNumber('8', 'RU')).to.throw('NOT_A_NUMBER')
71
- // Won't throw here because the regexp already demands length > 1.
72
- // expect(() => parsePhoneNumber('11', 'RU')).to.throw('TOO_SHORT')
70
+ expect(() => parsePhoneNumber('+', 'RU')).to.throw('NOT_A_NUMBER')
71
+ expect(() => parsePhoneNumber('a', 'RU')).to.throw('NOT_A_NUMBER')
72
+ expect(() => parsePhoneNumber('1', 'RU')).to.throw('TOO_SHORT')
73
+ expect(() => parsePhoneNumber('+4')).to.throw('TOO_SHORT')
74
+ expect(() => parsePhoneNumber('+44')).to.throw('TOO_SHORT')
73
75
  expect(() => parsePhoneNumber('+443')).to.throw('TOO_SHORT')
74
76
  expect(() => parsePhoneNumber('+370')).to.throw('TOO_SHORT')
75
77
  expect(() => parsePhoneNumber('88888888888888888888', 'RU')).to.throw('TOO_LONG')
package/source/parse_.js CHANGED
@@ -12,7 +12,7 @@ import {
12
12
 
13
13
  import ParseError from './ParseError'
14
14
  import Metadata from './metadata'
15
- import isViablePhoneNumber from './helpers/isViablePhoneNumber'
15
+ import isViablePhoneNumber, { isViablePhoneNumberStart } from './helpers/isViablePhoneNumber'
16
16
  import extractExtension from './helpers/extension/extractExtension'
17
17
  import parseIncompletePhoneNumber from './parseIncompletePhoneNumber'
18
18
  import getCountryCallingCode from './getCountryCallingCode'
@@ -67,11 +67,14 @@ export default function parse(text, options, metadata) {
67
67
  }
68
68
 
69
69
  // Parse the phone number.
70
- const { number: formattedPhoneNumber, ext } = parseInput(text, options.v2, options.extract)
70
+ const { number: formattedPhoneNumber, ext, error } = parseInput(text, options.v2, options.extract)
71
71
 
72
72
  // If the phone number is not viable then return nothing.
73
73
  if (!formattedPhoneNumber) {
74
74
  if (options.v2) {
75
+ if (error === 'TOO_SHORT') {
76
+ throw new ParseError('TOO_SHORT')
77
+ }
75
78
  throw new ParseError('NOT_A_NUMBER')
76
79
  }
77
80
  return {}
@@ -220,7 +223,13 @@ function parseInput(text, v2, extract) {
220
223
  }
221
224
  let number = extractFormattedPhoneNumber(text, extract, v2)
222
225
  // If the phone number is not viable, then abort.
223
- if (!number || !isViablePhoneNumber(number)) {
226
+ if (!number) {
227
+ return {}
228
+ }
229
+ if (!isViablePhoneNumber(number)) {
230
+ if (isViablePhoneNumberStart(number)) {
231
+ return { error: 'TOO_SHORT' }
232
+ }
224
233
  return {}
225
234
  }
226
235
  // Attempt to parse extension first, since it doesn't require region-specific
@@ -0,0 +1,31 @@
1
+ import { normalizeArguments } from './parsePhoneNumber'
2
+ import parsePhoneNumber from './parsePhoneNumber_'
3
+ import ParseError from './ParseError'
4
+ import Metadata from './metadata'
5
+ import checkNumberLength from './helpers/checkNumberLength'
6
+
7
+ export default function validatePhoneNumberLength() {
8
+ let { text, options, metadata } = normalizeArguments(arguments)
9
+ options = {
10
+ ...options,
11
+ extract: false
12
+ }
13
+
14
+ // Parse phone number.
15
+ try {
16
+ const phoneNumber = parsePhoneNumber(text, options, metadata)
17
+ metadata = new Metadata(metadata)
18
+ metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
19
+ const result = checkNumberLength(phoneNumber.nationalNumber, metadata)
20
+ if (result !== 'IS_POSSIBLE') {
21
+ return result
22
+ }
23
+ } catch (error) {
24
+ /* istanbul ignore else */
25
+ if (error instanceof ParseError) {
26
+ return error.message
27
+ } else {
28
+ throw error
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,30 @@
1
+ import _validatePhoneNumberLength from './validatePhoneNumberLength'
2
+ import metadata from '../metadata.min.json'
3
+
4
+ function validatePhoneNumberLength(...parameters) {
5
+ parameters.push(metadata)
6
+ return _validatePhoneNumberLength.apply(this, parameters)
7
+ }
8
+
9
+ describe('validatePhoneNumberLength', () => {
10
+ it('should detect whether a phone number length is valid', () => {
11
+ // Not a phone number.
12
+ validatePhoneNumberLength('+').should.equal('NOT_A_NUMBER')
13
+ validatePhoneNumberLength('abcde').should.equal('NOT_A_NUMBER')
14
+
15
+ // No country supplied for a national number.
16
+ validatePhoneNumberLength('123').should.equal('INVALID_COUNTRY')
17
+
18
+ // Too short while the number is not considered "viable"
19
+ // by Google's `libphonenumber`.
20
+ validatePhoneNumberLength('2', 'US').should.equal('TOO_SHORT')
21
+ validatePhoneNumberLength('+1', 'US').should.equal('TOO_SHORT')
22
+ validatePhoneNumberLength('+12', 'US').should.equal('TOO_SHORT')
23
+
24
+ // Test national (significant) number length.
25
+ validatePhoneNumberLength('444 1 44', 'TR').should.equal('TOO_SHORT')
26
+ expect(validatePhoneNumberLength('444 1 444', 'TR')).to.be.undefined
27
+ validatePhoneNumberLength('444 1 4444', 'TR').should.equal('INVALID_LENGTH')
28
+ validatePhoneNumberLength('444 1 4444444444', 'TR').should.equal('TOO_LONG')
29
+ })
30
+ })
package/types.d.ts CHANGED
@@ -91,3 +91,7 @@ export interface NumberFoundLegacy {
91
91
  export class ParseError {
92
92
  message: string;
93
93
  }
94
+
95
+ // The rationale for having a separate type for the result "enum" instead of just a `string`:
96
+ // https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
97
+ export type ValidatePhoneNumberLengthResult = 'INVALID_COUNTRY' | 'NOT_A_NUMBER' | 'TOO_SHORT' | 'TOO_LONG' | 'INVALID_LENGTH';