libphonenumber-js 1.12.33 → 1.12.35
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.
- package/CHANGELOG.md +30 -18
- package/README.md +3 -1
- package/build/format.js +18 -2
- package/build/format.js.map +1 -1
- package/build/format.test.js +3 -6
- package/build/format.test.js.map +1 -1
- package/build/helpers/getIddPrefix.js +2 -2
- package/build/helpers/getIddPrefix.js.map +1 -1
- package/build/legacy/format.test.js +3 -6
- package/build/legacy/format.test.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.d.cts +12 -4
- package/core/index.d.ts +12 -4
- package/custom.d.ts +7 -6
- package/es6/format.js +18 -2
- package/es6/format.js.map +1 -1
- package/es6/format.test.js +3 -6
- package/es6/format.test.js.map +1 -1
- package/es6/helpers/getIddPrefix.js +2 -2
- package/es6/helpers/getIddPrefix.js.map +1 -1
- package/es6/legacy/format.test.js +3 -6
- package/es6/legacy/format.test.js.map +1 -1
- package/index.d.cts +6 -4
- package/index.d.ts +6 -4
- package/max/index.d.cts +12 -4
- package/max/index.d.ts +12 -4
- package/max/metadata/metadata.max.json +1 -1
- package/max/metadata/metadata.max.json.js +1 -1
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.js +1 -1
- package/min/index.d.cts +12 -4
- package/min/index.d.ts +12 -4
- package/min/metadata/metadata.min.json +1 -1
- package/min/metadata/metadata.min.json.js +1 -1
- package/mobile/index.d.cts +12 -4
- package/mobile/index.d.ts +12 -4
- package/mobile/metadata/metadata.mobile.json +1 -1
- package/mobile/metadata/metadata.mobile.json.js +1 -1
- package/package.json +1 -1
- package/source/format.js +17 -2
- package/source/format.test.js +3 -3
- package/source/helpers/getIddPrefix.js +2 -2
- package/source/legacy/format.test.js +3 -3
- package/types.d.cts +21 -10
- package/types.d.ts +21 -10
package/package.json
CHANGED
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
|
-
//
|
|
71
|
-
//
|
|
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:
|
package/source/format.test.js
CHANGED
|
@@ -244,15 +244,15 @@ describe('format', () => {
|
|
|
244
244
|
|
|
245
245
|
// Same country calling code.
|
|
246
246
|
expect(
|
|
247
|
-
formatNumber('+12133734253', 'IDD', { fromCountry: 'CA'
|
|
247
|
+
formatNumber('+12133734253', 'IDD', { fromCountry: 'CA' })
|
|
248
248
|
).to.equal('1 (213) 373-4253')
|
|
249
249
|
expect(
|
|
250
|
-
formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ'
|
|
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'
|
|
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
|
-
//
|
|
15
|
-
//
|
|
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'
|
|
220
|
+
formatNumber('+12133734253', 'IDD', { fromCountry: 'CA' })
|
|
221
221
|
).to.equal('1 (213) 373-4253')
|
|
222
222
|
expect(
|
|
223
|
-
formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ'
|
|
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'
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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?:
|
|
128
|
-
// formatInternational(options?:
|
|
129
|
-
// getURI(
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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?:
|
|
128
|
-
// formatInternational(options?:
|
|
129
|
-
// getURI(
|
|
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
|
// }
|