@mdgascar/date 0.10.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/README.md +22 -4
- package/dist/index.cjs +182 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +107 -7
- package/dist/index.d.ts +107 -7
- package/dist/index.js +178 -23
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
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.
|
|
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.9.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'
|
|
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,6 +105,107 @@ declare class MdgError extends Error {
|
|
|
106
105
|
static [Symbol.hasInstance](value: unknown): boolean;
|
|
107
106
|
}
|
|
108
107
|
|
|
108
|
+
/**
|
|
109
|
+
* A time of day: hour 0–23 and minute 0–59.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const time: ClockTime = { hour: 14, minute: 30 };
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
interface ClockTime {
|
|
117
|
+
/** Hour, 0 to 23. */
|
|
118
|
+
readonly hour: number;
|
|
119
|
+
/** Minute, 0 to 59. */
|
|
120
|
+
readonly minute: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Says a time of day in Malagasy: `amin'ny`, the hour on a 12-hour clock, the minutes, then
|
|
124
|
+
* the part of the day.
|
|
125
|
+
*
|
|
126
|
+
* @param time - `HH:MM` text, `{ hour, minute }`, or a `Date` (read in its local time).
|
|
127
|
+
* @returns The time in words.
|
|
128
|
+
* @throws {@link MdgError} `INVALID_FORMAT` for text that is not `HH:MM`, `OUT_OF_RANGE` for an
|
|
129
|
+
* hour outside 0–23 or a minute outside 0–59, `INVALID_TYPE` for other input.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* spellTime('14:30'); // "amin'ny roa sy sasany tolakandro"
|
|
134
|
+
* spellTime('07:15'); // "amin'ny fito sy fahefany maraina"
|
|
135
|
+
* spellTime('17:45'); // "amin'ny enina latsaka fahefany hariva"
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
declare function spellTime(time: string | ClockTime | Date): string;
|
|
139
|
+
/**
|
|
140
|
+
* Reads a time said in Malagasy without throwing.
|
|
141
|
+
*
|
|
142
|
+
* @param text - A time as {@link spellTime} says it.
|
|
143
|
+
* @returns `{ success: true, data }` with the time, or `{ success: false, error }`.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const result = safeParseTime("amin'ny roa sy sasany tolakandro");
|
|
148
|
+
* if (result.success) result.data; // { hour: 14, minute: 30 }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function safeParseTime(text: unknown): SafeParseResult<ClockTime>;
|
|
152
|
+
/**
|
|
153
|
+
* Reads a time said in Malagasy back into hour and minute: the inverse of {@link spellTime}.
|
|
154
|
+
*
|
|
155
|
+
* @param text - A time as {@link spellTime} says it, in any case and spacing, with or without
|
|
156
|
+
* `amin'ny`.
|
|
157
|
+
* @returns The time.
|
|
158
|
+
* @throws {@link MdgError} `INVALID_TYPE` for non-strings, `INVALID_FORMAT` for anything else.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* parseTime("amin'ny roa sy sasany tolakandro"); // { hour: 14, minute: 30 }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function parseTime(text: string): ClockTime;
|
|
166
|
+
/**
|
|
167
|
+
* Reads a time said in Malagasy, or gives `undefined` instead of throwing.
|
|
168
|
+
*
|
|
169
|
+
* @param text - A time as {@link spellTime} says it.
|
|
170
|
+
* @returns The time, or `undefined` when {@link parseTime} would throw.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* tryParseTime("amin'ny fito maraina"); // { hour: 7, minute: 0 }
|
|
175
|
+
* tryParseTime('seven'); // undefined
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
declare function tryParseTime(text: unknown): ClockTime | undefined;
|
|
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
|
+
|
|
109
209
|
/**
|
|
110
210
|
* Reads a Malagasy date without throwing.
|
|
111
211
|
*
|
|
@@ -130,8 +230,8 @@ declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResul
|
|
|
130
230
|
* @returns The calendar date.
|
|
131
231
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
|
|
132
232
|
* `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
|
|
134
|
-
*
|
|
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).
|
|
135
235
|
*
|
|
136
236
|
* @example
|
|
137
237
|
* ```ts
|
|
@@ -197,5 +297,5 @@ declare function monthName(month: number, width?: NameWidth): string;
|
|
|
197
297
|
*/
|
|
198
298
|
declare function weekdayName(isoWeekday: number, width?: NameWidth): string;
|
|
199
299
|
|
|
200
|
-
export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, safeParseDate, tryParseDate, weekdayName };
|
|
201
|
-
export type { CalendarDate, 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.
|
|
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.9.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'
|
|
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,6 +105,107 @@ declare class MdgError extends Error {
|
|
|
106
105
|
static [Symbol.hasInstance](value: unknown): boolean;
|
|
107
106
|
}
|
|
108
107
|
|
|
108
|
+
/**
|
|
109
|
+
* A time of day: hour 0–23 and minute 0–59.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const time: ClockTime = { hour: 14, minute: 30 };
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
interface ClockTime {
|
|
117
|
+
/** Hour, 0 to 23. */
|
|
118
|
+
readonly hour: number;
|
|
119
|
+
/** Minute, 0 to 59. */
|
|
120
|
+
readonly minute: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Says a time of day in Malagasy: `amin'ny`, the hour on a 12-hour clock, the minutes, then
|
|
124
|
+
* the part of the day.
|
|
125
|
+
*
|
|
126
|
+
* @param time - `HH:MM` text, `{ hour, minute }`, or a `Date` (read in its local time).
|
|
127
|
+
* @returns The time in words.
|
|
128
|
+
* @throws {@link MdgError} `INVALID_FORMAT` for text that is not `HH:MM`, `OUT_OF_RANGE` for an
|
|
129
|
+
* hour outside 0–23 or a minute outside 0–59, `INVALID_TYPE` for other input.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* spellTime('14:30'); // "amin'ny roa sy sasany tolakandro"
|
|
134
|
+
* spellTime('07:15'); // "amin'ny fito sy fahefany maraina"
|
|
135
|
+
* spellTime('17:45'); // "amin'ny enina latsaka fahefany hariva"
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
declare function spellTime(time: string | ClockTime | Date): string;
|
|
139
|
+
/**
|
|
140
|
+
* Reads a time said in Malagasy without throwing.
|
|
141
|
+
*
|
|
142
|
+
* @param text - A time as {@link spellTime} says it.
|
|
143
|
+
* @returns `{ success: true, data }` with the time, or `{ success: false, error }`.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const result = safeParseTime("amin'ny roa sy sasany tolakandro");
|
|
148
|
+
* if (result.success) result.data; // { hour: 14, minute: 30 }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function safeParseTime(text: unknown): SafeParseResult<ClockTime>;
|
|
152
|
+
/**
|
|
153
|
+
* Reads a time said in Malagasy back into hour and minute: the inverse of {@link spellTime}.
|
|
154
|
+
*
|
|
155
|
+
* @param text - A time as {@link spellTime} says it, in any case and spacing, with or without
|
|
156
|
+
* `amin'ny`.
|
|
157
|
+
* @returns The time.
|
|
158
|
+
* @throws {@link MdgError} `INVALID_TYPE` for non-strings, `INVALID_FORMAT` for anything else.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* parseTime("amin'ny roa sy sasany tolakandro"); // { hour: 14, minute: 30 }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function parseTime(text: string): ClockTime;
|
|
166
|
+
/**
|
|
167
|
+
* Reads a time said in Malagasy, or gives `undefined` instead of throwing.
|
|
168
|
+
*
|
|
169
|
+
* @param text - A time as {@link spellTime} says it.
|
|
170
|
+
* @returns The time, or `undefined` when {@link parseTime} would throw.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* tryParseTime("amin'ny fito maraina"); // { hour: 7, minute: 0 }
|
|
175
|
+
* tryParseTime('seven'); // undefined
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
declare function tryParseTime(text: unknown): ClockTime | undefined;
|
|
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
|
+
|
|
109
209
|
/**
|
|
110
210
|
* Reads a Malagasy date without throwing.
|
|
111
211
|
*
|
|
@@ -130,8 +230,8 @@ declare function safeParseDate(text: unknown, style?: DateStyle): SafeParseResul
|
|
|
130
230
|
* @returns The calendar date.
|
|
131
231
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or an unknown style,
|
|
132
232
|
* `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
|
|
134
|
-
*
|
|
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).
|
|
135
235
|
*
|
|
136
236
|
* @example
|
|
137
237
|
* ```ts
|
|
@@ -197,5 +297,5 @@ declare function monthName(month: number, width?: NameWidth): string;
|
|
|
197
297
|
*/
|
|
198
298
|
declare function weekdayName(isoWeekday: number, width?: NameWidth): string;
|
|
199
299
|
|
|
200
|
-
export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, safeParseDate, tryParseDate, weekdayName };
|
|
201
|
-
export type { CalendarDate, 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
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { spell } from '@mdgascar/words';
|
|
2
|
+
|
|
1
3
|
// src/generated/data.ts
|
|
2
4
|
var MONTH_NAMES = [
|
|
3
5
|
"",
|
|
@@ -60,9 +62,48 @@ var DEFAULT_STYLE = "long";
|
|
|
60
62
|
var MIN_YEAR = 1;
|
|
61
63
|
var MAX_READ_LENGTH = 64;
|
|
62
64
|
var MAX_YEAR = 9999;
|
|
65
|
+
var DATE_TIME_PATTERN = "{1} {0}";
|
|
66
|
+
var TIME_WORDS = {
|
|
67
|
+
prefix: "amin'ny",
|
|
68
|
+
after: "sy",
|
|
69
|
+
before: "latsaka",
|
|
70
|
+
more: "mahery"
|
|
71
|
+
};
|
|
72
|
+
var TIME_FRACTIONS = [
|
|
73
|
+
[15, "fahefany"],
|
|
74
|
+
[20, "fahatelony"],
|
|
75
|
+
[30, "sasany"]
|
|
76
|
+
];
|
|
77
|
+
var DAY_PARTS = [
|
|
78
|
+
"alina",
|
|
79
|
+
"maraina",
|
|
80
|
+
"maraina",
|
|
81
|
+
"maraina",
|
|
82
|
+
"maraina",
|
|
83
|
+
"maraina",
|
|
84
|
+
"maraina",
|
|
85
|
+
"maraina",
|
|
86
|
+
"maraina",
|
|
87
|
+
"maraina",
|
|
88
|
+
"maraina",
|
|
89
|
+
"maraina",
|
|
90
|
+
"atoandro",
|
|
91
|
+
"tolakandro",
|
|
92
|
+
"tolakandro",
|
|
93
|
+
"tolakandro",
|
|
94
|
+
"tolakandro",
|
|
95
|
+
"tolakandro",
|
|
96
|
+
"hariva",
|
|
97
|
+
"hariva",
|
|
98
|
+
"hariva",
|
|
99
|
+
"hariva",
|
|
100
|
+
"hariva",
|
|
101
|
+
"hariva"
|
|
102
|
+
];
|
|
103
|
+
var MAX_TIME_READ_LENGTH = 128;
|
|
63
104
|
|
|
64
105
|
// src/generated/errors.ts
|
|
65
|
-
var SPEC_VERSION = "0.
|
|
106
|
+
var SPEC_VERSION = "0.11.0";
|
|
66
107
|
var MdgError = class extends Error {
|
|
67
108
|
/** Always `'MdgError'`. */
|
|
68
109
|
name = "MdgError";
|
|
@@ -203,6 +244,132 @@ function formatDate(date, style = DEFAULT_STYLE) {
|
|
|
203
244
|
}
|
|
204
245
|
return render(pattern, toCalendarDate(date));
|
|
205
246
|
}
|
|
247
|
+
var HOURS_PER_DAY = 24;
|
|
248
|
+
var HOURS_ON_THE_CLOCK = 12;
|
|
249
|
+
var MINUTES_PER_HOUR = 60;
|
|
250
|
+
var LAST_MINUTE_AFTER = 20;
|
|
251
|
+
var HALF_HOUR = 30;
|
|
252
|
+
var FIRST_MINUTE_BEFORE = MINUTES_PER_HOUR - LAST_MINUTE_AFTER;
|
|
253
|
+
var TIME_TEXT = /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/;
|
|
254
|
+
var APOSTROPHES = /[‘’ʼ`´]/g;
|
|
255
|
+
var SPACES = /\s+/gu;
|
|
256
|
+
var FRACTIONS = new Map(TIME_FRACTIONS);
|
|
257
|
+
var HALF_WORD = String(FRACTIONS.get(HALF_HOUR));
|
|
258
|
+
function minuteWords(minutes) {
|
|
259
|
+
return FRACTIONS.get(minutes) ?? spell(minutes);
|
|
260
|
+
}
|
|
261
|
+
function minutePhrase(minute) {
|
|
262
|
+
const { after, before, more } = TIME_WORDS;
|
|
263
|
+
if (minute === 0) return ["", 0];
|
|
264
|
+
if (minute <= LAST_MINUTE_AFTER) return [`${after} ${minuteWords(minute)}`, 0];
|
|
265
|
+
if (minute < HALF_HOUR)
|
|
266
|
+
return [`${after} ${HALF_WORD} ${before} ${minuteWords(HALF_HOUR - minute)}`, 0];
|
|
267
|
+
if (minute === HALF_HOUR) return [`${after} ${HALF_WORD}`, 0];
|
|
268
|
+
if (minute < FIRST_MINUTE_BEFORE)
|
|
269
|
+
return [`${after} ${HALF_WORD} ${more} ${minuteWords(minute - HALF_HOUR)}`, 0];
|
|
270
|
+
return [`${before} ${minuteWords(MINUTES_PER_HOUR - minute)}`, 1];
|
|
271
|
+
}
|
|
272
|
+
function timeWords({ hour, minute }) {
|
|
273
|
+
const [minutes, hoursLater] = minutePhrase(minute);
|
|
274
|
+
const named = (hour + hoursLater) % HOURS_PER_DAY;
|
|
275
|
+
const onTheClock = named % HOURS_ON_THE_CLOCK || HOURS_ON_THE_CLOCK;
|
|
276
|
+
return [TIME_WORDS.prefix, spell(onTheClock), minutes, String(DAY_PARTS[named])].filter((word) => word !== "").join(" ");
|
|
277
|
+
}
|
|
278
|
+
function isWhole(value, last) {
|
|
279
|
+
return Number.isInteger(value) && value >= 0 && value <= last;
|
|
280
|
+
}
|
|
281
|
+
function fromFields2(fields) {
|
|
282
|
+
const { hour, minute } = fields;
|
|
283
|
+
if (!Number.isInteger(hour) || !Number.isInteger(minute)) {
|
|
284
|
+
throw new MdgError("INVALID_TYPE", "Expected whole numbers for hour and minute.");
|
|
285
|
+
}
|
|
286
|
+
if (!isWhole(hour, HOURS_PER_DAY - 1) || !isWhole(minute, MINUTES_PER_HOUR - 1)) {
|
|
287
|
+
throw new MdgError("OUT_OF_RANGE", "Hours go from 0 to 23 and minutes from 0 to 59.");
|
|
288
|
+
}
|
|
289
|
+
return { hour, minute };
|
|
290
|
+
}
|
|
291
|
+
function toClockTime(time) {
|
|
292
|
+
if (typeof time === "string") {
|
|
293
|
+
const match = TIME_TEXT.exec(time);
|
|
294
|
+
if (match === null) throw new MdgError("INVALID_FORMAT", "Expected a time as HH:MM.");
|
|
295
|
+
return { hour: Number(match[1]), minute: Number(match[2]) };
|
|
296
|
+
}
|
|
297
|
+
if (time instanceof Date) {
|
|
298
|
+
if (Number.isNaN(time.getTime())) throw new MdgError("INVALID_FORMAT", "Invalid Date.");
|
|
299
|
+
return { hour: time.getHours(), minute: time.getMinutes() };
|
|
300
|
+
}
|
|
301
|
+
if (typeof time === "object" && time !== null) return fromFields2(time);
|
|
302
|
+
throw new MdgError("INVALID_TYPE", "Expected HH:MM text, { hour, minute } or a Date.");
|
|
303
|
+
}
|
|
304
|
+
function spellTime(time) {
|
|
305
|
+
return timeWords(toClockTime(time));
|
|
306
|
+
}
|
|
307
|
+
var byWords;
|
|
308
|
+
function timesByWords() {
|
|
309
|
+
byWords ??= new Map(
|
|
310
|
+
Array.from({ length: HOURS_PER_DAY * MINUTES_PER_HOUR }, (_, index) => {
|
|
311
|
+
const time = { hour: Math.floor(index / MINUTES_PER_HOUR), minute: index % MINUTES_PER_HOUR };
|
|
312
|
+
return [timeWords(time), time];
|
|
313
|
+
})
|
|
314
|
+
);
|
|
315
|
+
return byWords;
|
|
316
|
+
}
|
|
317
|
+
function normalise(text) {
|
|
318
|
+
const words = text.toLowerCase().replace(APOSTROPHES, "'").replace(SPACES, " ").trim();
|
|
319
|
+
return words.startsWith(`${TIME_WORDS.prefix} `) ? words : `${TIME_WORDS.prefix} ${words}`;
|
|
320
|
+
}
|
|
321
|
+
function failure(code, message) {
|
|
322
|
+
return { success: false, error: new MdgError(code, message) };
|
|
323
|
+
}
|
|
324
|
+
function safeParseTime(text) {
|
|
325
|
+
if (typeof text !== "string") return failure("INVALID_TYPE", "Expected the time as text.");
|
|
326
|
+
if (text.length > MAX_TIME_READ_LENGTH) {
|
|
327
|
+
return failure(
|
|
328
|
+
"INVALID_FORMAT",
|
|
329
|
+
`Times are at most ${String(MAX_TIME_READ_LENGTH)} characters.`
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
const time = timesByWords().get(normalise(text));
|
|
333
|
+
if (time === void 0) {
|
|
334
|
+
return failure("INVALID_FORMAT", "Expected a time such as amin'ny roa sy sasany tolakandro.");
|
|
335
|
+
}
|
|
336
|
+
return { success: true, data: time };
|
|
337
|
+
}
|
|
338
|
+
function parseTime(text) {
|
|
339
|
+
const result = safeParseTime(text);
|
|
340
|
+
if (!result.success) throw result.error;
|
|
341
|
+
return result.data;
|
|
342
|
+
}
|
|
343
|
+
function tryParseTime(text) {
|
|
344
|
+
const result = safeParseTime(text);
|
|
345
|
+
return result.success ? result.data : void 0;
|
|
346
|
+
}
|
|
347
|
+
|
|
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
|
+
}
|
|
206
373
|
|
|
207
374
|
// src/parse.ts
|
|
208
375
|
var PATTERN_TOKEN2 = /EEEE|MMMM|MMM|MM|dd|d|y|\s+|[^\sEMdy]+/g;
|
|
@@ -260,7 +427,7 @@ function readStyle(text, reader) {
|
|
|
260
427
|
weekday: fields.weekday
|
|
261
428
|
};
|
|
262
429
|
}
|
|
263
|
-
function
|
|
430
|
+
function failure2(code, message) {
|
|
264
431
|
return { success: false, error: new MdgError(code, message) };
|
|
265
432
|
}
|
|
266
433
|
function readersFor(style) {
|
|
@@ -272,7 +439,7 @@ function checkedFields(fields) {
|
|
|
272
439
|
try {
|
|
273
440
|
const date = checkedTextDate({ year: fields.year, month: fields.month, day: fields.day });
|
|
274
441
|
if (fields.weekday !== void 0 && fields.weekday !== isoWeekdayOf(date)) {
|
|
275
|
-
return
|
|
442
|
+
return failure2("INVALID_FORMAT", "The weekday does not match the date.");
|
|
276
443
|
}
|
|
277
444
|
return { success: true, data: date };
|
|
278
445
|
} catch (error) {
|
|
@@ -280,33 +447,21 @@ function checkedFields(fields) {
|
|
|
280
447
|
}
|
|
281
448
|
}
|
|
282
449
|
function safeParseDate(text, style) {
|
|
283
|
-
if (typeof text !== "string") return
|
|
450
|
+
if (typeof text !== "string") return failure2("INVALID_TYPE", "Expected the date as text.");
|
|
284
451
|
const readers = readersFor(style);
|
|
285
452
|
if (readers === void 0) {
|
|
286
|
-
return
|
|
453
|
+
return failure2("INVALID_TYPE", `Style is one of: ${[...READERS.keys()].join(", ")}.`);
|
|
287
454
|
}
|
|
288
455
|
const trimmed = text.trim();
|
|
289
456
|
if (text.length > MAX_READ_LENGTH) {
|
|
290
|
-
return
|
|
457
|
+
return failure2("INVALID_FORMAT", `Dates are at most ${String(MAX_READ_LENGTH)} characters.`);
|
|
291
458
|
}
|
|
292
|
-
const
|
|
459
|
+
const results = readers.flatMap((reader) => readStyle(trimmed, reader) ?? []).map(checkedFields);
|
|
460
|
+
const [first] = results;
|
|
293
461
|
if (first === void 0) {
|
|
294
|
-
return
|
|
295
|
-
}
|
|
296
|
-
return oneDate(checkedFields(first), others.map(checkedFields));
|
|
297
|
-
}
|
|
298
|
-
function dateKey({ year, month, day }) {
|
|
299
|
-
return [year, month, day].join("-");
|
|
300
|
-
}
|
|
301
|
-
function oneDate(first, others) {
|
|
302
|
-
const [date, ...rest] = [first, ...others].flatMap(
|
|
303
|
-
(result) => result.success ? [result.data] : []
|
|
304
|
-
);
|
|
305
|
-
if (date === void 0) return first;
|
|
306
|
-
if (rest.some((other) => dateKey(other) !== dateKey(date))) {
|
|
307
|
-
return failure("AMBIGUOUS", "This text reads as more than one date; pass the style.");
|
|
462
|
+
return failure2("INVALID_FORMAT", "Expected a date such as 23 Novambra 2026 or 23/11/2026.");
|
|
308
463
|
}
|
|
309
|
-
return
|
|
464
|
+
return results.find((result) => result.success) ?? first;
|
|
310
465
|
}
|
|
311
466
|
function parseDate(text, style) {
|
|
312
467
|
const result = safeParseDate(text, style);
|
|
@@ -318,6 +473,6 @@ function tryParseDate(text, style) {
|
|
|
318
473
|
return result.success ? result.data : void 0;
|
|
319
474
|
}
|
|
320
475
|
|
|
321
|
-
export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, safeParseDate, tryParseDate, weekdayName };
|
|
476
|
+
export { MdgError, SPEC_VERSION, formatDate, monthName, parseDate, parseTime, safeParseDate, safeParseTime, spellDateTime, spellTime, tryParseDate, tryParseTime, weekdayName };
|
|
322
477
|
//# sourceMappingURL=index.js.map
|
|
323
478
|
//# sourceMappingURL=index.js.map
|