@bevelo/date 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/LICENSE +21 -0
- package/README.md +35 -2
- package/dist/civil-GPupvlwZ.d.cts +116 -0
- package/dist/civil-GPupvlwZ.d.ts +116 -0
- package/dist/civil-QU5EukOm.cjs +461 -0
- package/dist/civil-SGLjw0gL.js +366 -0
- package/dist/index.cjs +506 -0
- package/dist/index.d.cts +302 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +449 -0
- package/dist/pattern.cjs +328 -0
- package/dist/pattern.d.cts +75 -0
- package/dist/pattern.d.ts +75 -0
- package/dist/pattern.js +323 -0
- package/dist/timezone-BmJA-u4N.js +417 -0
- package/dist/timezone-DiMSaNmI.cjs +560 -0
- package/dist/timezone.cjs +10 -0
- package/dist/timezone.d.cts +108 -0
- package/dist/timezone.d.ts +108 -0
- package/dist/timezone.js +1 -0
- package/package.json +45 -3
package/dist/pattern.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { h as pad, m as hasIntl, s as isValidDate } from "./civil-SGLjw0gL.js";
|
|
2
|
+
// src/pattern.ts
|
|
3
|
+
const ISO_LAYOUT = {
|
|
4
|
+
order: [
|
|
5
|
+
"year",
|
|
6
|
+
"month",
|
|
7
|
+
"day"
|
|
8
|
+
],
|
|
9
|
+
before: [
|
|
10
|
+
"",
|
|
11
|
+
"-",
|
|
12
|
+
"-"
|
|
13
|
+
],
|
|
14
|
+
after: ""
|
|
15
|
+
};
|
|
16
|
+
/** 2001-02-03, chosen so year, month and day are three different values. */
|
|
17
|
+
const PROBE = new Date(Date.UTC(2001, 1, 3));
|
|
18
|
+
/** A four-digit year, the ISO form that needs no locale to read. */
|
|
19
|
+
const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
20
|
+
const layouts = /* @__PURE__ */ new Map();
|
|
21
|
+
/**
|
|
22
|
+
* The order in which a locale writes a numeric date.
|
|
23
|
+
*
|
|
24
|
+
* Derived from `Intl.DateTimeFormat(locale).formatToParts` on a fixed probe
|
|
25
|
+
* date, never from a table: the formatter knows about locales the package has
|
|
26
|
+
* never heard of. Falls back to ISO order (year, month, day) when Intl is
|
|
27
|
+
* unavailable or rejects the tag, because that is the one order a reader cannot
|
|
28
|
+
* misread.
|
|
29
|
+
*
|
|
30
|
+
* @example dateOrder("en-US") // ["month", "day", "year"]
|
|
31
|
+
*/
|
|
32
|
+
function dateOrder(locale) {
|
|
33
|
+
return layoutFor(locale).order;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Reads a date typed in the locale's own order.
|
|
37
|
+
*
|
|
38
|
+
* Tolerant where a person is tolerant: surrounding whitespace, any run of
|
|
39
|
+
* non-digits as a separator (`-`, `/`, `.`, a space, the right-to-left marks an
|
|
40
|
+
* Arabic formatter inserts), and one- or two-digit month and day. It is not a
|
|
41
|
+
* general date parser: a month name or a time makes the input invalid, because
|
|
42
|
+
* guessing there is how a calendar picks the wrong day.
|
|
43
|
+
*
|
|
44
|
+
* A two-digit year is windowed the way a paper form is: 00-49 is 2000-2049 and
|
|
45
|
+
* 50-99 is 1950-1999. The window is fixed rather than relative to a clock, so
|
|
46
|
+
* the same text parses to the same date in a test, on a server and next year.
|
|
47
|
+
* A four-digit ISO date (`2024-02-03`) is accepted in every locale before the
|
|
48
|
+
* locale order is applied, because it cannot be misread.
|
|
49
|
+
*
|
|
50
|
+
* Returns undefined for anything invalid — an impossible date, a leap day in a
|
|
51
|
+
* non-leap year, too few or too many fields, a one- or three-digit year — and
|
|
52
|
+
* never throws: this runs on every keystroke of a date input.
|
|
53
|
+
*/
|
|
54
|
+
function parseDateInput(text, locale) {
|
|
55
|
+
if (typeof text !== "string") return;
|
|
56
|
+
const value = text.trim();
|
|
57
|
+
if (value === "") return;
|
|
58
|
+
const iso = parseIso(value);
|
|
59
|
+
if (iso !== void 0) return iso;
|
|
60
|
+
const tokens = value.split(/\D+/).filter((token) => token !== "");
|
|
61
|
+
if (tokens.length !== 3) return;
|
|
62
|
+
const layout = layoutFor(locale);
|
|
63
|
+
const fields = {};
|
|
64
|
+
for (const [index, field] of layout.order.entries()) {
|
|
65
|
+
const token = tokens[index] ?? "";
|
|
66
|
+
const number = field === "year" ? parseYearToken(token) : parseDayOrMonth(token);
|
|
67
|
+
if (number === void 0) return;
|
|
68
|
+
fields[field] = number;
|
|
69
|
+
}
|
|
70
|
+
const candidate = {
|
|
71
|
+
year: fields.year,
|
|
72
|
+
month: fields.month,
|
|
73
|
+
day: fields.day
|
|
74
|
+
};
|
|
75
|
+
return isValidDate(candidate) ? candidate : void 0;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Writes a date in the locale's own order, zero-padded to the width a date input
|
|
79
|
+
* expects: two digits for month and day, four for the year.
|
|
80
|
+
*
|
|
81
|
+
* The output is the string `parseDateInput` reads back for every year this
|
|
82
|
+
* format can name (0 through 9999), so a controlled input can format state and
|
|
83
|
+
* parse what the user typed with the same pair of functions.
|
|
84
|
+
*
|
|
85
|
+
* Returns undefined for an invalid civil date and for a year outside 0..9999,
|
|
86
|
+
* which the four-digit field cannot express. Falls back to ISO when Intl is
|
|
87
|
+
* unavailable, matching the rest of the package.
|
|
88
|
+
*/
|
|
89
|
+
function formatDateInput(date, locale) {
|
|
90
|
+
if (!isValidDate(date) || date.year < 0 || date.year > 9999) return;
|
|
91
|
+
const { order, before, after } = layoutFor(locale);
|
|
92
|
+
const field = (name) => name === "year" ? pad(date.year, 4) : pad(name === "month" ? date.month : date.day, 2);
|
|
93
|
+
return before[0] + field(order[0]) + before[1] + field(order[1]) + before[2] + field(order[2]) + after;
|
|
94
|
+
}
|
|
95
|
+
/** The layout for a locale, probed once and cached; a bad tag caches the ISO fallback. */
|
|
96
|
+
function layoutFor(locale) {
|
|
97
|
+
const cached = layouts.get(locale);
|
|
98
|
+
if (cached !== void 0) return cached;
|
|
99
|
+
let layout = ISO_LAYOUT;
|
|
100
|
+
if (hasIntl()) try {
|
|
101
|
+
layout = readLayout(new Intl.DateTimeFormat(locale, {
|
|
102
|
+
year: "numeric",
|
|
103
|
+
month: "2-digit",
|
|
104
|
+
day: "2-digit",
|
|
105
|
+
calendar: "gregory",
|
|
106
|
+
numberingSystem: "latn",
|
|
107
|
+
timeZone: "UTC"
|
|
108
|
+
}).formatToParts(PROBE)) ?? ISO_LAYOUT;
|
|
109
|
+
} catch {
|
|
110
|
+
layout = ISO_LAYOUT;
|
|
111
|
+
}
|
|
112
|
+
layouts.set(locale, layout);
|
|
113
|
+
return layout;
|
|
114
|
+
}
|
|
115
|
+
/** Reads the field order and the literals around them out of a formatter's parts. */
|
|
116
|
+
function readLayout(parts) {
|
|
117
|
+
const order = [];
|
|
118
|
+
const before = [];
|
|
119
|
+
let literal = "";
|
|
120
|
+
for (const part of parts) if (part.type === "year" || part.type === "month" || part.type === "day") {
|
|
121
|
+
order.push(part.type);
|
|
122
|
+
before.push(literal);
|
|
123
|
+
literal = "";
|
|
124
|
+
} else literal += part.value;
|
|
125
|
+
if (order.length !== 3) return;
|
|
126
|
+
return {
|
|
127
|
+
order,
|
|
128
|
+
before,
|
|
129
|
+
after: literal
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** A four-digit year as written, or a windowed two-digit one. */
|
|
133
|
+
function parseYearToken(token) {
|
|
134
|
+
if (/^\d{4}$/.test(token)) return Number(token);
|
|
135
|
+
if (!/^\d{2}$/.test(token)) return;
|
|
136
|
+
const short = Number(token);
|
|
137
|
+
return short <= 49 ? 2e3 + short : 1900 + short;
|
|
138
|
+
}
|
|
139
|
+
/** A month or day as written: one or two digits, no sign, no padding requirement. */
|
|
140
|
+
function parseDayOrMonth(token) {
|
|
141
|
+
return /^\d{1,2}$/.test(token) ? Number(token) : void 0;
|
|
142
|
+
}
|
|
143
|
+
/** The ISO form, tried first in every locale because it is unambiguous. */
|
|
144
|
+
function parseIso(value) {
|
|
145
|
+
const match = ISO_DATE.exec(value);
|
|
146
|
+
if (match === null) return;
|
|
147
|
+
const candidate = {
|
|
148
|
+
year: Number(match[1]),
|
|
149
|
+
month: Number(match[2]),
|
|
150
|
+
day: Number(match[3])
|
|
151
|
+
};
|
|
152
|
+
return isValidDate(candidate) ? candidate : void 0;
|
|
153
|
+
}
|
|
154
|
+
const DIRECTION_MARKS = /[]/g;
|
|
155
|
+
/**
|
|
156
|
+
* Reads a date a person typed or pasted into a date field.
|
|
157
|
+
*
|
|
158
|
+
* Wider than `parseDateInput`: besides the locale's numeric order with any
|
|
159
|
+
* separator, it reads digits alone ("10202026"), a month by name or by an
|
|
160
|
+
* unambiguous start of three letters ("dec 3", "3 December 2026"), weekday
|
|
161
|
+
* names beside the date, and a date with no year, which takes the year of
|
|
162
|
+
* `referenceDate`. A two-digit year lands in the hundred years that end
|
|
163
|
+
* twenty years after that year.
|
|
164
|
+
*
|
|
165
|
+
* Returns undefined for anything that is not one real date. Never throws.
|
|
166
|
+
*/
|
|
167
|
+
function parseTypedDate(text, locale, options = {}) {
|
|
168
|
+
if (typeof text !== "string") return;
|
|
169
|
+
const value = text.replace(DIRECTION_MARKS, "").trim();
|
|
170
|
+
if (value === "") return;
|
|
171
|
+
const iso = parseIso(value);
|
|
172
|
+
if (iso !== void 0) return iso;
|
|
173
|
+
const referenceYear = options.referenceDate?.year ?? (/* @__PURE__ */ new Date()).getFullYear();
|
|
174
|
+
const words = wordsFor(locale);
|
|
175
|
+
const numbers = [];
|
|
176
|
+
let namedMonth;
|
|
177
|
+
for (const token of value.toLocaleLowerCase(locale).match(/\d+|[\p{L}\p{M}]+/gu) ?? []) {
|
|
178
|
+
if (/^\d+$/.test(token)) {
|
|
179
|
+
numbers.push(token);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const month = monthFromName(token, words.months);
|
|
183
|
+
if (month !== void 0 && namedMonth === void 0) namedMonth = month;
|
|
184
|
+
else if (!words.other.has(token)) return;
|
|
185
|
+
}
|
|
186
|
+
const order = layoutFor(locale).order;
|
|
187
|
+
const fields = {};
|
|
188
|
+
if (namedMonth !== void 0) {
|
|
189
|
+
if (numbers.length === 0 || numbers.length > 2) return;
|
|
190
|
+
fields.month = String(namedMonth);
|
|
191
|
+
(numbers.length === 1 ? ["day"] : words.namedOrder.filter((field) => field !== "month")).forEach((field, index) => fields[field] = numbers[index] ?? "");
|
|
192
|
+
} else if (numbers.length === 1) {
|
|
193
|
+
const digits = numbers[0] ?? "";
|
|
194
|
+
if (digits.length !== 8 && digits.length !== 6) return;
|
|
195
|
+
let offset = 0;
|
|
196
|
+
for (const field of order) {
|
|
197
|
+
const length = field === "year" ? digits.length - 4 : 2;
|
|
198
|
+
fields[field] = digits.slice(offset, offset += length);
|
|
199
|
+
}
|
|
200
|
+
} else if (numbers.length === 2 || numbers.length === 3) (numbers.length === 3 ? order : order.filter((field) => field !== "year")).forEach((field, index) => fields[field] = numbers[index] ?? "");
|
|
201
|
+
else return;
|
|
202
|
+
const candidate = {
|
|
203
|
+
year: fields.year === void 0 ? referenceYear : typedYear(fields.year, referenceYear),
|
|
204
|
+
month: Number(fields.month),
|
|
205
|
+
day: Number(fields.day)
|
|
206
|
+
};
|
|
207
|
+
return isValidDate(candidate) ? candidate : void 0;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The format a locale's numeric date is typed in, as a placeholder: "MM/DD/YYYY"
|
|
211
|
+
* in American English, "TT.MM.JJJJ" in German. Each field repeats the first
|
|
212
|
+
* letter of its name in the locale when the script has capitals, else the
|
|
213
|
+
* English letter.
|
|
214
|
+
*/
|
|
215
|
+
function datePlaceholder(locale) {
|
|
216
|
+
const cached = placeholders.get(locale);
|
|
217
|
+
if (cached !== void 0) return cached;
|
|
218
|
+
const { order, before, after } = layoutFor(locale);
|
|
219
|
+
const letters = fieldLetters(locale);
|
|
220
|
+
const field = (name) => letters[name].repeat(name === "year" ? 4 : 2);
|
|
221
|
+
const placeholder = (before[0] + field(order[0]) + before[1] + field(order[1]) + before[2] + field(order[2]) + after).replace(DIRECTION_MARKS, "");
|
|
222
|
+
placeholders.set(locale, placeholder);
|
|
223
|
+
return placeholder;
|
|
224
|
+
}
|
|
225
|
+
const placeholders = /* @__PURE__ */ new Map();
|
|
226
|
+
const ENGLISH_LETTERS = {
|
|
227
|
+
year: "Y",
|
|
228
|
+
month: "M",
|
|
229
|
+
day: "D"
|
|
230
|
+
};
|
|
231
|
+
function fieldLetters(locale) {
|
|
232
|
+
if (!hasIntl() || typeof Intl.DisplayNames !== "function") return ENGLISH_LETTERS;
|
|
233
|
+
try {
|
|
234
|
+
const names = new Intl.DisplayNames(locale, { type: "dateTimeField" });
|
|
235
|
+
const letter = (field) => {
|
|
236
|
+
const first = names.of(field)?.[0]?.toLocaleUpperCase(locale);
|
|
237
|
+
return first !== void 0 && first !== first.toLocaleLowerCase(locale) ? first : ENGLISH_LETTERS[field];
|
|
238
|
+
};
|
|
239
|
+
return {
|
|
240
|
+
year: letter("year"),
|
|
241
|
+
month: letter("month"),
|
|
242
|
+
day: letter("day")
|
|
243
|
+
};
|
|
244
|
+
} catch {
|
|
245
|
+
return ENGLISH_LETTERS;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function typedYear(token, referenceYear) {
|
|
249
|
+
const year = Number(token);
|
|
250
|
+
if (token.length !== 2) return year;
|
|
251
|
+
const century = Math.floor((referenceYear + 20) / 100) * 100;
|
|
252
|
+
return century + year > referenceYear + 20 ? century - 100 + year : century + year;
|
|
253
|
+
}
|
|
254
|
+
const dateWords = /* @__PURE__ */ new Map();
|
|
255
|
+
function wordsFor(locale) {
|
|
256
|
+
const cached = dateWords.get(locale);
|
|
257
|
+
if (cached !== void 0) return cached;
|
|
258
|
+
const months = /* @__PURE__ */ new Map();
|
|
259
|
+
const other = /* @__PURE__ */ new Set();
|
|
260
|
+
let namedOrder = [
|
|
261
|
+
"month",
|
|
262
|
+
"day",
|
|
263
|
+
"year"
|
|
264
|
+
];
|
|
265
|
+
const addWords = (text) => {
|
|
266
|
+
for (const word of text.toLocaleLowerCase(locale).match(/[\p{L}\p{M}]+/gu) ?? []) other.add(word);
|
|
267
|
+
};
|
|
268
|
+
if (hasIntl()) for (const tag of [locale, "en-US"]) try {
|
|
269
|
+
for (const width of ["long", "short"]) {
|
|
270
|
+
const standalone = new Intl.DateTimeFormat(tag, {
|
|
271
|
+
month: width,
|
|
272
|
+
timeZone: "UTC"
|
|
273
|
+
});
|
|
274
|
+
const inDate = new Intl.DateTimeFormat(tag, {
|
|
275
|
+
month: width,
|
|
276
|
+
day: "numeric",
|
|
277
|
+
timeZone: "UTC"
|
|
278
|
+
});
|
|
279
|
+
const weekday = new Intl.DateTimeFormat(tag, {
|
|
280
|
+
weekday: width,
|
|
281
|
+
timeZone: "UTC"
|
|
282
|
+
});
|
|
283
|
+
for (let index = 0; index < 12; index++) {
|
|
284
|
+
const at = Date.UTC(2001, index, 16);
|
|
285
|
+
const name = inDate.formatToParts(at).find((part) => part.type === "month")?.value ?? "";
|
|
286
|
+
for (const text of [standalone.format(at), name]) {
|
|
287
|
+
const key = text.toLocaleLowerCase(locale).replace(/\.$/, "");
|
|
288
|
+
if (/\p{L}/u.test(key) && !months.has(key)) months.set(key, index + 1);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
for (let index = 0; index < 7; index++) addWords(weekday.format(Date.UTC(2001, 0, 7 + index)));
|
|
292
|
+
}
|
|
293
|
+
const named = new Intl.DateTimeFormat(tag, {
|
|
294
|
+
year: "numeric",
|
|
295
|
+
month: "long",
|
|
296
|
+
day: "numeric",
|
|
297
|
+
weekday: "long",
|
|
298
|
+
timeZone: "UTC"
|
|
299
|
+
}).formatToParts(PROBE);
|
|
300
|
+
if (tag === locale) namedOrder = named.map((part) => part.type).filter((type) => type === "year" || type === "month" || type === "day");
|
|
301
|
+
for (const part of named) if (part.type === "literal") addWords(part.value);
|
|
302
|
+
} catch {}
|
|
303
|
+
const words = {
|
|
304
|
+
months,
|
|
305
|
+
other,
|
|
306
|
+
namedOrder
|
|
307
|
+
};
|
|
308
|
+
dateWords.set(locale, words);
|
|
309
|
+
return words;
|
|
310
|
+
}
|
|
311
|
+
/** The month a word names: a whole name, or the start of exactly one month's names, three letters or more. */
|
|
312
|
+
function monthFromName(word, months) {
|
|
313
|
+
const exact = months.get(word);
|
|
314
|
+
if (exact !== void 0) return exact;
|
|
315
|
+
if (word.length < 3) return;
|
|
316
|
+
let found;
|
|
317
|
+
for (const [name, month] of months) if (name.startsWith(word)) {
|
|
318
|
+
if (found !== void 0 && found !== month) return;
|
|
319
|
+
found = month;
|
|
320
|
+
}
|
|
321
|
+
return found;
|
|
322
|
+
}
|
|
323
|
+
export { dateOrder, datePlaceholder, formatDateInput, parseDateInput, parseTypedDate };
|