@momotombodevs/istmo 0.0.0-stage → 0.1.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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +442 -2
- package/dist/dates.cjs +314 -0
- package/dist/dates.d.cts +127 -0
- package/dist/dates.d.ts +127 -0
- package/dist/dates.js +302 -0
- package/dist/formatted-input.cjs +51 -0
- package/dist/formatted-input.js +49 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/metadata.cjs +3 -0
- package/dist/metadata.js +4 -0
- package/dist/money.cjs +172 -0
- package/dist/money.d.cts +69 -0
- package/dist/money.d.ts +69 -0
- package/dist/money.js +166 -0
- package/dist/phone-examples.json.cjs +11 -0
- package/dist/phone-examples.json.js +11 -0
- package/dist/phone-input.cjs +34 -0
- package/dist/phone-input.d.cts +18 -0
- package/dist/phone-input.d.ts +18 -0
- package/dist/phone-input.js +33 -0
- package/dist/phone-metadata.json.cjs +224 -0
- package/dist/phone-metadata.json.js +224 -0
- package/dist/phone.cjs +121 -0
- package/dist/phone.d.cts +78 -0
- package/dist/phone.d.ts +78 -0
- package/dist/phone.js +117 -0
- package/dist/react.cjs +238 -0
- package/dist/react.d.cts +128 -0
- package/dist/react.d.ts +128 -0
- package/dist/react.js +232 -0
- package/dist/regions.cjs +94 -0
- package/dist/regions.d.cts +77 -0
- package/dist/regions.d.ts +77 -0
- package/dist/regions.js +85 -0
- package/dist/regions.json.cjs +136 -0
- package/dist/regions.json.js +125 -0
- package/dist/require.cjs +14 -0
- package/dist/require.js +13 -0
- package/flags/LICENSE +21 -0
- package/flags/bz.svg +145 -0
- package/flags/cr.svg +7 -0
- package/flags/gt.svg +204 -0
- package/flags/hn.svg +18 -0
- package/flags/ni.svg +129 -0
- package/flags/pa.svg +7 -0
- package/flags/sv.svg +593 -0
- package/package.json +201 -4
package/dist/react.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
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 DIGIT = /\d/;
|
|
11
|
+
const useIsomorphicLayoutEffect = "window" in globalThis ? useLayoutEffect : useEffect;
|
|
12
|
+
const digitsOf = (text) => text.replace(/\D/g, "");
|
|
13
|
+
function initialState({ defaultCountry, initialValue, countries }) {
|
|
14
|
+
const stored = initialValue ? parsePhone(initialValue, defaultCountry, { countries }) : null;
|
|
15
|
+
return stored ? {
|
|
16
|
+
text: stored.national,
|
|
17
|
+
country: stored.country,
|
|
18
|
+
touched: false,
|
|
19
|
+
caret: null
|
|
20
|
+
} : {
|
|
21
|
+
text: "",
|
|
22
|
+
country: requireCountry(defaultCountry).code,
|
|
23
|
+
touched: false,
|
|
24
|
+
caret: null
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function useCaret(caret, text) {
|
|
28
|
+
const element = useRef(null);
|
|
29
|
+
useIsomorphicLayoutEffect(() => {
|
|
30
|
+
if (caret === null || caret >= text.length) return;
|
|
31
|
+
element.current?.setSelectionRange?.(caret, caret);
|
|
32
|
+
element.current?.setSelection?.(caret, caret);
|
|
33
|
+
}, [caret, text]);
|
|
34
|
+
return useCallback((input) => {
|
|
35
|
+
element.current = input;
|
|
36
|
+
}, []);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
40
|
+
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
41
|
+
* flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
|
|
42
|
+
* The handlers keep the same identity while the options do not change.
|
|
43
|
+
*
|
|
44
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
45
|
+
* @example
|
|
46
|
+
* const field = usePhoneInput({ defaultCountry: 'NI', type: 'mobile' });
|
|
47
|
+
* <TextInput ref={field.ref} value={field.value} onChangeText={field.onChangeText} onBlur={field.onBlur} placeholder={field.placeholder} />
|
|
48
|
+
*/
|
|
49
|
+
function usePhoneInput(options) {
|
|
50
|
+
const { defaultCountry, initialValue, type } = options;
|
|
51
|
+
const allowedKey = (options.countries ?? countryCodes).join(",");
|
|
52
|
+
const [state, setState] = useState(() => initialState(options));
|
|
53
|
+
const ref = useCaret(state.caret, state.text);
|
|
54
|
+
const handlers = useMemo(() => {
|
|
55
|
+
const allowed = allowedKey.split(",");
|
|
56
|
+
const initial = () => initialState({
|
|
57
|
+
defaultCountry,
|
|
58
|
+
initialValue,
|
|
59
|
+
countries: allowed
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
allowed,
|
|
63
|
+
onChangeText: (next) => setState((current) => {
|
|
64
|
+
let detected;
|
|
65
|
+
const edit = applyEdit(current.text, next, (typed) => {
|
|
66
|
+
const formatted = formatPhoneInput(typed, current.country);
|
|
67
|
+
detected = formatted.country;
|
|
68
|
+
return formatted.text;
|
|
69
|
+
}, DIGIT);
|
|
70
|
+
const country = detected && allowed.includes(detected) ? detected : current.country;
|
|
71
|
+
return {
|
|
72
|
+
...current,
|
|
73
|
+
text: edit.text,
|
|
74
|
+
caret: edit.caret,
|
|
75
|
+
country
|
|
76
|
+
};
|
|
77
|
+
}),
|
|
78
|
+
onBlur: () => setState((current) => current.touched ? current : {
|
|
79
|
+
...current,
|
|
80
|
+
touched: true
|
|
81
|
+
}),
|
|
82
|
+
setCountry: (code) => {
|
|
83
|
+
if (!allowed.includes(code)) throw new RangeError("País fuera de la lista countries del campo.");
|
|
84
|
+
setState((current) => ({
|
|
85
|
+
...current,
|
|
86
|
+
country: code,
|
|
87
|
+
text: formatPhoneInput(current.text, code).text,
|
|
88
|
+
caret: null
|
|
89
|
+
}));
|
|
90
|
+
},
|
|
91
|
+
reset: () => setState(initial())
|
|
92
|
+
};
|
|
93
|
+
}, [
|
|
94
|
+
allowedKey,
|
|
95
|
+
defaultCountry,
|
|
96
|
+
initialValue
|
|
97
|
+
]);
|
|
98
|
+
const validation = digitsOf(state.text) ? validatePhone(state.text, state.country, {
|
|
99
|
+
type,
|
|
100
|
+
countries: handlers.allowed
|
|
101
|
+
}) : null;
|
|
102
|
+
return {
|
|
103
|
+
ref,
|
|
104
|
+
value: state.text,
|
|
105
|
+
country: requireCountry(state.country),
|
|
106
|
+
countries: countries.filter(({ code }) => handlers.allowed.includes(code)),
|
|
107
|
+
placeholder: phoneExample(state.country).national,
|
|
108
|
+
phone: validation?.valid ? validation.phone : null,
|
|
109
|
+
error: state.touched && validation && !validation.valid ? validation.error : null,
|
|
110
|
+
onChangeText: handlers.onChangeText,
|
|
111
|
+
onBlur: handlers.onBlur,
|
|
112
|
+
setCountry: handlers.setCountry,
|
|
113
|
+
reset: handlers.reset
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const AMOUNT_CHARACTER = /[\d.]/;
|
|
117
|
+
function initialMoney(currency, initialValue) {
|
|
118
|
+
return {
|
|
119
|
+
text: initialValue === null || initialValue === void 0 ? "" : formatMoney(initialValue, currency, { display: "none" }),
|
|
120
|
+
touched: false,
|
|
121
|
+
caret: null
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const FIELD_FORMAT = /^[\d,]*(?:\.\d*)?$/;
|
|
125
|
+
function typedAmount(previous, next, currency) {
|
|
126
|
+
const prefix = commonPrefix(previous, next);
|
|
127
|
+
const suffix = commonSuffix(previous, next, prefix);
|
|
128
|
+
if (next.slice(prefix, next.length - suffix) === ",") return `${next.slice(0, prefix)}.${next.slice(next.length - suffix)}`;
|
|
129
|
+
return FIELD_FORMAT.test(next) ? next : parseMoney(next, currency) ?? next;
|
|
130
|
+
}
|
|
131
|
+
function amountError(amount, text, currency, min, max) {
|
|
132
|
+
if (amount === null) return text ? "format" : null;
|
|
133
|
+
const minor = toMinorUnits(amount, currency);
|
|
134
|
+
if (min !== void 0 && minor < toMinorUnits(min, currency)) return "min";
|
|
135
|
+
if (max !== void 0 && minor > toMinorUnits(max, currency)) return "max";
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* An amount field that groups thousands while typing, keeps the caret where the person is typing, takes `,` from
|
|
140
|
+
* a Spanish decimal keypad as the decimal point, reads pasted amounts in any of the region's formats, and completes
|
|
141
|
+
* the decimals on blur. It renders nothing: show `currency.symbol` and the input with your own components.
|
|
142
|
+
*
|
|
143
|
+
* @throws {RangeError} When the currency is not supported or `initialValue`, `min` or `max` is not an amount.
|
|
144
|
+
* @example
|
|
145
|
+
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
146
|
+
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
147
|
+
*/
|
|
148
|
+
function useMoneyInput({ currency, initialValue, min, max }) {
|
|
149
|
+
const target = requireCurrency(currency);
|
|
150
|
+
const [state, setState] = useState(() => initialMoney(currency, initialValue));
|
|
151
|
+
const ref = useCaret(state.caret, state.text);
|
|
152
|
+
const handlers = useMemo(() => ({
|
|
153
|
+
onChangeText: (next) => setState((current) => {
|
|
154
|
+
const edit = applyEdit(current.text, typedAmount(current.text, next, currency), (typed) => formatMoneyInput(typed, currency).text, AMOUNT_CHARACTER);
|
|
155
|
+
return {
|
|
156
|
+
...current,
|
|
157
|
+
text: edit.text,
|
|
158
|
+
caret: edit.caret
|
|
159
|
+
};
|
|
160
|
+
}),
|
|
161
|
+
onBlur: () => setState((current) => {
|
|
162
|
+
const { amount } = formatMoneyInput(current.text, currency);
|
|
163
|
+
return {
|
|
164
|
+
text: amount === null ? current.text : formatMoney(amount, currency, { display: "none" }),
|
|
165
|
+
touched: true,
|
|
166
|
+
caret: null
|
|
167
|
+
};
|
|
168
|
+
}),
|
|
169
|
+
reset: () => setState(initialMoney(currency, initialValue))
|
|
170
|
+
}), [currency, initialValue]);
|
|
171
|
+
const { amount } = formatMoneyInput(state.text, currency);
|
|
172
|
+
const error = amountError(amount, state.text, currency, min, max);
|
|
173
|
+
return {
|
|
174
|
+
ref,
|
|
175
|
+
value: state.text,
|
|
176
|
+
amount: error === null ? amount : null,
|
|
177
|
+
error: state.touched ? error : null,
|
|
178
|
+
currency: target,
|
|
179
|
+
onChangeText: handlers.onChangeText,
|
|
180
|
+
onBlur: handlers.onBlur,
|
|
181
|
+
reset: handlers.reset
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const DATE_DIGIT = /\d/;
|
|
185
|
+
function initialDate(initialValue) {
|
|
186
|
+
return {
|
|
187
|
+
text: initialValue ? formatDate(initialValue) : "",
|
|
188
|
+
touched: false,
|
|
189
|
+
caret: null
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
194
|
+
* that do not exist or fall outside `min` and `max`. It renders nothing.
|
|
195
|
+
*
|
|
196
|
+
* @throws {RangeError} When `initialValue` is not a valid `YYYY-MM-DD` date.
|
|
197
|
+
* @example
|
|
198
|
+
* const delivery = useDateInput({ min: today('NI') });
|
|
199
|
+
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
200
|
+
*/
|
|
201
|
+
function useDateInput({ initialValue, min, max } = {}) {
|
|
202
|
+
const [state, setState] = useState(() => initialDate(initialValue));
|
|
203
|
+
const ref = useCaret(state.caret, state.text);
|
|
204
|
+
const handlers = useMemo(() => ({
|
|
205
|
+
onChangeText: (next) => setState((current) => {
|
|
206
|
+
const edit = applyEdit(current.text, next, (typed) => formatDateInput(typed).text, DATE_DIGIT);
|
|
207
|
+
return {
|
|
208
|
+
...current,
|
|
209
|
+
text: edit.text,
|
|
210
|
+
caret: edit.caret
|
|
211
|
+
};
|
|
212
|
+
}),
|
|
213
|
+
onBlur: () => setState((current) => current.touched ? current : {
|
|
214
|
+
...current,
|
|
215
|
+
touched: true
|
|
216
|
+
}),
|
|
217
|
+
reset: () => setState(initialDate(initialValue))
|
|
218
|
+
}), [initialValue]);
|
|
219
|
+
const { date } = formatDateInput(state.text);
|
|
220
|
+
const error = date === null ? state.text ? "format" : null : min && date < min ? "min" : max && date > max ? "max" : null;
|
|
221
|
+
return {
|
|
222
|
+
ref,
|
|
223
|
+
value: state.text,
|
|
224
|
+
date: error === null ? date : null,
|
|
225
|
+
error: state.touched ? error : null,
|
|
226
|
+
placeholder: "dd/mm/aaaa",
|
|
227
|
+
onChangeText: handlers.onChangeText,
|
|
228
|
+
onBlur: handlers.onBlur,
|
|
229
|
+
reset: handlers.reset
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
export { formatDateInput, formatMoneyInput, formatPhoneInput, useDateInput, useMoneyInput, usePhoneInput };
|
package/dist/regions.cjs
ADDED
|
@@ -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;
|
package/dist/regions.js
ADDED
|
@@ -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 };
|