@zappie/foundations 0.12.4 → 0.12.6

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 CHANGED
@@ -1,5 +1,20 @@
1
1
  # @zappie/foundations
2
2
 
3
+ ## 0.12.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 9543421: Typing in the phone field's country search stays quick on slow phones. The search box updates on each key and the list follows in a render React can interrupt, and a new search no longer renders the list twice.
8
+ - 6b8d29b: The phone field's country list is searchable with a mouse or trackpad. `PhoneCountryPicker` from `@zappie/foundations/phone` opens a combobox that finds a country by name, ISO code or dial code ("+44", "44", "kingdom" and "gb" all find the United Kingdom), with pinned countries first. It renders only the rows in view and loads, with the full country list, when the shopper points at or focuses the button. On touch it keeps the platform's own select. Where a system doesn't draw flag emoji, the field shows the ISO code instead. The kit's `phone-field` 1.1.0 uses it and takes `pinnedCountries`.
9
+
10
+ ## 0.12.5
11
+
12
+ ### Patch Changes
13
+
14
+ - 9810d26: Take a phone number from any country. `@zappie/foundations/phone` reads, groups and checks a number for its country and gives it in E.164, with `usePhoneField` for your own markup. The full country list loads on its own, from `@zappie/foundations/phone/countries` or through `loadPhoneCountries`, so a page pays for it only when a shopper reaches for the list. The kit's new `phone-field` is a labelled field with a country list showing each flag, name and dial code. A pasted number with its own code switches the country. Studio names it "Phone number".
15
+ - Updated dependencies [9810d26]
16
+ - @zappie/sdk@0.10.10
17
+
3
18
  ## 0.12.4
4
19
 
5
20
  ### Patch Changes
@@ -0,0 +1,195 @@
1
+ /** A country as a phone field needs it. The four markets carry their own number rules. */
2
+ type PhoneCountry = {
3
+ /** The ISO country code, such as `NG`. */
4
+ code: PhoneCountryCode;
5
+ /** The country's name, for the list. */
6
+ name: string;
7
+ /** The dial code without the plus, such as `234`. */
8
+ dial: string;
9
+ /**
10
+ * How many digits a mobile number has after the dial code, with no leading zero. Set for the
11
+ * four markets. Other countries take 6 digits or more, up to 15 with the dial code, as E.164
12
+ * allows.
13
+ */
14
+ length?: number;
15
+ /**
16
+ * How the national digits are grouped as you type: 803 123 4567 is `[3, 3, 4]`. Set for the
17
+ * four markets. Other countries group in threes, with a four at the end.
18
+ */
19
+ groups?: number[];
20
+ /** The national number keeps its leading 0 after the dial code, as Italy's does. */
21
+ keepsZero?: boolean;
22
+ };
23
+ /** The four markets Zappie runs in. */
24
+ type PhoneMarketCode = "NG" | "GH" | "KE" | "ZA";
25
+ /** An ISO country code. The four markets are spelled out so an editor offers them first. */
26
+ type PhoneCountryCode = PhoneMarketCode | (string & {});
27
+ /** What a phone number's digits add up to for its country. */
28
+ type PhoneCheck = {
29
+ /** The number is whole for its country. An empty number is not valid, and not wrong either. */
30
+ valid: boolean;
31
+ /** A sentence to show once the shopper leaves a part-typed number, or `null`. */
32
+ problem: string | null;
33
+ };
34
+ /** The four markets, with their exact number lengths and groups. */
35
+ declare const PHONE_MARKETS: readonly PhoneCountry[];
36
+ /**
37
+ * One of the four markets by its ISO code, or `undefined`. Every other country is in the full
38
+ * list, which `loadPhoneCountries` loads.
39
+ *
40
+ * @param code - The ISO country code, in any case.
41
+ */
42
+ declare function marketCountry(code: string | null | undefined): PhoneCountry | undefined;
43
+ /**
44
+ * The country's flag as an emoji, made from its ISO code, so it costs no images. A system
45
+ * without flag emoji shows the two letters instead.
46
+ *
47
+ * @param code - The ISO country code.
48
+ */
49
+ declare function countryFlag(code: string): string;
50
+ /**
51
+ * Whether this system draws flag emoji as flags. Windows draws the two regional letters
52
+ * instead, twice as wide, so a field shows the ISO code there. True on the server.
53
+ */
54
+ declare function flagEmojiRenders(): boolean;
55
+ /**
56
+ * The most national digits a country takes: its own length, or what E.164 leaves after the dial
57
+ * code.
58
+ *
59
+ * @param country - The country.
60
+ */
61
+ declare function maxNationalDigits(country: PhoneCountry): number;
62
+ /**
63
+ * Whether the text carries its own country code: a plus before the first digit, as in
64
+ * `+234 803 123 4567` or `Tel: +234 (0) 803 123 4567`, or the 00 people dial from a landline,
65
+ * as in `00234 803 123 4567`.
66
+ *
67
+ * @param raw - The text as typed or pasted.
68
+ */
69
+ declare function hasDialPrefix(raw: string): boolean;
70
+ /**
71
+ * The digits of a number that carries its own country code, without the plus or the 00, or
72
+ * `null` when it carries none.
73
+ *
74
+ * @param raw - The text as typed or pasted.
75
+ */
76
+ declare function internationalDigits(raw: string): string | null;
77
+ /**
78
+ * The national digits from what the shopper typed or pasted: digits only, a leading 0 dropped
79
+ * unless the country keeps it, at most the country's length. A number pasted with its plus and
80
+ * dial code, such as `+234 803 123 4567`, loses the code too.
81
+ *
82
+ * @param raw - The text as typed or pasted.
83
+ * @param country - The country the number is in.
84
+ */
85
+ declare function nationalDigits(raw: string, country: PhoneCountry): string;
86
+ /**
87
+ * The groups for a country without its own rule: threes, with a four at the end when a three
88
+ * would leave one digit over, and two fours when it would leave two. Up to five digits stay as
89
+ * one group.
90
+ *
91
+ * @param count - How many national digits there are.
92
+ */
93
+ declare function generalGroups(count: number): number[];
94
+ /**
95
+ * The national digits grouped as people read them: `8031234567` as `803 123 4567`. A part-typed
96
+ * number groups as far as it goes.
97
+ *
98
+ * @param digits - The national digits.
99
+ * @param country - The country the number is in.
100
+ */
101
+ declare function formatNational(digits: string, country: PhoneCountry): string;
102
+ /**
103
+ * The number in E.164, such as `+2348031234567`, when the national digits are a whole number
104
+ * for the country, or `null`.
105
+ *
106
+ * @param digits - The national digits.
107
+ * @param country - The country the number is in.
108
+ */
109
+ declare function toE164(digits: string, country: PhoneCountry): string | null;
110
+ /**
111
+ * What is wrong with a phone number, as a sentence to show under the field, or `null` when it is
112
+ * whole. An empty number is not wrong: say it is missing in your own words.
113
+ *
114
+ * @param digits - The national digits.
115
+ * @param country - The country the number is in.
116
+ */
117
+ declare function phoneProblem(digits: string, country: PhoneCountry): string | null;
118
+ /**
119
+ * Whether the national digits are a whole number, and what to say when they are not.
120
+ *
121
+ * @param digits - The national digits.
122
+ * @param country - The country the number is in.
123
+ */
124
+ declare function phoneCheck(digits: string, country: PhoneCountry): PhoneCheck;
125
+ /**
126
+ * A phone number as the API takes it: a plus and the country code, then the digits. A local
127
+ * number with a leading zero is read with the dial code given, so `024 000 0000` with `233`
128
+ * becomes `+233240000000`. Returns `null` when the text can't be read as a number.
129
+ *
130
+ * @param raw - The number as typed.
131
+ * @param dialCode - The dial code a local number takes, without the plus. Nigeria's by default.
132
+ */
133
+ declare function normalisePhone(raw: string, dialCode?: string): string | null;
134
+ /** The pinned countries in the order given, then every other country in list order. */
135
+ declare function pinnedFirst(countries: readonly PhoneCountry[], pinned: readonly string[]): {
136
+ pinned: PhoneCountry[];
137
+ rest: PhoneCountry[];
138
+ };
139
+
140
+ /**
141
+ * Every country a phone field can take, A to Z by name. The four markets carry their own number
142
+ * rules; everyone else is read by the general E.164 rules.
143
+ */
144
+ declare const PHONE_COUNTRIES: readonly PhoneCountry[];
145
+ /**
146
+ * A country by its ISO code, or `undefined` for a code the list doesn't have.
147
+ *
148
+ * @param code - The ISO country code, in any case.
149
+ */
150
+ declare function findPhoneCountry(code: string | null | undefined): PhoneCountry | undefined;
151
+ /**
152
+ * The dial code for a country, without the plus. An unknown code gives Nigeria's, `234`.
153
+ *
154
+ * @param code - The ISO country code.
155
+ */
156
+ declare function dialCodeFor(code: string | null | undefined): string;
157
+ /**
158
+ * Which country and national digits a number with its own country code belongs to, so a field
159
+ * can start from a saved number or follow a pasted one. The longest dial code that fits wins.
160
+ * When countries share it (+1, +44), `prefer` keeps the one the field is in, and otherwise the
161
+ * code's home country is used: +1 is the United States. `null` when the text carries no
162
+ * country code or no dial code fits.
163
+ *
164
+ * @param value - An E.164 number, or text with a plus or 00 before the number.
165
+ * @param prefer - The ISO code of the country the field is in.
166
+ */
167
+ declare function splitE164(value: string, prefer?: string): {
168
+ country: PhoneCountry;
169
+ digits: string;
170
+ } | null;
171
+ /**
172
+ * Whether the shopper is still typing a country code: a plus or a 00 with too few digits after
173
+ * it for any country. A field shows such text as typed until a code resolves.
174
+ *
175
+ * @param raw - The text as typed.
176
+ */
177
+ declare function typingDialCode(raw: string): boolean;
178
+
179
+ declare const __countries_PHONE_COUNTRIES: typeof PHONE_COUNTRIES;
180
+ declare const __countries_dialCodeFor: typeof dialCodeFor;
181
+ declare const __countries_findPhoneCountry: typeof findPhoneCountry;
182
+ declare const __countries_splitE164: typeof splitE164;
183
+ declare const __countries_typingDialCode: typeof typingDialCode;
184
+ declare namespace __countries {
185
+ export {
186
+ __countries_PHONE_COUNTRIES as PHONE_COUNTRIES,
187
+ __countries_dialCodeFor as dialCodeFor,
188
+ __countries_findPhoneCountry as findPhoneCountry,
189
+ __countries_splitE164 as splitE164,
190
+ __countries_typingDialCode as typingDialCode,
191
+ };
192
+ }
193
+
194
+ export { __countries as _, PHONE_MARKETS as c, countryFlag as e, flagEmojiRenders as f, formatNational as g, generalGroups as h, hasDialPrefix as i, internationalDigits as j, maxNationalDigits as k, normalisePhone as l, marketCountry as m, nationalDigits as n, phoneProblem as o, phoneCheck as p, pinnedFirst as q, PHONE_COUNTRIES as r, dialCodeFor as s, toE164 as t, findPhoneCountry as u, splitE164 as v, typingDialCode as w };
195
+ export type { PhoneCountry as P, PhoneCheck as a, PhoneCountryCode as b, PhoneMarketCode as d };
@@ -0,0 +1,296 @@
1
+ "use client";
2
+
3
+ // src/phone/combobox.tsx
4
+ import {
5
+ useDeferredValue,
6
+ useEffect,
7
+ useLayoutEffect,
8
+ useMemo,
9
+ useRef,
10
+ useState
11
+ } from "react";
12
+ import { LiveRegion } from "../internal/combobox.js";
13
+ import { pinnedFirst } from "./core.js";
14
+ import { PHONE_COUNTRIES } from "./countries.js";
15
+ import { SHARED_DIAL_HOME } from "./country-data.js";
16
+ import { jsx, jsxs } from "react/jsx-runtime";
17
+ var ROW = 36;
18
+ var GAP = 9;
19
+ var VISIBLE = 8;
20
+ var OVERSCAN = 5;
21
+ var fold = (text) => text.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
22
+ var indexes = /* @__PURE__ */ new WeakMap();
23
+ var indexOf = (countries) => {
24
+ let index = indexes.get(countries);
25
+ if (!index) {
26
+ index = countries.map((country) => ({
27
+ country,
28
+ name: fold(country.name),
29
+ code: country.code.toLowerCase()
30
+ }));
31
+ indexes.set(countries, index);
32
+ }
33
+ return index;
34
+ };
35
+ function searchCountries(countries, query) {
36
+ const text = fold(query.trim());
37
+ const digits = text.replace(/^\+/, "");
38
+ const numeric = /^\d+$/.test(digits);
39
+ const rank = ({ country, name, code }) => {
40
+ if (numeric && country.dial.startsWith(digits)) {
41
+ if (country.dial !== digits) {
42
+ return 1.5;
43
+ }
44
+ const home = SHARED_DIAL_HOME[digits];
45
+ return !home || home === country.code ? 0 : 1;
46
+ }
47
+ if (code === text) {
48
+ return 0;
49
+ }
50
+ if (name.startsWith(text)) {
51
+ return 2;
52
+ }
53
+ return name.includes(text) ? 3 : 9;
54
+ };
55
+ return indexOf(countries).map((entry, order) => ({
56
+ country: entry.country,
57
+ order,
58
+ rank: rank(entry)
59
+ })).filter((entry) => entry.rank < 9).sort((a, b) => a.rank - b.rank || a.order - b.order).map((entry) => entry.country);
60
+ }
61
+ function PhoneCombobox({
62
+ open,
63
+ id,
64
+ country,
65
+ pinned,
66
+ seed,
67
+ mark,
68
+ onChoose,
69
+ onClose
70
+ }) {
71
+ const [query, setQuery] = useState(seed);
72
+ const listed = useDeferredValue(query);
73
+ const [nav, setNav] = useState(null);
74
+ const [message, setMessage] = useState("");
75
+ const search = useRef(null);
76
+ const latest = useRef({
77
+ items: [],
78
+ active: 0,
79
+ top: 0
80
+ });
81
+ const list = useRef(null);
82
+ const pins = pinned.join();
83
+ const { items, split } = useMemo(() => {
84
+ if (listed.trim()) {
85
+ return { items: searchCountries(PHONE_COUNTRIES, listed), split: 0 };
86
+ }
87
+ const groups = pinnedFirst(PHONE_COUNTRIES, pins.split(","));
88
+ return {
89
+ items: [...groups.pinned, ...groups.rest],
90
+ split: groups.rest.length > 0 ? groups.pinned.length : 0
91
+ };
92
+ }, [listed, pins]);
93
+ const offset = (index) => index * ROW + (split && index >= split ? GAP : 0);
94
+ const indexAt = (y) => Math.floor((split && y >= split * ROW + GAP ? y - GAP : y) / ROW);
95
+ const place = (index, from) => {
96
+ const active2 = Math.max(0, Math.min(items.length - 1, index));
97
+ const height = VISIBLE * ROW;
98
+ const y = offset(active2);
99
+ return {
100
+ items,
101
+ active: active2,
102
+ top: y < from ? y : y + ROW > from + height ? y + ROW - height : from
103
+ };
104
+ };
105
+ const { active, top } = nav && nav.items === items ? nav : place(
106
+ listed.trim() ? 0 : Math.max(
107
+ 0,
108
+ items.findIndex((entry) => entry.code === country.code)
109
+ ),
110
+ 0
111
+ );
112
+ const go = (index) => setNav(place(index, top));
113
+ useEffect(() => {
114
+ if (open) {
115
+ setQuery(seed);
116
+ setNav(null);
117
+ search.current?.focus();
118
+ }
119
+ }, [open, seed]);
120
+ useEffect(() => {
121
+ const node = list.current;
122
+ if (!node) {
123
+ return;
124
+ }
125
+ let frame = 0;
126
+ const onScroll = () => {
127
+ if (!frame) {
128
+ frame = requestAnimationFrame(() => {
129
+ frame = 0;
130
+ const y = node.scrollTop;
131
+ setNav(
132
+ (now) => now && now.items === latest.current.items && now.top === y ? now : { ...latest.current, top: y }
133
+ );
134
+ });
135
+ }
136
+ };
137
+ node.addEventListener("scroll", onScroll, { passive: true });
138
+ return () => {
139
+ cancelAnimationFrame(frame);
140
+ node.removeEventListener("scroll", onScroll);
141
+ };
142
+ }, []);
143
+ useLayoutEffect(() => {
144
+ if (list.current && list.current.scrollTop !== top) {
145
+ list.current.scrollTop = top;
146
+ }
147
+ }, [top]);
148
+ latest.current = { items, active, top };
149
+ const onKeyDown = (event) => {
150
+ const moves = {
151
+ ArrowDown: active + 1,
152
+ ArrowUp: active - 1,
153
+ PageDown: active + VISIBLE,
154
+ PageUp: active - VISIBLE,
155
+ Home: 0,
156
+ End: items.length - 1
157
+ };
158
+ if (event.key in moves) {
159
+ event.preventDefault();
160
+ go(moves[event.key]);
161
+ } else if (event.key === "Enter") {
162
+ event.preventDefault();
163
+ const chosen = listed === query ? items[active] : query.trim() ? searchCountries(PHONE_COUNTRIES, query)[0] : items[active];
164
+ if (chosen) {
165
+ onChoose(chosen.code);
166
+ }
167
+ } else if (event.key === "Escape") {
168
+ event.preventDefault();
169
+ onClose(true);
170
+ } else if (event.key === "Tab") {
171
+ onClose(false);
172
+ }
173
+ };
174
+ const start = Math.max(0, indexAt(top) - OVERSCAN);
175
+ const end = Math.min(
176
+ items.length,
177
+ indexAt(top + VISIBLE * ROW) + 1 + OVERSCAN
178
+ );
179
+ const rows = items.slice(start, end);
180
+ const gapAbove = split && start > split ? GAP : 0;
181
+ const gapBelow = split && end <= split ? GAP : 0;
182
+ const optionId = (code) => `${id}-${code}`;
183
+ const optionProps = (code, index) => ({
184
+ id: optionId(code),
185
+ role: "option",
186
+ "aria-selected": code === country.code,
187
+ "aria-setsize": count,
188
+ "aria-posinset": index + 1,
189
+ onMouseDown: (event) => event.preventDefault(),
190
+ onMouseMove: () => index !== active && setNav({ items, active: index, top }),
191
+ onClick: () => onChoose(code)
192
+ });
193
+ const count = items.length;
194
+ useEffect(() => {
195
+ const timer = setTimeout(
196
+ () => setMessage(
197
+ !open ? "" : count === 0 ? "No countries match." : `${count} ${count === 1 ? "country" : "countries"}`
198
+ ),
199
+ 300
200
+ );
201
+ return () => clearTimeout(timer);
202
+ }, [open, count, listed]);
203
+ return /* @__PURE__ */ jsxs(
204
+ "div",
205
+ {
206
+ hidden: !open,
207
+ "data-phone-popup": "",
208
+ onBlur: (event) => {
209
+ if (open && !event.currentTarget.contains(event.relatedTarget)) {
210
+ onClose(false);
211
+ }
212
+ },
213
+ children: [
214
+ /* @__PURE__ */ jsx(
215
+ "input",
216
+ {
217
+ ref: search,
218
+ role: "combobox",
219
+ "aria-label": "Search countries",
220
+ "aria-expanded": open,
221
+ "aria-controls": id,
222
+ "aria-autocomplete": "list",
223
+ "aria-activedescendant": open && items[active] ? optionId(items[active].code) : void 0,
224
+ autoComplete: "off",
225
+ spellCheck: false,
226
+ value: query,
227
+ onChange: (event) => setQuery(event.currentTarget.value),
228
+ onKeyDown,
229
+ "data-phone-search": ""
230
+ }
231
+ ),
232
+ /* @__PURE__ */ jsxs(
233
+ "div",
234
+ {
235
+ ref: list,
236
+ id,
237
+ role: "listbox",
238
+ "aria-label": "Countries",
239
+ style: { maxHeight: VISIBLE * ROW, overflowY: "auto" },
240
+ "data-phone-list": "",
241
+ children: [
242
+ /* @__PURE__ */ jsx("div", { role: "presentation", style: { height: start * ROW + gapAbove } }),
243
+ rows.map((entry, at) => {
244
+ const index = start + at;
245
+ return [
246
+ split && index === split ? /* @__PURE__ */ jsx(
247
+ "div",
248
+ {
249
+ role: "presentation",
250
+ style: { height: GAP },
251
+ "data-phone-gap": ""
252
+ },
253
+ "gap"
254
+ ) : null,
255
+ /* @__PURE__ */ jsxs(
256
+ "div",
257
+ {
258
+ ...optionProps(entry.code, index),
259
+ "data-active": index === active ? "" : void 0,
260
+ style: { height: ROW },
261
+ children: [
262
+ /* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: mark(entry.code) }),
263
+ " ",
264
+ /* @__PURE__ */ jsx("span", { children: entry.name }),
265
+ " ",
266
+ /* @__PURE__ */ jsxs("span", { children: [
267
+ "+",
268
+ entry.dial
269
+ ] })
270
+ ]
271
+ },
272
+ entry.code
273
+ )
274
+ ];
275
+ }),
276
+ /* @__PURE__ */ jsx(
277
+ "div",
278
+ {
279
+ role: "presentation",
280
+ style: { height: (items.length - end) * ROW + gapBelow }
281
+ }
282
+ )
283
+ ]
284
+ }
285
+ ),
286
+ /* @__PURE__ */ jsx(LiveRegion, { message })
287
+ ]
288
+ }
289
+ );
290
+ }
291
+ export {
292
+ ROW,
293
+ PhoneCombobox as default,
294
+ searchCountries
295
+ };
296
+ //# sourceMappingURL=combobox.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/phone/combobox.tsx"],
4
+ "mappings": ";;;AAEA;AAAA,EACE;AAAA,E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AAA;AAAA;AAAA,EAChC;AAEJ;",
5
+ "names": ["active"]
6
+ }
@@ -0,0 +1,163 @@
1
+ // src/phone/core.ts
2
+ var PHONE_MARKETS = [
3
+ { code: "NG", name: "Nigeria", dial: "234", length: 10, groups: [3, 3, 4] },
4
+ { code: "GH", name: "Ghana", dial: "233", length: 9, groups: [2, 3, 4] },
5
+ { code: "KE", name: "Kenya", dial: "254", length: 9, groups: [3, 3, 3] },
6
+ {
7
+ code: "ZA",
8
+ name: "South Africa",
9
+ dial: "27",
10
+ length: 9,
11
+ groups: [2, 3, 4]
12
+ }
13
+ ];
14
+ function marketCountry(code) {
15
+ const wanted = code?.trim().toUpperCase();
16
+ return PHONE_MARKETS.find((country) => country.code === wanted);
17
+ }
18
+ function countryFlag(code) {
19
+ const letters = code.trim().toUpperCase();
20
+ if (!/^[A-Z]{2}$/.test(letters)) {
21
+ return "";
22
+ }
23
+ return String.fromCodePoint(
24
+ ...[...letters].map((letter) => 127462 + letter.charCodeAt(0) - 65)
25
+ );
26
+ }
27
+ function flagEmojiRenders() {
28
+ try {
29
+ const context = document.createElement("canvas").getContext("2d");
30
+ context.font = "32px sans-serif";
31
+ return context.measureText("\u{1F1F3}\u{1F1EC}").width < context.measureText("\u{1F1F3}").width * 1.5;
32
+ } catch {
33
+ return true;
34
+ }
35
+ }
36
+ var E164_MAX = 15;
37
+ var GENERAL_MIN = 6;
38
+ function maxNationalDigits(country) {
39
+ return country.length ?? E164_MAX - country.dial.length;
40
+ }
41
+ function hasDialPrefix(raw) {
42
+ return /^[^\d]*\+/.test(raw) || /^\D*00\d/.test(raw);
43
+ }
44
+ function internationalDigits(raw) {
45
+ if (!hasDialPrefix(raw)) {
46
+ return null;
47
+ }
48
+ const digits = raw.replace(/\D/g, "");
49
+ return /^[^\d]*\+/.test(raw) ? digits : digits.replace(/^00/, "");
50
+ }
51
+ function nationalDigits(raw, country) {
52
+ let digits = internationalDigits(raw) ?? raw.replace(/\D/g, "");
53
+ if (hasDialPrefix(raw) && digits.startsWith(country.dial)) {
54
+ digits = digits.slice(country.dial.length);
55
+ } else if (country.length !== void 0 && digits.startsWith(country.dial) && digits.length === country.dial.length + country.length) {
56
+ digits = digits.slice(country.dial.length);
57
+ }
58
+ if (!country.keepsZero) {
59
+ digits = digits.replace(/^0+/, "");
60
+ }
61
+ return digits.slice(0, maxNationalDigits(country));
62
+ }
63
+ function generalGroups(count) {
64
+ if (count <= 5) {
65
+ return [count];
66
+ }
67
+ const rest = count % 3;
68
+ const threes = rest === 0 ? count / 3 : rest === 1 ? (count - 4) / 3 : (count - 8) / 3;
69
+ return [
70
+ ...Array(threes).fill(3),
71
+ ...rest === 1 ? [4] : rest === 2 ? [4, 4] : []
72
+ ];
73
+ }
74
+ function formatNational(digits, country) {
75
+ const groups = country.groups ?? generalGroups(digits.length);
76
+ const parts = [];
77
+ let at = 0;
78
+ for (const size of groups) {
79
+ if (at >= digits.length) {
80
+ break;
81
+ }
82
+ parts.push(digits.slice(at, at + size));
83
+ at += size;
84
+ }
85
+ if (at < digits.length) {
86
+ parts.push(digits.slice(at));
87
+ }
88
+ return parts.join(" ");
89
+ }
90
+ function isWhole(digits, country) {
91
+ if (country.length !== void 0) {
92
+ return digits.length === country.length;
93
+ }
94
+ return digits.length >= GENERAL_MIN && digits.length <= maxNationalDigits(country);
95
+ }
96
+ function toE164(digits, country) {
97
+ if (!/^\d*$/.test(digits) || !isWhole(digits, country)) {
98
+ return null;
99
+ }
100
+ return `+${country.dial}${digits}`;
101
+ }
102
+ function phoneProblem(digits, country) {
103
+ if (digits === "" || isWhole(digits, country)) {
104
+ return null;
105
+ }
106
+ if (country.length !== void 0) {
107
+ return `Enter all ${country.length} digits of the number after +${country.dial}.`;
108
+ }
109
+ return `Enter ${GENERAL_MIN} to ${maxNationalDigits(country)} digits after +${country.dial}.`;
110
+ }
111
+ function phoneCheck(digits, country) {
112
+ return {
113
+ valid: toE164(digits, country) !== null,
114
+ problem: phoneProblem(digits, country)
115
+ };
116
+ }
117
+ function normalisePhone(raw, dialCode = "234") {
118
+ const trimmed = raw.trim();
119
+ const digits = trimmed.replace(/\D/g, "");
120
+ if (!digits) {
121
+ return null;
122
+ }
123
+ if (trimmed.startsWith("+")) {
124
+ return digits.length >= 8 && digits.length <= 15 ? `+${digits}` : null;
125
+ }
126
+ if (digits.startsWith("0") && digits.length >= 9 && digits.length <= 11) {
127
+ return `+${dialCode}${digits.slice(1)}`;
128
+ }
129
+ if (digits.length === 10) {
130
+ return `+${dialCode}${digits}`;
131
+ }
132
+ if (digits.startsWith(dialCode) && digits.length === dialCode.length + 10) {
133
+ return `+${digits}`;
134
+ }
135
+ return null;
136
+ }
137
+ function pinnedFirst(countries, pinned) {
138
+ const first = pinned.flatMap(
139
+ (code) => countries.find((country) => country.code === code) ?? []
140
+ );
141
+ return {
142
+ pinned: first,
143
+ rest: countries.filter((country) => !first.includes(country))
144
+ };
145
+ }
146
+ export {
147
+ PHONE_MARKETS,
148
+ countryFlag,
149
+ flagEmojiRenders,
150
+ formatNational,
151
+ generalGroups,
152
+ hasDialPrefix,
153
+ internationalDigits,
154
+ marketCountry,
155
+ maxNationalDigits,
156
+ nationalDigits,
157
+ normalisePhone,
158
+ phoneCheck,
159
+ phoneProblem,
160
+ pinnedFirst,
161
+ toE164
162
+ };
163
+ //# sourceMappingURL=core.js.map