@momotombodevs/istmo 0.0.0-stage → 0.2.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +21 -0
  3. package/README.md +458 -2
  4. package/dist/dates.cjs +314 -0
  5. package/dist/dates.d.cts +127 -0
  6. package/dist/dates.d.ts +127 -0
  7. package/dist/dates.js +302 -0
  8. package/dist/formatted-input.cjs +51 -0
  9. package/dist/formatted-input.js +49 -0
  10. package/dist/index.cjs +38 -0
  11. package/dist/index.d.cts +6 -0
  12. package/dist/index.d.ts +6 -0
  13. package/dist/index.js +6 -0
  14. package/dist/metadata.cjs +3 -0
  15. package/dist/metadata.js +4 -0
  16. package/dist/money.cjs +172 -0
  17. package/dist/money.d.cts +69 -0
  18. package/dist/money.d.ts +69 -0
  19. package/dist/money.js +166 -0
  20. package/dist/phone-examples.json.cjs +11 -0
  21. package/dist/phone-examples.json.js +11 -0
  22. package/dist/phone-input.cjs +34 -0
  23. package/dist/phone-input.d.cts +18 -0
  24. package/dist/phone-input.d.ts +18 -0
  25. package/dist/phone-input.js +33 -0
  26. package/dist/phone-metadata.json.cjs +224 -0
  27. package/dist/phone-metadata.json.js +224 -0
  28. package/dist/phone.cjs +121 -0
  29. package/dist/phone.d.cts +78 -0
  30. package/dist/phone.d.ts +78 -0
  31. package/dist/phone.js +117 -0
  32. package/dist/react.cjs +274 -0
  33. package/dist/react.d.cts +148 -0
  34. package/dist/react.d.ts +148 -0
  35. package/dist/react.js +268 -0
  36. package/dist/regions.cjs +94 -0
  37. package/dist/regions.d.cts +77 -0
  38. package/dist/regions.d.ts +77 -0
  39. package/dist/regions.js +85 -0
  40. package/dist/regions.json.cjs +136 -0
  41. package/dist/regions.json.js +125 -0
  42. package/dist/require.cjs +14 -0
  43. package/dist/require.js +13 -0
  44. package/flags/LICENSE +21 -0
  45. package/flags/bz.svg +145 -0
  46. package/flags/cr.svg +7 -0
  47. package/flags/gt.svg +204 -0
  48. package/flags/hn.svg +18 -0
  49. package/flags/ni.svg +129 -0
  50. package/flags/pa.svg +7 -0
  51. package/flags/sv.svg +593 -0
  52. package/package.json +201 -4
package/dist/react.js ADDED
@@ -0,0 +1,268 @@
1
+ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
2
+ import { formatDate, formatDateInput } from "./dates.js";
3
+ import { applyEdit, commonPrefix, commonSuffix } from "./formatted-input.js";
4
+ import { formatMoney, formatMoneyInput, parseMoney, toMinorUnits } from "./money.js";
5
+ import { parsePhone, phoneExample, validatePhone } from "./phone.js";
6
+ import { formatPhoneInput } from "./phone-input.js";
7
+ import { countries, countryCodes } from "./regions.js";
8
+ import { requireCountry, requireCurrency } from "./require.js";
9
+ // src/react.ts
10
+ const FRESH = {
11
+ touched: false,
12
+ caret: null
13
+ };
14
+ const DIGIT = /\d/;
15
+ const useIsomorphicLayoutEffect = "window" in globalThis ? useLayoutEffect : useEffect;
16
+ const digitsOf = (text) => text.replace(/\D/g, "");
17
+ function initialPhone(defaultCountry, initialValue, countries) {
18
+ const stored = initialValue ? parsePhone(initialValue, defaultCountry, { countries }) : null;
19
+ return stored ? {
20
+ value: stored.national,
21
+ country: stored.country
22
+ } : {
23
+ value: "",
24
+ country: requireCountry(defaultCountry).code
25
+ };
26
+ }
27
+ function useFieldValue(controlled, onChange, initial) {
28
+ const [own, setOwn] = useState(initial);
29
+ const value = controlled === void 0 ? own : controlled;
30
+ const latest = useRef({
31
+ value,
32
+ onChange,
33
+ controlled: controlled !== void 0
34
+ });
35
+ useIsomorphicLayoutEffect(() => {
36
+ latest.current = {
37
+ value,
38
+ onChange,
39
+ controlled: controlled !== void 0
40
+ };
41
+ });
42
+ return [value, useCallback((change) => {
43
+ const current = latest.current;
44
+ const next = change(current.value);
45
+ current.value = next;
46
+ if (!current.controlled) setOwn(next);
47
+ current.onChange?.(next);
48
+ }, [])];
49
+ }
50
+ function useCaret(caret, text) {
51
+ const element = useRef(null);
52
+ useIsomorphicLayoutEffect(() => {
53
+ if (caret === null || caret >= text.length) return;
54
+ element.current?.setSelectionRange?.(caret, caret);
55
+ element.current?.setSelection?.(caret, caret);
56
+ }, [caret, text]);
57
+ return useCallback((input) => {
58
+ element.current = input;
59
+ }, []);
60
+ }
61
+ /**
62
+ * A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
63
+ * number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
64
+ * flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
65
+ * The handlers keep the same identity while the options do not change. Pass `value`, `country` and `onChange` to
66
+ * control the field from a store or a form library; otherwise it keeps its own state.
67
+ *
68
+ * @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
69
+ * @example
70
+ * const field = usePhoneInput({ defaultCountry: 'NI', type: 'mobile' });
71
+ * <TextInput ref={field.ref} value={field.value} onChangeText={field.onChangeText} onBlur={field.onBlur} placeholder={field.placeholder} />
72
+ */
73
+ function usePhoneInput(options) {
74
+ const { defaultCountry, initialValue, type } = options;
75
+ const allowedKey = (options.countries ?? countryCodes).join(",");
76
+ const [field, update] = useFieldValue(options.value === void 0 ? void 0 : {
77
+ value: options.value,
78
+ country: options.country ?? defaultCountry
79
+ }, options.onChange, () => initialPhone(defaultCountry, initialValue, options.countries ?? countryCodes));
80
+ const [interaction, setInteraction] = useState(FRESH);
81
+ const ref = useCaret(interaction.caret, field.value);
82
+ const handlers = useMemo(() => {
83
+ const allowed = allowedKey.split(",");
84
+ return {
85
+ allowed,
86
+ onChangeText: (next) => update((current) => {
87
+ let detected;
88
+ const edit = applyEdit(current.value, next, (typed) => {
89
+ const formatted = formatPhoneInput(typed, current.country);
90
+ detected = formatted.country;
91
+ return formatted.text;
92
+ }, DIGIT);
93
+ setInteraction((state) => ({
94
+ ...state,
95
+ caret: edit.caret
96
+ }));
97
+ return {
98
+ value: edit.text,
99
+ country: detected && allowed.includes(detected) ? detected : current.country
100
+ };
101
+ }),
102
+ onBlur: () => setInteraction((state) => state.touched ? state : {
103
+ ...state,
104
+ touched: true
105
+ }),
106
+ setCountry: (code) => {
107
+ if (!allowed.includes(code)) throw new RangeError("País fuera de la lista countries del campo.");
108
+ setInteraction((state) => ({
109
+ ...state,
110
+ caret: null
111
+ }));
112
+ update((current) => ({
113
+ value: formatPhoneInput(current.value, code).text,
114
+ country: code
115
+ }));
116
+ },
117
+ reset: () => {
118
+ setInteraction(FRESH);
119
+ update(() => initialPhone(defaultCountry, initialValue, allowed));
120
+ }
121
+ };
122
+ }, [
123
+ allowedKey,
124
+ defaultCountry,
125
+ initialValue,
126
+ update
127
+ ]);
128
+ const validation = digitsOf(field.value) ? validatePhone(field.value, field.country, {
129
+ type,
130
+ countries: handlers.allowed
131
+ }) : null;
132
+ return {
133
+ ref,
134
+ value: field.value,
135
+ country: requireCountry(field.country),
136
+ countries: countries.filter(({ code }) => handlers.allowed.includes(code)),
137
+ placeholder: phoneExample(field.country).national,
138
+ phone: validation?.valid ? validation.phone : null,
139
+ error: interaction.touched && validation && !validation.valid ? validation.error : null,
140
+ onChangeText: handlers.onChangeText,
141
+ onBlur: handlers.onBlur,
142
+ setCountry: handlers.setCountry,
143
+ reset: handlers.reset
144
+ };
145
+ }
146
+ const AMOUNT_CHARACTER = /[\d.]/;
147
+ function initialMoney(currency, initialValue) {
148
+ return initialValue === null || initialValue === void 0 ? "" : formatMoney(initialValue, currency, { display: "none" });
149
+ }
150
+ const FIELD_FORMAT = /^[\d,]*(?:\.\d*)?$/;
151
+ function typedAmount(previous, next, currency) {
152
+ const prefix = commonPrefix(previous, next);
153
+ const suffix = commonSuffix(previous, next, prefix);
154
+ if (next.slice(prefix, next.length - suffix) === ",") return `${next.slice(0, prefix)}.${next.slice(next.length - suffix)}`;
155
+ return FIELD_FORMAT.test(next) ? next : parseMoney(next, currency) ?? next;
156
+ }
157
+ function amountError(amount, text, currency, min, max) {
158
+ if (amount === null) return text ? "format" : null;
159
+ const minor = toMinorUnits(amount, currency);
160
+ if (min !== void 0 && minor < toMinorUnits(min, currency)) return "min";
161
+ if (max !== void 0 && minor > toMinorUnits(max, currency)) return "max";
162
+ return null;
163
+ }
164
+ /**
165
+ * An amount field that groups thousands while typing, keeps the caret where the person is typing, takes `,` from
166
+ * a Spanish decimal keypad as the decimal point, reads pasted amounts in any of the region's formats, and completes
167
+ * the decimals on blur. It renders nothing: show `currency.symbol` and the input with your own components.
168
+ *
169
+ * @throws {RangeError} When the currency is not supported or `initialValue`, `min` or `max` is not an amount.
170
+ * @example
171
+ * const price = useMoneyInput({ currency: 'NIO', min: '1' });
172
+ * <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
173
+ */
174
+ function useMoneyInput({ currency, initialValue, min, max, value, onChange }) {
175
+ const target = requireCurrency(currency);
176
+ const [text, update] = useFieldValue(value, onChange, () => initialMoney(currency, initialValue));
177
+ const [interaction, setInteraction] = useState(FRESH);
178
+ const ref = useCaret(interaction.caret, text);
179
+ const handlers = useMemo(() => ({
180
+ onChangeText: (next) => update((current) => {
181
+ const edit = applyEdit(current, typedAmount(current, next, currency), (typed) => formatMoneyInput(typed, currency).text, AMOUNT_CHARACTER);
182
+ setInteraction((state) => ({
183
+ ...state,
184
+ caret: edit.caret
185
+ }));
186
+ return edit.text;
187
+ }),
188
+ onBlur: () => {
189
+ setInteraction({
190
+ touched: true,
191
+ caret: null
192
+ });
193
+ update((current) => {
194
+ const { amount } = formatMoneyInput(current, currency);
195
+ return amount === null ? current : formatMoney(amount, currency, { display: "none" });
196
+ });
197
+ },
198
+ reset: () => {
199
+ setInteraction(FRESH);
200
+ update(() => initialMoney(currency, initialValue));
201
+ }
202
+ }), [
203
+ currency,
204
+ initialValue,
205
+ update
206
+ ]);
207
+ const { amount } = formatMoneyInput(text, currency);
208
+ const error = amountError(amount, text, currency, min, max);
209
+ return {
210
+ ref,
211
+ value: text,
212
+ amount: error === null ? amount : null,
213
+ error: interaction.touched ? error : null,
214
+ currency: target,
215
+ onChangeText: handlers.onChangeText,
216
+ onBlur: handlers.onBlur,
217
+ reset: handlers.reset
218
+ };
219
+ }
220
+ const DATE_DIGIT = /\d/;
221
+ function initialDate(initialValue) {
222
+ return initialValue ? formatDate(initialValue) : "";
223
+ }
224
+ /**
225
+ * A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
226
+ * that do not exist or fall outside `min` and `max`. It renders nothing.
227
+ *
228
+ * @throws {RangeError} When `initialValue` is not a valid `YYYY-MM-DD` date.
229
+ * @example
230
+ * const delivery = useDateInput({ min: today('NI') });
231
+ * <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
232
+ */
233
+ function useDateInput({ initialValue, min, max, value, onChange } = {}) {
234
+ const [text, update] = useFieldValue(value, onChange, () => initialDate(initialValue));
235
+ const [interaction, setInteraction] = useState(FRESH);
236
+ const ref = useCaret(interaction.caret, text);
237
+ const handlers = useMemo(() => ({
238
+ onChangeText: (next) => update((current) => {
239
+ const edit = applyEdit(current, next, (typed) => formatDateInput(typed).text, DATE_DIGIT);
240
+ setInteraction((state) => ({
241
+ ...state,
242
+ caret: edit.caret
243
+ }));
244
+ return edit.text;
245
+ }),
246
+ onBlur: () => setInteraction((state) => state.touched ? state : {
247
+ ...state,
248
+ touched: true
249
+ }),
250
+ reset: () => {
251
+ setInteraction(FRESH);
252
+ update(() => initialDate(initialValue));
253
+ }
254
+ }), [initialValue, update]);
255
+ const { date } = formatDateInput(text);
256
+ const error = date === null ? text ? "format" : null : min && date < min ? "min" : max && date > max ? "max" : null;
257
+ return {
258
+ ref,
259
+ value: text,
260
+ date: error === null ? date : null,
261
+ error: interaction.touched ? error : null,
262
+ placeholder: "dd/mm/aaaa",
263
+ onChangeText: handlers.onChangeText,
264
+ onBlur: handlers.onBlur,
265
+ reset: handlers.reset
266
+ };
267
+ }
268
+ export { formatDateInput, formatMoneyInput, formatPhoneInput, useDateInput, useMoneyInput, usePhoneInput };
@@ -0,0 +1,94 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_regions = require("./regions.json.cjs");
3
+ // src/regions.ts
4
+ /** Codes of every supported country, in the same order as {@link countries}. */
5
+ const countryCodes = [
6
+ "GT",
7
+ "BZ",
8
+ "SV",
9
+ "HN",
10
+ "NI",
11
+ "CR",
12
+ "PA"
13
+ ];
14
+ /** Codes of every supported currency, in the same order as {@link currencies}. */
15
+ const currencyCodes = [
16
+ "GTQ",
17
+ "BZD",
18
+ "HNL",
19
+ "NIO",
20
+ "CRC",
21
+ "PAB",
22
+ "USD"
23
+ ];
24
+ Object.freeze(countryCodes);
25
+ Object.freeze(currencyCodes);
26
+ function deepFreeze(value) {
27
+ if (value && typeof value === "object") {
28
+ for (const child of Object.values(value)) deepFreeze(child);
29
+ Object.freeze(value);
30
+ }
31
+ return value;
32
+ }
33
+ function flagOf(code) {
34
+ return String.fromCodePoint(...[...code].map((letter) => 127462 + letter.charCodeAt(0) - 65));
35
+ }
36
+ /** Every supported country, frozen. */
37
+ const countries = deepFreeze(require_regions.countries.map((entry) => ({
38
+ ...entry,
39
+ flag: flagOf(entry.code)
40
+ })));
41
+ /** Every supported currency, frozen. */
42
+ const currencies = deepFreeze(require_regions.currencies);
43
+ /**
44
+ * Finds a country by its exact uppercase code.
45
+ *
46
+ * @example country('NI')?.callingCode // "505"
47
+ */
48
+ function country(code) {
49
+ return countries.find((entry) => entry.code === code);
50
+ }
51
+ /**
52
+ * Finds a currency by its exact uppercase code.
53
+ *
54
+ * @example currency('CRC')?.symbol // "₡"
55
+ */
56
+ function currency(code) {
57
+ return currencies.find((entry) => entry.code === code);
58
+ }
59
+ /**
60
+ * Narrows an untrusted value, such as an API field, to a {@link CountryCode}. Only exact uppercase codes pass.
61
+ *
62
+ * @example isCountryCode('NI') // true
63
+ * @example isCountryCode('ni') // false
64
+ */
65
+ function isCountryCode(value) {
66
+ return countryCodes.includes(value);
67
+ }
68
+ /**
69
+ * Narrows an untrusted value, such as an API field, to a {@link CurrencyCode}. Only exact uppercase codes pass.
70
+ *
71
+ * @example isCurrencyCode('NIO') // true
72
+ * @example isCurrencyCode('EUR') // false
73
+ */
74
+ function isCurrencyCode(value) {
75
+ return currencyCodes.includes(value);
76
+ }
77
+ /**
78
+ * Tells whether a country accepts a currency.
79
+ *
80
+ * @example acceptsCurrency('NI', 'USD') // true
81
+ * @example acceptsCurrency('NI', 'GTQ') // false
82
+ */
83
+ function acceptsCurrency(countryCode, currencyCode) {
84
+ return country(countryCode)?.currencies.includes(currencyCode) ?? false;
85
+ }
86
+ exports.acceptsCurrency = acceptsCurrency;
87
+ exports.countries = countries;
88
+ exports.country = country;
89
+ exports.countryCodes = countryCodes;
90
+ exports.currencies = currencies;
91
+ exports.currency = currency;
92
+ exports.currencyCodes = currencyCodes;
93
+ exports.isCountryCode = isCountryCode;
94
+ exports.isCurrencyCode = isCurrencyCode;
@@ -0,0 +1,77 @@
1
+ /** Codes of every supported country, in the same order as {@link countries}. */
2
+ export declare const countryCodes: readonly ["GT", "BZ", "SV", "HN", "NI", "CR", "PA"];
3
+ /** Codes of every supported currency, in the same order as {@link currencies}. */
4
+ export declare const currencyCodes: readonly ["GTQ", "BZD", "HNL", "NIO", "CRC", "PAB", "USD"];
5
+ /** ISO 3166-1 alpha-2 code of a supported Central American country. */
6
+ export type CountryCode = (typeof countryCodes)[number];
7
+ /** ISO 4217 code of a supported currency. */
8
+ export type CurrencyCode = (typeof currencyCodes)[number];
9
+ /** UTC offset written as `±HH:MM`, e.g. `-06:00`. */
10
+ export type UtcOffset = `${"+" | "-"}${number}:${number}`;
11
+ /** A supported country and its defaults. */
12
+ export interface Country {
13
+ readonly code: CountryCode;
14
+ /** Name in Spanish, e.g. `Panamá`. */
15
+ readonly name: string;
16
+ /** Flag emoji, e.g. `🇳🇮`. Windows browsers show the two letters instead of a flag. */
17
+ readonly flag: string;
18
+ /** Calling code without `+`, e.g. `505`. */
19
+ readonly callingCode: string;
20
+ /** Main currency used for prices. */
21
+ readonly currency: CurrencyCode;
22
+ /** Every currency the country accepts, main one first. */
23
+ readonly currencies: readonly CurrencyCode[];
24
+ /** IANA time zone, e.g. `America/Managua`. */
25
+ readonly timezone: string;
26
+ /** Fixed UTC offset. None of the supported countries observes daylight saving time. */
27
+ readonly utcOffset: UtcOffset;
28
+ }
29
+ /** A supported currency and how it is written. */
30
+ export interface Currency {
31
+ readonly code: CurrencyCode;
32
+ /** Name in Spanish, e.g. `Córdoba`. */
33
+ readonly name: string;
34
+ /** Symbol shown before the amount, e.g. `C$`. */
35
+ readonly symbol: string;
36
+ /** Number of decimal places. */
37
+ readonly decimals: number;
38
+ readonly decimalSeparator: string;
39
+ readonly groupSeparator: string;
40
+ }
41
+ /** Every supported country, frozen. */
42
+ export declare const countries: readonly Country[];
43
+ /** Every supported currency, frozen. */
44
+ export declare const currencies: readonly Currency[];
45
+ /**
46
+ * Finds a country by its exact uppercase code.
47
+ *
48
+ * @example country('NI')?.callingCode // "505"
49
+ */
50
+ export declare function country(code: CountryCode): Country | undefined;
51
+ /**
52
+ * Finds a currency by its exact uppercase code.
53
+ *
54
+ * @example currency('CRC')?.symbol // "₡"
55
+ */
56
+ export declare function currency(code: CurrencyCode): Currency | undefined;
57
+ /**
58
+ * Narrows an untrusted value, such as an API field, to a {@link CountryCode}. Only exact uppercase codes pass.
59
+ *
60
+ * @example isCountryCode('NI') // true
61
+ * @example isCountryCode('ni') // false
62
+ */
63
+ export declare function isCountryCode(value: unknown): value is CountryCode;
64
+ /**
65
+ * Narrows an untrusted value, such as an API field, to a {@link CurrencyCode}. Only exact uppercase codes pass.
66
+ *
67
+ * @example isCurrencyCode('NIO') // true
68
+ * @example isCurrencyCode('EUR') // false
69
+ */
70
+ export declare function isCurrencyCode(value: unknown): value is CurrencyCode;
71
+ /**
72
+ * Tells whether a country accepts a currency.
73
+ *
74
+ * @example acceptsCurrency('NI', 'USD') // true
75
+ * @example acceptsCurrency('NI', 'GTQ') // false
76
+ */
77
+ export declare function acceptsCurrency(countryCode: CountryCode, currencyCode: CurrencyCode): boolean;
@@ -0,0 +1,77 @@
1
+ /** Codes of every supported country, in the same order as {@link countries}. */
2
+ export declare const countryCodes: readonly ["GT", "BZ", "SV", "HN", "NI", "CR", "PA"];
3
+ /** Codes of every supported currency, in the same order as {@link currencies}. */
4
+ export declare const currencyCodes: readonly ["GTQ", "BZD", "HNL", "NIO", "CRC", "PAB", "USD"];
5
+ /** ISO 3166-1 alpha-2 code of a supported Central American country. */
6
+ export type CountryCode = (typeof countryCodes)[number];
7
+ /** ISO 4217 code of a supported currency. */
8
+ export type CurrencyCode = (typeof currencyCodes)[number];
9
+ /** UTC offset written as `±HH:MM`, e.g. `-06:00`. */
10
+ export type UtcOffset = `${"+" | "-"}${number}:${number}`;
11
+ /** A supported country and its defaults. */
12
+ export interface Country {
13
+ readonly code: CountryCode;
14
+ /** Name in Spanish, e.g. `Panamá`. */
15
+ readonly name: string;
16
+ /** Flag emoji, e.g. `🇳🇮`. Windows browsers show the two letters instead of a flag. */
17
+ readonly flag: string;
18
+ /** Calling code without `+`, e.g. `505`. */
19
+ readonly callingCode: string;
20
+ /** Main currency used for prices. */
21
+ readonly currency: CurrencyCode;
22
+ /** Every currency the country accepts, main one first. */
23
+ readonly currencies: readonly CurrencyCode[];
24
+ /** IANA time zone, e.g. `America/Managua`. */
25
+ readonly timezone: string;
26
+ /** Fixed UTC offset. None of the supported countries observes daylight saving time. */
27
+ readonly utcOffset: UtcOffset;
28
+ }
29
+ /** A supported currency and how it is written. */
30
+ export interface Currency {
31
+ readonly code: CurrencyCode;
32
+ /** Name in Spanish, e.g. `Córdoba`. */
33
+ readonly name: string;
34
+ /** Symbol shown before the amount, e.g. `C$`. */
35
+ readonly symbol: string;
36
+ /** Number of decimal places. */
37
+ readonly decimals: number;
38
+ readonly decimalSeparator: string;
39
+ readonly groupSeparator: string;
40
+ }
41
+ /** Every supported country, frozen. */
42
+ export declare const countries: readonly Country[];
43
+ /** Every supported currency, frozen. */
44
+ export declare const currencies: readonly Currency[];
45
+ /**
46
+ * Finds a country by its exact uppercase code.
47
+ *
48
+ * @example country('NI')?.callingCode // "505"
49
+ */
50
+ export declare function country(code: CountryCode): Country | undefined;
51
+ /**
52
+ * Finds a currency by its exact uppercase code.
53
+ *
54
+ * @example currency('CRC')?.symbol // "₡"
55
+ */
56
+ export declare function currency(code: CurrencyCode): Currency | undefined;
57
+ /**
58
+ * Narrows an untrusted value, such as an API field, to a {@link CountryCode}. Only exact uppercase codes pass.
59
+ *
60
+ * @example isCountryCode('NI') // true
61
+ * @example isCountryCode('ni') // false
62
+ */
63
+ export declare function isCountryCode(value: unknown): value is CountryCode;
64
+ /**
65
+ * Narrows an untrusted value, such as an API field, to a {@link CurrencyCode}. Only exact uppercase codes pass.
66
+ *
67
+ * @example isCurrencyCode('NIO') // true
68
+ * @example isCurrencyCode('EUR') // false
69
+ */
70
+ export declare function isCurrencyCode(value: unknown): value is CurrencyCode;
71
+ /**
72
+ * Tells whether a country accepts a currency.
73
+ *
74
+ * @example acceptsCurrency('NI', 'USD') // true
75
+ * @example acceptsCurrency('NI', 'GTQ') // false
76
+ */
77
+ export declare function acceptsCurrency(countryCode: CountryCode, currencyCode: CurrencyCode): boolean;
@@ -0,0 +1,85 @@
1
+ import { countries as countries$1, currencies as currencies$1 } from "./regions.json.js";
2
+ // src/regions.ts
3
+ /** Codes of every supported country, in the same order as {@link countries}. */
4
+ const countryCodes = [
5
+ "GT",
6
+ "BZ",
7
+ "SV",
8
+ "HN",
9
+ "NI",
10
+ "CR",
11
+ "PA"
12
+ ];
13
+ /** Codes of every supported currency, in the same order as {@link currencies}. */
14
+ const currencyCodes = [
15
+ "GTQ",
16
+ "BZD",
17
+ "HNL",
18
+ "NIO",
19
+ "CRC",
20
+ "PAB",
21
+ "USD"
22
+ ];
23
+ Object.freeze(countryCodes);
24
+ Object.freeze(currencyCodes);
25
+ function deepFreeze(value) {
26
+ if (value && typeof value === "object") {
27
+ for (const child of Object.values(value)) deepFreeze(child);
28
+ Object.freeze(value);
29
+ }
30
+ return value;
31
+ }
32
+ function flagOf(code) {
33
+ return String.fromCodePoint(...[...code].map((letter) => 127462 + letter.charCodeAt(0) - 65));
34
+ }
35
+ /** Every supported country, frozen. */
36
+ const countries = deepFreeze(countries$1.map((entry) => ({
37
+ ...entry,
38
+ flag: flagOf(entry.code)
39
+ })));
40
+ /** Every supported currency, frozen. */
41
+ const currencies = deepFreeze(currencies$1);
42
+ /**
43
+ * Finds a country by its exact uppercase code.
44
+ *
45
+ * @example country('NI')?.callingCode // "505"
46
+ */
47
+ function country(code) {
48
+ return countries.find((entry) => entry.code === code);
49
+ }
50
+ /**
51
+ * Finds a currency by its exact uppercase code.
52
+ *
53
+ * @example currency('CRC')?.symbol // "₡"
54
+ */
55
+ function currency(code) {
56
+ return currencies.find((entry) => entry.code === code);
57
+ }
58
+ /**
59
+ * Narrows an untrusted value, such as an API field, to a {@link CountryCode}. Only exact uppercase codes pass.
60
+ *
61
+ * @example isCountryCode('NI') // true
62
+ * @example isCountryCode('ni') // false
63
+ */
64
+ function isCountryCode(value) {
65
+ return countryCodes.includes(value);
66
+ }
67
+ /**
68
+ * Narrows an untrusted value, such as an API field, to a {@link CurrencyCode}. Only exact uppercase codes pass.
69
+ *
70
+ * @example isCurrencyCode('NIO') // true
71
+ * @example isCurrencyCode('EUR') // false
72
+ */
73
+ function isCurrencyCode(value) {
74
+ return currencyCodes.includes(value);
75
+ }
76
+ /**
77
+ * Tells whether a country accepts a currency.
78
+ *
79
+ * @example acceptsCurrency('NI', 'USD') // true
80
+ * @example acceptsCurrency('NI', 'GTQ') // false
81
+ */
82
+ function acceptsCurrency(countryCode, currencyCode) {
83
+ return country(countryCode)?.currencies.includes(currencyCode) ?? false;
84
+ }
85
+ export { acceptsCurrency, countries, country, countryCodes, currencies, currency, currencyCodes, isCountryCode, isCurrencyCode };