core-services-sdk 1.3.12 → 1.3.14
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/package.json
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
|
-
//
|
|
1
|
+
// normalize-phone-number.js
|
|
2
|
+
// Works with both CJS and ESM builds of google-libphonenumber
|
|
2
3
|
|
|
3
|
-
import * as
|
|
4
|
-
const { PhoneNumberUtil, PhoneNumberFormat } = pkg
|
|
4
|
+
import * as raw from 'google-libphonenumber'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/** Resolve libphonenumber regardless of interop shape */
|
|
7
|
+
export function getLib() {
|
|
8
|
+
// Prefer direct (CJS-style or ESM w/ named), else default
|
|
9
|
+
// e.g. raw.PhoneNumberUtil OR raw.default.PhoneNumberUtil
|
|
10
|
+
// eslint-disable-next-line no-unused-vars
|
|
11
|
+
/** @type {any} */
|
|
12
|
+
const anyRaw = raw
|
|
13
|
+
const lib = anyRaw.PhoneNumberUtil ? anyRaw : anyRaw.default ?? anyRaw
|
|
14
|
+
|
|
15
|
+
if (!lib || !lib.PhoneNumberUtil || !lib.PhoneNumberFormat) {
|
|
16
|
+
throw new Error('google-libphonenumber failed to load (exports not found)')
|
|
17
|
+
}
|
|
18
|
+
return lib
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let _util // lazy singleton
|
|
22
|
+
|
|
23
|
+
export function phoneUtil() {
|
|
24
|
+
if (!_util) {
|
|
25
|
+
const { PhoneNumberUtil } = getLib()
|
|
26
|
+
_util = PhoneNumberUtil.getInstance()
|
|
27
|
+
}
|
|
28
|
+
return _util
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formats() {
|
|
32
|
+
const { PhoneNumberFormat } = getLib()
|
|
33
|
+
return PhoneNumberFormat
|
|
34
|
+
}
|
|
7
35
|
|
|
8
36
|
/**
|
|
9
|
-
* Trim and remove invisible RTL markers
|
|
37
|
+
* Trim and remove invisible RTL markers.
|
|
10
38
|
* @param {string} input
|
|
11
39
|
* @returns {string}
|
|
12
40
|
*/
|
|
@@ -18,53 +46,57 @@ function clean(input) {
|
|
|
18
46
|
|
|
19
47
|
/**
|
|
20
48
|
* Convert a parsed libphonenumber object into a normalized result.
|
|
21
|
-
* @param {
|
|
49
|
+
* @param {any} parsed
|
|
22
50
|
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
23
51
|
*/
|
|
24
52
|
function toResult(parsed) {
|
|
53
|
+
const PNF = formats()
|
|
54
|
+
const util = phoneUtil()
|
|
25
55
|
return {
|
|
26
|
-
e164:
|
|
27
|
-
national:
|
|
28
|
-
international:
|
|
29
|
-
regionCode:
|
|
30
|
-
type:
|
|
56
|
+
e164: util.format(parsed, PNF.E164),
|
|
57
|
+
national: util.format(parsed, PNF.NATIONAL),
|
|
58
|
+
international: util.format(parsed, PNF.INTERNATIONAL),
|
|
59
|
+
regionCode: util.getRegionCodeForNumber(parsed),
|
|
60
|
+
type: util.getNumberType(parsed),
|
|
31
61
|
}
|
|
32
62
|
}
|
|
33
63
|
|
|
34
64
|
/**
|
|
35
65
|
* Parse & validate an international number (must start with '+').
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @param {string} input - International number, e.g. "+972541234567"
|
|
66
|
+
* @param {string} input
|
|
39
67
|
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
40
68
|
* @throws {Error} If the number is invalid
|
|
41
69
|
*/
|
|
42
70
|
export function normalizePhoneOrThrowIntl(input) {
|
|
43
71
|
try {
|
|
44
|
-
const
|
|
45
|
-
|
|
72
|
+
const util = phoneUtil()
|
|
73
|
+
const parsed = util.parseAndKeepRawInput(clean(input))
|
|
74
|
+
if (!util.isValidNumber(parsed)) throw new Error('x')
|
|
46
75
|
return toResult(parsed)
|
|
47
|
-
} catch {
|
|
48
|
-
|
|
76
|
+
} catch (e) {
|
|
77
|
+
const err = new Error('Invalid phone number')
|
|
78
|
+
err.cause = e
|
|
79
|
+
throw err
|
|
49
80
|
}
|
|
50
81
|
}
|
|
51
82
|
|
|
52
83
|
/**
|
|
53
84
|
* Parse & validate a national number using a region hint.
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @param {string} input - National number, e.g. "054-123-4567"
|
|
57
|
-
* @param {string} defaultRegion - ISO region like "IL" or "US"
|
|
85
|
+
* @param {string} input
|
|
86
|
+
* @param {string} defaultRegion
|
|
58
87
|
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
59
88
|
* @throws {Error} If the number is invalid
|
|
60
89
|
*/
|
|
61
90
|
export function normalizePhoneOrThrowWithRegion(input, defaultRegion) {
|
|
62
91
|
try {
|
|
63
|
-
const
|
|
64
|
-
|
|
92
|
+
const util = phoneUtil()
|
|
93
|
+
const parsed = util.parseAndKeepRawInput(clean(input), defaultRegion)
|
|
94
|
+
if (!util.isValidNumber(parsed)) throw new Error('x')
|
|
65
95
|
return toResult(parsed)
|
|
66
|
-
} catch {
|
|
67
|
-
|
|
96
|
+
} catch (e) {
|
|
97
|
+
const err = new Error('Invalid phone number')
|
|
98
|
+
err.cause = e
|
|
99
|
+
throw err
|
|
68
100
|
}
|
|
69
101
|
}
|
|
70
102
|
|
|
@@ -72,8 +104,6 @@ export function normalizePhoneOrThrowWithRegion(input, defaultRegion) {
|
|
|
72
104
|
* Smart normalization:
|
|
73
105
|
* - If input starts with '+', parse as international.
|
|
74
106
|
* - Otherwise require a defaultRegion and parse as national.
|
|
75
|
-
* Throws on invalid input or when defaultRegion is missing for non-international numbers.
|
|
76
|
-
*
|
|
77
107
|
* @param {string} input
|
|
78
108
|
* @param {{ defaultRegion?: string }} [opts]
|
|
79
109
|
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
@@ -86,7 +116,6 @@ export function normalizePhoneOrThrow(input, opts = {}) {
|
|
|
86
116
|
}
|
|
87
117
|
const { defaultRegion } = opts
|
|
88
118
|
if (!defaultRegion) {
|
|
89
|
-
// keep this one specific; your test relies on a different message here
|
|
90
119
|
throw new Error('defaultRegion is required for non-international numbers')
|
|
91
120
|
}
|
|
92
121
|
return normalizePhoneOrThrowWithRegion(cleaned, defaultRegion)
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import
|
|
2
|
+
import * as raw from 'google-libphonenumber'
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
phoneUtil,
|
|
5
6
|
normalizePhoneOrThrow,
|
|
6
7
|
normalizePhoneOrThrowIntl,
|
|
7
8
|
normalizePhoneOrThrowWithRegion,
|
|
8
9
|
} from '../../src/core/normalize-phone-number.js'
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
+
const { PhoneNumberFormat } = raw
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Get a valid example number for a given region from libphonenumber.
|
|
@@ -15,10 +16,10 @@ const phoneUtil = PhoneNumberUtil.getInstance()
|
|
|
15
16
|
* @param {string} region - e.g. "IL", "US"
|
|
16
17
|
*/
|
|
17
18
|
function exampleForRegion(region) {
|
|
18
|
-
const ex = phoneUtil.getExampleNumber(region)
|
|
19
|
+
const ex = phoneUtil().getExampleNumber(region)
|
|
19
20
|
return {
|
|
20
|
-
e164: phoneUtil.format(ex, PhoneNumberFormat.E164),
|
|
21
|
-
national: phoneUtil.format(ex, PhoneNumberFormat.NATIONAL),
|
|
21
|
+
e164: phoneUtil().format(ex, PhoneNumberFormat.E164),
|
|
22
|
+
national: phoneUtil().format(ex, PhoneNumberFormat.NATIONAL),
|
|
22
23
|
region,
|
|
23
24
|
}
|
|
24
25
|
}
|