asma-core-helpers 0.36.0 → 0.37.0
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/README.md +14 -0
- package/lib/phone/phoneNumber.d.ts +43 -2
- package/lib/phone/phoneNumber.d.ts.map +1 -1
- package/lib/phone/phoneNumber.js +51 -11
- package/lib/phone/phoneNumber.js.map +1 -1
- package/package.json +1 -1
- package/src/phone/phoneNumber.test.ts +231 -11
- package/src/phone/phoneNumber.ts +65 -11
package/README.md
CHANGED
|
@@ -41,6 +41,20 @@ parsePhoneNr('12345678', 'NO') // → { ok: false, reason: 'INVALID', input: '12
|
|
|
41
41
|
The stored form is bare E.164 (`+4748012345`). The `tel:` scheme belongs to the render of
|
|
42
42
|
a click-to-call link and is produced by `phoneTelHref`, never written to a field.
|
|
43
43
|
|
|
44
|
+
The read-only helpers refuse the same values the parser does, so nothing is invented at the
|
|
45
|
+
last moment either. `'0701234567'` is a Swedish mobile written nationally, and under `NO` it
|
|
46
|
+
is not a number — it stays readable and dialable, but as itself:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
formatPhoneForDisplay('0701234567', 'NO') // → '0701234567', shown as stored, not '+47 0701234567'
|
|
50
|
+
phoneTelHref('0701234567', 'NO') // → 'tel:0701234567', the stored digits, not 'tel:+470701234567'
|
|
51
|
+
phoneTelHref('ana@example.com') // → '', so the caller renders text instead of a link
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A number the plan confirms is canonicalised (`'48012345'` under `NO` → `tel:+4748012345`);
|
|
55
|
+
anything else is linked verbatim, so the dialer opens with the digits a therapist would have
|
|
56
|
+
read off the field rather than with a country we composed for them.
|
|
57
|
+
|
|
44
58
|
## Features
|
|
45
59
|
|
|
46
60
|
- **Environment utilities**: Environment detection, URL helpers, domain utilities
|
|
@@ -10,7 +10,21 @@ import type { PhoneCountry } from './phoneCountries.js';
|
|
|
10
10
|
export declare const PHONE_MIN_DIGITS = 4;
|
|
11
11
|
export declare const PHONE_MAX_DIGITS = 15;
|
|
12
12
|
export interface ParsedPhone {
|
|
13
|
+
/**
|
|
14
|
+
* The country to *edit* with. Always set, because a field needs a selection even for a value
|
|
15
|
+
* that carries no country of its own — in that case this is `fallbackIso2`.
|
|
16
|
+
*/
|
|
13
17
|
iso2: PhoneCountry;
|
|
18
|
+
/**
|
|
19
|
+
* The country the value actually *resolves to*, or `undefined` when nothing in the value
|
|
20
|
+
* says. Mirrors `parsePhoneNr(...).country`, and the two must agree: REQ-007.
|
|
21
|
+
*
|
|
22
|
+
* Read this, not `iso2`, before showing a user what country their stored number belongs to.
|
|
23
|
+
* `iso2` answers "which selection do I put in the picker"; `country` answers "do I actually
|
|
24
|
+
* know". Conflating them is what would let a therapist see a Norwegian flag on a foreign
|
|
25
|
+
* number and "correct" it into somebody else's valid Norwegian one.
|
|
26
|
+
*/
|
|
27
|
+
country: PhoneCountry | undefined;
|
|
14
28
|
/** National significant number, digits only — no dial code, no separators. */
|
|
15
29
|
national: string;
|
|
16
30
|
}
|
|
@@ -42,6 +56,12 @@ export declare function parsePhoneNr(input: string | null | undefined, defaultRe
|
|
|
42
56
|
/**
|
|
43
57
|
* Split a stored value into the country and the national number the field edits.
|
|
44
58
|
*
|
|
59
|
+
* `country` is what the **value** says, and `undefined` when it says nothing recognisable. A
|
|
60
|
+
* leading `+` is evidence: `'+4748'` resolves to `NO` while still being typed, because the dial
|
|
61
|
+
* code names the country even though the number is incomplete. A value with no `+` carries no
|
|
62
|
+
* such evidence, so the fallback is reported as the country only when the numbering plan confirms
|
|
63
|
+
* it — otherwise `country` is `undefined` and `iso2` is merely the selection to edit with.
|
|
64
|
+
*
|
|
45
65
|
* Three shapes reach this function:
|
|
46
66
|
* - E.164 (`'+4748012345'`) — the format this ticket introduces;
|
|
47
67
|
* - a bare national number (`'48012345'`) — every record written before it, which
|
|
@@ -90,12 +110,33 @@ export declare function isValidPhone(nationalInput: string, iso2: PhoneCountry):
|
|
|
90
110
|
export declare function isValidPhoneValue(value: string | null | undefined, fallbackIso2?: PhoneCountry): boolean;
|
|
91
111
|
/**
|
|
92
112
|
* Human-readable international form for read-only views, e.g. `'+47 48 01 23 45'`.
|
|
93
|
-
* Falls back to the
|
|
113
|
+
* Falls back to the stored value, byte for byte, for anything that is not a real number.
|
|
114
|
+
*
|
|
115
|
+
* The fallback covers the 13 production rows whose country cannot be derived — a plus-less
|
|
116
|
+
* `'0701234567'` is a Swedish mobile written nationally, and under the `NO` fallback it is not a
|
|
117
|
+
* number at all. Formatting it anyway produced `'+47 0701234567'`: a country the value never named,
|
|
118
|
+
* attached to digits that are not Norwegian. That is invariant 7 ("junk is refused, not
|
|
119
|
+
* transformed — none of them acquires a `+47` prefix") broken in the render rather than in the
|
|
120
|
+
* column, and it reads to a therapist as a number the system understood.
|
|
121
|
+
*
|
|
122
|
+
* Validity, not parseability, is the test. `parsePhoneNumberFromString` returns an object for
|
|
123
|
+
* `'+470701234567'` too, so the old `?? trimmed` fallback could never fire.
|
|
94
124
|
*/
|
|
95
125
|
export declare function formatPhoneForDisplay(value: string | null | undefined, fallbackIso2?: PhoneCountry): string;
|
|
96
126
|
/**
|
|
97
127
|
* `tel:` URI for a click-to-call link — the single place the scheme is added (ASMA-7485).
|
|
98
|
-
* Returns `''` when
|
|
128
|
+
* Returns `''` only when the value holds no number at all, so callers can skip the link.
|
|
129
|
+
*
|
|
130
|
+
* A number we can confirm is dialled in its canonical E.164 form. Anything else is still
|
|
131
|
+
* dialable — a therapist has to be able to ring the 13 production rows whose country is not
|
|
132
|
+
* derivable — but it is linked **exactly as stored**, so the dialer opens pre-filled with the
|
|
133
|
+
* digits a human would have read off the field and typed.
|
|
134
|
+
*
|
|
135
|
+
* What it must never link is a number we composed. `'0701234567'` under `NO` used to yield
|
|
136
|
+
* `tel:+470701234567`: a Swedish mobile with a Norwegian code welded on, which is a different
|
|
137
|
+
* subscriber, reads as authoritative, and on a phone dials without asking. `tel:0701234567` is
|
|
138
|
+
* the honest version of the same link — it may not connect from a Norwegian handset either, but
|
|
139
|
+
* it hands the therapist the real digits to complete rather than a confident wrong number.
|
|
99
140
|
*/
|
|
100
141
|
export declare function phoneTelHref(value: string | null | undefined, fallbackIso2?: PhoneCountry): string;
|
|
101
142
|
//# sourceMappingURL=phoneNumber.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"phoneNumber.d.ts","sourceRoot":"","sources":["../../src/phone/phoneNumber.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAUH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAEvD,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,gBAAgB,KAAK,CAAA;AAElC,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,YAAY,CAAA;IAClB,8EAA8E;IAC9E,QAAQ,EAAE,MAAM,CAAA;CACnB;AAID,MAAM,MAAM,YAAY,GAClB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,YAAY,GAAG,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAClF;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAA;AAE/D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,YAAY,GAAG,YAAY,CAYxG;AAED
|
|
1
|
+
{"version":3,"file":"phoneNumber.d.ts","sourceRoot":"","sources":["../../src/phone/phoneNumber.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAUH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAEvD,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,e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AGT;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACjC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAChC,YAAY,GAAE,YAAoC,GACnD,MAAM,CAQR;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CACxB,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAChC,YAAY,GAAE,YAAoC,GACnD,MAAM,CAUR"}
|
package/lib/phone/phoneNumber.js
CHANGED
|
@@ -42,6 +42,12 @@ export function parsePhoneNr(input, defaultRegion) {
|
|
|
42
42
|
/**
|
|
43
43
|
* Split a stored value into the country and the national number the field edits.
|
|
44
44
|
*
|
|
45
|
+
* `country` is what the **value** says, and `undefined` when it says nothing recognisable. A
|
|
46
|
+
* leading `+` is evidence: `'+4748'` resolves to `NO` while still being typed, because the dial
|
|
47
|
+
* code names the country even though the number is incomplete. A value with no `+` carries no
|
|
48
|
+
* such evidence, so the fallback is reported as the country only when the numbering plan confirms
|
|
49
|
+
* it — otherwise `country` is `undefined` and `iso2` is merely the selection to edit with.
|
|
50
|
+
*
|
|
45
51
|
* Three shapes reach this function:
|
|
46
52
|
* - E.164 (`'+4748012345'`) — the format this ticket introduces;
|
|
47
53
|
* - a bare national number (`'48012345'`) — every record written before it, which
|
|
@@ -52,13 +58,20 @@ export function parsePhoneNr(input, defaultRegion) {
|
|
|
52
58
|
export function parsePhoneValue(value, fallbackIso2 = DEFAULT_PHONE_COUNTRY) {
|
|
53
59
|
const trimmed = (value ?? '').trim();
|
|
54
60
|
if (trimmed.length === 0)
|
|
55
|
-
return { iso2: fallbackIso2, national: '' };
|
|
61
|
+
return { iso2: fallbackIso2, country: undefined, national: '' };
|
|
56
62
|
if (!trimmed.startsWith('+')) {
|
|
57
|
-
|
|
63
|
+
// No `+`, so the value itself names no country. The fallback is only *confirmed* when the
|
|
64
|
+
// digits are a real number under it; otherwise the country stays unknown and the caller
|
|
65
|
+
// must not present the fallback as fact. `libphonenumber-js` cannot recover a foreign
|
|
66
|
+
// calling code from a plus-less string — `'37376002949'` under `NO` yields `+4737376002949`
|
|
67
|
+
// with `isValid() === false`, not Moldova — so guessing here is always a guess.
|
|
68
|
+
const national = digitsOnly(trimmed);
|
|
69
|
+
const confirmed = isValidPhone(national, fallbackIso2);
|
|
70
|
+
return { iso2: fallbackIso2, country: confirmed ? fallbackIso2 : undefined, national };
|
|
58
71
|
}
|
|
59
72
|
const parsed = parsePhoneNumberFromString(trimmed);
|
|
60
73
|
if (parsed?.country !== undefined) {
|
|
61
|
-
return { iso2: parsed.country, national: parsed.nationalNumber };
|
|
74
|
+
return { iso2: parsed.country, country: parsed.country, national: parsed.nationalNumber };
|
|
62
75
|
}
|
|
63
76
|
const digits = digitsOnly(trimmed);
|
|
64
77
|
// Keep the country the caller already holds when the digits still agree with it,
|
|
@@ -66,12 +79,12 @@ export function parsePhoneValue(value, fallbackIso2 = DEFAULT_PHONE_COUNTRY) {
|
|
|
66
79
|
// to another country sharing the prefix.
|
|
67
80
|
const fallbackDialCode = getPhoneDialCode(fallbackIso2);
|
|
68
81
|
if (digits.startsWith(fallbackDialCode)) {
|
|
69
|
-
return { iso2: fallbackIso2, national: digits.slice(fallbackDialCode.length) };
|
|
82
|
+
return { iso2: fallbackIso2, country: fallbackIso2, national: digits.slice(fallbackDialCode.length) };
|
|
70
83
|
}
|
|
71
84
|
const iso2 = findCountryByDialCode(digits);
|
|
72
85
|
if (iso2 === undefined)
|
|
73
|
-
return { iso2: fallbackIso2, national: digits };
|
|
74
|
-
return { iso2, national: digits.slice(getPhoneDialCode(iso2).length) };
|
|
86
|
+
return { iso2: fallbackIso2, country: undefined, national: digits };
|
|
87
|
+
return { iso2, country: iso2, national: digits.slice(getPhoneDialCode(iso2).length) };
|
|
75
88
|
}
|
|
76
89
|
/**
|
|
77
90
|
* Compose the stored value from the country and whatever the user typed.
|
|
@@ -136,7 +149,17 @@ export function isValidPhoneValue(value, fallbackIso2 = DEFAULT_PHONE_COUNTRY) {
|
|
|
136
149
|
}
|
|
137
150
|
/**
|
|
138
151
|
* Human-readable international form for read-only views, e.g. `'+47 48 01 23 45'`.
|
|
139
|
-
* Falls back to the
|
|
152
|
+
* Falls back to the stored value, byte for byte, for anything that is not a real number.
|
|
153
|
+
*
|
|
154
|
+
* The fallback covers the 13 production rows whose country cannot be derived — a plus-less
|
|
155
|
+
* `'0701234567'` is a Swedish mobile written nationally, and under the `NO` fallback it is not a
|
|
156
|
+
* number at all. Formatting it anyway produced `'+47 0701234567'`: a country the value never named,
|
|
157
|
+
* attached to digits that are not Norwegian. That is invariant 7 ("junk is refused, not
|
|
158
|
+
* transformed — none of them acquires a `+47` prefix") broken in the render rather than in the
|
|
159
|
+
* column, and it reads to a therapist as a number the system understood.
|
|
160
|
+
*
|
|
161
|
+
* Validity, not parseability, is the test. `parsePhoneNumberFromString` returns an object for
|
|
162
|
+
* `'+470701234567'` too, so the old `?? trimmed` fallback could never fire.
|
|
140
163
|
*/
|
|
141
164
|
export function formatPhoneForDisplay(value, fallbackIso2 = DEFAULT_PHONE_COUNTRY) {
|
|
142
165
|
const trimmed = (value ?? '').trim();
|
|
@@ -144,15 +167,32 @@ export function formatPhoneForDisplay(value, fallbackIso2 = DEFAULT_PHONE_COUNTR
|
|
|
144
167
|
return '';
|
|
145
168
|
const { iso2, national } = parsePhoneValue(trimmed, fallbackIso2);
|
|
146
169
|
const parsed = parsePhoneNumberFromString(toE164(national, iso2));
|
|
147
|
-
return parsed?.formatInternational()
|
|
170
|
+
return parsed?.isValid() === true ? parsed.formatInternational() : trimmed;
|
|
148
171
|
}
|
|
149
172
|
/**
|
|
150
173
|
* `tel:` URI for a click-to-call link — the single place the scheme is added (ASMA-7485).
|
|
151
|
-
* Returns `''` when
|
|
174
|
+
* Returns `''` only when the value holds no number at all, so callers can skip the link.
|
|
175
|
+
*
|
|
176
|
+
* A number we can confirm is dialled in its canonical E.164 form. Anything else is still
|
|
177
|
+
* dialable — a therapist has to be able to ring the 13 production rows whose country is not
|
|
178
|
+
* derivable — but it is linked **exactly as stored**, so the dialer opens pre-filled with the
|
|
179
|
+
* digits a human would have read off the field and typed.
|
|
180
|
+
*
|
|
181
|
+
* What it must never link is a number we composed. `'0701234567'` under `NO` used to yield
|
|
182
|
+
* `tel:+470701234567`: a Swedish mobile with a Norwegian code welded on, which is a different
|
|
183
|
+
* subscriber, reads as authoritative, and on a phone dials without asking. `tel:0701234567` is
|
|
184
|
+
* the honest version of the same link — it may not connect from a Norwegian handset either, but
|
|
185
|
+
* it hands the therapist the real digits to complete rather than a confident wrong number.
|
|
152
186
|
*/
|
|
153
187
|
export function phoneTelHref(value, fallbackIso2 = DEFAULT_PHONE_COUNTRY) {
|
|
154
188
|
const { iso2, national } = parsePhoneValue(value, fallbackIso2);
|
|
155
|
-
|
|
156
|
-
|
|
189
|
+
if (isValidPhone(national, iso2))
|
|
190
|
+
return `tel:${toE164(national, iso2)}`;
|
|
191
|
+
const trimmed = (value ?? '').trim();
|
|
192
|
+
const digits = digitsOnly(trimmed);
|
|
193
|
+
if (digits.length < PHONE_MIN_DIGITS)
|
|
194
|
+
return '';
|
|
195
|
+
// The leading `+` is kept when the stored value carried one, and never added when it did not.
|
|
196
|
+
return `tel:${trimmed.startsWith('+') ? '+' : ''}${digits}`;
|
|
157
197
|
}
|
|
158
198
|
//# sourceMappingURL=phoneNumber.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"phoneNumber.js","sourceRoot":"","sources":["../../src/phone/phoneNumber.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACH,SAAS,EACT,kBAAkB,EAClB,0BAA0B,EAC1B,yBAAyB,GAC5B,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAGpG,0EAA0E;AAC1E,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAA;AACjC,MAAM,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E,MAAM,MAAM,GAAG,0BAA0B,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAA;IAEjE,OAAO,MAAM,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,mBAAmB,EAAE,CAAC,CAAC,CAAC,OAAO,CAAA;AAC9E,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CACxB,KAAgC,EAChC,YAAY,GAAiB,qBAAqB;IAElD,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;IAC/D,IAAI,YAAY,CAAC,QAAQ,EAAE,IAAI,CAAC;QAAE,OAAO,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAA;IAExE,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;IACpC,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAClC,IAAI,MAAM,CAAC,MAAM,GAAG,gBAAgB;QAAE,OAAO,EAAE,CAAA;IAE/C,8FAA8F;IAC9F,OAAO,OAAO,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,CAAA;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -65,33 +65,34 @@ describe('parsePhoneNr', () => {
|
|
|
65
65
|
|
|
66
66
|
describe('parsePhoneValue', () => {
|
|
67
67
|
it('splits a stored E.164 value into country and national number', () => {
|
|
68
|
-
assert.deepEqual(parsePhoneValue('+4748012345'), { iso2: 'NO', national: '48012345' })
|
|
69
|
-
assert.deepEqual(parsePhoneValue('+12015550123', 'NO'), { iso2: 'US', national: '2015550123' })
|
|
68
|
+
assert.deepEqual(parsePhoneValue('+4748012345'), { iso2: 'NO', country: 'NO', national: '48012345' })
|
|
69
|
+
assert.deepEqual(parsePhoneValue('+12015550123', 'NO'), { iso2: 'US', country: 'US', national: '2015550123' })
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
it('reads a pre-ASMA-7485 bare national number as the fallback country', () => {
|
|
73
|
-
// Every record written before this ticket is 8 bare digits with no country.
|
|
74
|
-
|
|
75
|
-
assert.deepEqual(parsePhoneValue('48012345', '
|
|
73
|
+
// Every record written before this ticket is 8 bare digits with no country. The fallback is
|
|
74
|
+
// what the field edits with; `country` says whether the numbering plan actually confirms it.
|
|
75
|
+
assert.deepEqual(parsePhoneValue('48012345', 'NO'), { iso2: 'NO', country: 'NO', national: '48012345' })
|
|
76
|
+
assert.deepEqual(parsePhoneValue('48012345', 'SE'), { iso2: 'SE', country: 'SE', national: '48012345' })
|
|
76
77
|
})
|
|
77
78
|
|
|
78
79
|
it('keeps the caller country while a matching number is still being typed', () => {
|
|
79
|
-
assert.deepEqual(parsePhoneValue('+47', 'NO'), { iso2: 'NO', national: '' })
|
|
80
|
-
assert.deepEqual(parsePhoneValue('+4748', 'NO'), { iso2: 'NO', national: '48' })
|
|
80
|
+
assert.deepEqual(parsePhoneValue('+47', 'NO'), { iso2: 'NO', country: 'NO', national: '' })
|
|
81
|
+
assert.deepEqual(parsePhoneValue('+4748', 'NO'), { iso2: 'NO', country: 'NO', national: '48' })
|
|
81
82
|
})
|
|
82
83
|
|
|
83
84
|
it('switches country when the typed prefix belongs to another one', () => {
|
|
84
|
-
assert.deepEqual(parsePhoneValue('+46701', 'NO'), { iso2: 'SE', national: '701' })
|
|
85
|
+
assert.deepEqual(parsePhoneValue('+46701', 'NO'), { iso2: 'SE', country: 'SE', national: '701' })
|
|
85
86
|
})
|
|
86
87
|
|
|
87
88
|
it('treats empty, whitespace and nullish values as an empty number', () => {
|
|
88
89
|
for (const value of ['', ' ', null, undefined]) {
|
|
89
|
-
assert.deepEqual(parsePhoneValue(value, 'NO'), { iso2: 'NO', national: '' })
|
|
90
|
+
assert.deepEqual(parsePhoneValue(value, 'NO'), { iso2: 'NO', country: undefined, national: '' })
|
|
90
91
|
}
|
|
91
92
|
})
|
|
92
93
|
|
|
93
94
|
it('falls back to the given country for an unknown calling code', () => {
|
|
94
|
-
assert.deepEqual(parsePhoneValue('+9991234', 'NO'), { iso2: 'NO', national: '9991234' })
|
|
95
|
+
assert.deepEqual(parsePhoneValue('+9991234', 'NO'), { iso2: 'NO', country: undefined, national: '9991234' })
|
|
95
96
|
})
|
|
96
97
|
})
|
|
97
98
|
|
|
@@ -136,7 +137,7 @@ describe('toE164', () => {
|
|
|
136
137
|
|
|
137
138
|
it('round-trips through parsePhoneValue', () => {
|
|
138
139
|
const parsed = parsePhoneValue(toE164('48012345', 'NO'))
|
|
139
|
-
assert.deepEqual(parsed, { iso2: 'NO', national: '48012345' })
|
|
140
|
+
assert.deepEqual(parsed, { iso2: 'NO', country: 'NO', national: '48012345' })
|
|
140
141
|
})
|
|
141
142
|
})
|
|
142
143
|
|
|
@@ -178,6 +179,16 @@ describe('isValidPhone', () => {
|
|
|
178
179
|
assert.equal(isValidPhone('00000000', 'NO'), false)
|
|
179
180
|
})
|
|
180
181
|
|
|
182
|
+
it('accepts a short number where the plan is genuinely short', () => {
|
|
183
|
+
// The same rule in the accepting direction, which the case above does not cover: six digits
|
|
184
|
+
// is not a number in Norway and is one in the Faroes and Greenland — both reachable from
|
|
185
|
+
// the picker, and both plausible for a Nordic customer. Raising PHONE_MIN_DIGITS to the
|
|
186
|
+
// Norwegian length would read as a tidy-up and would silently lock those subscribers out.
|
|
187
|
+
assert.equal(isValidPhone('211234', 'FO'), true)
|
|
188
|
+
assert.equal(isValidPhone('221234', 'GL'), true)
|
|
189
|
+
assert.equal(isValidPhone('123456', 'NO'), false)
|
|
190
|
+
})
|
|
191
|
+
|
|
181
192
|
it('rejects numbers outside the E.164 length bounds', () => {
|
|
182
193
|
assert.equal(isValidPhone('123', 'NO'), false)
|
|
183
194
|
assert.equal(isValidPhone('1234567890123456', 'NO'), false)
|
|
@@ -223,6 +234,21 @@ describe('formatPhoneForDisplay', () => {
|
|
|
223
234
|
assert.equal(formatPhoneForDisplay(''), '')
|
|
224
235
|
assert.equal(formatPhoneForDisplay(null), '')
|
|
225
236
|
})
|
|
237
|
+
|
|
238
|
+
it('shows a value it cannot resolve exactly as stored, inventing no country', () => {
|
|
239
|
+
// The 13 rows measured 2026-09-14 whose country is not derivable, plus the junk classes.
|
|
240
|
+
// `'+47 0701234567'` is what this used to render for the first of them: the fallback region
|
|
241
|
+
// presented as fact, on digits that are not Norwegian (invariant 7).
|
|
242
|
+
assert.equal(formatPhoneForDisplay('0701234567', 'NO'), '0701234567')
|
|
243
|
+
assert.equal(formatPhoneForDisplay('37376002949', 'NO'), '37376002949')
|
|
244
|
+
assert.equal(formatPhoneForDisplay('12345678', 'NO'), '12345678')
|
|
245
|
+
assert.equal(formatPhoneForDisplay('ana@example.com', 'NO'), 'ana@example.com')
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('resolves the same digits once the country is known', () => {
|
|
249
|
+
// The same number, stored after a therapist picked Sweden. Nothing was guessed here.
|
|
250
|
+
assert.equal(formatPhoneForDisplay('+46701234567'), '+46 70 123 45 67')
|
|
251
|
+
})
|
|
226
252
|
})
|
|
227
253
|
|
|
228
254
|
describe('phoneTelHref', () => {
|
|
@@ -239,4 +265,198 @@ describe('phoneTelHref', () => {
|
|
|
239
265
|
assert.equal(phoneTelHref(''), '')
|
|
240
266
|
assert.equal(phoneTelHref(null), '')
|
|
241
267
|
})
|
|
268
|
+
|
|
269
|
+
it('still dials a value it cannot resolve, using the digits as stored', () => {
|
|
270
|
+
// These stay reachable on purpose — a therapist has to be able to ring them — but the link
|
|
271
|
+
// carries what the column holds, not a country composed for it. `tel:+470701234567` was
|
|
272
|
+
// offered here: a Swedish mobile with a Norwegian code welded on, a different subscriber.
|
|
273
|
+
assert.equal(phoneTelHref('0701234567', 'NO'), 'tel:0701234567')
|
|
274
|
+
assert.equal(phoneTelHref('37376002949', 'NO'), 'tel:37376002949')
|
|
275
|
+
assert.equal(phoneTelHref('12345678', 'NO'), 'tel:12345678')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it('keeps a stored `+` and never invents one', () => {
|
|
279
|
+
// Invalid under NO, but the value names its own code, so the link repeats it verbatim.
|
|
280
|
+
assert.equal(phoneTelHref('+4712345678', 'NO'), 'tel:+4712345678')
|
|
281
|
+
assert.equal(phoneTelHref('47 01 23 45 67', 'NO'), 'tel:4701234567')
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it('offers no link when the value holds no number at all', () => {
|
|
285
|
+
assert.equal(phoneTelHref('ana@example.com', 'NO'), '')
|
|
286
|
+
assert.equal(phoneTelHref('ukjent', 'NO'), '')
|
|
287
|
+
assert.equal(phoneTelHref('123', 'NO'), '')
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('still dials a legacy bare number that is valid for the fallback country', () => {
|
|
291
|
+
assert.equal(phoneTelHref('48012345', 'NO'), 'tel:+4748012345')
|
|
292
|
+
})
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Phase 7 invariants (plan §2, TASK-030). These are the properties the production corpus is run
|
|
297
|
+
* against; the cases here are the shape classes measured on 2026-09-14, so a regression shows up
|
|
298
|
+
* in CI rather than only in a corpus run someone has to remember to do.
|
|
299
|
+
*
|
|
300
|
+
* No real number appears here (CON-008). Fixtures are the ticket's example and libphonenumber's.
|
|
301
|
+
*/
|
|
302
|
+
describe('REQ-007 — the two parsers agree (TEST-009)', () => {
|
|
303
|
+
const REGION: PhoneCountry = 'NO'
|
|
304
|
+
|
|
305
|
+
// One value per measured shape class, plus the shapes that only exist in the ambiguous tail.
|
|
306
|
+
const corpus = [
|
|
307
|
+
'45456565',
|
|
308
|
+
'48012345',
|
|
309
|
+
'+4745456565',
|
|
310
|
+
'4745456565',
|
|
311
|
+
'004745456565',
|
|
312
|
+
'+47 454 56 565',
|
|
313
|
+
'+46701234567',
|
|
314
|
+
'+37379094538',
|
|
315
|
+
'37376002949',
|
|
316
|
+
'0701234567',
|
|
317
|
+
'12345678',
|
|
318
|
+
'00000000',
|
|
319
|
+
'123',
|
|
320
|
+
'ana@example.com',
|
|
321
|
+
'',
|
|
322
|
+
]
|
|
323
|
+
|
|
324
|
+
for (const value of corpus) {
|
|
325
|
+
it(`agrees on ${value === '' ? '(empty)' : value}`, () => {
|
|
326
|
+
const loose = parsePhoneValue(value, REGION)
|
|
327
|
+
const strict = parsePhoneNr(value, REGION)
|
|
328
|
+
|
|
329
|
+
if (strict.ok) {
|
|
330
|
+
assert.equal(loose.country, strict.country)
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// The failure this guards: reporting the fallback as though the value named it. A
|
|
335
|
+
// therapist would see a Norwegian flag on a foreign number and "correct" it into
|
|
336
|
+
// somebody else's valid Norwegian one.
|
|
337
|
+
assert.notEqual(
|
|
338
|
+
loose.country,
|
|
339
|
+
REGION,
|
|
340
|
+
`parsePhoneValue claimed ${REGION} for a value parsePhoneNr rejects: ${value}`,
|
|
341
|
+
)
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
it('still offers a country to edit with even when it does not know one', () => {
|
|
346
|
+
// `iso2` and `country` answer different questions, and the field needs both.
|
|
347
|
+
const parsed = parsePhoneValue('37376002949', 'NO')
|
|
348
|
+
|
|
349
|
+
assert.equal(parsed.iso2, 'NO')
|
|
350
|
+
assert.equal(parsed.country, undefined)
|
|
351
|
+
})
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
describe('REQ-007 — idempotence (TEST-010)', () => {
|
|
355
|
+
const shapes: [string, PhoneCountry][] = [
|
|
356
|
+
['45456565', 'NO'],
|
|
357
|
+
['+4745456565', 'NO'],
|
|
358
|
+
['4745456565', 'NO'],
|
|
359
|
+
['004745456565', 'NO'],
|
|
360
|
+
['+47 454 56 565', 'NO'],
|
|
361
|
+
['0701234567', 'SE'],
|
|
362
|
+
['+46701234567', 'SE'],
|
|
363
|
+
['2015550123', 'US'],
|
|
364
|
+
]
|
|
365
|
+
|
|
366
|
+
for (const [value, region] of shapes) {
|
|
367
|
+
it(`normalising ${value} twice changes nothing the second time`, () => {
|
|
368
|
+
const once = toE164(parsePhoneValue(value, region).national, region)
|
|
369
|
+
const twice = toE164(parsePhoneValue(once, region).national, region)
|
|
370
|
+
|
|
371
|
+
// A failure here means the Adopus sync rewrites the same row forever (RISK-001).
|
|
372
|
+
assert.equal(twice, once)
|
|
373
|
+
})
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
it('is idempotent through parsePhoneNr as well', () => {
|
|
377
|
+
const first = parsePhoneNr('004745456565', 'NO')
|
|
378
|
+
assert.equal(first.ok, true)
|
|
379
|
+
|
|
380
|
+
const second = parsePhoneNr(first.ok ? first.e164 : '', 'NO')
|
|
381
|
+
assert.equal(second.ok && second.e164, first.ok && first.e164)
|
|
382
|
+
})
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
describe('REQ-007 — no value gains validity (TEST-011)', () => {
|
|
386
|
+
it('does not turn a Swedish national number into a Norwegian one', () => {
|
|
387
|
+
// `0701234567` is a real Swedish mobile written nationally. Under NO it is nothing, and it
|
|
388
|
+
// must stay nothing: the plus-less branch has no way to learn the country, and guessing
|
|
389
|
+
// would silently hand the SMS to whoever owns the Norwegian number it invents.
|
|
390
|
+
assert.equal(parsePhoneNr('0701234567', 'NO').ok, false)
|
|
391
|
+
assert.equal(isValidPhone('0701234567', 'NO'), false)
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
it('does not resolve a plus-less Moldovan number to Norway', () => {
|
|
395
|
+
// Measured 2026-09-14: `parsePhoneNumberWithError('37376002949', 'NO')` yields
|
|
396
|
+
// `+4737376002949` with `isValid() === false` — not Moldova. The INVALID verdict is what
|
|
397
|
+
// stops that string being stored as a Norwegian number.
|
|
398
|
+
const parsed = parsePhoneNr('37376002949', 'NO')
|
|
399
|
+
|
|
400
|
+
assert.equal(parsed.ok, false)
|
|
401
|
+
assert.equal(parsed.ok === false && parsed.reason, 'INVALID')
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
it('keeps junk invalid rather than prefixing it', () => {
|
|
405
|
+
for (const junk of ['12345678', '00000000', '123', 'ana@example.com']) {
|
|
406
|
+
assert.equal(parsePhoneNr(junk, 'NO').ok, false, junk)
|
|
407
|
+
}
|
|
408
|
+
})
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
describe('REQ-007 — one case per measured shape class (TEST-012)', () => {
|
|
412
|
+
// The classes and their production counts, measured 2026-09-14 (plan §0.2).
|
|
413
|
+
const classes: [string, string, string | 'INVALID'][] = [
|
|
414
|
+
['bare 8 digits · 90,190 rows', '45456565', '+4745456565'],
|
|
415
|
+
['already E.164 +47 · 1,730 rows', '+4745456565', '+4745456565'],
|
|
416
|
+
['47 + 8, no plus · 268 rows', '4745456565', '+4745456565'],
|
|
417
|
+
['contains separators · 127 rows', '+47 454 56 565', '+4745456565'],
|
|
418
|
+
['E.164, not Norway · 25 rows', '+37379094538', '+37379094538'],
|
|
419
|
+
['1–7 digits · 16 rows', '123456', 'INVALID'],
|
|
420
|
+
['0047 + 8 · 9 rows', '004745456565', '+4745456565'],
|
|
421
|
+
['9+ digits, no plus · 9 rows', '37376002949', 'INVALID'],
|
|
422
|
+
['leading zero national · 4 rows', '0745456565', 'INVALID'],
|
|
423
|
+
['contains @ · 26 rows', 'ana@example.com', 'INVALID'],
|
|
424
|
+
]
|
|
425
|
+
|
|
426
|
+
for (const [label, value, expected] of classes) {
|
|
427
|
+
it(label, () => {
|
|
428
|
+
const parsed = parsePhoneNr(value, 'NO')
|
|
429
|
+
|
|
430
|
+
if (expected === 'INVALID') {
|
|
431
|
+
assert.equal(parsed.ok, false, `${value} should not parse`)
|
|
432
|
+
return
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
assert.equal(parsed.ok && parsed.e164, expected)
|
|
436
|
+
})
|
|
437
|
+
}
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
describe('REQ-007 — the shared-plan collision is known, not solved (TEST-013)', () => {
|
|
441
|
+
it('reads the same digits as two different valid numbers depending on the region', () => {
|
|
442
|
+
// `20123456` is a valid Norwegian landline and a valid Danish mobile. Nothing in the digits
|
|
443
|
+
// distinguishes them, so the region must come from an explicit selection wherever a foreign
|
|
444
|
+
// number is possible — never from a default. This test documents the limit; it does not
|
|
445
|
+
// pretend the limit is gone.
|
|
446
|
+
const asNorwegian = parsePhoneNr('20123456', 'NO')
|
|
447
|
+
const asDanish = parsePhoneNr('20123456', 'DK')
|
|
448
|
+
|
|
449
|
+
assert.equal(asNorwegian.ok, true)
|
|
450
|
+
assert.equal(asNorwegian.ok && asNorwegian.e164, '+4720123456')
|
|
451
|
+
|
|
452
|
+
assert.equal(asDanish.ok, true)
|
|
453
|
+
assert.equal(asDanish.ok && asDanish.e164, '+4520123456')
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
it('is why a default region may never stand in for a user choice', () => {
|
|
457
|
+
// The same string, stored bare, is unrecoverable: both readings are valid numbers owned by
|
|
458
|
+
// different people. `parsePhoneValue` must therefore not claim a country for it.
|
|
459
|
+
assert.equal(parsePhoneValue('20123456', 'NO').country, 'NO')
|
|
460
|
+
assert.equal(parsePhoneValue('20123456', 'DK').country, 'DK')
|
|
461
|
+
})
|
|
242
462
|
})
|
package/src/phone/phoneNumber.ts
CHANGED
|
@@ -21,7 +21,21 @@ export const PHONE_MIN_DIGITS = 4
|
|
|
21
21
|
export const PHONE_MAX_DIGITS = 15
|
|
22
22
|
|
|
23
23
|
export interface ParsedPhone {
|
|
24
|
+
/**
|
|
25
|
+
* The country to *edit* with. Always set, because a field needs a selection even for a value
|
|
26
|
+
* that carries no country of its own — in that case this is `fallbackIso2`.
|
|
27
|
+
*/
|
|
24
28
|
iso2: PhoneCountry
|
|
29
|
+
/**
|
|
30
|
+
* The country the value actually *resolves to*, or `undefined` when nothing in the value
|
|
31
|
+
* says. Mirrors `parsePhoneNr(...).country`, and the two must agree: REQ-007.
|
|
32
|
+
*
|
|
33
|
+
* Read this, not `iso2`, before showing a user what country their stored number belongs to.
|
|
34
|
+
* `iso2` answers "which selection do I put in the picker"; `country` answers "do I actually
|
|
35
|
+
* know". Conflating them is what would let a therapist see a Norwegian flag on a foreign
|
|
36
|
+
* number and "correct" it into somebody else's valid Norwegian one.
|
|
37
|
+
*/
|
|
38
|
+
country: PhoneCountry | undefined
|
|
25
39
|
/** National significant number, digits only — no dial code, no separators. */
|
|
26
40
|
national: string
|
|
27
41
|
}
|
|
@@ -63,6 +77,12 @@ export function parsePhoneNr(input: string | null | undefined, defaultRegion: Ph
|
|
|
63
77
|
/**
|
|
64
78
|
* Split a stored value into the country and the national number the field edits.
|
|
65
79
|
*
|
|
80
|
+
* `country` is what the **value** says, and `undefined` when it says nothing recognisable. A
|
|
81
|
+
* leading `+` is evidence: `'+4748'` resolves to `NO` while still being typed, because the dial
|
|
82
|
+
* code names the country even though the number is incomplete. A value with no `+` carries no
|
|
83
|
+
* such evidence, so the fallback is reported as the country only when the numbering plan confirms
|
|
84
|
+
* it — otherwise `country` is `undefined` and `iso2` is merely the selection to edit with.
|
|
85
|
+
*
|
|
66
86
|
* Three shapes reach this function:
|
|
67
87
|
* - E.164 (`'+4748012345'`) — the format this ticket introduces;
|
|
68
88
|
* - a bare national number (`'48012345'`) — every record written before it, which
|
|
@@ -75,15 +95,23 @@ export function parsePhoneValue(
|
|
|
75
95
|
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
76
96
|
): ParsedPhone {
|
|
77
97
|
const trimmed = (value ?? '').trim()
|
|
78
|
-
if (trimmed.length === 0) return { iso2: fallbackIso2, national: '' }
|
|
98
|
+
if (trimmed.length === 0) return { iso2: fallbackIso2, country: undefined, national: '' }
|
|
79
99
|
|
|
80
100
|
if (!trimmed.startsWith('+')) {
|
|
81
|
-
|
|
101
|
+
// No `+`, so the value itself names no country. The fallback is only *confirmed* when the
|
|
102
|
+
// digits are a real number under it; otherwise the country stays unknown and the caller
|
|
103
|
+
// must not present the fallback as fact. `libphonenumber-js` cannot recover a foreign
|
|
104
|
+
// calling code from a plus-less string — `'37376002949'` under `NO` yields `+4737376002949`
|
|
105
|
+
// with `isValid() === false`, not Moldova — so guessing here is always a guess.
|
|
106
|
+
const national = digitsOnly(trimmed)
|
|
107
|
+
const confirmed = isValidPhone(national, fallbackIso2)
|
|
108
|
+
|
|
109
|
+
return { iso2: fallbackIso2, country: confirmed ? fallbackIso2 : undefined, national }
|
|
82
110
|
}
|
|
83
111
|
|
|
84
112
|
const parsed = parsePhoneNumberFromString(trimmed)
|
|
85
113
|
if (parsed?.country !== undefined) {
|
|
86
|
-
return { iso2: parsed.country, national: parsed.nationalNumber }
|
|
114
|
+
return { iso2: parsed.country, country: parsed.country, national: parsed.nationalNumber }
|
|
87
115
|
}
|
|
88
116
|
|
|
89
117
|
const digits = digitsOnly(trimmed)
|
|
@@ -93,13 +121,13 @@ export function parsePhoneValue(
|
|
|
93
121
|
// to another country sharing the prefix.
|
|
94
122
|
const fallbackDialCode = getPhoneDialCode(fallbackIso2)
|
|
95
123
|
if (digits.startsWith(fallbackDialCode)) {
|
|
96
|
-
return { iso2: fallbackIso2, national: digits.slice(fallbackDialCode.length) }
|
|
124
|
+
return { iso2: fallbackIso2, country: fallbackIso2, national: digits.slice(fallbackDialCode.length) }
|
|
97
125
|
}
|
|
98
126
|
|
|
99
127
|
const iso2 = findCountryByDialCode(digits)
|
|
100
|
-
if (iso2 === undefined) return { iso2: fallbackIso2, national: digits }
|
|
128
|
+
if (iso2 === undefined) return { iso2: fallbackIso2, country: undefined, national: digits }
|
|
101
129
|
|
|
102
|
-
return { iso2, national: digits.slice(getPhoneDialCode(iso2).length) }
|
|
130
|
+
return { iso2, country: iso2, national: digits.slice(getPhoneDialCode(iso2).length) }
|
|
103
131
|
}
|
|
104
132
|
|
|
105
133
|
/**
|
|
@@ -173,7 +201,17 @@ export function isValidPhoneValue(
|
|
|
173
201
|
|
|
174
202
|
/**
|
|
175
203
|
* Human-readable international form for read-only views, e.g. `'+47 48 01 23 45'`.
|
|
176
|
-
* Falls back to the
|
|
204
|
+
* Falls back to the stored value, byte for byte, for anything that is not a real number.
|
|
205
|
+
*
|
|
206
|
+
* The fallback covers the 13 production rows whose country cannot be derived — a plus-less
|
|
207
|
+
* `'0701234567'` is a Swedish mobile written nationally, and under the `NO` fallback it is not a
|
|
208
|
+
* number at all. Formatting it anyway produced `'+47 0701234567'`: a country the value never named,
|
|
209
|
+
* attached to digits that are not Norwegian. That is invariant 7 ("junk is refused, not
|
|
210
|
+
* transformed — none of them acquires a `+47` prefix") broken in the render rather than in the
|
|
211
|
+
* column, and it reads to a therapist as a number the system understood.
|
|
212
|
+
*
|
|
213
|
+
* Validity, not parseability, is the test. `parsePhoneNumberFromString` returns an object for
|
|
214
|
+
* `'+470701234567'` too, so the old `?? trimmed` fallback could never fire.
|
|
177
215
|
*/
|
|
178
216
|
export function formatPhoneForDisplay(
|
|
179
217
|
value: string | null | undefined,
|
|
@@ -185,19 +223,35 @@ export function formatPhoneForDisplay(
|
|
|
185
223
|
const { iso2, national } = parsePhoneValue(trimmed, fallbackIso2)
|
|
186
224
|
const parsed = parsePhoneNumberFromString(toE164(national, iso2))
|
|
187
225
|
|
|
188
|
-
return parsed?.formatInternational()
|
|
226
|
+
return parsed?.isValid() === true ? parsed.formatInternational() : trimmed
|
|
189
227
|
}
|
|
190
228
|
|
|
191
229
|
/**
|
|
192
230
|
* `tel:` URI for a click-to-call link — the single place the scheme is added (ASMA-7485).
|
|
193
|
-
* Returns `''` when
|
|
231
|
+
* Returns `''` only when the value holds no number at all, so callers can skip the link.
|
|
232
|
+
*
|
|
233
|
+
* A number we can confirm is dialled in its canonical E.164 form. Anything else is still
|
|
234
|
+
* dialable — a therapist has to be able to ring the 13 production rows whose country is not
|
|
235
|
+
* derivable — but it is linked **exactly as stored**, so the dialer opens pre-filled with the
|
|
236
|
+
* digits a human would have read off the field and typed.
|
|
237
|
+
*
|
|
238
|
+
* What it must never link is a number we composed. `'0701234567'` under `NO` used to yield
|
|
239
|
+
* `tel:+470701234567`: a Swedish mobile with a Norwegian code welded on, which is a different
|
|
240
|
+
* subscriber, reads as authoritative, and on a phone dials without asking. `tel:0701234567` is
|
|
241
|
+
* the honest version of the same link — it may not connect from a Norwegian handset either, but
|
|
242
|
+
* it hands the therapist the real digits to complete rather than a confident wrong number.
|
|
194
243
|
*/
|
|
195
244
|
export function phoneTelHref(
|
|
196
245
|
value: string | null | undefined,
|
|
197
246
|
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
198
247
|
): string {
|
|
199
248
|
const { iso2, national } = parsePhoneValue(value, fallbackIso2)
|
|
200
|
-
|
|
249
|
+
if (isValidPhone(national, iso2)) return `tel:${toE164(national, iso2)}`
|
|
250
|
+
|
|
251
|
+
const trimmed = (value ?? '').trim()
|
|
252
|
+
const digits = digitsOnly(trimmed)
|
|
253
|
+
if (digits.length < PHONE_MIN_DIGITS) return ''
|
|
201
254
|
|
|
202
|
-
|
|
255
|
+
// The leading `+` is kept when the stored value carried one, and never added when it did not.
|
|
256
|
+
return `tel:${trimmed.startsWith('+') ? '+' : ''}${digits}`
|
|
203
257
|
}
|