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