asma-core-helpers 0.35.0 → 0.36.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 +25 -0
- package/lib/phone/index.d.ts +15 -0
- package/lib/phone/index.d.ts.map +1 -0
- package/lib/phone/index.js +15 -0
- package/lib/phone/index.js.map +1 -0
- package/lib/phone/phoneCountries.d.ts +44 -0
- package/lib/phone/phoneCountries.d.ts.map +1 -0
- package/lib/phone/phoneCountries.js +84 -0
- package/lib/phone/phoneCountries.js.map +1 -0
- package/lib/phone/phoneExample.d.ts +17 -0
- package/lib/phone/phoneExample.d.ts.map +1 -0
- package/lib/phone/phoneExample.js +21 -0
- package/lib/phone/phoneExample.js.map +1 -0
- package/lib/phone/phoneNumber.d.ts +101 -0
- package/lib/phone/phoneNumber.d.ts.map +1 -0
- package/lib/phone/phoneNumber.js +158 -0
- package/lib/phone/phoneNumber.js.map +1 -0
- package/package.json +6 -1
- package/src/phone/index.ts +38 -0
- package/src/phone/phoneCountries.test.ts +96 -0
- package/src/phone/phoneCountries.ts +118 -0
- package/src/phone/phoneExample.test.ts +17 -0
- package/src/phone/phoneExample.ts +24 -0
- package/src/phone/phoneNumber.test.ts +242 -0
- package/src/phone/phoneNumber.ts +203 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phone-number formatting, parsing and validation for ASMA forms (ASMA-7485).
|
|
3
|
+
*
|
|
4
|
+
* The stored form is bare E.164 (`+4748012345`) — digits and a leading `+`, no
|
|
5
|
+
* spaces, no `tel:`. The `tel:` scheme belongs to the render of a click-to-call
|
|
6
|
+
* link and is produced by `phoneTelHref`, never written to a field.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
AsYouType,
|
|
11
|
+
isValidPhoneNumber,
|
|
12
|
+
parsePhoneNumberFromString,
|
|
13
|
+
parsePhoneNumberWithError,
|
|
14
|
+
} from 'libphonenumber-js'
|
|
15
|
+
|
|
16
|
+
import { DEFAULT_PHONE_COUNTRY, findCountryByDialCode, getPhoneDialCode } from './phoneCountries.js'
|
|
17
|
+
import type { PhoneCountry } from './phoneCountries.js'
|
|
18
|
+
|
|
19
|
+
/** Shortest and longest national significant number in the E.164 plan. */
|
|
20
|
+
export const PHONE_MIN_DIGITS = 4
|
|
21
|
+
export const PHONE_MAX_DIGITS = 15
|
|
22
|
+
|
|
23
|
+
export interface ParsedPhone {
|
|
24
|
+
iso2: PhoneCountry
|
|
25
|
+
/** National significant number, digits only — no dial code, no separators. */
|
|
26
|
+
national: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const digitsOnly = (value: string): string => value.replace(/\D/g, '')
|
|
30
|
+
|
|
31
|
+
export type PhoneNrParse =
|
|
32
|
+
| { ok: true; e164: string; country: PhoneCountry | undefined; callingCode: string }
|
|
33
|
+
| { ok: false; reason: 'EMPTY' | 'INVALID'; input: string }
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Canonicalise a value of unknown shape into E.164 — the one function every writer and
|
|
37
|
+
* the backfill share, so runtime and migration agree byte for byte.
|
|
38
|
+
*
|
|
39
|
+
* `defaultRegion` is required rather than defaulted on purpose. Guessing a region at
|
|
40
|
+
* *read* time, repeatedly and implicitly, is what left the fleet with several
|
|
41
|
+
* `+47`-assuming normalizers that disagree; the region is an explicit input applied
|
|
42
|
+
* once, where a value enters the system, and only when the input carries no `+`.
|
|
43
|
+
*
|
|
44
|
+
* Returns a result rather than throwing or coercing: a value that is not a real number
|
|
45
|
+
* under that region — junk like `'12345678'`, or a Swedish national number stored bare
|
|
46
|
+
* in a Norwegian tenant — is unrecoverable by any algorithm and must be reported to a
|
|
47
|
+
* human, never rewritten into something plausible.
|
|
48
|
+
*/
|
|
49
|
+
export function parsePhoneNr(input: string | null | undefined, defaultRegion: PhoneCountry): PhoneNrParse {
|
|
50
|
+
const text = (input ?? '').trim()
|
|
51
|
+
if (text.length === 0) return { ok: false, reason: 'EMPTY', input: text }
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const parsed = parsePhoneNumberWithError(text, defaultRegion)
|
|
55
|
+
if (!parsed.isValid()) return { ok: false, reason: 'INVALID', input: text }
|
|
56
|
+
|
|
57
|
+
return { ok: true, e164: parsed.number, country: parsed.country, callingCode: parsed.countryCallingCode }
|
|
58
|
+
} catch {
|
|
59
|
+
return { ok: false, reason: 'INVALID', input: text }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Split a stored value into the country and the national number the field edits.
|
|
65
|
+
*
|
|
66
|
+
* Three shapes reach this function:
|
|
67
|
+
* - E.164 (`'+4748012345'`) — the format this ticket introduces;
|
|
68
|
+
* - a bare national number (`'48012345'`) — every record written before it, which
|
|
69
|
+
* is why `fallbackIso2` exists rather than the value being rejected;
|
|
70
|
+
* - a partially typed `+47…`, which `parsePhoneNumberFromString` cannot parse yet
|
|
71
|
+
* and which therefore falls through to the longest-dial-code match.
|
|
72
|
+
*/
|
|
73
|
+
export function parsePhoneValue(
|
|
74
|
+
value: string | null | undefined,
|
|
75
|
+
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
76
|
+
): ParsedPhone {
|
|
77
|
+
const trimmed = (value ?? '').trim()
|
|
78
|
+
if (trimmed.length === 0) return { iso2: fallbackIso2, national: '' }
|
|
79
|
+
|
|
80
|
+
if (!trimmed.startsWith('+')) {
|
|
81
|
+
return { iso2: fallbackIso2, national: digitsOnly(trimmed) }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const parsed = parsePhoneNumberFromString(trimmed)
|
|
85
|
+
if (parsed?.country !== undefined) {
|
|
86
|
+
return { iso2: parsed.country, national: parsed.nationalNumber }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const digits = digitsOnly(trimmed)
|
|
90
|
+
|
|
91
|
+
// Keep the country the caller already holds when the digits still agree with it,
|
|
92
|
+
// so typing `+47…` one character at a time never reassigns the user's selection
|
|
93
|
+
// to another country sharing the prefix.
|
|
94
|
+
const fallbackDialCode = getPhoneDialCode(fallbackIso2)
|
|
95
|
+
if (digits.startsWith(fallbackDialCode)) {
|
|
96
|
+
return { iso2: fallbackIso2, national: digits.slice(fallbackDialCode.length) }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const iso2 = findCountryByDialCode(digits)
|
|
100
|
+
if (iso2 === undefined) return { iso2: fallbackIso2, national: digits }
|
|
101
|
+
|
|
102
|
+
return { iso2, national: digits.slice(getPhoneDialCode(iso2).length) }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Compose the stored value from the country and whatever the user typed.
|
|
107
|
+
*
|
|
108
|
+
* Two branches, and the split is the point. Once the input is a real number for the
|
|
109
|
+
* country, the stored value is the library's own E.164 rendering, which drops what a
|
|
110
|
+
* plain `+dialCode + digits` concatenation would double: a trunk prefix (`0701234567`
|
|
111
|
+
* for SE), a redundant country code (`4748012345` for NO) or an IDD prefix
|
|
112
|
+
* (`004748012345`). Concatenating those produced `+460701234567` and `+474748012345`.
|
|
113
|
+
*
|
|
114
|
+
* Until then it falls back to concatenation, because an in-progress number must still
|
|
115
|
+
* round-trip through the store for the field to show it back — so this deliberately
|
|
116
|
+
* does not reject anything. Use `isValidPhone` for the save gate. Returns `''` for an
|
|
117
|
+
* empty national part so an untouched field stores nothing rather than a bare `'+47'`.
|
|
118
|
+
*/
|
|
119
|
+
export function toE164(nationalInput: string, iso2: PhoneCountry): string {
|
|
120
|
+
const digits = digitsOnly(nationalInput)
|
|
121
|
+
if (digits.length === 0) return ''
|
|
122
|
+
|
|
123
|
+
const parsed = parsePhoneNumberFromString(digits, iso2)
|
|
124
|
+
if (parsed?.isValid() === true) return parsed.number
|
|
125
|
+
|
|
126
|
+
return `+${getPhoneDialCode(iso2)}${digits}`
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Progressive per-country grouping for the input mask, e.g. `'48012345'` → `'48 01 23 45'`
|
|
131
|
+
* for NO, `'70 123 45 67'` for SE and `'213 373 4253'` for US.
|
|
132
|
+
*
|
|
133
|
+
* Groups by the country's **international** format rather than its national one, and
|
|
134
|
+
* the difference is not cosmetic: countries with a trunk prefix (SE, GB, DE, FR) define
|
|
135
|
+
* their national formats *with* the leading `0`, so feeding the bare national number to
|
|
136
|
+
* `AsYouType(iso2)` returns it unformatted. The field edits the national part while the
|
|
137
|
+
* selector carries the country code, which is exactly the international shape.
|
|
138
|
+
*
|
|
139
|
+
* Grouping therefore follows each country's official format — for Norwegian numbers
|
|
140
|
+
* `xx xx xx xx`, not the `### ## ###` the legacy `NORWEGIAN_PHONE_NUMBER` constant
|
|
141
|
+
* hardcoded for every Norwegian number regardless of its prefix.
|
|
142
|
+
*/
|
|
143
|
+
export function formatNationalAsYouType(nationalInput: string, iso2: PhoneCountry): string {
|
|
144
|
+
const digits = digitsOnly(nationalInput)
|
|
145
|
+
if (digits.length === 0) return ''
|
|
146
|
+
|
|
147
|
+
const dialCode = `+${getPhoneDialCode(iso2)}`
|
|
148
|
+
const formatted = new AsYouType().input(`${dialCode}${digits}`)
|
|
149
|
+
|
|
150
|
+
return formatted.startsWith(dialCode) ? formatted.slice(dialCode.length).trimStart() : formatted
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Whether the national number is a real, dialable number for the country —
|
|
155
|
+
* a per-country pattern check, not a digit count. `'00000000'` and `'12345678'`
|
|
156
|
+
* are both rejected for NO, `'48012345'` is accepted.
|
|
157
|
+
*/
|
|
158
|
+
export function isValidPhone(nationalInput: string, iso2: PhoneCountry): boolean {
|
|
159
|
+
const digits = digitsOnly(nationalInput)
|
|
160
|
+
if (digits.length < PHONE_MIN_DIGITS || digits.length > PHONE_MAX_DIGITS) return false
|
|
161
|
+
|
|
162
|
+
return isValidPhoneNumber(digits, iso2)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Whether a stored value is a valid number, resolving its country from the value itself. */
|
|
166
|
+
export function isValidPhoneValue(
|
|
167
|
+
value: string | null | undefined,
|
|
168
|
+
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
169
|
+
): boolean {
|
|
170
|
+
const { iso2, national } = parsePhoneValue(value, fallbackIso2)
|
|
171
|
+
return isValidPhone(national, iso2)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Human-readable international form for read-only views, e.g. `'+47 48 01 23 45'`.
|
|
176
|
+
* Falls back to the raw value so an unparseable legacy record still renders.
|
|
177
|
+
*/
|
|
178
|
+
export function formatPhoneForDisplay(
|
|
179
|
+
value: string | null | undefined,
|
|
180
|
+
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
181
|
+
): string {
|
|
182
|
+
const trimmed = (value ?? '').trim()
|
|
183
|
+
if (trimmed.length === 0) return ''
|
|
184
|
+
|
|
185
|
+
const { iso2, national } = parsePhoneValue(trimmed, fallbackIso2)
|
|
186
|
+
const parsed = parsePhoneNumberFromString(toE164(national, iso2))
|
|
187
|
+
|
|
188
|
+
return parsed?.formatInternational() ?? trimmed
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* `tel:` URI for a click-to-call link — the single place the scheme is added (ASMA-7485).
|
|
193
|
+
* Returns `''` when there is nothing to dial, so callers can skip rendering the link.
|
|
194
|
+
*/
|
|
195
|
+
export function phoneTelHref(
|
|
196
|
+
value: string | null | undefined,
|
|
197
|
+
fallbackIso2: PhoneCountry = DEFAULT_PHONE_COUNTRY,
|
|
198
|
+
): string {
|
|
199
|
+
const { iso2, national } = parsePhoneValue(value, fallbackIso2)
|
|
200
|
+
const e164 = toE164(national, iso2)
|
|
201
|
+
|
|
202
|
+
return e164.length === 0 ? '' : `tel:${e164}`
|
|
203
|
+
}
|