libphonenumber-js 1.12.31 → 1.12.34

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 (58) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +3 -1
  3. package/build/format.js +18 -2
  4. package/build/format.js.map +1 -1
  5. package/build/format.test.js +3 -6
  6. package/build/format.test.js.map +1 -1
  7. package/build/helpers/getIddPrefix.js +2 -2
  8. package/build/helpers/getIddPrefix.js.map +1 -1
  9. package/build/legacy/format.test.js +3 -6
  10. package/build/legacy/format.test.js.map +1 -1
  11. package/bundle/libphonenumber-js.min.js +1 -1
  12. package/bundle/libphonenumber-js.min.js.map +1 -1
  13. package/bundle/libphonenumber-max.js +1 -1
  14. package/bundle/libphonenumber-max.js.map +1 -1
  15. package/bundle/libphonenumber-min.js +1 -1
  16. package/bundle/libphonenumber-min.js.map +1 -1
  17. package/bundle/libphonenumber-mobile.js +1 -1
  18. package/bundle/libphonenumber-mobile.js.map +1 -1
  19. package/core/index.d.cts +12 -4
  20. package/core/index.d.ts +12 -4
  21. package/custom.d.ts +7 -6
  22. package/es6/format.js +18 -2
  23. package/es6/format.js.map +1 -1
  24. package/es6/format.test.js +3 -6
  25. package/es6/format.test.js.map +1 -1
  26. package/es6/helpers/getIddPrefix.js +2 -2
  27. package/es6/helpers/getIddPrefix.js.map +1 -1
  28. package/es6/legacy/format.test.js +3 -6
  29. package/es6/legacy/format.test.js.map +1 -1
  30. package/index.d.cts +6 -4
  31. package/index.d.ts +6 -4
  32. package/max/index.d.cts +12 -4
  33. package/max/index.d.ts +12 -4
  34. package/max/metadata/metadata.max.json +1 -1
  35. package/max/metadata/metadata.max.json.js +1 -1
  36. package/metadata.full.json +1 -1
  37. package/metadata.full.json.js +1 -1
  38. package/metadata.max.json +1 -1
  39. package/metadata.max.json.js +1 -1
  40. package/metadata.min.json +1 -1
  41. package/metadata.min.json.js +1 -1
  42. package/metadata.mobile.json +1 -1
  43. package/metadata.mobile.json.js +1 -1
  44. package/min/index.d.cts +12 -4
  45. package/min/index.d.ts +12 -4
  46. package/min/metadata/metadata.min.json +1 -1
  47. package/min/metadata/metadata.min.json.js +1 -1
  48. package/mobile/index.d.cts +12 -4
  49. package/mobile/index.d.ts +12 -4
  50. package/mobile/metadata/metadata.mobile.json +1 -1
  51. package/mobile/metadata/metadata.mobile.json.js +1 -1
  52. package/package.json +1 -1
  53. package/source/format.js +17 -2
  54. package/source/format.test.js +3 -3
  55. package/source/helpers/getIddPrefix.js +2 -2
  56. package/source/legacy/format.test.js +3 -3
  57. package/types.d.cts +21 -10
  58. package/types.d.ts +21 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.12.31",
3
+ "version": "1.12.34",
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",
package/source/format.js CHANGED
@@ -45,6 +45,14 @@ export default function formatNumber(input, format, options, metadata) {
45
45
 
46
46
  metadata = new Metadata(metadata)
47
47
 
48
+ // Normally, the `input` object is supposed to be a `PhoneNumber` class instance.
49
+ // Also, according to the `PhoneNumber` class source code, `country` can't be "001".
50
+ // It means that normally `input.country` or `input.countryCallingCode` is supposed
51
+ // to be present because either of the two is always required to exist in a `PhoneNumber` instance.
52
+ // This means that realistically, it's gonna step into either the first `if`
53
+ // or the following `else if`, and normally it won't even reach the legacy-compatibility
54
+ // `else return input.phone || ''` part.
55
+ // So normally, it won't ever return an empty string here.
48
56
  if (input.country && input.country !== '001') {
49
57
  // Validate `input.country`.
50
58
  if (!metadata.hasCountry(input.country)) {
@@ -67,8 +75,10 @@ export default function formatNumber(input, format, options, metadata) {
67
75
 
68
76
  switch (format) {
69
77
  case 'NATIONAL':
70
- // Legacy argument support.
71
- // (`{ country: ..., phone: '' }`)
78
+ // Normally, the `input` object is supposed to be a `PhoneNumber` class instance,
79
+ // and a `PhoneNumber` class instance is always required to have a `nationalNumber`.
80
+ // This means that the `if (!nationalNumber)` below is just for legacy-compatibility
81
+ // and it normally can't really happen, so normally it won't ever return an empty string here.
72
82
  if (!nationalNumber) {
73
83
  return ''
74
84
  }
@@ -100,6 +110,7 @@ export default function formatNumber(input, format, options, metadata) {
100
110
  // Not saying that this IDD formatter replicates it 1:1, but it seems to work.
101
111
  // Who would even need to format phone numbers in IDD format anyway?
102
112
  case 'IDD':
113
+ // If the required `fromCountry` parameter is not passed, it will return `undefined`.
103
114
  if (!options.fromCountry) {
104
115
  return
105
116
  // throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')
@@ -111,6 +122,10 @@ export default function formatNumber(input, format, options, metadata) {
111
122
  options.fromCountry,
112
123
  metadata
113
124
  )
125
+ // If the country of the phone number doesn't support IDD calling, it will return `undefined`.
126
+ if (!formattedNumber) {
127
+ return
128
+ }
114
129
  return addExtension(formattedNumber, input.ext, metadata, options.formatExtension)
115
130
 
116
131
  default:
@@ -244,15 +244,15 @@ describe('format', () => {
244
244
 
245
245
  // Same country calling code.
246
246
  expect(
247
- formatNumber('+12133734253', 'IDD', { fromCountry: 'CA', humanReadable: true })
247
+ formatNumber('+12133734253', 'IDD', { fromCountry: 'CA' })
248
248
  ).to.equal('1 (213) 373-4253')
249
249
  expect(
250
- formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ', humanReadable: true })
250
+ formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ' })
251
251
  ).to.equal('8 (800) 555-35-35')
252
252
 
253
253
  // formatNumber('+78005553535', 'IDD', { fromCountry: 'US' }).should.equal('01178005553535')
254
254
  expect(
255
- formatNumber('+78005553535', 'IDD', { fromCountry: 'US', humanReadable: true })
255
+ formatNumber('+78005553535', 'IDD', { fromCountry: 'US' })
256
256
  ).to.equal('011 7 800 555 35 35')
257
257
  })
258
258
 
@@ -11,8 +11,8 @@ import Metadata from '../metadata.js'
11
11
  */
12
12
  const SINGLE_IDD_PREFIX_REG_EXP = /^[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?$/
13
13
 
14
- // For regions that have multiple IDD prefixes
15
- // a preferred IDD prefix is returned.
14
+ // If the `country` supports IDD calling, it returns a preferred IDD prefix.
15
+ // If the `country` doesn't support IDD calling, it returns `undefined`.
16
16
  export default function getIddPrefix(country, callingCode, metadata) {
17
17
  const countryMetadata = new Metadata(metadata)
18
18
  countryMetadata.selectNumberingPlan(country, callingCode)
@@ -217,15 +217,15 @@ describe('format', () => {
217
217
 
218
218
  // Same country calling code.
219
219
  expect(
220
- formatNumber('+12133734253', 'IDD', { fromCountry: 'CA', humanReadable: true })
220
+ formatNumber('+12133734253', 'IDD', { fromCountry: 'CA' })
221
221
  ).to.equal('1 (213) 373-4253')
222
222
  expect(
223
- formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ', humanReadable: true })
223
+ formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ' })
224
224
  ).to.equal('8 (800) 555-35-35')
225
225
 
226
226
  // formatNumber('+78005553535', 'IDD', { fromCountry: 'US' }).should.equal('01178005553535')
227
227
  expect(
228
- formatNumber('+78005553535', 'IDD', { fromCountry: 'US', humanReadable: true })
228
+ formatNumber('+78005553535', 'IDD', { fromCountry: 'US' })
229
229
  ).to.equal('011 7 800 555 35 35')
230
230
  })
231
231
 
package/types.d.cts CHANGED
@@ -91,19 +91,30 @@ export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
91
91
 
92
92
  type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
93
93
 
94
- export interface FormatNumberOptions {
95
- v2?: boolean;
96
- fromCountry?: CountryCode;
97
- humanReadable?: boolean;
94
+ // The options for the `format()` function when the format is "NATIONAL" or "INTERNATIONAL".
95
+ export interface FormatNumberOptionsForNationalOrInternational {
96
+ // Adds a national prefix in the formatted number.
97
+ // Works when formatting a "NATIONAL" or "INTERNATIONAL" phone number.
98
98
  nationalPrefix?: boolean;
99
+ // A function that appends an "extension" to a formatted phone number.
99
100
  formatExtension?: FormatExtension;
100
101
  }
101
102
 
102
- export interface FormatNumberOptionsWithoutIDD {
103
- v2?: boolean;
104
- formatExtension?: FormatExtension;
103
+ // The options for the `format()` function when the format is "IDD".
104
+ export interface FormatNumberOptionsForIDD extends FormatNumberOptionsForNationalOrInternational {
105
+ // A country from which the call will be made.
106
+ // It is only relevant when calling via "IDD" (International Direct Dialing)
107
+ // when the caller can use a special "00" prefix instead of using the "+" character.
108
+ fromCountry: CountryCode;
109
+ // `humanReadable` option was removed in version `1.8.6`.
110
+ // Now it's always `true` by default.
111
+ // humanReadable?: boolean;
105
112
  }
106
113
 
114
+ // All possible `options` for the `format()` function,
115
+ // regardless of the actual format being used.
116
+ export type FormatNumberOptions = FormatNumberOptionsForNationalOrInternational | FormatNumberOptionsForIDD
117
+
107
118
  // // https://stackoverflow.com/a/67026991
108
119
  // type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
109
120
 
@@ -124,9 +135,9 @@ export interface FormatNumberOptionsWithoutIDD {
124
135
  // isValid(): boolean;
125
136
  // getType(): NumberType;
126
137
  // format(format: NumberFormat, options?: FormatNumberOptions): string;
127
- // formatNational(options?: FormatNumberOptionsWithoutIDD): string;
128
- // formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
129
- // getURI(options?: FormatNumberOptionsWithoutIDD): string;
138
+ // formatNational(options?: FormatNumberOptionsForNationalOrInternational): string;
139
+ // formatInternational(options?: FormatNumberOptionsForNationalOrInternational): string;
140
+ // getURI(): string;
130
141
  // isNonGeographic(): boolean;
131
142
  // isEqual(phoneNumber: PhoneNumber): boolean;
132
143
  // }
package/types.d.ts CHANGED
@@ -91,19 +91,30 @@ export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
91
91
 
92
92
  type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
93
93
 
94
- export interface FormatNumberOptions {
95
- v2?: boolean;
96
- fromCountry?: CountryCode;
97
- humanReadable?: boolean;
94
+ // The options for the `format()` function when the format is "NATIONAL" or "INTERNATIONAL".
95
+ export interface FormatNumberOptionsForNationalOrInternational {
96
+ // Adds a national prefix in the formatted number.
97
+ // Works when formatting a "NATIONAL" or "INTERNATIONAL" phone number.
98
98
  nationalPrefix?: boolean;
99
+ // A function that appends an "extension" to a formatted phone number.
99
100
  formatExtension?: FormatExtension;
100
101
  }
101
102
 
102
- export interface FormatNumberOptionsWithoutIDD {
103
- v2?: boolean;
104
- formatExtension?: FormatExtension;
103
+ // The options for the `format()` function when the format is "IDD".
104
+ export interface FormatNumberOptionsForIDD extends FormatNumberOptionsForNationalOrInternational {
105
+ // A country from which the call will be made.
106
+ // It is only relevant when calling via "IDD" (International Direct Dialing)
107
+ // when the caller can use a special "00" prefix instead of using the "+" character.
108
+ fromCountry: CountryCode;
109
+ // `humanReadable` option was removed in version `1.8.6`.
110
+ // Now it's always `true` by default.
111
+ // humanReadable?: boolean;
105
112
  }
106
113
 
114
+ // All possible `options` for the `format()` function,
115
+ // regardless of the actual format being used.
116
+ export type FormatNumberOptions = FormatNumberOptionsForNationalOrInternational | FormatNumberOptionsForIDD
117
+
107
118
  // // https://stackoverflow.com/a/67026991
108
119
  // type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
109
120
 
@@ -124,9 +135,9 @@ export interface FormatNumberOptionsWithoutIDD {
124
135
  // isValid(): boolean;
125
136
  // getType(): NumberType;
126
137
  // format(format: NumberFormat, options?: FormatNumberOptions): string;
127
- // formatNational(options?: FormatNumberOptionsWithoutIDD): string;
128
- // formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
129
- // getURI(options?: FormatNumberOptionsWithoutIDD): string;
138
+ // formatNational(options?: FormatNumberOptionsForNationalOrInternational): string;
139
+ // formatInternational(options?: FormatNumberOptionsForNationalOrInternational): string;
140
+ // getURI(): string;
130
141
  // isNonGeographic(): boolean;
131
142
  // isEqual(phoneNumber: PhoneNumber): boolean;
132
143
  // }