|
1 | 1 | import { Scope } from "./scope.js";
|
2 | 2 |
|
| 3 | +// Temporary workaround to support environments without Temporal |
| 4 | +// Replace with Temporal.* types once they are provided by TypeScript |
| 5 | +// In addition to this minimal interface, these objects are also expected |
| 6 | +// to be supported by Intl.DateTimeFormat |
| 7 | +interface TemporalObject { |
| 8 | + epochMilliseconds?: number; |
| 9 | + toZonedDateTime?(timeZone: string): { epochMilliseconds: number }; |
| 10 | + calendarId?: string; |
| 11 | + toString(): string; |
| 12 | +} |
| 13 | + |
3 | 14 | export type FluentValue = FluentType<unknown> | string;
|
4 | 15 |
|
| 16 | +export type FluentVariable = |
| 17 | + | FluentValue |
| 18 | + | TemporalObject |
| 19 | + | string |
| 20 | + | number |
| 21 | + | Date; |
| 22 | + |
5 | 23 | export type FluentFunction = (
|
6 | 24 | positional: Array<FluentValue>,
|
7 | 25 | named: Record<string, FluentValue>
|
@@ -104,37 +122,110 @@ export class FluentNumber extends FluentType<number> {
|
104 | 122 | /**
|
105 | 123 | * A `FluentType` representing a date and time.
|
106 | 124 | *
|
107 |
| - * A `FluentDateTime` instance stores the number value of the date it |
108 |
| - * represents, as a numerical timestamp in milliseconds. It may also store an |
| 125 | + * A `FluentDateTime` instance stores a Date object, Temporal object, or a number |
| 126 | + * as a numerical timestamp in milliseconds. It may also store an |
109 | 127 | * option bag of options which will be passed to `Intl.DateTimeFormat` when the
|
110 | 128 | * `FluentDateTime` is formatted to a string.
|
111 | 129 | */
|
112 |
| -export class FluentDateTime extends FluentType<number> { |
| 130 | +export class FluentDateTime extends FluentType< |
| 131 | + | number |
| 132 | + | Date |
| 133 | + | TemporalObject |
| 134 | +> { |
113 | 135 | /** Options passed to `Intl.DateTimeFormat`. */
|
114 | 136 | public opts: Intl.DateTimeFormatOptions;
|
115 | 137 |
|
| 138 | + static supportsValue(value: unknown): value is ConstructorParameters<typeof FluentDateTime>[0] { |
| 139 | + if (typeof value === "number") return true; |
| 140 | + if (value instanceof Date) return true; |
| 141 | + if (value instanceof FluentType) return FluentDateTime.supportsValue(value.valueOf()); |
| 142 | + // Temporary workaround to support environments without Temporal |
| 143 | + if ('Temporal' in globalThis) { |
| 144 | + // for TypeScript, which doesn't know about Temporal yet |
| 145 | + const _Temporal = ( |
| 146 | + globalThis as unknown as { Temporal: Record<string, () => unknown> } |
| 147 | + ).Temporal; |
| 148 | + if ( |
| 149 | + value instanceof _Temporal.Instant || |
| 150 | + value instanceof _Temporal.PlainDateTime || |
| 151 | + value instanceof _Temporal.PlainDate || |
| 152 | + value instanceof _Temporal.PlainMonthDay || |
| 153 | + value instanceof _Temporal.PlainTime || |
| 154 | + value instanceof _Temporal.PlainYearMonth |
| 155 | + ) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + return false |
| 160 | + } |
| 161 | + |
116 | 162 | /**
|
117 | 163 | * Create an instance of `FluentDateTime` with options to the
|
118 | 164 | * `Intl.DateTimeFormat` constructor.
|
119 | 165 | *
|
120 | 166 | * @param value The number value of this `FluentDateTime`, in milliseconds.
|
121 | 167 | * @param opts Options which will be passed to `Intl.DateTimeFormat`.
|
122 | 168 | */
|
123 |
| - constructor(value: number, opts: Intl.DateTimeFormatOptions = {}) { |
| 169 | + constructor( |
| 170 | + value: |
| 171 | + | number |
| 172 | + | Date |
| 173 | + | TemporalObject |
| 174 | + | FluentDateTime |
| 175 | + | FluentType<number>, |
| 176 | + opts: Intl.DateTimeFormatOptions = {} |
| 177 | + ) { |
| 178 | + // unwrap any FluentType value, but only retain the opts from FluentDateTime |
| 179 | + if (value instanceof FluentDateTime) { |
| 180 | + opts = { ...value.opts, ...opts }; |
| 181 | + value = value.value; |
| 182 | + } else if (value instanceof FluentType) { |
| 183 | + value = value.valueOf(); |
| 184 | + } |
| 185 | + |
| 186 | + // Intl.DateTimeFormat defaults to gregorian calendar, but Temporal defaults to iso8601 |
| 187 | + if (typeof value === "object" && 'calendarId' in value && opts.calendar === undefined) { |
| 188 | + opts = { ...opts, calendar: value.calendarId }; |
| 189 | + } |
| 190 | + |
124 | 191 | super(value);
|
125 | 192 | this.opts = opts;
|
126 | 193 | }
|
127 | 194 |
|
| 195 | + /** |
| 196 | + * Convert this `FluentDateTime` to a number. |
| 197 | + * Note that this isn't always possible due to the nature of Temporal objects. |
| 198 | + * In such cases, a TypeError will be thrown. |
| 199 | + */ |
| 200 | + toNumber(): number { |
| 201 | + const value = this.value; |
| 202 | + if (typeof value === "number") return value; |
| 203 | + if (value instanceof Date) return value.getTime(); |
| 204 | + |
| 205 | + if ('epochMilliseconds' in value) { |
| 206 | + return value.epochMilliseconds as number; |
| 207 | + } |
| 208 | + |
| 209 | + if ('toZonedDateTime' in value) { |
| 210 | + return value.toZonedDateTime!("UTC").epochMilliseconds; |
| 211 | + } |
| 212 | + |
| 213 | + throw new TypeError("Unwrapping a non-number value as a number"); |
| 214 | + } |
| 215 | + |
128 | 216 | /**
|
129 | 217 | * Format this `FluentDateTime` to a string.
|
130 | 218 | */
|
131 | 219 | toString(scope: Scope): string {
|
132 | 220 | try {
|
133 | 221 | const dtf = scope.memoizeIntlObject(Intl.DateTimeFormat, this.opts);
|
134 |
| - return dtf.format(this.value); |
| 222 | + return dtf.format(this.value as Parameters<Intl.DateTimeFormat["format"]>[0]); |
135 | 223 | } catch (err) {
|
136 | 224 | scope.reportError(err);
|
137 |
| - return new Date(this.value).toISOString(); |
| 225 | + if (typeof this.value === "number" || this.value instanceof Date) { |
| 226 | + return new Date(this.value).toISOString(); |
| 227 | + } |
| 228 | + return this.value.toString(); |
138 | 229 | }
|
139 | 230 | }
|
140 | 231 | }
|
0 commit comments