libphonenumber-js 1.11.20 → 1.12.4
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 +6 -1
- package/README.md +12 -1
- package/build/PhoneNumber.js +56 -6
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumber.test.js +30 -5
- package/build/PhoneNumber.test.js.map +1 -1
- package/build/metadata.js +3 -2
- package/build/metadata.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.cjs +2 -1
- package/core/index.cjs.js +2 -1
- package/core/index.d.cts +29 -4
- package/core/index.d.ts +29 -4
- package/core/index.js +1 -0
- package/custom.d.ts +28 -4
- package/custom.js +1 -0
- package/es6/PhoneNumber.js +48 -6
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumber.test.js +30 -5
- package/es6/PhoneNumber.test.js.map +1 -1
- package/es6/metadata.js +3 -2
- package/es6/metadata.js.map +1 -1
- package/index.cjs +2 -1
- package/index.cjs.js +2 -1
- package/index.d.cts +29 -19
- package/index.d.ts +29 -19
- package/index.js +1 -0
- package/max/exports/PhoneNumber.js +11 -0
- package/max/index.cjs +2 -1
- package/max/index.cjs.js +2 -1
- package/max/index.d.cts +29 -4
- package/max/index.d.ts +29 -4
- package/max/index.js +1 -0
- package/min/exports/PhoneNumber.js +11 -0
- package/min/index.cjs +7 -1
- package/min/index.cjs.js +7 -1
- package/min/index.d.cts +29 -4
- package/min/index.d.ts +29 -4
- package/min/index.js +1 -0
- package/mobile/exports/PhoneNumber.js +11 -0
- package/mobile/index.cjs +2 -1
- package/mobile/index.cjs.js +2 -1
- package/mobile/index.d.cts +29 -4
- package/mobile/index.d.ts +29 -4
- package/mobile/index.js +1 -0
- package/package.json +1 -1
- package/source/PhoneNumber.js +43 -6
- package/source/PhoneNumber.test.js +21 -4
- package/source/metadata.js +3 -2
- package/types.d.cts +40 -33
- package/types.d.ts +40 -33
package/min/index.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MetadataJson,
|
|
3
3
|
Examples,
|
|
4
|
-
PhoneNumber,
|
|
5
4
|
E164Number,
|
|
6
5
|
CountryCallingCode,
|
|
7
6
|
CountryCode,
|
|
@@ -10,10 +9,11 @@ import {
|
|
|
10
9
|
Extension,
|
|
11
10
|
ParseError,
|
|
12
11
|
NumberFoundLegacy,
|
|
13
|
-
NumberFound,
|
|
14
12
|
NumberType,
|
|
15
13
|
NumberFormat,
|
|
16
14
|
NumberingPlan,
|
|
15
|
+
FormatNumberOptions,
|
|
16
|
+
FormatNumberOptionsWithoutIDD,
|
|
17
17
|
ValidatePhoneNumberLengthResult
|
|
18
18
|
} from '../types.d.cjs';
|
|
19
19
|
|
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
export {
|
|
23
23
|
MetadataJson,
|
|
24
24
|
Examples,
|
|
25
|
-
PhoneNumber,
|
|
26
25
|
E164Number,
|
|
27
26
|
CountryCallingCode,
|
|
28
27
|
CountryCode,
|
|
@@ -31,13 +30,39 @@ export {
|
|
|
31
30
|
Extension,
|
|
32
31
|
ParseError,
|
|
33
32
|
NumberFoundLegacy,
|
|
34
|
-
NumberFound,
|
|
35
33
|
NumberType,
|
|
36
34
|
NumberFormat,
|
|
37
35
|
NumberingPlan,
|
|
38
36
|
ValidatePhoneNumberLengthResult
|
|
39
37
|
};
|
|
40
38
|
|
|
39
|
+
export class PhoneNumber {
|
|
40
|
+
constructor(number: E164Number);
|
|
41
|
+
countryCallingCode: CountryCallingCode;
|
|
42
|
+
country?: CountryCode;
|
|
43
|
+
nationalNumber: NationalNumber;
|
|
44
|
+
number: E164Number;
|
|
45
|
+
carrierCode?: CarrierCode;
|
|
46
|
+
ext?: Extension;
|
|
47
|
+
setExt(ext: Extension): void;
|
|
48
|
+
getPossibleCountries(): CountryCode[];
|
|
49
|
+
isPossible(): boolean;
|
|
50
|
+
isValid(): boolean;
|
|
51
|
+
getType(): NumberType;
|
|
52
|
+
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
|
+
formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
54
|
+
formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
55
|
+
getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
56
|
+
isNonGeographic(): boolean;
|
|
57
|
+
isEqual(phoneNumber: PhoneNumber): boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface NumberFound {
|
|
61
|
+
number: PhoneNumber;
|
|
62
|
+
startsAt: number;
|
|
63
|
+
endsAt: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
41
66
|
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
|
|
42
67
|
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
|
|
43
68
|
|
package/min/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MetadataJson,
|
|
3
3
|
Examples,
|
|
4
|
-
PhoneNumber,
|
|
5
4
|
E164Number,
|
|
6
5
|
CountryCallingCode,
|
|
7
6
|
CountryCode,
|
|
@@ -10,10 +9,11 @@ import {
|
|
|
10
9
|
Extension,
|
|
11
10
|
ParseError,
|
|
12
11
|
NumberFoundLegacy,
|
|
13
|
-
NumberFound,
|
|
14
12
|
NumberType,
|
|
15
13
|
NumberFormat,
|
|
16
14
|
NumberingPlan,
|
|
15
|
+
FormatNumberOptions,
|
|
16
|
+
FormatNumberOptionsWithoutIDD,
|
|
17
17
|
ValidatePhoneNumberLengthResult
|
|
18
18
|
} from '../types.d.js';
|
|
19
19
|
|
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
export {
|
|
23
23
|
MetadataJson,
|
|
24
24
|
Examples,
|
|
25
|
-
PhoneNumber,
|
|
26
25
|
E164Number,
|
|
27
26
|
CountryCallingCode,
|
|
28
27
|
CountryCode,
|
|
@@ -31,13 +30,39 @@ export {
|
|
|
31
30
|
Extension,
|
|
32
31
|
ParseError,
|
|
33
32
|
NumberFoundLegacy,
|
|
34
|
-
NumberFound,
|
|
35
33
|
NumberType,
|
|
36
34
|
NumberFormat,
|
|
37
35
|
NumberingPlan,
|
|
38
36
|
ValidatePhoneNumberLengthResult
|
|
39
37
|
};
|
|
40
38
|
|
|
39
|
+
export class PhoneNumber {
|
|
40
|
+
constructor(number: E164Number);
|
|
41
|
+
countryCallingCode: CountryCallingCode;
|
|
42
|
+
country?: CountryCode;
|
|
43
|
+
nationalNumber: NationalNumber;
|
|
44
|
+
number: E164Number;
|
|
45
|
+
carrierCode?: CarrierCode;
|
|
46
|
+
ext?: Extension;
|
|
47
|
+
setExt(ext: Extension): void;
|
|
48
|
+
getPossibleCountries(): CountryCode[];
|
|
49
|
+
isPossible(): boolean;
|
|
50
|
+
isValid(): boolean;
|
|
51
|
+
getType(): NumberType;
|
|
52
|
+
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
|
+
formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
54
|
+
formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
55
|
+
getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
56
|
+
isNonGeographic(): boolean;
|
|
57
|
+
isEqual(phoneNumber: PhoneNumber): boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface NumberFound {
|
|
61
|
+
number: PhoneNumber;
|
|
62
|
+
startsAt: number;
|
|
63
|
+
endsAt: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
41
66
|
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
|
|
42
67
|
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
|
|
43
68
|
|
package/min/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export { Metadata } from './exports/Metadata.js'
|
|
|
27
27
|
export { getExampleNumber } from './exports/getExampleNumber.js'
|
|
28
28
|
|
|
29
29
|
export { formatIncompletePhoneNumber } from './exports/formatIncompletePhoneNumber.js'
|
|
30
|
+
export { PhoneNumber } from './exports/PhoneNumber.js'
|
|
30
31
|
|
|
31
32
|
export {
|
|
32
33
|
ParseError,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Importing from a ".js" file is a workaround for Node.js "ES Modules"
|
|
2
|
+
// importing system which is even uncapable of importing "*.json" files.
|
|
3
|
+
import metadata from '../../metadata.mobile.json.js'
|
|
4
|
+
|
|
5
|
+
import { PhoneNumber as _PhoneNumber } from '../../core/index.js'
|
|
6
|
+
|
|
7
|
+
export function PhoneNumber(number) {
|
|
8
|
+
return _PhoneNumber.call(this, number, metadata)
|
|
9
|
+
}
|
|
10
|
+
PhoneNumber.prototype = Object.create(_PhoneNumber.prototype, {})
|
|
11
|
+
PhoneNumber.prototype.constructor = PhoneNumber
|
package/mobile/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ function parsePhoneNumberFromString() {
|
|
|
20
20
|
exports = module.exports = parsePhoneNumberFromString
|
|
21
21
|
exports['default'] = parsePhoneNumberFromString
|
|
22
22
|
|
|
23
|
+
exports.PhoneNumber = core.PhoneNumber
|
|
23
24
|
exports.ParseError = core.ParseError
|
|
24
25
|
|
|
25
26
|
function parsePhoneNumberWithError() {
|
|
@@ -104,4 +105,4 @@ exports.parseDigits = core.parseDigits
|
|
|
104
105
|
exports.DIGIT_PLACEHOLDER = core.DIGIT_PLACEHOLDER
|
|
105
106
|
|
|
106
107
|
exports.parseRFC3966 = core.parseRFC3966
|
|
107
|
-
exports.formatRFC3966 = core.formatRFC3966
|
|
108
|
+
exports.formatRFC3966 = core.formatRFC3966
|
package/mobile/index.cjs.js
CHANGED
|
@@ -25,6 +25,7 @@ function parsePhoneNumberFromString() {
|
|
|
25
25
|
exports = module.exports = parsePhoneNumberFromString
|
|
26
26
|
exports['default'] = parsePhoneNumberFromString
|
|
27
27
|
|
|
28
|
+
exports.PhoneNumber = core.PhoneNumber
|
|
28
29
|
exports.ParseError = core.ParseError
|
|
29
30
|
|
|
30
31
|
function parsePhoneNumberWithError() {
|
|
@@ -109,4 +110,4 @@ exports.parseDigits = core.parseDigits
|
|
|
109
110
|
exports.DIGIT_PLACEHOLDER = core.DIGIT_PLACEHOLDER
|
|
110
111
|
|
|
111
112
|
exports.parseRFC3966 = core.parseRFC3966
|
|
112
|
-
exports.formatRFC3966 = core.formatRFC3966
|
|
113
|
+
exports.formatRFC3966 = core.formatRFC3966
|
package/mobile/index.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MetadataJson,
|
|
3
3
|
Examples,
|
|
4
|
-
PhoneNumber,
|
|
5
4
|
E164Number,
|
|
6
5
|
CountryCallingCode,
|
|
7
6
|
CountryCode,
|
|
@@ -10,10 +9,11 @@ import {
|
|
|
10
9
|
Extension,
|
|
11
10
|
ParseError,
|
|
12
11
|
NumberFoundLegacy,
|
|
13
|
-
NumberFound,
|
|
14
12
|
NumberType,
|
|
15
13
|
NumberFormat,
|
|
16
14
|
NumberingPlan,
|
|
15
|
+
FormatNumberOptions,
|
|
16
|
+
FormatNumberOptionsWithoutIDD,
|
|
17
17
|
ValidatePhoneNumberLengthResult
|
|
18
18
|
} from '../types.d.cjs';
|
|
19
19
|
|
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
export {
|
|
23
23
|
MetadataJson,
|
|
24
24
|
Examples,
|
|
25
|
-
PhoneNumber,
|
|
26
25
|
E164Number,
|
|
27
26
|
CountryCallingCode,
|
|
28
27
|
CountryCode,
|
|
@@ -31,13 +30,39 @@ export {
|
|
|
31
30
|
Extension,
|
|
32
31
|
ParseError,
|
|
33
32
|
NumberFoundLegacy,
|
|
34
|
-
NumberFound,
|
|
35
33
|
NumberType,
|
|
36
34
|
NumberFormat,
|
|
37
35
|
NumberingPlan,
|
|
38
36
|
ValidatePhoneNumberLengthResult
|
|
39
37
|
};
|
|
40
38
|
|
|
39
|
+
export class PhoneNumber {
|
|
40
|
+
constructor(number: E164Number);
|
|
41
|
+
countryCallingCode: CountryCallingCode;
|
|
42
|
+
country?: CountryCode;
|
|
43
|
+
nationalNumber: NationalNumber;
|
|
44
|
+
number: E164Number;
|
|
45
|
+
carrierCode?: CarrierCode;
|
|
46
|
+
ext?: Extension;
|
|
47
|
+
setExt(ext: Extension): void;
|
|
48
|
+
getPossibleCountries(): CountryCode[];
|
|
49
|
+
isPossible(): boolean;
|
|
50
|
+
isValid(): boolean;
|
|
51
|
+
getType(): NumberType;
|
|
52
|
+
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
|
+
formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
54
|
+
formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
55
|
+
getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
56
|
+
isNonGeographic(): boolean;
|
|
57
|
+
isEqual(phoneNumber: PhoneNumber): boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface NumberFound {
|
|
61
|
+
number: PhoneNumber;
|
|
62
|
+
startsAt: number;
|
|
63
|
+
endsAt: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
41
66
|
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
|
|
42
67
|
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
|
|
43
68
|
|
package/mobile/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MetadataJson,
|
|
3
3
|
Examples,
|
|
4
|
-
PhoneNumber,
|
|
5
4
|
E164Number,
|
|
6
5
|
CountryCallingCode,
|
|
7
6
|
CountryCode,
|
|
@@ -10,10 +9,11 @@ import {
|
|
|
10
9
|
Extension,
|
|
11
10
|
ParseError,
|
|
12
11
|
NumberFoundLegacy,
|
|
13
|
-
NumberFound,
|
|
14
12
|
NumberType,
|
|
15
13
|
NumberFormat,
|
|
16
14
|
NumberingPlan,
|
|
15
|
+
FormatNumberOptions,
|
|
16
|
+
FormatNumberOptionsWithoutIDD,
|
|
17
17
|
ValidatePhoneNumberLengthResult
|
|
18
18
|
} from '../types.d.js';
|
|
19
19
|
|
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
export {
|
|
23
23
|
MetadataJson,
|
|
24
24
|
Examples,
|
|
25
|
-
PhoneNumber,
|
|
26
25
|
E164Number,
|
|
27
26
|
CountryCallingCode,
|
|
28
27
|
CountryCode,
|
|
@@ -31,13 +30,39 @@ export {
|
|
|
31
30
|
Extension,
|
|
32
31
|
ParseError,
|
|
33
32
|
NumberFoundLegacy,
|
|
34
|
-
NumberFound,
|
|
35
33
|
NumberType,
|
|
36
34
|
NumberFormat,
|
|
37
35
|
NumberingPlan,
|
|
38
36
|
ValidatePhoneNumberLengthResult
|
|
39
37
|
};
|
|
40
38
|
|
|
39
|
+
export class PhoneNumber {
|
|
40
|
+
constructor(number: E164Number);
|
|
41
|
+
countryCallingCode: CountryCallingCode;
|
|
42
|
+
country?: CountryCode;
|
|
43
|
+
nationalNumber: NationalNumber;
|
|
44
|
+
number: E164Number;
|
|
45
|
+
carrierCode?: CarrierCode;
|
|
46
|
+
ext?: Extension;
|
|
47
|
+
setExt(ext: Extension): void;
|
|
48
|
+
getPossibleCountries(): CountryCode[];
|
|
49
|
+
isPossible(): boolean;
|
|
50
|
+
isValid(): boolean;
|
|
51
|
+
getType(): NumberType;
|
|
52
|
+
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
|
+
formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
54
|
+
formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
55
|
+
getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
56
|
+
isNonGeographic(): boolean;
|
|
57
|
+
isEqual(phoneNumber: PhoneNumber): boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface NumberFound {
|
|
61
|
+
number: PhoneNumber;
|
|
62
|
+
startsAt: number;
|
|
63
|
+
endsAt: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
41
66
|
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
|
|
42
67
|
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
|
|
43
68
|
|
package/mobile/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export { Metadata } from './exports/Metadata.js'
|
|
|
27
27
|
export { getExampleNumber } from './exports/getExampleNumber.js'
|
|
28
28
|
|
|
29
29
|
export { formatIncompletePhoneNumber } from './exports/formatIncompletePhoneNumber.js'
|
|
30
|
+
export { PhoneNumber } from './exports/PhoneNumber.js'
|
|
30
31
|
|
|
31
32
|
export {
|
|
32
33
|
ParseError,
|
package/package.json
CHANGED
package/source/PhoneNumber.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import Metadata from './metadata.js'
|
|
1
|
+
import Metadata, { validateMetadata } from './metadata.js'
|
|
2
2
|
import isPossibleNumber from './isPossible.js'
|
|
3
3
|
import isValidNumber from './isValid.js'
|
|
4
4
|
// import checkNumberLength from './helpers/checkNumberLength.js'
|
|
5
5
|
import getNumberType from './helpers/getNumberType.js'
|
|
6
6
|
import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
|
|
7
|
+
import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
|
|
8
|
+
import isObject from './helpers/isObject.js'
|
|
7
9
|
import formatNumber from './format.js'
|
|
8
10
|
|
|
9
11
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
@@ -16,15 +18,48 @@ export default class PhoneNumber {
|
|
|
16
18
|
* @return {PhoneNumber}
|
|
17
19
|
*/
|
|
18
20
|
constructor(countryOrCountryCallingCode, nationalNumber, metadata) {
|
|
21
|
+
// Validate `countryOrCountryCallingCode` argument.
|
|
19
22
|
if (!countryOrCountryCallingCode) {
|
|
20
|
-
throw new TypeError('
|
|
23
|
+
throw new TypeError('First argument is required')
|
|
21
24
|
}
|
|
25
|
+
if (typeof countryOrCountryCallingCode !== 'string') {
|
|
26
|
+
throw new TypeError('First argument must be a string')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// In case of public API use: `constructor(number, metadata)`.
|
|
30
|
+
// Transform the arguments from `constructor(number, metadata)` to
|
|
31
|
+
// `constructor(countryOrCountryCallingCode, nationalNumber, metadata)`.
|
|
32
|
+
if (typeof countryOrCountryCallingCode === 'string') {
|
|
33
|
+
if (countryOrCountryCallingCode[0] === '+' && !nationalNumber) {
|
|
34
|
+
throw new TypeError('`metadata` argument not passed')
|
|
35
|
+
}
|
|
36
|
+
if (isObject(nationalNumber) && isObject(nationalNumber.countries)) {
|
|
37
|
+
metadata = nationalNumber
|
|
38
|
+
const e164Number = countryOrCountryCallingCode
|
|
39
|
+
if (!E164_NUMBER_REGEXP.test(e164Number)) {
|
|
40
|
+
throw new Error('Invalid `number` argument passed: must consist of a "+" followed by digits')
|
|
41
|
+
}
|
|
42
|
+
const { countryCallingCode, number } = extractCountryCallingCode(e164Number, undefined, undefined, metadata)
|
|
43
|
+
nationalNumber = number
|
|
44
|
+
countryOrCountryCallingCode = countryCallingCode
|
|
45
|
+
if (!nationalNumber) {
|
|
46
|
+
throw new Error('Invalid `number` argument passed: too short')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Validate `nationalNumber` argument.
|
|
22
52
|
if (!nationalNumber) {
|
|
23
|
-
throw new TypeError('`nationalNumber`
|
|
53
|
+
throw new TypeError('`nationalNumber` argument is required')
|
|
24
54
|
}
|
|
25
|
-
if (
|
|
26
|
-
throw new TypeError('`
|
|
55
|
+
if (typeof nationalNumber !== 'string') {
|
|
56
|
+
throw new TypeError('`nationalNumber` argument must be a string')
|
|
27
57
|
}
|
|
58
|
+
|
|
59
|
+
// Validate `metadata` argument.
|
|
60
|
+
validateMetadata(metadata)
|
|
61
|
+
|
|
62
|
+
// Initialize properties.
|
|
28
63
|
const { country, countryCallingCode } = getCountryAndCountryCallingCode(
|
|
29
64
|
countryOrCountryCallingCode,
|
|
30
65
|
metadata
|
|
@@ -140,4 +175,6 @@ function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJs
|
|
|
140
175
|
country,
|
|
141
176
|
countryCallingCode
|
|
142
177
|
}
|
|
143
|
-
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const E164_NUMBER_REGEXP = /^\+\d+$/
|
|
@@ -2,10 +2,27 @@ import metadata from '../metadata.min.json' assert { type: 'json' }
|
|
|
2
2
|
import PhoneNumber from './PhoneNumber.js'
|
|
3
3
|
|
|
4
4
|
describe('PhoneNumber', () => {
|
|
5
|
-
it('should
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
expect(
|
|
5
|
+
it('should create a phone number via a public constructor', () => {
|
|
6
|
+
const phoneNumber = new PhoneNumber('+78005553535', metadata)
|
|
7
|
+
phoneNumber.setExt('1234')
|
|
8
|
+
expect(phoneNumber.country).to.be.undefined
|
|
9
|
+
phoneNumber.countryCallingCode.should.equal('7')
|
|
10
|
+
phoneNumber.nationalNumber.should.equal('8005553535')
|
|
11
|
+
phoneNumber.formatNational().should.equal('8 (800) 555-35-35 ext. 1234')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should validate constructor arguments (public constructor)', () => {
|
|
15
|
+
expect(() => new PhoneNumber()).to.throw('argument is required')
|
|
16
|
+
expect(() => new PhoneNumber(undefined, metadata)).to.throw('argument is required')
|
|
17
|
+
expect(() => new PhoneNumber('7', metadata)).to.throw('must consist of a "+"')
|
|
18
|
+
expect(() => new PhoneNumber('+7', metadata)).to.throw('too short')
|
|
19
|
+
expect(() => new PhoneNumber('+7800')).to.throw('`metadata` argument not passed')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should validate constructor arguments (private constructor)', () => {
|
|
23
|
+
expect(() => new PhoneNumber(undefined, '800', metadata)).to.throw('First argument is required')
|
|
24
|
+
expect(() => new PhoneNumber('7', undefined, metadata)).to.throw('`nationalNumber` argument is required')
|
|
25
|
+
expect(() => new PhoneNumber('7', '8005553535')).to.throw('`metadata` argument not passed')
|
|
9
26
|
})
|
|
10
27
|
|
|
11
28
|
it('should accept country code argument', () => {
|
package/source/metadata.js
CHANGED
|
@@ -457,8 +457,9 @@ export function validateMetadata(metadata) {
|
|
|
457
457
|
throw new Error('[libphonenumber-js] `metadata` argument not passed. Check your arguments.')
|
|
458
458
|
}
|
|
459
459
|
|
|
460
|
-
// `country_phone_code_to_countries` was renamed to
|
|
461
|
-
//
|
|
460
|
+
// `country_phone_code_to_countries` was renamed to `country_calling_codes` in `1.0.18`.
|
|
461
|
+
// For that reason, it's not used in this detection algorithm.
|
|
462
|
+
// Instead, it detects by `countries: {}` property existence.
|
|
462
463
|
if (!isObject(metadata) || !isObject(metadata.countries)) {
|
|
463
464
|
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}.`)
|
|
464
465
|
}
|
package/types.d.cts
CHANGED
|
@@ -91,48 +91,55 @@ export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
|
|
|
91
91
|
|
|
92
92
|
type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
v2?: boolean;
|
|
96
|
-
formatExtension?: FormatExtension;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
type FormatNumberOptions = {
|
|
94
|
+
export interface FormatNumberOptions {
|
|
100
95
|
v2?: boolean;
|
|
101
96
|
fromCountry?: CountryCode;
|
|
102
97
|
humanReadable?: boolean;
|
|
103
98
|
nationalPrefix?: boolean;
|
|
104
99
|
formatExtension?: FormatExtension;
|
|
105
|
-
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface FormatNumberOptionsWithoutIDD {
|
|
103
|
+
v2?: boolean;
|
|
104
|
+
formatExtension?: FormatExtension;
|
|
105
|
+
}
|
|
106
106
|
|
|
107
107
|
// // https://stackoverflow.com/a/67026991
|
|
108
108
|
// type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
|
|
109
109
|
|
|
110
|
-
export class PhoneNumber {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
110
|
+
// For some weird reason, `export class PhoneNumber implements IPhoneNumber {}` approach
|
|
111
|
+
// didn't work, so the declaration of the `PhoneNumber` class had to be copy-pasted everywhere
|
|
112
|
+
// instead of being declared once in this file.
|
|
113
|
+
//
|
|
114
|
+
// export interface PhoneNumber {
|
|
115
|
+
// countryCallingCode: CountryCallingCode;
|
|
116
|
+
// country?: CountryCode;
|
|
117
|
+
// nationalNumber: NationalNumber;
|
|
118
|
+
// number: E164Number;
|
|
119
|
+
// carrierCode?: CarrierCode;
|
|
120
|
+
// ext?: Extension;
|
|
121
|
+
// setExt(ext: Extension): void;
|
|
122
|
+
// getPossibleCountries(): CountryCode[];
|
|
123
|
+
// isPossible(): boolean;
|
|
124
|
+
// isValid(): boolean;
|
|
125
|
+
// getType(): NumberType;
|
|
126
|
+
// format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
127
|
+
// formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
128
|
+
// formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
129
|
+
// getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
130
|
+
// isNonGeographic(): boolean;
|
|
131
|
+
// isEqual(phoneNumber: PhoneNumber): boolean;
|
|
132
|
+
// }
|
|
133
|
+
//
|
|
134
|
+
// Because `PhoneNumber` interface was commented out,
|
|
135
|
+
// `NumberFound` also had to be copy-pasted everywhere
|
|
136
|
+
// instead of being declared once in this file.
|
|
137
|
+
//
|
|
138
|
+
// export interface NumberFound {
|
|
139
|
+
// number: PhoneNumber;
|
|
140
|
+
// startsAt: number;
|
|
141
|
+
// endsAt: number;
|
|
142
|
+
// }
|
|
136
143
|
|
|
137
144
|
// Deprecated
|
|
138
145
|
export interface NumberFoundLegacy {
|
package/types.d.ts
CHANGED
|
@@ -91,48 +91,55 @@ export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
|
|
|
91
91
|
|
|
92
92
|
type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
v2?: boolean;
|
|
96
|
-
formatExtension?: FormatExtension;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
type FormatNumberOptions = {
|
|
94
|
+
export interface FormatNumberOptions {
|
|
100
95
|
v2?: boolean;
|
|
101
96
|
fromCountry?: CountryCode;
|
|
102
97
|
humanReadable?: boolean;
|
|
103
98
|
nationalPrefix?: boolean;
|
|
104
99
|
formatExtension?: FormatExtension;
|
|
105
|
-
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface FormatNumberOptionsWithoutIDD {
|
|
103
|
+
v2?: boolean;
|
|
104
|
+
formatExtension?: FormatExtension;
|
|
105
|
+
}
|
|
106
106
|
|
|
107
107
|
// // https://stackoverflow.com/a/67026991
|
|
108
108
|
// type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
|
|
109
109
|
|
|
110
|
-
export class PhoneNumber {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
110
|
+
// For some weird reason, `export class PhoneNumber implements IPhoneNumber {}` approach
|
|
111
|
+
// didn't work, so the declaration of the `PhoneNumber` class had to be copy-pasted everywhere
|
|
112
|
+
// instead of being declared once in this file.
|
|
113
|
+
//
|
|
114
|
+
// export interface PhoneNumber {
|
|
115
|
+
// countryCallingCode: CountryCallingCode;
|
|
116
|
+
// country?: CountryCode;
|
|
117
|
+
// nationalNumber: NationalNumber;
|
|
118
|
+
// number: E164Number;
|
|
119
|
+
// carrierCode?: CarrierCode;
|
|
120
|
+
// ext?: Extension;
|
|
121
|
+
// setExt(ext: Extension): void;
|
|
122
|
+
// getPossibleCountries(): CountryCode[];
|
|
123
|
+
// isPossible(): boolean;
|
|
124
|
+
// isValid(): boolean;
|
|
125
|
+
// getType(): NumberType;
|
|
126
|
+
// format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
127
|
+
// formatNational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
128
|
+
// formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
|
|
129
|
+
// getURI(options?: FormatNumberOptionsWithoutIDD): string;
|
|
130
|
+
// isNonGeographic(): boolean;
|
|
131
|
+
// isEqual(phoneNumber: PhoneNumber): boolean;
|
|
132
|
+
// }
|
|
133
|
+
//
|
|
134
|
+
// Because `PhoneNumber` interface was commented out,
|
|
135
|
+
// `NumberFound` also had to be copy-pasted everywhere
|
|
136
|
+
// instead of being declared once in this file.
|
|
137
|
+
//
|
|
138
|
+
// export interface NumberFound {
|
|
139
|
+
// number: PhoneNumber;
|
|
140
|
+
// startsAt: number;
|
|
141
|
+
// endsAt: number;
|
|
142
|
+
// }
|
|
136
143
|
|
|
137
144
|
// Deprecated
|
|
138
145
|
export interface NumberFoundLegacy {
|