core-services-sdk 1.3.11 → 1.3.12
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,7 +1,7 @@
|
|
|
1
|
-
// src/core/phone-validate.js
|
|
2
1
|
// Validate & normalize using google-libphonenumber
|
|
3
2
|
|
|
4
|
-
import
|
|
3
|
+
import * as pkg from 'google-libphonenumber'
|
|
4
|
+
const { PhoneNumberUtil, PhoneNumberFormat } = pkg
|
|
5
5
|
|
|
6
6
|
const phoneUtil = PhoneNumberUtil.getInstance()
|
|
7
7
|
|
|
@@ -22,94 +22,95 @@ function exampleForRegion(region) {
|
|
|
22
22
|
region,
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
describe('phone normalization', () => {
|
|
26
|
+
describe('phone normalization helpers', () => {
|
|
27
|
+
it('normalizePhoneOrThrowIntl: parses a valid international number (US)', () => {
|
|
28
|
+
const us = exampleForRegion('US')
|
|
29
|
+
const out = normalizePhoneOrThrowIntl(us.e164)
|
|
30
|
+
expect(out.e164).toBe(us.e164)
|
|
31
|
+
expect(out.regionCode).toBe('US')
|
|
32
|
+
expect(out.international).toMatch(/^\+1\b/)
|
|
33
|
+
})
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
it('normalizePhoneOrThrowWithRegion: parses a national number with region (IL)', () => {
|
|
36
|
-
const il = exampleForRegion('IL')
|
|
37
|
-
// introduce some separators/spaces to mimic user input
|
|
38
|
-
const dirty = ` ${il.national.replace(/\s/g, '-')} `
|
|
39
|
-
const out = normalizePhoneOrThrowWithRegion(dirty, 'IL')
|
|
40
|
-
expect(out.e164).toBe(il.e164)
|
|
41
|
-
expect(out.regionCode).toBe('IL')
|
|
42
|
-
expect(out.international).toMatch(/^\+972/)
|
|
43
|
-
})
|
|
35
|
+
it('normalizePhoneOrThrowWithRegion: parses a national number with region (IL)', () => {
|
|
36
|
+
const il = exampleForRegion('IL')
|
|
37
|
+
// introduce some separators/spaces to mimic user input
|
|
38
|
+
const dirty = ` ${il.national.replace(/\s/g, '-')} `
|
|
39
|
+
const out = normalizePhoneOrThrowWithRegion(dirty, 'IL')
|
|
40
|
+
expect(out.e164).toBe(il.e164)
|
|
41
|
+
expect(out.regionCode).toBe('IL')
|
|
42
|
+
expect(out.international).toMatch(/^\+972/)
|
|
43
|
+
})
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
it('normalizePhoneOrThrow (smart): international path without region', () => {
|
|
46
|
+
const us = exampleForRegion('US')
|
|
47
|
+
const out = normalizePhoneOrThrow(us.e164)
|
|
48
|
+
expect(out.e164).toBe(us.e164)
|
|
49
|
+
expect(out.regionCode).toBe('US')
|
|
50
|
+
})
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
it('normalizePhoneOrThrow (smart): national path requires defaultRegion', () => {
|
|
53
|
+
const il = exampleForRegion('IL')
|
|
54
|
+
const dirty = il.national.replace(/\s/g, ' - ')
|
|
55
|
+
const out = normalizePhoneOrThrow(dirty, { defaultRegion: 'IL' })
|
|
56
|
+
expect(out.e164).toBe(il.e164)
|
|
57
|
+
expect(out.regionCode).toBe('IL')
|
|
58
|
+
})
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
it('normalizePhoneOrThrow (smart): throws if national with no defaultRegion', () => {
|
|
61
|
+
expect(() => normalizePhoneOrThrow('054-123-4567')).toThrow(
|
|
62
|
+
/defaultRegion is required/i,
|
|
63
|
+
)
|
|
64
|
+
})
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
66
|
+
it('all helpers: throw on truly invalid numbers', () => {
|
|
67
|
+
expect(() => normalizePhoneOrThrowIntl('++972')).toThrow(
|
|
68
|
+
/Invalid phone number/i,
|
|
69
|
+
)
|
|
70
|
+
expect(() => normalizePhoneOrThrowWithRegion('123', 'IL')).toThrow(
|
|
71
|
+
/Invalid phone number/i,
|
|
72
|
+
)
|
|
73
|
+
expect(() => normalizePhoneOrThrow('++972')).toThrow(
|
|
74
|
+
/Invalid phone number/i,
|
|
75
|
+
)
|
|
76
|
+
})
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
it('should normalize a valid international number', () => {
|
|
79
|
+
const result = normalizePhoneOrThrowIntl('+972523444444')
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
expect(result).toMatchObject({
|
|
82
|
+
e164: '+972523444444',
|
|
83
|
+
national: expect.stringContaining('052'),
|
|
84
|
+
international: expect.stringContaining('+972'),
|
|
85
|
+
regionCode: 'IL',
|
|
86
|
+
type: expect.any(Number), // e.g. 1 = MOBILE
|
|
87
|
+
})
|
|
87
88
|
})
|
|
88
89
|
})
|
|
89
|
-
})
|
|
90
90
|
|
|
91
|
-
describe('phone normalization — no region (international) & with region', () => {
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
describe('phone normalization — no region (international) & with region', () => {
|
|
92
|
+
// Valid full international IL mobile number (E.164)
|
|
93
|
+
const intlIl = '+972523444444' // 052-344-4444
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
it('normalizePhoneOrThrowIntl: accepts full international number without region', () => {
|
|
96
|
+
const out = normalizePhoneOrThrowIntl(intlIl)
|
|
97
|
+
expect(out.e164).toBe(intlIl)
|
|
98
|
+
expect(out.regionCode).toBe('IL')
|
|
99
|
+
expect(out.international).toMatch(/^\+972/)
|
|
100
|
+
expect(typeof out.type).toBe('number')
|
|
101
|
+
})
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
it('normalizePhoneOrThrow (smart): accepts +972... without defaultRegion', () => {
|
|
104
|
+
const out = normalizePhoneOrThrow(intlIl) // no opts.defaultRegion
|
|
105
|
+
expect(out.e164).toBe(intlIl)
|
|
106
|
+
expect(out.regionCode).toBe('IL')
|
|
107
|
+
})
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
it('normalizePhoneOrThrowWithRegion: accepts national number with region', () => {
|
|
110
|
+
const out = normalizePhoneOrThrowWithRegion('052-344-4444', 'IL')
|
|
111
|
+
expect(out.e164).toBe(intlIl)
|
|
112
|
+
expect(out.regionCode).toBe('IL')
|
|
113
|
+
expect(out.national).toMatch(/052/)
|
|
114
|
+
})
|
|
114
115
|
})
|
|
115
116
|
})
|