-
Notifications
You must be signed in to change notification settings - Fork 28
/
intrinsicclass.ts
165 lines (154 loc) · 6.49 KB
/
intrinsicclass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type JSBI from 'jsbi';
import type { Temporal } from '..';
import { DEBUG } from './debug';
type OmitConstructor<T> = { [P in keyof T as T[P] extends new (...args: any[]) => any ? P : never]: T[P] };
type TemporalIntrinsics = Omit<typeof Temporal, 'Now' | 'Instant' | 'ZonedDateTime'> & {
Instant: OmitConstructor<Temporal.Instant> &
(new (epochNanoseconds: JSBI) => Temporal.Instant) & { prototype: typeof Temporal.Instant.prototype };
ZonedDateTime: OmitConstructor<Temporal.ZonedDateTime> &
(new (
epochNanoseconds: JSBI,
timeZone: string | Temporal.TimeZoneProtocol,
calendar?: string | Temporal.CalendarProtocol
) => Temporal.ZonedDateTime) & {
prototype: typeof Temporal.ZonedDateTime.prototype;
from: typeof Temporal.ZonedDateTime.from;
compare: typeof Temporal.ZonedDateTime.compare;
};
};
type TemporalIntrinsicRegistrations = {
[key in keyof TemporalIntrinsics as `Temporal.${key}`]: TemporalIntrinsics[key];
};
type TemporalIntrinsicPrototypeRegistrations = {
[key in keyof TemporalIntrinsics as `Temporal.${key}.prototype`]: TemporalIntrinsics[key]['prototype'];
};
type TemporalIntrinsicRegisteredKeys = {
[key in keyof TemporalIntrinsicRegistrations as `%${key}%`]: TemporalIntrinsicRegistrations[key];
};
type TemporalIntrinsicPrototypeRegisteredKeys = {
[key in keyof TemporalIntrinsicPrototypeRegistrations as `%${key}%`]: TemporalIntrinsicPrototypeRegistrations[key];
};
type CalendarPrototypeKeys = keyof Omit<Temporal.Calendar, typeof Symbol.toStringTag>;
type TemporalCalendarIntrinsicRegistrations = {
[key in CalendarPrototypeKeys as `Temporal.Calendar.prototype.${key}`]: Temporal.Calendar[key];
} & {
'Temporal.Calendar.from': typeof Temporal.Calendar.from;
};
type TemporalCalendarIntrinsicRegisteredKeys = {
[key in keyof TemporalCalendarIntrinsicRegistrations as `%${key}%`]: TemporalCalendarIntrinsicRegistrations[key];
};
type TimeZonePrototypeKeys = 'getOffsetNanosecondsFor' | 'getPossibleInstantsFor';
type TemporalTimeZoneIntrinsicRegistrations = {
[key in TimeZonePrototypeKeys as `Temporal.TimeZone.prototype.${key}`]: Temporal.TimeZone[key];
} & {
'Temporal.TimeZone.from': typeof Temporal.TimeZone.from;
};
type TemporalTimeZoneIntrinsicRegisteredKeys = {
[key in keyof TemporalTimeZoneIntrinsicRegistrations as `%${key}%`]: TemporalTimeZoneIntrinsicRegistrations[key];
};
const INTRINSICS = {} as TemporalIntrinsicRegisteredKeys &
TemporalIntrinsicPrototypeRegisteredKeys &
TemporalTimeZoneIntrinsicRegisteredKeys &
TemporalCalendarIntrinsicRegisteredKeys;
type customFormatFunction<T> = (
this: T,
depth: number,
options: { stylize: (value: unknown, type: 'number' | 'special') => string }
) => string;
const customUtilInspectFormatters: Partial<{
[key in keyof TemporalIntrinsicRegistrations]: customFormatFunction<
InstanceType<TemporalIntrinsicRegistrations[key]>
>;
}> = {
['Temporal.Duration'](depth, options) {
const descr = options.stylize(`${this[Symbol.toStringTag]} <${this}>`, 'special');
if (depth < 1) return descr;
const entries = [];
for (const prop of [
'years',
'months',
'weeks',
'days',
'hours',
'minutes',
'seconds',
'milliseconds',
'microseconds',
'nanoseconds'
] as const) {
if (this[prop] !== 0) entries.push(` ${prop}: ${options.stylize(this[prop], 'number')}`);
}
return descr + ' {\n' + entries.join(',\n') + '\n}';
}
};
type InspectFormatterOptions = { stylize: (str: string, styleType: string) => string };
function defaultUtilInspectFormatter(this: any, depth: number, options: InspectFormatterOptions) {
return options.stylize(`${this[Symbol.toStringTag]} <${this}>`, 'special');
}
export function MakeIntrinsicClass(
Class: TemporalIntrinsicRegistrations[typeof name],
name: keyof TemporalIntrinsicRegistrations
) {
Object.defineProperty(Class.prototype, Symbol.toStringTag, {
value: name,
writable: false,
enumerable: false,
configurable: true
});
if (DEBUG) {
Object.defineProperty(Class.prototype, Symbol.for('nodejs.util.inspect.custom'), {
value: customUtilInspectFormatters[name] || defaultUtilInspectFormatter,
writable: false,
enumerable: false,
configurable: true
});
}
for (const prop of Object.getOwnPropertyNames(Class)) {
// we know that `prop` is present, so the descriptor is never undefined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const desc = Object.getOwnPropertyDescriptor(Class, prop)!;
if (!desc.configurable || !desc.enumerable) continue;
desc.enumerable = false;
Object.defineProperty(Class, prop, desc);
}
for (const prop of Object.getOwnPropertyNames(Class.prototype)) {
// we know that `prop` is present, so the descriptor is never undefined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const desc = Object.getOwnPropertyDescriptor(Class.prototype, prop)!;
if (!desc.configurable || !desc.enumerable) continue;
desc.enumerable = false;
Object.defineProperty(Class.prototype, prop, desc);
}
DefineIntrinsic(name, Class);
DefineIntrinsic(`${name}.prototype`, Class.prototype);
}
type IntrinsicDefinitionKeys =
| keyof TemporalIntrinsicRegistrations
| keyof TemporalIntrinsicPrototypeRegistrations
| keyof TemporalCalendarIntrinsicRegistrations
| keyof TemporalTimeZoneIntrinsicRegistrations;
export function DefineIntrinsic<KeyT extends keyof TemporalIntrinsicRegistrations>(
name: KeyT,
value: TemporalIntrinsicRegistrations[KeyT]
): void;
export function DefineIntrinsic<KeyT extends keyof TemporalIntrinsicPrototypeRegistrations>(
name: KeyT,
value: TemporalIntrinsicPrototypeRegistrations[KeyT]
): void;
export function DefineIntrinsic<KeyT extends keyof TemporalCalendarIntrinsicRegistrations>(
name: KeyT,
value: TemporalCalendarIntrinsicRegistrations[KeyT]
): void;
export function DefineIntrinsic<KeyT extends keyof TemporalTimeZoneIntrinsicRegistrations>(
name: KeyT,
value: TemporalTimeZoneIntrinsicRegistrations[KeyT]
): void;
export function DefineIntrinsic<KeyT>(name: KeyT, value: never): void;
export function DefineIntrinsic<KeyT extends IntrinsicDefinitionKeys>(name: KeyT, value: unknown): void {
const key: `%${IntrinsicDefinitionKeys}%` = `%${name}%`;
if (INTRINSICS[key] !== undefined) throw new Error(`intrinsic ${name} already exists`);
INTRINSICS[key] = value;
}
export function GetIntrinsic<KeyT extends keyof typeof INTRINSICS>(intrinsic: KeyT): (typeof INTRINSICS)[KeyT] {
return INTRINSICS[intrinsic];
}