Skip to content

Commit

Permalink
Hide toJSON & valueOf from emitted declarations
Browse files Browse the repository at this point in the history
These aren't meant to be called directly
  • Loading branch information
CarsonF committed Nov 28, 2023
1 parent 594e5fd commit 2f30cba
Showing 1 changed file with 33 additions and 44 deletions.
77 changes: 33 additions & 44 deletions packages/driver/src/datatypes/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export class LocalTime {
this.millisecond = isoMillisecond;
this.microsecond = isoMicrosecond;
this.nanosecond = isoNanosecond;

forwardJsonAsToString(this);
throwOnValueOf(this, "LocalTime");
}

toString(): string {
Expand All @@ -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<object, Date>();
Expand Down Expand Up @@ -171,6 +166,9 @@ export class LocalDate {
date.setUTCFullYear(isoYear);
}
localDateInstances.set(this, date);

forwardJsonAsToString(this);
throwOnValueOf(this, "LocalDate");
}

get year(): number {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -264,6 +254,7 @@ export class LocalDateTime extends LocalDate {
isoNanosecond
);
localTimeInstances.set(this, time);
throwOnValueOf(this, "LocalDateTime");
}

get hour(): number {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 } = {
Expand Down Expand Up @@ -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,
});
};

0 comments on commit 2f30cba

Please sign in to comment.