Skip to content

Commit 8c480a4

Browse files
ptomato12wrigja
authored andcommitted
Change MergeLargestUnitOption to match spec
This was previously done with the spread syntax, but we are going to make a change to it, so we'll need a function instead. The spec text has the operation MergeLargestUnitOption, so name it that. The ObjectCreate(null) step was previously missing, so add it in order to be compliant with the spec. UPSTREAM_COMMIT=1660e23ae982d84ff7958c85fb44ae981e315d59
1 parent f586d0d commit 8c480a4

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/ecmascript.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,15 @@ export function LargerOfTwoTemporalUnits<T1 extends Temporal.DateTimeUnit, T2 ex
11291129
return unit1;
11301130
}
11311131

1132+
function MergeLargestUnitOption<T extends Temporal.DateTimeUnit>(
1133+
optionsParam: Temporal.DifferenceOptions<T> | undefined,
1134+
largestUnit: T
1135+
): Temporal.DifferenceOptions<T> {
1136+
let options = optionsParam;
1137+
if (options === undefined) options = ObjectCreate(null);
1138+
return { ...options, largestUnit };
1139+
}
1140+
11321141
export function ToPartialRecord<B extends AnyTemporalLikeType>(bagParam: B, fieldsParam: ReadonlyArray<keyof B>) {
11331142
// External callers are limited to specific types, but this function's
11341143
// implementation uses generic property types. The casts below (and at the
@@ -4098,7 +4107,7 @@ function DifferenceISODateTime(
40984107
const date1 = CreateTemporalDate(y1, mon1, d1, calendar);
40994108
const date2 = CreateTemporalDate(y2, mon2, d2, calendar);
41004109
const dateLargestUnit = LargerOfTwoTemporalUnits('day', largestUnit);
4101-
const untilOptions = { ...options, largestUnit: dateLargestUnit };
4110+
const untilOptions = MergeLargestUnitOption(options, dateLargestUnit);
41024111
// TODO untilOptions doesn't want to compile as it seems that smallestUnit is not clamped?
41034112
// Type 'SmallestUnit<DateTimeUnit> | undefined' is not assignable to type
41044113
// 'SmallestUnit<"year" | "month" | "day" | "week"> | undefined'.
@@ -4274,7 +4283,7 @@ export function DifferenceTemporalPlainDate(
42744283
if (operation === 'since') roundingMode = NegateTemporalRoundingMode(roundingMode);
42754284
const roundingIncrement = ToTemporalRoundingIncrement(options, undefined, false);
42764285

4277-
const untilOptions = { ...options, largestUnit };
4286+
const untilOptions = MergeLargestUnitOption(options, largestUnit);
42784287
let { years, months, weeks, days } = CalendarDateUntil(calendar, plainDate, other, untilOptions);
42794288

42804289
if (smallestUnit !== 'day' || roundingIncrement !== 1) {
@@ -4511,7 +4520,7 @@ export function DifferenceTemporalPlainYearMonth(
45114520
const otherDate = CalendarDateFromFields(calendar, { ...otherFields, day: 1 });
45124521
const thisDate = CalendarDateFromFields(calendar, { ...thisFields, day: 1 });
45134522

4514-
const untilOptions = { ...options, largestUnit };
4523+
const untilOptions = MergeLargestUnitOption(options, largestUnit);
45154524
let { years, months } = CalendarDateUntil(calendar, thisDate, otherDate, untilOptions);
45164525

45174526
if (smallestUnit !== 'month' || roundingIncrement !== 1) {
@@ -4598,7 +4607,7 @@ export function DifferenceTemporalZonedDateTime(
45984607
'or smaller because day lengths can vary between time zones due to DST or time zone offset changes.'
45994608
);
46004609
}
4601-
const untilOptions = { ...options, largestUnit };
4610+
const untilOptions = MergeLargestUnitOption(options, largestUnit);
46024611
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
46034612
DifferenceZonedDateTime(ns1, ns2, timeZone, calendar, largestUnit, untilOptions));
46044613
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = RoundDuration(

0 commit comments

Comments
 (0)