@c9up/chronos 0.1.15 → 0.1.16

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.
Files changed (52) hide show
  1. package/README.md +63 -16
  2. package/dist/DateTime.d.ts +145 -4
  3. package/dist/DateTime.d.ts.map +1 -1
  4. package/dist/DateTime.js +504 -87
  5. package/dist/DateTime.js.map +1 -1
  6. package/dist/Duration.d.ts +37 -20
  7. package/dist/Duration.d.ts.map +1 -1
  8. package/dist/Duration.js +287 -120
  9. package/dist/Duration.js.map +1 -1
  10. package/dist/Interval.d.ts +50 -7
  11. package/dist/Interval.d.ts.map +1 -1
  12. package/dist/Interval.js +142 -29
  13. package/dist/Interval.js.map +1 -1
  14. package/dist/atlas.d.ts +11 -15
  15. package/dist/atlas.d.ts.map +1 -1
  16. package/dist/atlas.js +22 -17
  17. package/dist/atlas.js.map +1 -1
  18. package/dist/format.d.ts +37 -0
  19. package/dist/format.d.ts.map +1 -0
  20. package/dist/format.js +387 -0
  21. package/dist/format.js.map +1 -0
  22. package/dist/index.d.ts +5 -5
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +3 -3
  25. package/dist/index.js.map +1 -1
  26. package/dist/native/generated.d.ts +2 -2
  27. package/dist/native/generated.d.ts.map +1 -1
  28. package/dist/native.js +2 -2
  29. package/dist/native.js.map +1 -1
  30. package/dist/rrule.d.ts +25 -1
  31. package/dist/rrule.d.ts.map +1 -1
  32. package/dist/rrule.js +130 -32
  33. package/dist/rrule.js.map +1 -1
  34. package/index.darwin-arm64.node +0 -0
  35. package/index.darwin-x64.node +0 -0
  36. package/index.linux-arm64-gnu.node +0 -0
  37. package/index.linux-x64-gnu.node +0 -0
  38. package/index.win32-x64-msvc.node +0 -0
  39. package/package.json +1 -1
  40. package/src/DateTime.ts +645 -96
  41. package/src/Duration.ts +318 -139
  42. package/src/Interval.ts +160 -53
  43. package/src/atlas.ts +23 -17
  44. package/src/format.ts +449 -0
  45. package/src/index.ts +17 -7
  46. package/src/native/generated.ts +6 -2
  47. package/src/native.ts +3 -3
  48. package/src/rrule.ts +189 -37
  49. package/wasm/chronos_engine_wasm.d.ts +4 -4
  50. package/wasm/chronos_engine_wasm.js +26 -16
  51. package/wasm/chronos_engine_wasm_bg.wasm +0 -0
  52. package/wasm/chronos_engine_wasm_bg.wasm.d.ts +2 -2
package/src/format.ts ADDED
@@ -0,0 +1,449 @@
1
+ /**
2
+ * `toFormat` — Luxon's format tokens (`yyyy-MM-dd HH:mm`, `dd LLL yyyy`,
3
+ * `EEEE`, `ZZ`, …), with Luxon's quoting (`'literal'`, `''` for a quote) and
4
+ * its macro tokens (`D`, `DD`, `t`, `f`, …) rendered through Intl.
5
+ *
6
+ * Numbers are written with Latin digits and names in English unless a locale
7
+ * is given; Luxon reads the host's default locale instead, which made the
8
+ * same call print differently per machine.
9
+ */
10
+
11
+ export interface FormatParts {
12
+ year: number;
13
+ month: number;
14
+ day: number;
15
+ hour: number;
16
+ minute: number;
17
+ second: number;
18
+ millisecond: number;
19
+ /** ISO weekday, 1 = Monday. */
20
+ weekday: number;
21
+ weekNumber: number;
22
+ weekYear: number;
23
+ ordinal: number;
24
+ quarter: number;
25
+ }
26
+
27
+ export interface FormatContext {
28
+ parts: FormatParts;
29
+ /** Epoch milliseconds. */
30
+ ts: number;
31
+ /** UTC offset in minutes. */
32
+ offset: number;
33
+ zone: string;
34
+ locale: string;
35
+ /** Write a zero offset as `Z` (the UTC zone), as Luxon's ISO and SQL output do. */
36
+ allowZ?: boolean;
37
+ }
38
+
39
+ const MONTHS_LONG = [
40
+ "January",
41
+ "February",
42
+ "March",
43
+ "April",
44
+ "May",
45
+ "June",
46
+ "July",
47
+ "August",
48
+ "September",
49
+ "October",
50
+ "November",
51
+ "December",
52
+ ];
53
+ const WEEKDAYS_LONG = [
54
+ "Monday",
55
+ "Tuesday",
56
+ "Wednesday",
57
+ "Thursday",
58
+ "Friday",
59
+ "Saturday",
60
+ "Sunday",
61
+ ];
62
+
63
+ type NameLength = "short" | "long" | "narrow";
64
+
65
+ const MACROS: Readonly<Record<string, Intl.DateTimeFormatOptions>> = {
66
+ D: { year: "numeric", month: "numeric", day: "numeric" },
67
+ DD: { year: "numeric", month: "short", day: "numeric" },
68
+ DDD: { year: "numeric", month: "long", day: "numeric" },
69
+ DDDD: { year: "numeric", month: "long", day: "numeric", weekday: "long" },
70
+ t: { hour: "numeric", minute: "numeric" },
71
+ tt: { hour: "numeric", minute: "numeric", second: "numeric" },
72
+ ttt: {
73
+ hour: "numeric",
74
+ minute: "numeric",
75
+ second: "numeric",
76
+ timeZoneName: "short",
77
+ },
78
+ tttt: {
79
+ hour: "numeric",
80
+ minute: "numeric",
81
+ second: "numeric",
82
+ timeZoneName: "long",
83
+ },
84
+ T: { hour: "numeric", minute: "numeric", hourCycle: "h23" },
85
+ TT: {
86
+ hour: "numeric",
87
+ minute: "numeric",
88
+ second: "numeric",
89
+ hourCycle: "h23",
90
+ },
91
+ TTT: {
92
+ hour: "numeric",
93
+ minute: "numeric",
94
+ second: "numeric",
95
+ hourCycle: "h23",
96
+ timeZoneName: "short",
97
+ },
98
+ TTTT: {
99
+ hour: "numeric",
100
+ minute: "numeric",
101
+ second: "numeric",
102
+ hourCycle: "h23",
103
+ timeZoneName: "long",
104
+ },
105
+ f: {
106
+ year: "numeric",
107
+ month: "numeric",
108
+ day: "numeric",
109
+ hour: "numeric",
110
+ minute: "numeric",
111
+ },
112
+ ff: {
113
+ year: "numeric",
114
+ month: "short",
115
+ day: "numeric",
116
+ hour: "numeric",
117
+ minute: "numeric",
118
+ },
119
+ fff: {
120
+ year: "numeric",
121
+ month: "long",
122
+ day: "numeric",
123
+ hour: "numeric",
124
+ minute: "numeric",
125
+ timeZoneName: "short",
126
+ },
127
+ ffff: {
128
+ year: "numeric",
129
+ month: "long",
130
+ day: "numeric",
131
+ weekday: "long",
132
+ hour: "numeric",
133
+ minute: "numeric",
134
+ timeZoneName: "long",
135
+ },
136
+ F: {
137
+ year: "numeric",
138
+ month: "numeric",
139
+ day: "numeric",
140
+ hour: "numeric",
141
+ minute: "numeric",
142
+ second: "numeric",
143
+ },
144
+ FF: {
145
+ year: "numeric",
146
+ month: "short",
147
+ day: "numeric",
148
+ hour: "numeric",
149
+ minute: "numeric",
150
+ second: "numeric",
151
+ },
152
+ FFF: {
153
+ year: "numeric",
154
+ month: "long",
155
+ day: "numeric",
156
+ hour: "numeric",
157
+ minute: "numeric",
158
+ second: "numeric",
159
+ timeZoneName: "short",
160
+ },
161
+ FFFF: {
162
+ year: "numeric",
163
+ month: "long",
164
+ day: "numeric",
165
+ weekday: "long",
166
+ hour: "numeric",
167
+ minute: "numeric",
168
+ second: "numeric",
169
+ timeZoneName: "long",
170
+ },
171
+ };
172
+
173
+ interface Token {
174
+ literal: boolean;
175
+ val: string;
176
+ }
177
+
178
+ /** Luxon's `Formatter.parseFormat`: runs of one letter are a token, `'…'` is literal. */
179
+ function parseFormat(fmt: string): Token[] {
180
+ let current: string | null = null;
181
+ let currentFull = "";
182
+ let bracketed = false;
183
+ const splits: Token[] = [];
184
+ for (const c of fmt) {
185
+ if (c === "'") {
186
+ if (currentFull.length > 0 || bracketed) {
187
+ splits.push({
188
+ literal: bracketed || /^\s+$/.test(currentFull),
189
+ val: currentFull === "" ? "'" : currentFull,
190
+ });
191
+ }
192
+ current = null;
193
+ currentFull = "";
194
+ bracketed = !bracketed;
195
+ } else if (bracketed) {
196
+ currentFull += c;
197
+ } else if (c === current) {
198
+ currentFull += c;
199
+ } else {
200
+ if (currentFull.length > 0) {
201
+ splits.push({ literal: /^\s+$/.test(currentFull), val: currentFull });
202
+ }
203
+ currentFull = c;
204
+ current = c;
205
+ }
206
+ }
207
+ if (currentFull.length > 0) {
208
+ splits.push({
209
+ literal: bracketed || /^\s+$/.test(currentFull),
210
+ val: currentFull,
211
+ });
212
+ }
213
+ return splits;
214
+ }
215
+
216
+ function pad(n: number | string, width = 0): string {
217
+ const value = typeof n === "number" ? n : Number(n);
218
+ const sign = value < 0 ? "-" : "";
219
+ return sign + String(Math.abs(value)).padStart(width, "0");
220
+ }
221
+
222
+ function formatOffset(
223
+ offset: number,
224
+ format: "narrow" | "short" | "techie",
225
+ allowZ = false,
226
+ ): string {
227
+ if (allowZ && offset === 0) return "Z";
228
+ const hours = Math.trunc(Math.abs(offset / 60));
229
+ const minutes = Math.trunc(Math.abs(offset % 60));
230
+ const sign = offset >= 0 ? "+" : "-";
231
+ switch (format) {
232
+ case "short":
233
+ return `${sign}${pad(hours, 2)}:${pad(minutes, 2)}`;
234
+ case "narrow":
235
+ return `${sign}${hours}${minutes > 0 ? `:${minutes}` : ""}`;
236
+ default:
237
+ return `${sign}${pad(hours, 2)}${pad(minutes, 2)}`;
238
+ }
239
+ }
240
+
241
+ function isEnglish(locale: string): boolean {
242
+ return /^en(?:-|$)/i.test(locale);
243
+ }
244
+
245
+ /** One part of an Intl rendering of the instant, in its zone. */
246
+ function intlPart(
247
+ ctx: FormatContext,
248
+ options: Intl.DateTimeFormatOptions,
249
+ type: Intl.DateTimeFormatPartTypes,
250
+ ): string {
251
+ const parts = new Intl.DateTimeFormat(ctx.locale, {
252
+ ...options,
253
+ timeZone: ctx.zone,
254
+ }).formatToParts(new Date(ctx.ts));
255
+ return parts.find((p) => p.type === type)?.value ?? "";
256
+ }
257
+
258
+ function monthName(
259
+ ctx: FormatContext,
260
+ length: NameLength,
261
+ standalone: boolean,
262
+ ): string {
263
+ if (isEnglish(ctx.locale)) {
264
+ const name = MONTHS_LONG[ctx.parts.month - 1] ?? "";
265
+ if (length === "long") return name;
266
+ return length === "short" ? name.slice(0, 3) : name.slice(0, 1);
267
+ }
268
+ return intlPart(
269
+ ctx,
270
+ standalone ? { month: length } : { month: length, day: "numeric" },
271
+ "month",
272
+ );
273
+ }
274
+
275
+ function weekdayName(
276
+ ctx: FormatContext,
277
+ length: NameLength,
278
+ standalone: boolean,
279
+ ): string {
280
+ if (isEnglish(ctx.locale)) {
281
+ const name = WEEKDAYS_LONG[ctx.parts.weekday - 1] ?? "";
282
+ if (length === "long") return name;
283
+ return length === "short" ? name.slice(0, 3) : name.slice(0, 1);
284
+ }
285
+ return intlPart(
286
+ ctx,
287
+ standalone
288
+ ? { weekday: length }
289
+ : { weekday: length, month: "long", day: "numeric" },
290
+ "weekday",
291
+ );
292
+ }
293
+
294
+ function tokenToString(token: string, ctx: FormatContext): string {
295
+ const p = ctx.parts;
296
+ const english = isEnglish(ctx.locale);
297
+ switch (token) {
298
+ case "S":
299
+ return pad(p.millisecond);
300
+ case "u":
301
+ case "SSS":
302
+ return pad(p.millisecond, 3);
303
+ case "s":
304
+ return pad(p.second);
305
+ case "ss":
306
+ return pad(p.second, 2);
307
+ case "uu":
308
+ return pad(Math.floor(p.millisecond / 10), 2);
309
+ case "uuu":
310
+ return pad(Math.floor(p.millisecond / 100));
311
+ case "m":
312
+ return pad(p.minute);
313
+ case "mm":
314
+ return pad(p.minute, 2);
315
+ case "h":
316
+ return pad(p.hour % 12 === 0 ? 12 : p.hour % 12);
317
+ case "hh":
318
+ return pad(p.hour % 12 === 0 ? 12 : p.hour % 12, 2);
319
+ case "H":
320
+ return pad(p.hour);
321
+ case "HH":
322
+ return pad(p.hour, 2);
323
+ case "Z":
324
+ return formatOffset(ctx.offset, "narrow", ctx.allowZ);
325
+ case "ZZ":
326
+ return formatOffset(ctx.offset, "short", ctx.allowZ);
327
+ case "ZZZ":
328
+ return formatOffset(ctx.offset, "techie", ctx.allowZ);
329
+ case "ZZZZ":
330
+ return intlPart(ctx, { timeZoneName: "short" }, "timeZoneName");
331
+ case "ZZZZZ":
332
+ return intlPart(ctx, { timeZoneName: "long" }, "timeZoneName");
333
+ case "z":
334
+ return ctx.zone;
335
+ case "a":
336
+ return english
337
+ ? p.hour < 12
338
+ ? "AM"
339
+ : "PM"
340
+ : intlPart(ctx, { hour: "numeric", hourCycle: "h12" }, "dayPeriod");
341
+ case "d":
342
+ return pad(p.day);
343
+ case "dd":
344
+ return pad(p.day, 2);
345
+ case "c":
346
+ case "E":
347
+ return pad(p.weekday);
348
+ case "ccc":
349
+ return weekdayName(ctx, "short", true);
350
+ case "cccc":
351
+ return weekdayName(ctx, "long", true);
352
+ case "ccccc":
353
+ return weekdayName(ctx, "narrow", true);
354
+ case "EEE":
355
+ return weekdayName(ctx, "short", false);
356
+ case "EEEE":
357
+ return weekdayName(ctx, "long", false);
358
+ case "EEEEE":
359
+ return weekdayName(ctx, "narrow", false);
360
+ case "L":
361
+ case "M":
362
+ return pad(p.month);
363
+ case "LL":
364
+ case "MM":
365
+ return pad(p.month, 2);
366
+ case "LLL":
367
+ return monthName(ctx, "short", true);
368
+ case "LLLL":
369
+ return monthName(ctx, "long", true);
370
+ case "LLLLL":
371
+ return monthName(ctx, "narrow", true);
372
+ case "MMM":
373
+ return monthName(ctx, "short", false);
374
+ case "MMMM":
375
+ return monthName(ctx, "long", false);
376
+ case "MMMMM":
377
+ return monthName(ctx, "narrow", false);
378
+ case "y":
379
+ return pad(p.year);
380
+ case "yy":
381
+ return pad(p.year.toString().slice(-2), 2);
382
+ case "yyyy":
383
+ return pad(p.year, 4);
384
+ case "yyyyyy":
385
+ return pad(p.year, 6);
386
+ case "G":
387
+ return english
388
+ ? p.year > 0
389
+ ? "AD"
390
+ : "BC"
391
+ : intlPart(ctx, { era: "short" }, "era");
392
+ case "GG":
393
+ return english
394
+ ? p.year > 0
395
+ ? "Anno Domini"
396
+ : "Before Christ"
397
+ : intlPart(ctx, { era: "long" }, "era");
398
+ case "GGGGG":
399
+ return english
400
+ ? p.year > 0
401
+ ? "A"
402
+ : "B"
403
+ : intlPart(ctx, { era: "narrow" }, "era");
404
+ case "kk":
405
+ return pad(p.weekYear.toString().slice(-2), 2);
406
+ case "kkkk":
407
+ return pad(p.weekYear, 4);
408
+ case "W":
409
+ return pad(p.weekNumber);
410
+ case "WW":
411
+ return pad(p.weekNumber, 2);
412
+ case "o":
413
+ return pad(p.ordinal);
414
+ case "ooo":
415
+ return pad(p.ordinal, 3);
416
+ case "q":
417
+ return pad(p.quarter);
418
+ case "qq":
419
+ return pad(p.quarter, 2);
420
+ case "X":
421
+ return pad(Math.floor(ctx.ts / 1000));
422
+ case "x":
423
+ return pad(ctx.ts);
424
+ default: {
425
+ const macro = Object.hasOwn(MACROS, token) ? MACROS[token] : undefined;
426
+ if (macro === undefined) return token;
427
+ const dtf = new Intl.DateTimeFormat(ctx.locale, {
428
+ ...macro,
429
+ timeZone: ctx.zone,
430
+ });
431
+ // Luxon renders a UTC value from its parts, which keep ICU's narrow
432
+ // no-break space before AM/PM; `format()` swaps it for a plain one.
433
+ return ctx.zone === "UTC"
434
+ ? dtf
435
+ .formatToParts(new Date(ctx.ts))
436
+ .map((part) => part.value)
437
+ .join("")
438
+ : dtf.format(new Date(ctx.ts));
439
+ }
440
+ }
441
+ }
442
+
443
+ export function formatLuxon(fmt: string, ctx: FormatContext): string {
444
+ let out = "";
445
+ for (const token of parseFormat(fmt)) {
446
+ out += token.literal ? token.val : tokenToString(token.val, ctx);
447
+ }
448
+ return out;
449
+ }
package/src/index.ts CHANGED
@@ -19,10 +19,14 @@ export {
19
19
  inRange,
20
20
  overlapsRange,
21
21
  } from "./DateTime.js";
22
- export type { DurationObject, DurationUnit } from "./Duration.js";
22
+ export type {
23
+ DurationObject,
24
+ DurationUnit,
25
+ DurationUnitLike,
26
+ } from "./Duration.js";
23
27
  export { Duration } from "./Duration.js";
24
28
  export { Interval } from "./Interval.js";
25
- export type { RRuleBuild } from "./rrule.js";
29
+ export type { ExpandRRuleOptions, RRuleBuild } from "./rrule.js";
26
30
  export { expandRRule, toRRuleString } from "./rrule.js";
27
31
 
28
32
  // `isNativeAvailable` removed — NAPI is now mandatory (no fallback).
@@ -30,6 +34,7 @@ export { expandRRule, toRRuleString } from "./rrule.js";
30
34
  import {
31
35
  analyzeRange,
32
36
  containsRange,
37
+ type DateInput,
33
38
  type DateRange,
34
39
  DateTime,
35
40
  type DateUnit,
@@ -37,7 +42,12 @@ import {
37
42
  overlapsRange,
38
43
  type RangeCompareOptions,
39
44
  } from "./DateTime.js";
40
- import { expandRRule, type RRuleBuild, toRRuleString } from "./rrule.js";
45
+ import {
46
+ type ExpandRRuleOptions,
47
+ expandRRule,
48
+ type RRuleBuild,
49
+ toRRuleString,
50
+ } from "./rrule.js";
41
51
 
42
52
  export function at(input?: string | Date): DateTime {
43
53
  return new DateTime(input);
@@ -52,7 +62,7 @@ export const Chronos = {
52
62
  subtract: (input: string | Date, amount: number, unit: DateUnit): DateTime =>
53
63
  new DateTime(input).minus(amount, unit),
54
64
  diff: (a: string | Date, b: string | Date, unit: DateUnit): number =>
55
- new DateTime(a).diff(b, unit),
65
+ new DateTime(a).diff(b, unit).as(unit),
56
66
  inRange,
57
67
  rangeContains: (
58
68
  outer: DateRange,
@@ -67,9 +77,9 @@ export const Chronos = {
67
77
  rangeRelation: (a: DateRange, b: DateRange, options?: RangeCompareOptions) =>
68
78
  analyzeRange(a, b, options),
69
79
  rrule: (
70
- startIso: string,
80
+ start: DateInput,
71
81
  rrule: string | RRuleBuild,
72
- limit = 100,
73
- ): string[] => expandRRule(startIso, rrule, limit),
82
+ options?: number | ExpandRRuleOptions,
83
+ ): string[] => expandRRule(start, rrule, options),
74
84
  buildRRule: (rule: RRuleBuild): string => toRRuleString(rule),
75
85
  };
@@ -76,12 +76,16 @@ export declare function fromLocal(naiveIso: string, zone: string): string;
76
76
 
77
77
  export declare function parseRfc2822(input: string): string;
78
78
 
79
- export declare function parseSql(input: string): string;
79
+ export declare function parseSql(
80
+ input: string,
81
+ zone?: string | undefined | null,
82
+ ): string;
80
83
 
81
84
  export declare function parseHttp(input: string): string;
82
85
 
83
86
  export declare function rruleExpand(
84
- startIso: string,
87
+ startIso: string | undefined | null,
85
88
  rrule: string,
86
89
  limit: number,
90
+ tzid?: string | undefined | null,
87
91
  ): Array<string>;
package/src/native.ts CHANGED
@@ -76,10 +76,10 @@ if (isNode) {
76
76
  zoneOffset: (iso, zone) => wasm.zone_offset(iso, zone),
77
77
  fromLocal: (naive, zone) => wasm.from_local(naive, zone),
78
78
  parseRfc2822: (input) => wasm.parse_rfc2822(input),
79
- parseSql: (input) => wasm.parse_sql(input),
79
+ parseSql: (input, zone) => wasm.parse_sql(input, zone),
80
80
  parseHttp: (input) => wasm.parse_http(input),
81
- rruleExpand: (start, rrule, limit) =>
82
- wasm.rrule_expand(start, rrule, limit),
81
+ rruleExpand: (start, rrule, limit, tzid) =>
82
+ wasm.rrule_expand(start, rrule, limit, tzid),
83
83
  calendarParts: (iso) => wasm.calendar_parts(iso),
84
84
  };
85
85
  } catch (e) {