From 2f30cba7ae309b8c9d1cb5a1b57e49f4359c9d80 Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 27 Nov 2023 17:19:29 -0600 Subject: [PATCH] Hide toJSON & valueOf from emitted declarations These aren't meant to be called directly --- packages/driver/src/datatypes/datetime.ts | 77 ++++++++++------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/packages/driver/src/datatypes/datetime.ts b/packages/driver/src/datatypes/datetime.ts index 3da3f2818..746502fde 100644 --- a/packages/driver/src/datatypes/datetime.ts +++ b/packages/driver/src/datatypes/datetime.ts @@ -113,6 +113,9 @@ export class LocalTime { this.millisecond = isoMillisecond; this.microsecond = isoMicrosecond; this.nanosecond = isoNanosecond; + + forwardJsonAsToString(this); + throwOnValueOf(this, "LocalTime"); } toString(): string { @@ -131,14 +134,6 @@ export class LocalTime { } return repr; } - - toJSON(): string { - return this.toString(); - } - - valueOf(): any { - throw new TypeError("Not possible to compare LocalTime"); - } } export const localDateInstances = new WeakMap(); @@ -171,6 +166,9 @@ export class LocalDate { date.setUTCFullYear(isoYear); } localDateInstances.set(this, date); + + forwardJsonAsToString(this); + throwOnValueOf(this, "LocalDate"); } get year(): number { @@ -222,14 +220,6 @@ export class LocalDate { const day = this.day.toString().padStart(2, "0"); return `${year}-${month}-${day}`; } - - toJSON(): string { - return this.toString(); - } - - valueOf(): any { - throw new TypeError("Not possible to compare LocalDate"); - } } export function LocalDateToOrdinal(localdate: LocalDate): number { @@ -264,6 +254,7 @@ export class LocalDateTime extends LocalDate { isoNanosecond ); localTimeInstances.set(this, time); + throwOnValueOf(this, "LocalDateTime"); } get hour(): number { @@ -288,10 +279,6 @@ export class LocalDateTime extends LocalDate { toString(): string { return `${super.toString()}T${localTimeInstances.get(this)!.toString()}`; } - - valueOf(): any { - throw new TypeError("Not possible to compare LocalDateTime"); - } } interface DurationLike { @@ -389,6 +376,9 @@ export class Duration { this.microseconds = microseconds || 0; this.nanoseconds = nanoseconds || 0; this.sign = sign || 0; + + forwardJsonAsToString(this); + throwOnValueOf(this, "TemporalDuration"); } get blank(): boolean { @@ -453,14 +443,6 @@ export class Duration { ); } - toJSON(): string { - return this.toString(); - } - - valueOf(): any { - throw new TypeError("Not possible to compare TemporalDuration"); - } - static from(item: string | Duration | DurationLike): Duration { let result: DurationLike; if (item instanceof Duration) { @@ -606,6 +588,9 @@ export class RelativeDuration { this.seconds = Math.trunc(seconds) || 0; this.milliseconds = Math.trunc(milliseconds) || 0; this.microseconds = Math.trunc(microseconds) || 0; + + forwardJsonAsToString(this); + throwOnValueOf(this, "RelativeDuration"); } toString(): string { @@ -646,14 +631,6 @@ export class RelativeDuration { return str; } - - toJSON(): string { - return this.toString(); - } - - valueOf(): any { - throw new TypeError("Not possible to compare RelativeDuration"); - } } export class DateDuration { @@ -672,6 +649,9 @@ export class DateDuration { this.months = Math.trunc(months) || 0; this.weeks = Math.trunc(weeks) || 0; this.days = Math.trunc(days) || 0; + + forwardJsonAsToString(this); + throwOnValueOf(this, "DateDuration"); } toString(): string { @@ -693,14 +673,6 @@ export class DateDuration { return str; } - - toJSON(): string { - return this.toString(); - } - - valueOf(): any { - throw new TypeError("Not possible to compare DateDuration"); - } } const humanDurationPrefixes: { [key: string]: number } = { @@ -740,3 +712,20 @@ export function parseHumanDurationString(durationStr: string): number { } return duration; } + +const forwardJsonAsToString = (obj: object) => { + Object.defineProperty(obj, "toJSON", { + value: () => obj.toString(), + enumerable: false, + configurable: true, + }); +}; +const throwOnValueOf = (obj: object, typename: string) => { + Object.defineProperty(obj, "valueOf", { + value: () => { + throw new TypeError(`Not possible to compare ${typename}`); + }, + enumerable: false, + configurable: true, + }); +};