|
1 | 1 | import { Scope } from "./scope.js";
|
2 | 2 |
|
3 | 3 | export type FluentValue = FluentType<unknown> | string;
|
| 4 | +export type FluentStrictDateValue = FluentTemporal.Plain | Date; |
| 5 | +export type FluentDateValue = FluentStrictDateValue | number; |
| 6 | +export type FluentDateInfo = FluentDateValue | Date | FluentTemporal.Instant | FluentTemporal.ZonedDateTime; |
| 7 | +export type FluentVariable = FluentValue | FluentDateInfo | string | number; |
4 | 8 |
|
5 | 9 | export type FluentFunction = (
|
6 | 10 | positional: Array<FluentValue>,
|
7 | 11 | named: Record<string, FluentValue>
|
8 | 12 | ) => FluentValue;
|
9 | 13 |
|
| 14 | +// helper function to determine if a value is a Temporal object, without having to define a global Temporal namespace |
| 15 | +function isTemporal(value: any, type: "Duration"): value is FluentTemporal.Duration; |
| 16 | +function isTemporal(value: any, type: "Instant"): value is FluentTemporal.Instant; |
| 17 | +function isTemporal(value: any, type: "PlainDate"): value is FluentTemporal.PlainDate; |
| 18 | +function isTemporal(value: any, type: "PlainDateTime"): value is FluentTemporal.PlainDateTime; |
| 19 | +function isTemporal(value: any, type: "PlainMonthDay"): value is FluentTemporal.PlainMonthDay; |
| 20 | +function isTemporal(value: any, type: "PlainTime"): value is FluentTemporal.PlainTime; |
| 21 | +function isTemporal(value: any, type: "PlainYearMonth"): value is FluentTemporal.PlainYearMonth; |
| 22 | +function isTemporal(value: any, type: "ZonedDateTime"): value is FluentTemporal.ZonedDateTime; |
| 23 | +function isTemporal(value: any, type: keyof FluentTemporal.TemporalContainer): boolean { |
| 24 | + const temporal = (globalThis as any).Temporal as FluentTemporal.TemporalContainer | undefined; |
| 25 | + const prototype = temporal && temporal[type]; |
| 26 | + if (!prototype) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + return value instanceof prototype; |
| 30 | +} |
| 31 | + |
10 | 32 | /**
|
11 | 33 | * The `FluentType` class is the base of Fluent's type system.
|
12 | 34 | *
|
@@ -109,32 +131,128 @@ export class FluentNumber extends FluentType<number> {
|
109 | 131 | * option bag of options which will be passed to `Intl.DateTimeFormat` when the
|
110 | 132 | * `FluentDateTime` is formatted to a string.
|
111 | 133 | */
|
112 |
| -export class FluentDateTime extends FluentType<number> { |
| 134 | +export class FluentDateTime extends FluentType<FluentDateValue> { |
113 | 135 | /** Options passed to `Intl.DateTimeFormat`. */
|
114 | 136 | public opts: Intl.DateTimeFormatOptions;
|
115 | 137 |
|
| 138 | + /** ignore */ |
| 139 | + static isDateTimeValue(info: any): info is FluentStrictDateValue { |
| 140 | + return info instanceof Date || isTemporal(info, "Instant") || |
| 141 | + isTemporal(info, "PlainDateTime") || isTemporal(info, "PlainDate") || |
| 142 | + isTemporal(info, "PlainMonthDay") || isTemporal(info, "PlainTime") || |
| 143 | + isTemporal(info, "PlainYearMonth") || isTemporal(info, "ZonedDateTime"); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Converts various types representing dates and times to a `FluentDateTime`. |
| 148 | + */ |
| 149 | + static from( |
| 150 | + info: FluentDateInfo | FluentType<FluentDateInfo>, |
| 151 | + opts: Intl.DateTimeFormatOptions = {} |
| 152 | + ): FluentDateTime { |
| 153 | + if (typeof info === "number") { |
| 154 | + return new FluentDateTime(info, opts); |
| 155 | + } |
| 156 | + |
| 157 | + if (info instanceof FluentDateTime) { |
| 158 | + return new FluentDateTime(info.value, { |
| 159 | + ...info.opts, ...opts |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + if (info instanceof FluentType) { |
| 164 | + return FluentDateTime.from(info.valueOf(), opts); |
| 165 | + } |
| 166 | + |
| 167 | + if (info instanceof Date) { |
| 168 | + return new FluentDateTime(info.getTime(), opts); |
| 169 | + } |
| 170 | + |
| 171 | + if (isTemporal(info, "Instant")) { |
| 172 | + return new FluentDateTime(info.epochMilliseconds, opts); |
| 173 | + } |
| 174 | + |
| 175 | + if (isTemporal(info, "ZonedDateTime")) { |
| 176 | + let zoned: FluentTemporal.ZonedDateTime = info; |
| 177 | + if (opts.timeZone) { |
| 178 | + zoned = info.withTimeZone(opts.timeZone); |
| 179 | + } else { |
| 180 | + opts = { ...opts, timeZone: info.timeZoneId }; |
| 181 | + } |
| 182 | + if (opts.calendar === undefined) { |
| 183 | + opts = { ...opts, calendar: zoned.calendarId }; |
| 184 | + } |
| 185 | + return new FluentDateTime(zoned.epochMilliseconds, opts); |
| 186 | + } |
| 187 | + |
| 188 | + if (isTemporal(info, "PlainYearMonth")) { |
| 189 | + if (opts.calendar === undefined) { |
| 190 | + opts = { ...opts, calendar: info.calendarId }; |
| 191 | + } else { |
| 192 | + info = info.withCalendar(opts.calendar); |
| 193 | + } |
| 194 | + return new FluentDateTime(info, opts); |
| 195 | + } |
| 196 | + |
| 197 | + if ( |
| 198 | + isTemporal(info, "PlainDateTime") || isTemporal(info, "PlainDate") || |
| 199 | + isTemporal(info, "PlainMonthDay") || isTemporal(info, "PlainTime") |
| 200 | + ) { |
| 201 | + return new FluentDateTime(info, opts); |
| 202 | + } |
| 203 | + |
| 204 | + throw new Error("Invalid value passed to FluentDateTime"); |
| 205 | + } |
| 206 | + |
116 | 207 | /**
|
117 | 208 | * Create an instance of `FluentDateTime` with options to the
|
118 | 209 | * `Intl.DateTimeFormat` constructor.
|
119 | 210 | *
|
120 | 211 | * @param value The number value of this `FluentDateTime`, in milliseconds.
|
121 | 212 | * @param opts Options which will be passed to `Intl.DateTimeFormat`.
|
122 | 213 | */
|
123 |
| - constructor(value: number, opts: Intl.DateTimeFormatOptions = {}) { |
| 214 | + constructor(value: FluentDateValue, opts: Intl.DateTimeFormatOptions = {}) { |
124 | 215 | super(value);
|
125 | 216 | this.opts = opts;
|
126 | 217 | }
|
127 | 218 |
|
| 219 | + /** |
| 220 | + * Convert this `FluentDateTime` to a number. |
| 221 | + * Note that this isn't always possible due to the nature of Temporal objects. |
| 222 | + * In such cases, a TypeError will be thrown. |
| 223 | + */ |
| 224 | + toNumber(): number { |
| 225 | + const value = this.value; |
| 226 | + |
| 227 | + if (typeof value === "number") { |
| 228 | + return value; |
| 229 | + } |
| 230 | + |
| 231 | + if (value instanceof Date) { |
| 232 | + return value.getTime(); |
| 233 | + } |
| 234 | + |
| 235 | + if (isTemporal(value, "PlainDateTime") || isTemporal(value, "PlainDate")) { |
| 236 | + return value.toZonedDateTime("UTC").epochMilliseconds; |
| 237 | + } |
| 238 | + |
| 239 | + throw new TypeError("Unwrapping a non-number value as a number"); |
| 240 | + } |
| 241 | + |
128 | 242 | /**
|
129 | 243 | * Format this `FluentDateTime` to a string.
|
130 | 244 | */
|
131 | 245 | toString(scope: Scope): string {
|
132 | 246 | try {
|
133 | 247 | const dtf = scope.memoizeIntlObject(Intl.DateTimeFormat, this.opts);
|
134 |
| - return dtf.format(this.value); |
| 248 | + return dtf.format(this.value as Parameters<Intl.DateTimeFormat["format"]>[0]); |
135 | 249 | } catch (err) {
|
136 | 250 | scope.reportError(err);
|
137 |
| - return new Date(this.value).toISOString(); |
| 251 | + if (typeof this.value === "number" || this.value instanceof Date) { |
| 252 | + return new Date(this.value).toISOString(); |
| 253 | + } else { |
| 254 | + return this.value.toString(); |
| 255 | + } |
138 | 256 | }
|
139 | 257 | }
|
140 | 258 | }
|
0 commit comments