@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/dates.cjs
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_require = require("./require.cjs");
|
|
3
|
+
// src/dates.ts
|
|
4
|
+
const LOCAL_DATE = /^(\d{1,2})([/-])(\d{1,2})\2(\d{4})$/;
|
|
5
|
+
const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
6
|
+
const ISO_INSTANT = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(?:Z|([+-])(\d{2}):(\d{2}))$/;
|
|
7
|
+
const TIME = /^(\d{1,2}):(\d{2})$/;
|
|
8
|
+
const pad = (value, length = 2) => String(value).padStart(length, "0");
|
|
9
|
+
function isLeapYear(year) {
|
|
10
|
+
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
11
|
+
}
|
|
12
|
+
function toIsoDate(year, month, day) {
|
|
13
|
+
const lengths = [
|
|
14
|
+
31,
|
|
15
|
+
isLeapYear(year) ? 29 : 28,
|
|
16
|
+
31,
|
|
17
|
+
30,
|
|
18
|
+
31,
|
|
19
|
+
30,
|
|
20
|
+
31,
|
|
21
|
+
31,
|
|
22
|
+
30,
|
|
23
|
+
31,
|
|
24
|
+
30,
|
|
25
|
+
31
|
|
26
|
+
];
|
|
27
|
+
if (!Number.isInteger(year) || year < 1 || year > 9999 || month < 1 || month > 12 || day < 1 || day > (lengths[month - 1] ?? 0)) return null;
|
|
28
|
+
return `${pad(year, 4)}-${pad(month)}-${pad(day)}`;
|
|
29
|
+
}
|
|
30
|
+
function offsetMinutes({ utcOffset }) {
|
|
31
|
+
const sign = utcOffset.startsWith("-") ? -1 : 1;
|
|
32
|
+
const [hours = 0, minutes = 0] = utcOffset.slice(1).split(":").map(Number);
|
|
33
|
+
return sign * (hours * 60 + minutes);
|
|
34
|
+
}
|
|
35
|
+
function requireIsoDate(date) {
|
|
36
|
+
const match = typeof date === "string" ? ISO_DATE.exec(date) : null;
|
|
37
|
+
if (!match || toIsoDate(Number(match[1]), Number(match[2]), Number(match[3])) === null) throw new RangeError("Fecha inválida: se espera YYYY-MM-DD.");
|
|
38
|
+
}
|
|
39
|
+
function utcEpoch(year, month, day, minutes, milliseconds) {
|
|
40
|
+
const moment = /* @__PURE__ */ new Date(0);
|
|
41
|
+
moment.setUTCFullYear(year, month - 1, day);
|
|
42
|
+
return moment.getTime() + minutes * 6e4 + milliseconds;
|
|
43
|
+
}
|
|
44
|
+
function invalidInstant() {
|
|
45
|
+
return /* @__PURE__ */ new RangeError("Instante inválido: use ISO 8601 con Z o con desfase, como 2026-11-24T20:30:00Z.");
|
|
46
|
+
}
|
|
47
|
+
const toNumber = (part) => Number(part ?? 0);
|
|
48
|
+
function parseInstant(text) {
|
|
49
|
+
const match = ISO_INSTANT.exec(text.trim());
|
|
50
|
+
if (!match) throw invalidInstant();
|
|
51
|
+
const [year, month, day, hours, minutes, seconds, fraction = "", sign, offsetHours, offsetMinutes] = match.slice(1);
|
|
52
|
+
const date = {
|
|
53
|
+
year: toNumber(year),
|
|
54
|
+
month: toNumber(month),
|
|
55
|
+
day: toNumber(day)
|
|
56
|
+
};
|
|
57
|
+
const time = {
|
|
58
|
+
hours: toNumber(hours),
|
|
59
|
+
minutes: toNumber(minutes),
|
|
60
|
+
seconds: toNumber(seconds)
|
|
61
|
+
};
|
|
62
|
+
const offset = {
|
|
63
|
+
hours: toNumber(offsetHours),
|
|
64
|
+
minutes: toNumber(offsetMinutes)
|
|
65
|
+
};
|
|
66
|
+
if (toIsoDate(date.year, date.month, date.day) === null || time.hours > 23 || time.minutes > 59 || time.seconds > 59 || offset.hours > 23 || offset.minutes > 59) throw invalidInstant();
|
|
67
|
+
const offsetMinutesTotal = (sign === "-" ? -1 : 1) * (offset.hours * 60 + offset.minutes);
|
|
68
|
+
const milliseconds = time.seconds * 1e3 + Number(fraction.padEnd(3, "0").slice(0, 3));
|
|
69
|
+
return utcEpoch(date.year, date.month, date.day, time.hours * 60 + time.minutes - offsetMinutesTotal, milliseconds);
|
|
70
|
+
}
|
|
71
|
+
function toEpoch(instant) {
|
|
72
|
+
const time = typeof instant === "string" ? parseInstant(instant) : typeof instant === "number" ? instant : instant.getTime();
|
|
73
|
+
if (!Number.isFinite(time)) throw new RangeError("Instante inválido.");
|
|
74
|
+
return time;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Reads a date written `dd/mm/aaaa` (also `d/m/aaaa` and `dd-mm-aaaa`) and rejects dates that do not exist.
|
|
78
|
+
*
|
|
79
|
+
* @returns The date as `YYYY-MM-DD`, or `null` when it is not a valid date.
|
|
80
|
+
* @example parseDate('24/11/2026') // "2026-11-24"
|
|
81
|
+
* @example parseDate('29/02/2026') // null
|
|
82
|
+
*/
|
|
83
|
+
function parseDate(input) {
|
|
84
|
+
if (typeof input !== "string") return null;
|
|
85
|
+
const match = LOCAL_DATE.exec(input.trim());
|
|
86
|
+
if (!match) return null;
|
|
87
|
+
return toIsoDate(Number(match[4]), Number(match[3]), Number(match[1]));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Tells whether a text is a valid `dd/mm/aaaa` date. Same rules as {@link parseDate}.
|
|
91
|
+
*
|
|
92
|
+
* @example isValidDate('31/04/2026') // false
|
|
93
|
+
*/
|
|
94
|
+
function isValidDate(input) {
|
|
95
|
+
return parseDate(input) !== null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Writes a `YYYY-MM-DD` date as `dd/mm/aaaa`.
|
|
99
|
+
*
|
|
100
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
101
|
+
* @example formatDate('2026-11-24') // "24/11/2026"
|
|
102
|
+
*/
|
|
103
|
+
function formatDate(date) {
|
|
104
|
+
requireIsoDate(date);
|
|
105
|
+
const [year, month, day] = date.split("-");
|
|
106
|
+
return `${day}/${month}/${year}`;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Date and time on the wall clock of a country, whatever the device or server time zone is.
|
|
110
|
+
*
|
|
111
|
+
* @param instant The moment to read. Default: now.
|
|
112
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
113
|
+
* @example localDateTime('NI', '2026-11-25T04:30:00Z') // { date: '2026-11-24', time: '22:30', iso: '2026-11-24T22:30:00-06:00' }
|
|
114
|
+
*/
|
|
115
|
+
function localDateTime(countryCode, instant = Date.now()) {
|
|
116
|
+
const target = require_require.requireCountry(countryCode);
|
|
117
|
+
const local = new Date(toEpoch(instant) + offsetMinutes(target) * 6e4);
|
|
118
|
+
const date = toIsoDate(local.getUTCFullYear(), local.getUTCMonth() + 1, local.getUTCDate());
|
|
119
|
+
if (date === null) throw new RangeError("Instante fuera del rango de fechas soportado.");
|
|
120
|
+
const time = `${pad(local.getUTCHours())}:${pad(local.getUTCMinutes())}`;
|
|
121
|
+
return {
|
|
122
|
+
date,
|
|
123
|
+
time,
|
|
124
|
+
iso: `${date}T${time}:${pad(local.getUTCSeconds())}${target.utcOffset}`
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Today's date in a country, whatever the device or server time zone is.
|
|
129
|
+
*
|
|
130
|
+
* @param instant The moment to read. Default: now.
|
|
131
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
132
|
+
* @example today('NI') // "2026-11-24"
|
|
133
|
+
*/
|
|
134
|
+
function today(countryCode, instant) {
|
|
135
|
+
return localDateTime(countryCode, instant).date;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The moment a wall-clock date and time happen in a country, as ISO 8601 with the country's offset.
|
|
139
|
+
* Use it to send a chosen date and time (a reservation, a delivery slot) to the server.
|
|
140
|
+
*
|
|
141
|
+
* @param time `HH:MM` or `H:MM`, 24-hour clock.
|
|
142
|
+
* @throws {RangeError} When the date, the time or the country is not valid.
|
|
143
|
+
* @example zonedDateTime('2026-11-24', '14:30', 'NI') // "2026-11-24T14:30:00-06:00"
|
|
144
|
+
*/
|
|
145
|
+
function zonedDateTime(date, time, countryCode) {
|
|
146
|
+
const target = require_require.requireCountry(countryCode);
|
|
147
|
+
requireIsoDate(date);
|
|
148
|
+
const match = typeof time === "string" ? TIME.exec(time.trim()) : null;
|
|
149
|
+
const hours = Number(match?.[1]);
|
|
150
|
+
const minutes = Number(match?.[2]);
|
|
151
|
+
if (!match || hours > 23 || minutes > 59) throw new RangeError("Hora inválida: se espera HH:MM.");
|
|
152
|
+
return `${date}T${pad(hours)}:${pad(minutes)}:00${target.utcOffset}`;
|
|
153
|
+
}
|
|
154
|
+
const MONTHS = [
|
|
155
|
+
"enero",
|
|
156
|
+
"febrero",
|
|
157
|
+
"marzo",
|
|
158
|
+
"abril",
|
|
159
|
+
"mayo",
|
|
160
|
+
"junio",
|
|
161
|
+
"julio",
|
|
162
|
+
"agosto",
|
|
163
|
+
"septiembre",
|
|
164
|
+
"octubre",
|
|
165
|
+
"noviembre",
|
|
166
|
+
"diciembre"
|
|
167
|
+
];
|
|
168
|
+
const WEEKDAYS = [
|
|
169
|
+
"domingo",
|
|
170
|
+
"lunes",
|
|
171
|
+
"martes",
|
|
172
|
+
"miércoles",
|
|
173
|
+
"jueves",
|
|
174
|
+
"viernes",
|
|
175
|
+
"sábado"
|
|
176
|
+
];
|
|
177
|
+
const TIME_TEXT = /^(\d{1,2})(?::(\d{2}))?\s*(?:([ap])\.?\s*m\.?)?$/i;
|
|
178
|
+
const DAY_MS = 864e5;
|
|
179
|
+
function dateParts(date) {
|
|
180
|
+
requireIsoDate(date);
|
|
181
|
+
const [year = 0, month = 0, day = 0] = date.split("-").map(Number);
|
|
182
|
+
return {
|
|
183
|
+
year,
|
|
184
|
+
month,
|
|
185
|
+
day
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function dayNumber(date) {
|
|
189
|
+
const { year, month, day } = dateParts(date);
|
|
190
|
+
return Math.round(utcEpoch(year, month, day, 0, 0) / DAY_MS);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Reads a time as people write it: `14:30`, `2:30 pm`, `2:30 p. m.`, `2 pm`, `12 am`.
|
|
194
|
+
*
|
|
195
|
+
* @returns The time as `HH:MM` on a 24-hour clock, or `null` when it is not a valid time.
|
|
196
|
+
* @example parseTime('2:30 p. m.') // "14:30"
|
|
197
|
+
* @example parseTime('12 am') // "00:00"
|
|
198
|
+
* @example parseTime('25:00') // null
|
|
199
|
+
*/
|
|
200
|
+
function parseTime(input) {
|
|
201
|
+
if (typeof input !== "string") return null;
|
|
202
|
+
const match = TIME_TEXT.exec(input.trim());
|
|
203
|
+
if (!match) return null;
|
|
204
|
+
const meridiem = match[3]?.toLowerCase();
|
|
205
|
+
const minutes = Number(match[2] ?? 0);
|
|
206
|
+
let hours = Number(match[1]);
|
|
207
|
+
if (minutes > 59) return null;
|
|
208
|
+
if (meridiem) {
|
|
209
|
+
if (hours < 1 || hours > 12) return null;
|
|
210
|
+
hours = hours % 12 + (meridiem === "p" ? 12 : 0);
|
|
211
|
+
} else if (hours > 23 || match[2] === void 0) return null;
|
|
212
|
+
return `${pad(hours)}:${pad(minutes)}`;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Writes a `HH:MM` time the way it is read in the region: `2:30 p. m.`, or `14:30` with `clock: '24h'`.
|
|
216
|
+
*
|
|
217
|
+
* @throws {RangeError} When the time is not a valid `HH:MM` time.
|
|
218
|
+
* @example formatTime('14:30') // "2:30 p. m."
|
|
219
|
+
* @example formatTime('00:05') // "12:05 a. m."
|
|
220
|
+
* @example formatTime('14:30', { clock: '24h' }) // "14:30"
|
|
221
|
+
*/
|
|
222
|
+
function formatTime(time, options = {}) {
|
|
223
|
+
const match = typeof time === "string" ? /^(\d{2}):(\d{2})$/.exec(time) : null;
|
|
224
|
+
const hours = Number(match?.[1]);
|
|
225
|
+
const minutes = Number(match?.[2]);
|
|
226
|
+
if (!match || hours > 23 || minutes > 59) throw new RangeError("Hora inválida: se espera HH:MM.");
|
|
227
|
+
if (options.clock === "24h") return time;
|
|
228
|
+
return `${hours % 12 || 12}:${pad(minutes)} ${hours < 12 ? "a. m." : "p. m."}`;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Writes a `YYYY-MM-DD` date in words, in Spanish.
|
|
232
|
+
*
|
|
233
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
234
|
+
* @example formatLongDate('2026-11-24') // "martes 24 de noviembre de 2026"
|
|
235
|
+
* @example formatLongDate('2026-11-24', { weekday: false, year: false }) // "24 de noviembre"
|
|
236
|
+
*/
|
|
237
|
+
function formatLongDate(date, options = {}) {
|
|
238
|
+
const { year, month, day } = dateParts(date);
|
|
239
|
+
const weekday = WEEKDAYS[new Date(utcEpoch(year, month, day, 0, 0)).getUTCDay()];
|
|
240
|
+
const text = `${day} de ${MONTHS[month - 1]}${options.year === false ? "" : ` de ${year}`}`;
|
|
241
|
+
return options.weekday === false ? text : `${weekday} ${text}`;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Days from one `YYYY-MM-DD` date to another; negative when `to` is earlier.
|
|
245
|
+
*
|
|
246
|
+
* @throws {RangeError} When a date is not a valid `YYYY-MM-DD` date.
|
|
247
|
+
* @example daysBetween('2026-11-24', '2026-12-25') // 31
|
|
248
|
+
*/
|
|
249
|
+
function daysBetween(from, to) {
|
|
250
|
+
return dayNumber(to) - dayNumber(from);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Completed years between a birth date and another date, such as {@link today}.
|
|
254
|
+
*
|
|
255
|
+
* @throws {RangeError} When a date is not valid or `onDate` is before `birthDate`.
|
|
256
|
+
* @example ageOn('2008-11-25', '2026-11-24') // 17
|
|
257
|
+
* @example ageOn('2008-11-24', '2026-11-24') // 18
|
|
258
|
+
*/
|
|
259
|
+
function ageOn(birthDate, onDate) {
|
|
260
|
+
const birth = dateParts(birthDate);
|
|
261
|
+
const on = dateParts(onDate);
|
|
262
|
+
if (onDate < birthDate) throw new RangeError("La fecha es anterior al nacimiento.");
|
|
263
|
+
const birthdayPassed = on.month > birth.month || on.month === birth.month && on.day >= birth.day;
|
|
264
|
+
return on.year - birth.year - (birthdayPassed ? 0 : 1);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Formats a `dd/mm/aaaa` date while it is being typed: adds `/` after the day and the month, and pads a one-digit
|
|
268
|
+
* day or month when the person types the `/` themselves (`1/` becomes `01/`).
|
|
269
|
+
*
|
|
270
|
+
* @example formatDateInput('2411') // { text: '24/11', date: null }
|
|
271
|
+
* @example formatDateInput('24112026') // { text: '24/11/2026', date: '2026-11-24' }
|
|
272
|
+
* @example formatDateInput('1/2/2026') // { text: '01/02/2026', date: '2026-02-01' }
|
|
273
|
+
*/
|
|
274
|
+
function formatDateInput(text) {
|
|
275
|
+
const typed = typeof text === "string" ? text.slice(0, 32) : "";
|
|
276
|
+
const fields = [
|
|
277
|
+
"",
|
|
278
|
+
"",
|
|
279
|
+
""
|
|
280
|
+
];
|
|
281
|
+
const limits = [
|
|
282
|
+
2,
|
|
283
|
+
2,
|
|
284
|
+
4
|
|
285
|
+
];
|
|
286
|
+
let field = 0;
|
|
287
|
+
let closedBySeparator = false;
|
|
288
|
+
for (const character of typed) if (/\d/.test(character)) {
|
|
289
|
+
if (field < 2 && fields[field]?.length === limits[field]) field++;
|
|
290
|
+
if ((fields[field]?.length ?? 0) < (limits[field] ?? 0)) fields[field] += character;
|
|
291
|
+
closedBySeparator = false;
|
|
292
|
+
} else if (/[/.-]/.test(character) && field < 2 && fields[field]) {
|
|
293
|
+
fields[field] = (fields[field] ?? "").padStart(2, "0");
|
|
294
|
+
field++;
|
|
295
|
+
closedBySeparator = true;
|
|
296
|
+
}
|
|
297
|
+
const shown = fields.filter(Boolean).join("/") + (closedBySeparator ? "/" : "");
|
|
298
|
+
return {
|
|
299
|
+
text: shown,
|
|
300
|
+
date: fields[2]?.length === 4 ? parseDate(shown) : null
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
exports.ageOn = ageOn;
|
|
304
|
+
exports.daysBetween = daysBetween;
|
|
305
|
+
exports.formatDate = formatDate;
|
|
306
|
+
exports.formatDateInput = formatDateInput;
|
|
307
|
+
exports.formatLongDate = formatLongDate;
|
|
308
|
+
exports.formatTime = formatTime;
|
|
309
|
+
exports.isValidDate = isValidDate;
|
|
310
|
+
exports.localDateTime = localDateTime;
|
|
311
|
+
exports.parseDate = parseDate;
|
|
312
|
+
exports.parseTime = parseTime;
|
|
313
|
+
exports.today = today;
|
|
314
|
+
exports.zonedDateTime = zonedDateTime;
|
package/dist/dates.d.cts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { CountryCode } from "./regions.cjs";
|
|
2
|
+
/** A calendar date written as `YYYY-MM-DD`, as returned by {@link parseDate} and {@link today}. The type only hints the shape. */
|
|
3
|
+
export type IsoDate = `${number}-${number}-${number}`;
|
|
4
|
+
/**
|
|
5
|
+
* A moment in time: a `Date`, milliseconds since the epoch, or ISO 8601 text with uppercase `T` and `Z` or an
|
|
6
|
+
* offset (`2026-11-24T20:30:00Z`, `2026-11-24T14:30:00-06:00`). The text is read by this library, not by
|
|
7
|
+
* `Date.parse`, so every engine gives the same result. Text without an offset, or with a date or time that does
|
|
8
|
+
* not exist, is rejected.
|
|
9
|
+
*/
|
|
10
|
+
export type Instant = Date | number | string;
|
|
11
|
+
/** Date and time on the wall clock of a country. */
|
|
12
|
+
export interface LocalDateTime {
|
|
13
|
+
/** `YYYY-MM-DD`. */
|
|
14
|
+
readonly date: IsoDate;
|
|
15
|
+
/** `HH:MM`, 24-hour clock. */
|
|
16
|
+
readonly time: string;
|
|
17
|
+
/** ISO 8601 with the country's offset, e.g. `2026-11-24T22:30:00-06:00`. */
|
|
18
|
+
readonly iso: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reads a date written `dd/mm/aaaa` (also `d/m/aaaa` and `dd-mm-aaaa`) and rejects dates that do not exist.
|
|
22
|
+
*
|
|
23
|
+
* @returns The date as `YYYY-MM-DD`, or `null` when it is not a valid date.
|
|
24
|
+
* @example parseDate('24/11/2026') // "2026-11-24"
|
|
25
|
+
* @example parseDate('29/02/2026') // null
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseDate(input: unknown): IsoDate | null;
|
|
28
|
+
/**
|
|
29
|
+
* Tells whether a text is a valid `dd/mm/aaaa` date. Same rules as {@link parseDate}.
|
|
30
|
+
*
|
|
31
|
+
* @example isValidDate('31/04/2026') // false
|
|
32
|
+
*/
|
|
33
|
+
export declare function isValidDate(input: unknown): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Writes a `YYYY-MM-DD` date as `dd/mm/aaaa`.
|
|
36
|
+
*
|
|
37
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
38
|
+
* @example formatDate('2026-11-24') // "24/11/2026"
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatDate(date: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Date and time on the wall clock of a country, whatever the device or server time zone is.
|
|
43
|
+
*
|
|
44
|
+
* @param instant The moment to read. Default: now.
|
|
45
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
46
|
+
* @example localDateTime('NI', '2026-11-25T04:30:00Z') // { date: '2026-11-24', time: '22:30', iso: '2026-11-24T22:30:00-06:00' }
|
|
47
|
+
*/
|
|
48
|
+
export declare function localDateTime(countryCode: CountryCode, instant?: Instant): LocalDateTime;
|
|
49
|
+
/**
|
|
50
|
+
* Today's date in a country, whatever the device or server time zone is.
|
|
51
|
+
*
|
|
52
|
+
* @param instant The moment to read. Default: now.
|
|
53
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
54
|
+
* @example today('NI') // "2026-11-24"
|
|
55
|
+
*/
|
|
56
|
+
export declare function today(countryCode: CountryCode, instant?: Instant): IsoDate;
|
|
57
|
+
/**
|
|
58
|
+
* The moment a wall-clock date and time happen in a country, as ISO 8601 with the country's offset.
|
|
59
|
+
* Use it to send a chosen date and time (a reservation, a delivery slot) to the server.
|
|
60
|
+
*
|
|
61
|
+
* @param time `HH:MM` or `H:MM`, 24-hour clock.
|
|
62
|
+
* @throws {RangeError} When the date, the time or the country is not valid.
|
|
63
|
+
* @example zonedDateTime('2026-11-24', '14:30', 'NI') // "2026-11-24T14:30:00-06:00"
|
|
64
|
+
*/
|
|
65
|
+
export declare function zonedDateTime(date: string, time: string, countryCode: CountryCode): string;
|
|
66
|
+
/**
|
|
67
|
+
* Reads a time as people write it: `14:30`, `2:30 pm`, `2:30 p. m.`, `2 pm`, `12 am`.
|
|
68
|
+
*
|
|
69
|
+
* @returns The time as `HH:MM` on a 24-hour clock, or `null` when it is not a valid time.
|
|
70
|
+
* @example parseTime('2:30 p. m.') // "14:30"
|
|
71
|
+
* @example parseTime('12 am') // "00:00"
|
|
72
|
+
* @example parseTime('25:00') // null
|
|
73
|
+
*/
|
|
74
|
+
export declare function parseTime(input: unknown): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* Writes a `HH:MM` time the way it is read in the region: `2:30 p. m.`, or `14:30` with `clock: '24h'`.
|
|
77
|
+
*
|
|
78
|
+
* @throws {RangeError} When the time is not a valid `HH:MM` time.
|
|
79
|
+
* @example formatTime('14:30') // "2:30 p. m."
|
|
80
|
+
* @example formatTime('00:05') // "12:05 a. m."
|
|
81
|
+
* @example formatTime('14:30', { clock: '24h' }) // "14:30"
|
|
82
|
+
*/
|
|
83
|
+
export declare function formatTime(time: string, options?: {
|
|
84
|
+
readonly clock?: "12h" | "24h";
|
|
85
|
+
}): string;
|
|
86
|
+
/**
|
|
87
|
+
* Writes a `YYYY-MM-DD` date in words, in Spanish.
|
|
88
|
+
*
|
|
89
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
90
|
+
* @example formatLongDate('2026-11-24') // "martes 24 de noviembre de 2026"
|
|
91
|
+
* @example formatLongDate('2026-11-24', { weekday: false, year: false }) // "24 de noviembre"
|
|
92
|
+
*/
|
|
93
|
+
export declare function formatLongDate(date: string, options?: {
|
|
94
|
+
readonly weekday?: boolean;
|
|
95
|
+
readonly year?: boolean;
|
|
96
|
+
}): string;
|
|
97
|
+
/**
|
|
98
|
+
* Days from one `YYYY-MM-DD` date to another; negative when `to` is earlier.
|
|
99
|
+
*
|
|
100
|
+
* @throws {RangeError} When a date is not a valid `YYYY-MM-DD` date.
|
|
101
|
+
* @example daysBetween('2026-11-24', '2026-12-25') // 31
|
|
102
|
+
*/
|
|
103
|
+
export declare function daysBetween(from: string, to: string): number;
|
|
104
|
+
/**
|
|
105
|
+
* Completed years between a birth date and another date, such as {@link today}.
|
|
106
|
+
*
|
|
107
|
+
* @throws {RangeError} When a date is not valid or `onDate` is before `birthDate`.
|
|
108
|
+
* @example ageOn('2008-11-25', '2026-11-24') // 17
|
|
109
|
+
* @example ageOn('2008-11-24', '2026-11-24') // 18
|
|
110
|
+
*/
|
|
111
|
+
export declare function ageOn(birthDate: string, onDate: string): number;
|
|
112
|
+
/** What {@link formatDateInput} returns. */
|
|
113
|
+
export interface DateInputText {
|
|
114
|
+
/** The text to show in the field, with `/` added while typing, e.g. `24/11`. */
|
|
115
|
+
readonly text: string;
|
|
116
|
+
/** The date as `YYYY-MM-DD` once it is complete and exists, otherwise `null`. */
|
|
117
|
+
readonly date: IsoDate | null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Formats a `dd/mm/aaaa` date while it is being typed: adds `/` after the day and the month, and pads a one-digit
|
|
121
|
+
* day or month when the person types the `/` themselves (`1/` becomes `01/`).
|
|
122
|
+
*
|
|
123
|
+
* @example formatDateInput('2411') // { text: '24/11', date: null }
|
|
124
|
+
* @example formatDateInput('24112026') // { text: '24/11/2026', date: '2026-11-24' }
|
|
125
|
+
* @example formatDateInput('1/2/2026') // { text: '01/02/2026', date: '2026-02-01' }
|
|
126
|
+
*/
|
|
127
|
+
export declare function formatDateInput(text: unknown): DateInputText;
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { CountryCode } from "./regions.js";
|
|
2
|
+
/** A calendar date written as `YYYY-MM-DD`, as returned by {@link parseDate} and {@link today}. The type only hints the shape. */
|
|
3
|
+
export type IsoDate = `${number}-${number}-${number}`;
|
|
4
|
+
/**
|
|
5
|
+
* A moment in time: a `Date`, milliseconds since the epoch, or ISO 8601 text with uppercase `T` and `Z` or an
|
|
6
|
+
* offset (`2026-11-24T20:30:00Z`, `2026-11-24T14:30:00-06:00`). The text is read by this library, not by
|
|
7
|
+
* `Date.parse`, so every engine gives the same result. Text without an offset, or with a date or time that does
|
|
8
|
+
* not exist, is rejected.
|
|
9
|
+
*/
|
|
10
|
+
export type Instant = Date | number | string;
|
|
11
|
+
/** Date and time on the wall clock of a country. */
|
|
12
|
+
export interface LocalDateTime {
|
|
13
|
+
/** `YYYY-MM-DD`. */
|
|
14
|
+
readonly date: IsoDate;
|
|
15
|
+
/** `HH:MM`, 24-hour clock. */
|
|
16
|
+
readonly time: string;
|
|
17
|
+
/** ISO 8601 with the country's offset, e.g. `2026-11-24T22:30:00-06:00`. */
|
|
18
|
+
readonly iso: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reads a date written `dd/mm/aaaa` (also `d/m/aaaa` and `dd-mm-aaaa`) and rejects dates that do not exist.
|
|
22
|
+
*
|
|
23
|
+
* @returns The date as `YYYY-MM-DD`, or `null` when it is not a valid date.
|
|
24
|
+
* @example parseDate('24/11/2026') // "2026-11-24"
|
|
25
|
+
* @example parseDate('29/02/2026') // null
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseDate(input: unknown): IsoDate | null;
|
|
28
|
+
/**
|
|
29
|
+
* Tells whether a text is a valid `dd/mm/aaaa` date. Same rules as {@link parseDate}.
|
|
30
|
+
*
|
|
31
|
+
* @example isValidDate('31/04/2026') // false
|
|
32
|
+
*/
|
|
33
|
+
export declare function isValidDate(input: unknown): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Writes a `YYYY-MM-DD` date as `dd/mm/aaaa`.
|
|
36
|
+
*
|
|
37
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
38
|
+
* @example formatDate('2026-11-24') // "24/11/2026"
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatDate(date: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Date and time on the wall clock of a country, whatever the device or server time zone is.
|
|
43
|
+
*
|
|
44
|
+
* @param instant The moment to read. Default: now.
|
|
45
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
46
|
+
* @example localDateTime('NI', '2026-11-25T04:30:00Z') // { date: '2026-11-24', time: '22:30', iso: '2026-11-24T22:30:00-06:00' }
|
|
47
|
+
*/
|
|
48
|
+
export declare function localDateTime(countryCode: CountryCode, instant?: Instant): LocalDateTime;
|
|
49
|
+
/**
|
|
50
|
+
* Today's date in a country, whatever the device or server time zone is.
|
|
51
|
+
*
|
|
52
|
+
* @param instant The moment to read. Default: now.
|
|
53
|
+
* @throws {RangeError} When the country is not supported or the instant is not valid.
|
|
54
|
+
* @example today('NI') // "2026-11-24"
|
|
55
|
+
*/
|
|
56
|
+
export declare function today(countryCode: CountryCode, instant?: Instant): IsoDate;
|
|
57
|
+
/**
|
|
58
|
+
* The moment a wall-clock date and time happen in a country, as ISO 8601 with the country's offset.
|
|
59
|
+
* Use it to send a chosen date and time (a reservation, a delivery slot) to the server.
|
|
60
|
+
*
|
|
61
|
+
* @param time `HH:MM` or `H:MM`, 24-hour clock.
|
|
62
|
+
* @throws {RangeError} When the date, the time or the country is not valid.
|
|
63
|
+
* @example zonedDateTime('2026-11-24', '14:30', 'NI') // "2026-11-24T14:30:00-06:00"
|
|
64
|
+
*/
|
|
65
|
+
export declare function zonedDateTime(date: string, time: string, countryCode: CountryCode): string;
|
|
66
|
+
/**
|
|
67
|
+
* Reads a time as people write it: `14:30`, `2:30 pm`, `2:30 p. m.`, `2 pm`, `12 am`.
|
|
68
|
+
*
|
|
69
|
+
* @returns The time as `HH:MM` on a 24-hour clock, or `null` when it is not a valid time.
|
|
70
|
+
* @example parseTime('2:30 p. m.') // "14:30"
|
|
71
|
+
* @example parseTime('12 am') // "00:00"
|
|
72
|
+
* @example parseTime('25:00') // null
|
|
73
|
+
*/
|
|
74
|
+
export declare function parseTime(input: unknown): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* Writes a `HH:MM` time the way it is read in the region: `2:30 p. m.`, or `14:30` with `clock: '24h'`.
|
|
77
|
+
*
|
|
78
|
+
* @throws {RangeError} When the time is not a valid `HH:MM` time.
|
|
79
|
+
* @example formatTime('14:30') // "2:30 p. m."
|
|
80
|
+
* @example formatTime('00:05') // "12:05 a. m."
|
|
81
|
+
* @example formatTime('14:30', { clock: '24h' }) // "14:30"
|
|
82
|
+
*/
|
|
83
|
+
export declare function formatTime(time: string, options?: {
|
|
84
|
+
readonly clock?: "12h" | "24h";
|
|
85
|
+
}): string;
|
|
86
|
+
/**
|
|
87
|
+
* Writes a `YYYY-MM-DD` date in words, in Spanish.
|
|
88
|
+
*
|
|
89
|
+
* @throws {RangeError} When the date is not a valid `YYYY-MM-DD` date.
|
|
90
|
+
* @example formatLongDate('2026-11-24') // "martes 24 de noviembre de 2026"
|
|
91
|
+
* @example formatLongDate('2026-11-24', { weekday: false, year: false }) // "24 de noviembre"
|
|
92
|
+
*/
|
|
93
|
+
export declare function formatLongDate(date: string, options?: {
|
|
94
|
+
readonly weekday?: boolean;
|
|
95
|
+
readonly year?: boolean;
|
|
96
|
+
}): string;
|
|
97
|
+
/**
|
|
98
|
+
* Days from one `YYYY-MM-DD` date to another; negative when `to` is earlier.
|
|
99
|
+
*
|
|
100
|
+
* @throws {RangeError} When a date is not a valid `YYYY-MM-DD` date.
|
|
101
|
+
* @example daysBetween('2026-11-24', '2026-12-25') // 31
|
|
102
|
+
*/
|
|
103
|
+
export declare function daysBetween(from: string, to: string): number;
|
|
104
|
+
/**
|
|
105
|
+
* Completed years between a birth date and another date, such as {@link today}.
|
|
106
|
+
*
|
|
107
|
+
* @throws {RangeError} When a date is not valid or `onDate` is before `birthDate`.
|
|
108
|
+
* @example ageOn('2008-11-25', '2026-11-24') // 17
|
|
109
|
+
* @example ageOn('2008-11-24', '2026-11-24') // 18
|
|
110
|
+
*/
|
|
111
|
+
export declare function ageOn(birthDate: string, onDate: string): number;
|
|
112
|
+
/** What {@link formatDateInput} returns. */
|
|
113
|
+
export interface DateInputText {
|
|
114
|
+
/** The text to show in the field, with `/` added while typing, e.g. `24/11`. */
|
|
115
|
+
readonly text: string;
|
|
116
|
+
/** The date as `YYYY-MM-DD` once it is complete and exists, otherwise `null`. */
|
|
117
|
+
readonly date: IsoDate | null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Formats a `dd/mm/aaaa` date while it is being typed: adds `/` after the day and the month, and pads a one-digit
|
|
121
|
+
* day or month when the person types the `/` themselves (`1/` becomes `01/`).
|
|
122
|
+
*
|
|
123
|
+
* @example formatDateInput('2411') // { text: '24/11', date: null }
|
|
124
|
+
* @example formatDateInput('24112026') // { text: '24/11/2026', date: '2026-11-24' }
|
|
125
|
+
* @example formatDateInput('1/2/2026') // { text: '01/02/2026', date: '2026-02-01' }
|
|
126
|
+
*/
|
|
127
|
+
export declare function formatDateInput(text: unknown): DateInputText;
|