libphonenumber-js 1.10.45 → 1.10.47

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 (84) hide show
  1. package/README.md +15 -2
  2. package/build/AsYouType.js +5 -3
  3. package/build/AsYouType.js.map +1 -1
  4. package/build/formatIncompletePhoneNumber.js +5 -5
  5. package/build/formatIncompletePhoneNumber.js.map +1 -1
  6. package/build/formatIncompletePhoneNumber.test.js +8 -0
  7. package/build/formatIncompletePhoneNumber.test.js.map +1 -1
  8. package/build/helpers/getNumberType.js +4 -3
  9. package/build/helpers/getNumberType.js.map +1 -1
  10. package/build/helpers/getNumberType.test.js +5 -0
  11. package/build/helpers/getNumberType.test.js.map +1 -1
  12. package/build/helpers/isObject.js +12 -0
  13. package/build/helpers/isObject.js.map +1 -0
  14. package/build/isValid.test.js +3 -0
  15. package/build/isValid.test.js.map +1 -1
  16. package/build/legacy/format.js +4 -12
  17. package/build/legacy/format.js.map +1 -1
  18. package/build/legacy/getNumberType.js +5 -13
  19. package/build/legacy/getNumberType.js.map +1 -1
  20. package/build/metadata.js +5 -11
  21. package/build/metadata.js.map +1 -1
  22. package/build/normalizeArguments.js +5 -10
  23. package/build/normalizeArguments.js.map +1 -1
  24. package/bundle/libphonenumber-js.min.js +1 -1
  25. package/bundle/libphonenumber-js.min.js.map +1 -1
  26. package/bundle/libphonenumber-max.js +1 -1
  27. package/bundle/libphonenumber-max.js.map +1 -1
  28. package/bundle/libphonenumber-min.js +1 -1
  29. package/bundle/libphonenumber-min.js.map +1 -1
  30. package/bundle/libphonenumber-mobile.js +1 -1
  31. package/bundle/libphonenumber-mobile.js.map +1 -1
  32. package/core/index.d.cts +1 -1
  33. package/core/index.d.ts +1 -1
  34. package/custom.d.ts +1 -1
  35. package/es6/AsYouType.js +2 -3
  36. package/es6/AsYouType.js.map +1 -1
  37. package/es6/formatIncompletePhoneNumber.js +5 -5
  38. package/es6/formatIncompletePhoneNumber.js.map +1 -1
  39. package/es6/formatIncompletePhoneNumber.test.js +8 -0
  40. package/es6/formatIncompletePhoneNumber.test.js.map +1 -1
  41. package/es6/helpers/getNumberType.js +4 -3
  42. package/es6/helpers/getNumberType.js.map +1 -1
  43. package/es6/helpers/getNumberType.test.js +5 -0
  44. package/es6/helpers/getNumberType.test.js.map +1 -1
  45. package/es6/helpers/isObject.js +5 -0
  46. package/es6/helpers/isObject.js.map +1 -0
  47. package/es6/isValid.test.js +3 -0
  48. package/es6/isValid.test.js.map +1 -1
  49. package/es6/legacy/format.js +3 -12
  50. package/es6/legacy/format.js.map +1 -1
  51. package/es6/legacy/getNumberType.js +4 -12
  52. package/es6/legacy/getNumberType.js.map +1 -1
  53. package/es6/metadata.js +5 -12
  54. package/es6/metadata.js.map +1 -1
  55. package/es6/normalizeArguments.js +3 -10
  56. package/es6/normalizeArguments.js.map +1 -1
  57. package/index.d.cts +1 -0
  58. package/index.d.ts +1 -0
  59. package/max/index.d.cts +1 -1
  60. package/max/index.d.ts +1 -1
  61. package/metadata.full.json +1 -1
  62. package/metadata.full.json.js +1 -1
  63. package/metadata.max.json +1 -1
  64. package/metadata.max.json.js +1 -1
  65. package/metadata.min.json +1 -1
  66. package/metadata.min.json.js +1 -1
  67. package/metadata.mobile.json +1 -1
  68. package/metadata.mobile.json.js +1 -1
  69. package/min/index.d.cts +1 -1
  70. package/min/index.d.ts +1 -1
  71. package/mobile/index.d.cts +1 -1
  72. package/mobile/index.d.ts +1 -1
  73. package/package.json +1 -1
  74. package/source/AsYouType.js +2 -1
  75. package/source/formatIncompletePhoneNumber.js +5 -5
  76. package/source/formatIncompletePhoneNumber.test.js +5 -0
  77. package/source/helpers/getNumberType.js +4 -3
  78. package/source/helpers/getNumberType.test.js +8 -0
  79. package/source/helpers/isObject.js +5 -0
  80. package/source/isValid.test.js +4 -0
  81. package/source/legacy/format.js +3 -7
  82. package/source/legacy/getNumberType.js +4 -8
  83. package/source/metadata.js +4 -8
  84. package/source/normalizeArguments.js +3 -5
package/min/index.d.cts CHANGED
@@ -75,7 +75,7 @@ export function getExtPrefix(countryCode: CountryCode): string;
75
75
 
76
76
  export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
77
77
 
78
- export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
78
+ export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
79
79
  export function parseIncompletePhoneNumber(text: string): string;
80
80
  export function parsePhoneNumberCharacter(character: string): string;
81
81
  export function parseDigits(character: string): string;
package/min/index.d.ts CHANGED
@@ -75,7 +75,7 @@ export function getExtPrefix(countryCode: CountryCode): string;
75
75
 
76
76
  export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
77
77
 
78
- export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
78
+ export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
79
79
  export function parseIncompletePhoneNumber(text: string): string;
80
80
  export function parsePhoneNumberCharacter(character: string): string;
81
81
  export function parseDigits(character: string): string;
@@ -75,7 +75,7 @@ export function getExtPrefix(countryCode: CountryCode): string;
75
75
 
76
76
  export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
77
77
 
78
- export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
78
+ export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
79
79
  export function parseIncompletePhoneNumber(text: string): string;
80
80
  export function parsePhoneNumberCharacter(character: string): string;
81
81
  export function parseDigits(character: string): string;
package/mobile/index.d.ts CHANGED
@@ -75,7 +75,7 @@ export function getExtPrefix(countryCode: CountryCode): string;
75
75
 
76
76
  export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
77
77
 
78
- export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
78
+ export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
79
79
  export function parseIncompletePhoneNumber(text: string): string;
80
80
  export function parsePhoneNumberCharacter(character: string): string;
81
81
  export function parseDigits(character: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.10.45",
3
+ "version": "1.10.47",
4
4
  "description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",
5
5
  "main": "index.cjs",
6
6
  "module": "index.js",
@@ -5,6 +5,7 @@ import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.js'
5
5
  import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser.js'
6
6
  import getCountryByCallingCode from './helpers/getCountryByCallingCode.js'
7
7
  import getCountryByNationalNumber from './helpers/getCountryByNationalNumber.js'
8
+ import isObject from './helpers/isObject.js'
8
9
 
9
10
  const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
10
11
 
@@ -30,7 +31,7 @@ export default class AsYouType {
30
31
  let defaultCallingCode
31
32
  // Turns out `null` also has type "object". Weird.
32
33
  if (optionsOrDefaultCountry) {
33
- if (typeof optionsOrDefaultCountry === 'object') {
34
+ if (isObject(optionsOrDefaultCountry)) {
34
35
  defaultCountry = optionsOrDefaultCountry.defaultCountry
35
36
  defaultCallingCode = optionsOrDefaultCountry.defaultCallingCode
36
37
  } else {
@@ -5,13 +5,13 @@ import AsYouType from './AsYouType.js'
5
5
  * The phone number can be either in E.164 format
6
6
  * or in a form of national number digits.
7
7
  * @param {string} value - A possibly incomplete phone number. Either in E.164 format or in a form of national number digits.
8
- * @param {string?} country - Two-letter ("ISO 3166-1 alpha-2") country code.
8
+ * @param {string|object} [optionsOrDefaultCountry] - A two-letter ("ISO 3166-1 alpha-2") country code, or an object of shape `{ defaultCountry?: string, defaultCallingCode?: string }`.
9
9
  * @return {string} Formatted (possibly incomplete) phone number.
10
10
  */
11
- export default function formatIncompletePhoneNumber(value, country, metadata) {
11
+ export default function formatIncompletePhoneNumber(value, optionsOrDefaultCountry, metadata) {
12
12
  if (!metadata) {
13
- metadata = country
14
- country = undefined
13
+ metadata = optionsOrDefaultCountry
14
+ optionsOrDefaultCountry = undefined
15
15
  }
16
- return new AsYouType(country, metadata).input(value)
16
+ return new AsYouType(optionsOrDefaultCountry, metadata).input(value)
17
17
  }
@@ -18,4 +18,9 @@ describe('formatIncompletePhoneNumber', () => {
18
18
  // International input, with country.
19
19
  formatIncompletePhoneNumber('+780055535', 'RU', metadata).should.equal('+7 800 555 35')
20
20
  })
21
+
22
+ it('should support an object argument', () => {
23
+ formatIncompletePhoneNumber('880055535', { defaultCountry: 'RU' }, metadata).should.equal('8 (800) 555-35')
24
+ formatIncompletePhoneNumber('880055535', { defaultCallingCode: '7' }, metadata).should.equal('8 (800) 555-35')
25
+ })
21
26
  })
@@ -20,9 +20,10 @@ export default function getNumberType(input, options, metadata)
20
20
  // code coverage would decrease for some weird reason.
21
21
  options = options || {}
22
22
 
23
- // When `parse()` returned `{}`
24
- // meaning that the phone number is not a valid one.
25
- if (!input.country) {
23
+ // When `parse()` returns an empty object — `{}` —
24
+ // that means that the phone number is malformed,
25
+ // so it can't possibly be valid.
26
+ if (!input.country && !input.countryCallingCode) {
26
27
  return
27
28
  }
28
29
 
@@ -15,4 +15,12 @@ describe('getNumberType', function() {
15
15
  oldMetadata
16
16
  ).should.equal('FIXED_LINE_OR_MOBILE')
17
17
  })
18
+
19
+ it('should return `undefined` when the phone number is a malformed one', function() {
20
+ expect(getNumberType(
21
+ {},
22
+ { v2: true },
23
+ oldMetadata
24
+ )).to.equal(undefined)
25
+ })
18
26
  })
@@ -0,0 +1,5 @@
1
+ const objectConstructor = {}.constructor;
2
+
3
+ export default function isObject(object) {
4
+ return object !== undefined && object !== null && object.constructor === objectConstructor;
5
+ }
@@ -113,4 +113,8 @@ describe('validate', () => {
113
113
  // National
114
114
  isValidNumber('88005553535 x123', { defaultCountry: 'RU' }).should.equal(true)
115
115
  })
116
+
117
+ it('should validate non-geographic toll-free phone numbers', () => {
118
+ isValidNumber('+80074454123').should.equal(true)
119
+ })
116
120
  })
@@ -1,5 +1,6 @@
1
1
  import _formatNumber from '../format.js'
2
2
  import parse from '../parse.js'
3
+ import isObject from '../helpers/isObject.js'
3
4
 
4
5
  export default function formatNumber() {
5
6
  const {
@@ -72,7 +73,7 @@ function normalizeArguments(args)
72
73
  }
73
74
  // If the phone number is passed as a parsed number object.
74
75
  // `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
75
- else if (is_object(arg_1))
76
+ else if (isObject(arg_1))
76
77
  {
77
78
  input = arg_1
78
79
  format = arg_2
@@ -102,9 +103,4 @@ function normalizeArguments(args)
102
103
  options,
103
104
  metadata
104
105
  }
105
- }
106
-
107
- // Babel transforms `typeof` into some "branches"
108
- // so istanbul will show this as "branch not covered".
109
- /* istanbul ignore next */
110
- const is_object = _ => typeof _ === 'object'
106
+ }
@@ -1,5 +1,6 @@
1
1
  import isViablePhoneNumber from '../helpers/isViablePhoneNumber.js'
2
2
  import _getNumberType from '../helpers/getNumberType.js'
3
+ import isObject from '../helpers/isObject.js'
3
4
  import parse from '../parse.js'
4
5
 
5
6
  // Finds out national phone number type (fixed line, mobile, etc)
@@ -28,7 +29,7 @@ export function normalizeArguments(args)
28
29
  // If "default country" argument is being passed
29
30
  // then convert it to an `options` object.
30
31
  // `getNumberType('88005553535', 'RU', metadata)`.
31
- if (typeof arg_2 !== 'object')
32
+ if (!isObject(arg_2))
32
33
  {
33
34
  if (arg_4)
34
35
  {
@@ -86,7 +87,7 @@ export function normalizeArguments(args)
86
87
  }
87
88
  // If the phone number is passed as a parsed phone number.
88
89
  // `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`.
89
- else if (is_object(arg_1))
90
+ else if (isObject(arg_1))
90
91
  {
91
92
  input = arg_1
92
93
 
@@ -107,9 +108,4 @@ export function normalizeArguments(args)
107
108
  options,
108
109
  metadata
109
110
  }
110
- }
111
-
112
- // Babel transforms `typeof` into some "branches"
113
- // so istanbul will show this as "branch not covered".
114
- /* istanbul ignore next */
115
- const is_object = _ => typeof _ === 'object'
111
+ }
@@ -1,4 +1,5 @@
1
1
  import compare from './tools/semver-compare.js'
2
+ import isObject from './helpers/isObject.js'
2
3
 
3
4
  // Added "possibleLengths" and renamed
4
5
  // "country_phone_code_to_countries" to "country_calling_codes".
@@ -458,20 +459,15 @@ export function validateMetadata(metadata) {
458
459
 
459
460
  // `country_phone_code_to_countries` was renamed to
460
461
  // `country_calling_codes` in `1.0.18`.
461
- if (!is_object(metadata) || !is_object(metadata.countries)) {
462
- throw new Error(`[libphonenumber-js] \`metadata\` argument was passed but it's not a valid metadata. Must be an object having \`.countries\` child object property. Got ${is_object(metadata) ? 'an object of shape: { ' + Object.keys(metadata).join(', ') + ' }' : 'a ' + type_of(metadata) + ': ' + metadata}.`)
462
+ if (!isObject(metadata) || !isObject(metadata.countries)) {
463
+ throw new Error(`[libphonenumber-js] \`metadata\` argument was passed but it's not a valid metadata. Must be an object having \`.countries\` child object property. Got ${isObject(metadata) ? 'an object of shape: { ' + Object.keys(metadata).join(', ') + ' }' : 'a ' + typeOf(metadata) + ': ' + metadata}.`)
463
464
  }
464
465
  }
465
466
 
466
467
  // Babel transforms `typeof` into some "branches"
467
468
  // so istanbul will show this as "branch not covered".
468
469
  /* istanbul ignore next */
469
- const is_object = _ => typeof _ === 'object'
470
-
471
- // Babel transforms `typeof` into some "branches"
472
- // so istanbul will show this as "branch not covered".
473
- /* istanbul ignore next */
474
- const type_of = _ => typeof _
470
+ const typeOf = _ => typeof _
475
471
 
476
472
  /**
477
473
  * Returns extension prefix for a country.
@@ -1,3 +1,5 @@
1
+ import isObject from './helpers/isObject.js'
2
+
1
3
  // Extracts the following properties from function arguments:
2
4
  // * input `text`
3
5
  // * `options` object
@@ -50,8 +52,4 @@ export default function normalizeArguments(args) {
50
52
  options,
51
53
  metadata
52
54
  }
53
- }
54
-
55
- // Otherwise istanbul would show this as "branch not covered".
56
- /* istanbul ignore next */
57
- const isObject = _ => typeof _ === 'object'
55
+ }