@mdgascar/date 0.11.0 → 0.12.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/dist/index.d.cts CHANGED
@@ -39,7 +39,7 @@ type DateStyle = 'long' | 'full' | 'medium' | 'short' | 'numeric';
39
39
  declare function formatDate(date: string | CalendarDate | Date, style?: DateStyle): string;
40
40
 
41
41
  /** Version of the shared spec this code was generated from. */
42
- declare const SPEC_VERSION = "0.10.0";
42
+ declare const SPEC_VERSION = "0.11.0";
43
43
  /**
44
44
  * Error code shared by every mdg-utils language port.
45
45
  *
@@ -48,9 +48,8 @@ declare const SPEC_VERSION = "0.10.0";
48
48
  * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
49
49
  * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
50
50
  * - `PRECISION_LOSS`: The value is exact in the text but the port's number type cannot hold it exactly; ask for the exact text instead.
51
- * - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
52
51
  */
53
- type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
52
+ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS';
54
53
  /**
55
54
  * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
56
55
  *
@@ -106,55 +105,6 @@ declare class MdgError extends Error {
106
105
  static [Symbol.hasInstance](value: unknown): boolean;
107
106
  }
108
107
 
109
- /**
110
- * Reads a Malagasy date without throwing.
111
- *
112
- * @param text - A date as {@link formatDate} writes it, in any style, any case and spacing.
113
- * @param style - Optional: read only this style.
114
- * @returns `{ success: true, data }` with the calendar date, or `{ success: false, error }`.
115
- *
116
- * @example
117
- * ```ts
118
- * const result = safeParseDate('23 Novambra 2026');
119
- * if (result.success) result.data; // { year: 2026, month: 11, day: 23 }
120
- * ```
121
- */
122
- declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResult<CalendarDate>;
123
- /**
124
- * Reads a Malagasy date back into calendar fields: the inverse of {@link formatDate}.
125
- *
126
- * @param text - A date in any style {@link formatDate} writes: `23 Novambra 2026`,
127
- * `Alatsinainy 23 Novambra 2026`, `2026 Nov 23`, `2026-11-23` or `23/11/2026`. Case and
128
- * spacing do not matter; day and month may have one digit.
129
- * @param style - Optional: read only this style.
130
- * @returns The calendar date.
131
- * @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
132
- * `INVALID_FORMAT` for text in no style, a day that does not exist or a wrong weekday,
133
- * `OUT_OF_RANGE` for a year outside 1–9999, `AMBIGUOUS` when two styles read different dates
134
- * (only for years up to 31, such as `5 Mey 12`; pass the style).
135
- *
136
- * @example
137
- * ```ts
138
- * parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }
139
- * parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
140
- * ```
141
- */
142
- declare function parseDate(text: string, style?: DateStyle): CalendarDate;
143
- /**
144
- * Reads a Malagasy date, or gives `undefined` instead of throwing.
145
- *
146
- * @param text - A date in any style {@link formatDate} writes.
147
- * @param style - Optional: read only this style.
148
- * @returns The calendar date, or `undefined` when {@link parseDate} would throw.
149
- *
150
- * @example
151
- * ```ts
152
- * tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
153
- * tryParseDate('30/02/2026'); // undefined
154
- * ```
155
- */
156
- declare function tryParseDate(text: unknown, style?: DateStyle): CalendarDate | undefined;
157
-
158
108
  /**
159
109
  * A time of day: hour 0–23 and minute 0–59.
160
110
  *
@@ -227,6 +177,84 @@ declare function parseTime(text: string): ClockTime;
227
177
  */
228
178
  declare function tryParseTime(text: unknown): ClockTime | undefined;
229
179
 
180
+ /**
181
+ * A date and a time of day: calendar fields plus hour and minute.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const meeting: CalendarDateTime = { year: 2026, month: 11, day: 23, hour: 14, minute: 30 };
186
+ * ```
187
+ */
188
+ interface CalendarDateTime extends CalendarDate, ClockTime {
189
+ }
190
+ /**
191
+ * Writes a date and a time in Malagasy: the date in a style, then the time in words.
192
+ *
193
+ * @param dateTime - `YYYY-MM-DDTHH:MM` text (a space may replace the `T`),
194
+ * `{ year, month, day, hour, minute }`, or a `Date` (read in its local time).
195
+ * @param style - The date style: `long` (default), `full`, `medium`, `short` or `numeric`.
196
+ * @returns The date, then the time in words.
197
+ * @throws {@link MdgError} `INVALID_FORMAT` for text that is not a date and a time or a day
198
+ * that does not exist, `OUT_OF_RANGE` for fields out of range, `INVALID_TYPE` for other
199
+ * input or an unknown style.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * spellDateTime('2026-11-23T14:30'); // "23 Novambra 2026 amin'ny roa sy sasany tolakandro"
204
+ * spellDateTime('2026-11-23T14:30', 'full'); // "Alatsinainy 23 Novambra 2026 amin'ny roa sy sasany tolakandro"
205
+ * ```
206
+ */
207
+ declare function spellDateTime(dateTime: string | CalendarDateTime | Date, style?: DateStyle): string;
208
+
209
+ /**
210
+ * Reads a Malagasy date without throwing.
211
+ *
212
+ * @param text - A date as {@link formatDate} writes it, in any style, any case and spacing.
213
+ * @param style - Optional: read only this style.
214
+ * @returns `{ success: true, data }` with the calendar date, or `{ success: false, error }`.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * const result = safeParseDate('23 Novambra 2026');
219
+ * if (result.success) result.data; // { year: 2026, month: 11, day: 23 }
220
+ * ```
221
+ */
222
+ declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResult<CalendarDate>;
223
+ /**
224
+ * Reads a Malagasy date back into calendar fields: the inverse of {@link formatDate}.
225
+ *
226
+ * @param text - A date in any style {@link formatDate} writes: `23 Novambra 2026`,
227
+ * `Alatsinainy 23 Novambra 2026`, `2026 Nov 23`, `2026-11-23` or `23/11/2026`. Case and
228
+ * spacing do not matter; day and month may have one digit.
229
+ * @param style - Optional: read only this style.
230
+ * @returns The calendar date.
231
+ * @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
232
+ * `INVALID_FORMAT` for text in no style, a day that does not exist or a wrong weekday,
233
+ * `OUT_OF_RANGE` for a year outside 1–9999. Without a style, the styles are tried in order and
234
+ * the first that reads wins (`5 Mey 12` is 5 May of year 12).
235
+ *
236
+ * @example
237
+ * ```ts
238
+ * parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }
239
+ * parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
240
+ * ```
241
+ */
242
+ declare function parseDate(text: string, style?: DateStyle): CalendarDate;
243
+ /**
244
+ * Reads a Malagasy date, or gives `undefined` instead of throwing.
245
+ *
246
+ * @param text - A date in any style {@link formatDate} writes.
247
+ * @param style - Optional: read only this style.
248
+ * @returns The calendar date, or `undefined` when {@link parseDate} would throw.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
253
+ * tryParseDate('30/02/2026'); // undefined
254
+ * ```
255
+ */
256
+ declare function tryParseDate(text: unknown, style?: DateStyle): CalendarDate | undefined;
257
+
230
258
  /**
231
259
  * Name width: `wide` (`Novambra`, default) or `abbreviated` (`Nov`).
232
260
  *
@@ -269,5 +297,5 @@ declare function monthName(month: number, width?: NameWidth): string;
269
297
  */
270
298
  declare function weekdayName(isoWeekday: number, width?: NameWidth): string;
271
299
 
272
- export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellTime, tryParseDate, tryParseTime, weekdayName };
273
- export type { CalendarDate, ClockTime, DateStyle, ErrorCode, NameWidth, SafeParseResult };
300
+ export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellDateTime, spellTime, tryParseDate, tryParseTime, weekdayName };
301
+ export type { CalendarDate, CalendarDateTime, ClockTime, DateStyle, ErrorCode, NameWidth, SafeParseResult };
package/dist/index.d.ts CHANGED
@@ -39,7 +39,7 @@ type DateStyle = 'long' | 'full' | 'medium' | 'short' | 'numeric';
39
39
  declare function formatDate(date: string | CalendarDate | Date, style?: DateStyle): string;
40
40
 
41
41
  /** Version of the shared spec this code was generated from. */
42
- declare const SPEC_VERSION = "0.10.0";
42
+ declare const SPEC_VERSION = "0.11.0";
43
43
  /**
44
44
  * Error code shared by every mdg-utils language port.
45
45
  *
@@ -48,9 +48,8 @@ declare const SPEC_VERSION = "0.10.0";
48
48
  * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
49
49
  * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
50
50
  * - `PRECISION_LOSS`: The value is exact in the text but the port's number type cannot hold it exactly; ask for the exact text instead.
51
- * - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
52
51
  */
53
- type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
52
+ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS';
54
53
  /**
55
54
  * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
56
55
  *
@@ -106,55 +105,6 @@ declare class MdgError extends Error {
106
105
  static [Symbol.hasInstance](value: unknown): boolean;
107
106
  }
108
107
 
109
- /**
110
- * Reads a Malagasy date without throwing.
111
- *
112
- * @param text - A date as {@link formatDate} writes it, in any style, any case and spacing.
113
- * @param style - Optional: read only this style.
114
- * @returns `{ success: true, data }` with the calendar date, or `{ success: false, error }`.
115
- *
116
- * @example
117
- * ```ts
118
- * const result = safeParseDate('23 Novambra 2026');
119
- * if (result.success) result.data; // { year: 2026, month: 11, day: 23 }
120
- * ```
121
- */
122
- declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResult<CalendarDate>;
123
- /**
124
- * Reads a Malagasy date back into calendar fields: the inverse of {@link formatDate}.
125
- *
126
- * @param text - A date in any style {@link formatDate} writes: `23 Novambra 2026`,
127
- * `Alatsinainy 23 Novambra 2026`, `2026 Nov 23`, `2026-11-23` or `23/11/2026`. Case and
128
- * spacing do not matter; day and month may have one digit.
129
- * @param style - Optional: read only this style.
130
- * @returns The calendar date.
131
- * @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
132
- * `INVALID_FORMAT` for text in no style, a day that does not exist or a wrong weekday,
133
- * `OUT_OF_RANGE` for a year outside 1–9999, `AMBIGUOUS` when two styles read different dates
134
- * (only for years up to 31, such as `5 Mey 12`; pass the style).
135
- *
136
- * @example
137
- * ```ts
138
- * parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }
139
- * parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
140
- * ```
141
- */
142
- declare function parseDate(text: string, style?: DateStyle): CalendarDate;
143
- /**
144
- * Reads a Malagasy date, or gives `undefined` instead of throwing.
145
- *
146
- * @param text - A date in any style {@link formatDate} writes.
147
- * @param style - Optional: read only this style.
148
- * @returns The calendar date, or `undefined` when {@link parseDate} would throw.
149
- *
150
- * @example
151
- * ```ts
152
- * tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
153
- * tryParseDate('30/02/2026'); // undefined
154
- * ```
155
- */
156
- declare function tryParseDate(text: unknown, style?: DateStyle): CalendarDate | undefined;
157
-
158
108
  /**
159
109
  * A time of day: hour 0–23 and minute 0–59.
160
110
  *
@@ -227,6 +177,84 @@ declare function parseTime(text: string): ClockTime;
227
177
  */
228
178
  declare function tryParseTime(text: unknown): ClockTime | undefined;
229
179
 
180
+ /**
181
+ * A date and a time of day: calendar fields plus hour and minute.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const meeting: CalendarDateTime = { year: 2026, month: 11, day: 23, hour: 14, minute: 30 };
186
+ * ```
187
+ */
188
+ interface CalendarDateTime extends CalendarDate, ClockTime {
189
+ }
190
+ /**
191
+ * Writes a date and a time in Malagasy: the date in a style, then the time in words.
192
+ *
193
+ * @param dateTime - `YYYY-MM-DDTHH:MM` text (a space may replace the `T`),
194
+ * `{ year, month, day, hour, minute }`, or a `Date` (read in its local time).
195
+ * @param style - The date style: `long` (default), `full`, `medium`, `short` or `numeric`.
196
+ * @returns The date, then the time in words.
197
+ * @throws {@link MdgError} `INVALID_FORMAT` for text that is not a date and a time or a day
198
+ * that does not exist, `OUT_OF_RANGE` for fields out of range, `INVALID_TYPE` for other
199
+ * input or an unknown style.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * spellDateTime('2026-11-23T14:30'); // "23 Novambra 2026 amin'ny roa sy sasany tolakandro"
204
+ * spellDateTime('2026-11-23T14:30', 'full'); // "Alatsinainy 23 Novambra 2026 amin'ny roa sy sasany tolakandro"
205
+ * ```
206
+ */
207
+ declare function spellDateTime(dateTime: string | CalendarDateTime | Date, style?: DateStyle): string;
208
+
209
+ /**
210
+ * Reads a Malagasy date without throwing.
211
+ *
212
+ * @param text - A date as {@link formatDate} writes it, in any style, any case and spacing.
213
+ * @param style - Optional: read only this style.
214
+ * @returns `{ success: true, data }` with the calendar date, or `{ success: false, error }`.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * const result = safeParseDate('23 Novambra 2026');
219
+ * if (result.success) result.data; // { year: 2026, month: 11, day: 23 }
220
+ * ```
221
+ */
222
+ declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResult<CalendarDate>;
223
+ /**
224
+ * Reads a Malagasy date back into calendar fields: the inverse of {@link formatDate}.
225
+ *
226
+ * @param text - A date in any style {@link formatDate} writes: `23 Novambra 2026`,
227
+ * `Alatsinainy 23 Novambra 2026`, `2026 Nov 23`, `2026-11-23` or `23/11/2026`. Case and
228
+ * spacing do not matter; day and month may have one digit.
229
+ * @param style - Optional: read only this style.
230
+ * @returns The calendar date.
231
+ * @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
232
+ * `INVALID_FORMAT` for text in no style, a day that does not exist or a wrong weekday,
233
+ * `OUT_OF_RANGE` for a year outside 1–9999. Without a style, the styles are tried in order and
234
+ * the first that reads wins (`5 Mey 12` is 5 May of year 12).
235
+ *
236
+ * @example
237
+ * ```ts
238
+ * parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }
239
+ * parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
240
+ * ```
241
+ */
242
+ declare function parseDate(text: string, style?: DateStyle): CalendarDate;
243
+ /**
244
+ * Reads a Malagasy date, or gives `undefined` instead of throwing.
245
+ *
246
+ * @param text - A date in any style {@link formatDate} writes.
247
+ * @param style - Optional: read only this style.
248
+ * @returns The calendar date, or `undefined` when {@link parseDate} would throw.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
253
+ * tryParseDate('30/02/2026'); // undefined
254
+ * ```
255
+ */
256
+ declare function tryParseDate(text: unknown, style?: DateStyle): CalendarDate | undefined;
257
+
230
258
  /**
231
259
  * Name width: `wide` (`Novambra`, default) or `abbreviated` (`Nov`).
232
260
  *
@@ -269,5 +297,5 @@ declare function monthName(month: number, width?: NameWidth): string;
269
297
  */
270
298
  declare function weekdayName(isoWeekday: number, width?: NameWidth): string;
271
299
 
272
- export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellTime, tryParseDate, tryParseTime, weekdayName };
273
- export type { CalendarDate, ClockTime, DateStyle, ErrorCode, NameWidth, SafeParseResult };
300
+ export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellDateTime, spellTime, tryParseDate, tryParseTime, weekdayName };
301
+ export type { CalendarDate, CalendarDateTime, ClockTime, DateStyle, ErrorCode, NameWidth, SafeParseResult };
package/dist/index.js CHANGED
@@ -62,6 +62,7 @@ var DEFAULT_STYLE = "long";
62
62
  var MIN_YEAR = 1;
63
63
  var MAX_READ_LENGTH = 64;
64
64
  var MAX_YEAR = 9999;
65
+ var DATE_TIME_PATTERN = "{1} {0}";
65
66
  var TIME_WORDS = {
66
67
  prefix: "amin'ny",
67
68
  after: "sy",
@@ -102,7 +103,7 @@ var DAY_PARTS = [
102
103
  var MAX_TIME_READ_LENGTH = 128;
103
104
 
104
105
  // src/generated/errors.ts
105
- var SPEC_VERSION = "0.10.0";
106
+ var SPEC_VERSION = "0.11.0";
106
107
  var MdgError = class extends Error {
107
108
  /** Always `'MdgError'`. */
108
109
  name = "MdgError";
@@ -243,120 +244,6 @@ function formatDate(date, style = DEFAULT_STYLE) {
243
244
  }
244
245
  return render(pattern, toCalendarDate(date));
245
246
  }
246
-
247
- // src/parse.ts
248
- var PATTERN_TOKEN2 = /EEEE|MMMM|MMM|MM|dd|d|y|\s+|[^\sEMdy]+/g;
249
- var REGEX_SPECIAL = /[\\^$.*+?()[\]{}|-]/g;
250
- var ANY_SPACE = "\\s+";
251
- var DAY_OR_MONTH = "(\\d{1,2})";
252
- var YEAR = "(\\d{1,4})";
253
- var WHITESPACE = /^\s+$/;
254
- function namesPattern(names) {
255
- return `(${names.filter((name) => name !== "").join("|")})`;
256
- }
257
- var TOKEN_PATTERNS = {
258
- EEEE: [namesPattern(WEEKDAY_NAMES), "weekday"],
259
- MMMM: [namesPattern(MONTH_NAMES), "month"],
260
- MMM: [namesPattern(MONTH_ABBREVIATIONS), "month"],
261
- MM: [DAY_OR_MONTH, "month"],
262
- dd: [DAY_OR_MONTH, "day"],
263
- d: [DAY_OR_MONTH, "day"],
264
- y: [YEAR, "year"]
265
- };
266
- function styleReader(stylePattern) {
267
- const fields = [];
268
- const source = Array.from(stylePattern.matchAll(PATTERN_TOKEN2), ([part]) => {
269
- if (WHITESPACE.test(part)) return ANY_SPACE;
270
- const token = TOKEN_PATTERNS[part];
271
- if (token === void 0) return part.replace(REGEX_SPECIAL, "\\$&");
272
- fields.push([token[1], part]);
273
- return token[0];
274
- });
275
- return { pattern: new RegExp(`^${source.join("")}$`, "i"), fields };
276
- }
277
- var READERS = new Map(
278
- [...STYLE_PATTERNS].map(([style, pattern]) => [style, styleReader(pattern)])
279
- );
280
- function nameIndex(names, text) {
281
- return names.findIndex((name) => name.toLowerCase() === text.toLowerCase());
282
- }
283
- function capturedValue(token, text) {
284
- if (token === "EEEE") return nameIndex(WEEKDAY_NAMES, text);
285
- if (token === "MMMM") return nameIndex(MONTH_NAMES, text);
286
- if (token === "MMM") return nameIndex(MONTH_ABBREVIATIONS, text);
287
- return Number(text);
288
- }
289
- function readStyle(text, reader) {
290
- const match = reader.pattern.exec(text);
291
- if (match === null) return void 0;
292
- const fields = {};
293
- reader.fields.forEach(([name, token], index) => {
294
- fields[name] = capturedValue(token, String(match[index + 1]));
295
- });
296
- return {
297
- year: Number(fields.year),
298
- month: Number(fields.month),
299
- day: Number(fields.day),
300
- weekday: fields.weekday
301
- };
302
- }
303
- function failure(code, message) {
304
- return { success: false, error: new MdgError(code, message) };
305
- }
306
- function readersFor(style) {
307
- if (style === void 0) return [...READERS.values()];
308
- const reader = READERS.get(style);
309
- return reader === void 0 ? void 0 : [reader];
310
- }
311
- function checkedFields(fields) {
312
- try {
313
- const date = checkedTextDate({ year: fields.year, month: fields.month, day: fields.day });
314
- if (fields.weekday !== void 0 && fields.weekday !== isoWeekdayOf(date)) {
315
- return failure("INVALID_FORMAT", "The weekday does not match the date.");
316
- }
317
- return { success: true, data: date };
318
- } catch (error) {
319
- return { success: false, error };
320
- }
321
- }
322
- function safeParseDate(text, style) {
323
- if (typeof text !== "string") return failure("INVALID_TYPE", "Expected the date as text.");
324
- const readers = readersFor(style);
325
- if (readers === void 0) {
326
- return failure("INVALID_TYPE", `Style is one of: ${[...READERS.keys()].join(", ")}.`);
327
- }
328
- const trimmed = text.trim();
329
- if (text.length > MAX_READ_LENGTH) {
330
- return failure("INVALID_FORMAT", `Dates are at most ${String(MAX_READ_LENGTH)} characters.`);
331
- }
332
- const [first, ...others] = readers.flatMap((reader) => readStyle(trimmed, reader) ?? []);
333
- if (first === void 0) {
334
- return failure("INVALID_FORMAT", "Expected a date such as 23 Novambra 2026 or 23/11/2026.");
335
- }
336
- return oneDate(checkedFields(first), others.map(checkedFields));
337
- }
338
- function dateKey({ year, month, day }) {
339
- return [year, month, day].join("-");
340
- }
341
- function oneDate(first, others) {
342
- const [date, ...rest] = [first, ...others].flatMap(
343
- (result) => result.success ? [result.data] : []
344
- );
345
- if (date === void 0) return first;
346
- if (rest.some((other) => dateKey(other) !== dateKey(date))) {
347
- return failure("AMBIGUOUS", "This text reads as more than one date; pass the style.");
348
- }
349
- return { success: true, data: date };
350
- }
351
- function parseDate(text, style) {
352
- const result = safeParseDate(text, style);
353
- if (!result.success) throw result.error;
354
- return result.data;
355
- }
356
- function tryParseDate(text, style) {
357
- const result = safeParseDate(text, style);
358
- return result.success ? result.data : void 0;
359
- }
360
247
  var HOURS_PER_DAY = 24;
361
248
  var HOURS_ON_THE_CLOCK = 12;
362
249
  var MINUTES_PER_HOUR = 60;
@@ -431,20 +318,20 @@ function normalise(text) {
431
318
  const words = text.toLowerCase().replace(APOSTROPHES, "'").replace(SPACES, " ").trim();
432
319
  return words.startsWith(`${TIME_WORDS.prefix} `) ? words : `${TIME_WORDS.prefix} ${words}`;
433
320
  }
434
- function failure2(code, message) {
321
+ function failure(code, message) {
435
322
  return { success: false, error: new MdgError(code, message) };
436
323
  }
437
324
  function safeParseTime(text) {
438
- if (typeof text !== "string") return failure2("INVALID_TYPE", "Expected the time as text.");
325
+ if (typeof text !== "string") return failure("INVALID_TYPE", "Expected the time as text.");
439
326
  if (text.length > MAX_TIME_READ_LENGTH) {
440
- return failure2(
327
+ return failure(
441
328
  "INVALID_FORMAT",
442
329
  `Times are at most ${String(MAX_TIME_READ_LENGTH)} characters.`
443
330
  );
444
331
  }
445
332
  const time = timesByWords().get(normalise(text));
446
333
  if (time === void 0) {
447
- return failure2("INVALID_FORMAT", "Expected a time such as amin'ny roa sy sasany tolakandro.");
334
+ return failure("INVALID_FORMAT", "Expected a time such as amin'ny roa sy sasany tolakandro.");
448
335
  }
449
336
  return { success: true, data: time };
450
337
  }
@@ -458,6 +345,134 @@ function tryParseTime(text) {
458
345
  return result.success ? result.data : void 0;
459
346
  }
460
347
 
461
- export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellTime, tryParseDate, tryParseTime, weekdayName };
348
+ // src/date-time.ts
349
+ var DATE_TIME_TEXT = /^(\d{4}-\d{2}-\d{2})[T ](\d{1,2}:\d{2})$/;
350
+ var DATE_PLACE = "{1}";
351
+ var TIME_PLACE = "{0}";
352
+ function parts(input) {
353
+ if (typeof input === "string") {
354
+ const match = DATE_TIME_TEXT.exec(input);
355
+ if (match === null) throw new MdgError("INVALID_FORMAT", "Expected YYYY-MM-DDTHH:MM.");
356
+ return [String(match[1]), String(match[2])];
357
+ }
358
+ if (input instanceof Date) return [input, input];
359
+ if (typeof input === "object" && input !== null) {
360
+ const { year, month, day, hour, minute } = input;
361
+ return [
362
+ { year, month, day },
363
+ { hour, minute }
364
+ ];
365
+ }
366
+ throw new MdgError("INVALID_TYPE", "Expected YYYY-MM-DDTHH:MM text, date-time fields or a Date.");
367
+ }
368
+ function spellDateTime(dateTime, style) {
369
+ const [date, time] = parts(dateTime);
370
+ const dateText = formatDate(date, style);
371
+ return DATE_TIME_PATTERN.replace(DATE_PLACE, dateText).replace(TIME_PLACE, spellTime(time));
372
+ }
373
+
374
+ // src/parse.ts
375
+ var PATTERN_TOKEN2 = /EEEE|MMMM|MMM|MM|dd|d|y|\s+|[^\sEMdy]+/g;
376
+ var REGEX_SPECIAL = /[\\^$.*+?()[\]{}|-]/g;
377
+ var ANY_SPACE = "\\s+";
378
+ var DAY_OR_MONTH = "(\\d{1,2})";
379
+ var YEAR = "(\\d{1,4})";
380
+ var WHITESPACE = /^\s+$/;
381
+ function namesPattern(names) {
382
+ return `(${names.filter((name) => name !== "").join("|")})`;
383
+ }
384
+ var TOKEN_PATTERNS = {
385
+ EEEE: [namesPattern(WEEKDAY_NAMES), "weekday"],
386
+ MMMM: [namesPattern(MONTH_NAMES), "month"],
387
+ MMM: [namesPattern(MONTH_ABBREVIATIONS), "month"],
388
+ MM: [DAY_OR_MONTH, "month"],
389
+ dd: [DAY_OR_MONTH, "day"],
390
+ d: [DAY_OR_MONTH, "day"],
391
+ y: [YEAR, "year"]
392
+ };
393
+ function styleReader(stylePattern) {
394
+ const fields = [];
395
+ const source = Array.from(stylePattern.matchAll(PATTERN_TOKEN2), ([part]) => {
396
+ if (WHITESPACE.test(part)) return ANY_SPACE;
397
+ const token = TOKEN_PATTERNS[part];
398
+ if (token === void 0) return part.replace(REGEX_SPECIAL, "\\$&");
399
+ fields.push([token[1], part]);
400
+ return token[0];
401
+ });
402
+ return { pattern: new RegExp(`^${source.join("")}$`, "i"), fields };
403
+ }
404
+ var READERS = new Map(
405
+ [...STYLE_PATTERNS].map(([style, pattern]) => [style, styleReader(pattern)])
406
+ );
407
+ function nameIndex(names, text) {
408
+ return names.findIndex((name) => name.toLowerCase() === text.toLowerCase());
409
+ }
410
+ function capturedValue(token, text) {
411
+ if (token === "EEEE") return nameIndex(WEEKDAY_NAMES, text);
412
+ if (token === "MMMM") return nameIndex(MONTH_NAMES, text);
413
+ if (token === "MMM") return nameIndex(MONTH_ABBREVIATIONS, text);
414
+ return Number(text);
415
+ }
416
+ function readStyle(text, reader) {
417
+ const match = reader.pattern.exec(text);
418
+ if (match === null) return void 0;
419
+ const fields = {};
420
+ reader.fields.forEach(([name, token], index) => {
421
+ fields[name] = capturedValue(token, String(match[index + 1]));
422
+ });
423
+ return {
424
+ year: Number(fields.year),
425
+ month: Number(fields.month),
426
+ day: Number(fields.day),
427
+ weekday: fields.weekday
428
+ };
429
+ }
430
+ function failure2(code, message) {
431
+ return { success: false, error: new MdgError(code, message) };
432
+ }
433
+ function readersFor(style) {
434
+ if (style === void 0) return [...READERS.values()];
435
+ const reader = READERS.get(style);
436
+ return reader === void 0 ? void 0 : [reader];
437
+ }
438
+ function checkedFields(fields) {
439
+ try {
440
+ const date = checkedTextDate({ year: fields.year, month: fields.month, day: fields.day });
441
+ if (fields.weekday !== void 0 && fields.weekday !== isoWeekdayOf(date)) {
442
+ return failure2("INVALID_FORMAT", "The weekday does not match the date.");
443
+ }
444
+ return { success: true, data: date };
445
+ } catch (error) {
446
+ return { success: false, error };
447
+ }
448
+ }
449
+ function safeParseDate(text, style) {
450
+ if (typeof text !== "string") return failure2("INVALID_TYPE", "Expected the date as text.");
451
+ const readers = readersFor(style);
452
+ if (readers === void 0) {
453
+ return failure2("INVALID_TYPE", `Style is one of: ${[...READERS.keys()].join(", ")}.`);
454
+ }
455
+ const trimmed = text.trim();
456
+ if (text.length > MAX_READ_LENGTH) {
457
+ return failure2("INVALID_FORMAT", `Dates are at most ${String(MAX_READ_LENGTH)} characters.`);
458
+ }
459
+ const results = readers.flatMap((reader) => readStyle(trimmed, reader) ?? []).map(checkedFields);
460
+ const [first] = results;
461
+ if (first === void 0) {
462
+ return failure2("INVALID_FORMAT", "Expected a date such as 23 Novambra 2026 or 23/11/2026.");
463
+ }
464
+ return results.find((result) => result.success) ?? first;
465
+ }
466
+ function parseDate(text, style) {
467
+ const result = safeParseDate(text, style);
468
+ if (!result.success) throw result.error;
469
+ return result.data;
470
+ }
471
+ function tryParseDate(text, style) {
472
+ const result = safeParseDate(text, style);
473
+ return result.success ? result.data : void 0;
474
+ }
475
+
476
+ export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellDateTime, spellTime, tryParseDate, tryParseTime, weekdayName };
462
477
  //# sourceMappingURL=index.js.map
463
478
  //# sourceMappingURL=index.js.map