Skip to content

Commit c6cdc99

Browse files
ptomatojustingrant
authored andcommitted
Editorial: DRY CopyDataProperties into CopyOptions operation
CopyOptions is an operation that's used many times, so refactor it into a new abstract operation. Note that GetOptionsObject is idempotent, so adding a call to it is still unobservable as long as it's already been called once. UPSTREAM_COMMIT=49059188e0d9384b2048dd3ec34d75ad76999c7c
1 parent 79624c2 commit c6cdc99

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

lib/ecmascript.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4639,8 +4639,7 @@ function DifferenceISODateTime(
46394639
const date1 = CreateTemporalDate(y1, mon1, d1, calendar);
46404640
const date2 = CreateTemporalDate(y2, mon2, d2, calendar);
46414641
const dateLargestUnit = LargerOfTwoTemporalUnits('day', largestUnit);
4642-
const untilOptions = ObjectCreate(null) as NonNullable<typeof options>;
4643-
CopyDataProperties(untilOptions, options, []);
4642+
const untilOptions = CopyOptions(options);
46444643
untilOptions.largestUnit = dateLargestUnit;
46454644
// TODO untilOptions doesn't want to compile as it seems that smallestUnit is not clamped?
46464645
// Type 'SmallestUnit<DateTimeUnit> | undefined' is not assignable to type
@@ -4798,8 +4797,7 @@ export function DifferenceTemporalInstant(
47984797
const sign = operation === 'since' ? -1 : 1;
47994798
const other = ToTemporalInstant(otherParam);
48004799

4801-
const resolvedOptions = ObjectCreate(null);
4802-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
4800+
const resolvedOptions = CopyOptions(options);
48034801
const settings = GetDifferenceSettings(operation, resolvedOptions, 'time', [], 'nanosecond', 'second');
48044802

48054803
const onens = GetSlot(instant, EPOCHNANOSECONDS);
@@ -4839,8 +4837,7 @@ export function DifferenceTemporalPlainDate(
48394837
const otherCalendar = GetSlot(other, CALENDAR);
48404838
ThrowIfCalendarsNotEqual(calendar, otherCalendar, 'compute difference between dates');
48414839

4842-
const resolvedOptions = ObjectCreate(null) as NonNullable<typeof options>;
4843-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
4840+
const resolvedOptions = CopyOptions(options);
48444841
const settings = GetDifferenceSettings(operation, resolvedOptions, 'date', [], 'day', 'day');
48454842
resolvedOptions.largestUnit = settings.largestUnit;
48464843

@@ -4881,8 +4878,7 @@ export function DifferenceTemporalPlainDateTime(
48814878
const otherCalendar = GetSlot(other, CALENDAR);
48824879
ThrowIfCalendarsNotEqual(calendar, otherCalendar, 'compute difference between dates');
48834880

4884-
const resolvedOptions = ObjectCreate(null) as NonNullable<typeof options>;
4885-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
4881+
const resolvedOptions = CopyOptions(options);
48864882
const settings = GetDifferenceSettings(operation, resolvedOptions, 'datetime', [], 'nanosecond', 'day');
48874883

48884884
let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
@@ -4962,8 +4958,7 @@ export function DifferenceTemporalPlainTime(
49624958
const sign = operation === 'since' ? -1 : 1;
49634959
const other = ToTemporalTime(otherParam);
49644960

4965-
const resolvedOptions = ObjectCreate(null);
4966-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
4961+
const resolvedOptions = CopyOptions(options);
49674962
const settings = GetDifferenceSettings(operation, resolvedOptions, 'time', [], 'nanosecond', 'hour');
49684963

49694964
let { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = DifferenceTime(
@@ -5032,8 +5027,7 @@ export function DifferenceTemporalPlainYearMonth(
50325027
const otherCalendar = GetSlot(other, CALENDAR);
50335028
ThrowIfCalendarsNotEqual(calendar, otherCalendar, 'compute difference between months');
50345029

5035-
const resolvedOptions = ObjectCreate(null) as NonNullable<typeof options>;
5036-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
5030+
const resolvedOptions = CopyOptions(options);
50375031
const settings = GetDifferenceSettings(operation, resolvedOptions, 'date', ['week', 'day'], 'month', 'year');
50385032
resolvedOptions.largestUnit = settings.largestUnit;
50395033

@@ -5082,8 +5076,7 @@ export function DifferenceTemporalZonedDateTime(
50825076
const otherCalendar = GetSlot(other, CALENDAR);
50835077
ThrowIfCalendarsNotEqual(calendar, otherCalendar, 'compute difference between dates');
50845078

5085-
const resolvedOptions = ObjectCreate(null) as NonNullable<typeof options>;
5086-
CopyDataProperties(resolvedOptions, GetOptionsObject(options), []);
5079+
const resolvedOptions = CopyOptions(options);
50875080
const settings = GetDifferenceSettings(operation, resolvedOptions, 'datetime', [], 'nanosecond', 'hour');
50885081
resolvedOptions.largestUnit = settings.largestUnit;
50895082

@@ -5679,8 +5672,7 @@ export function AddDurationToOrSubtractDurationFromPlainYearMonth(
56795672
const startDate = CalendarDateFromFields(calendar, fields as typeof fields & { day: number });
56805673
const Duration = GetIntrinsic('%Temporal.Duration%');
56815674
const durationToAdd = new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
5682-
const optionsCopy = ObjectCreate(null) as typeof options;
5683-
CopyDataProperties(optionsCopy, options, []);
5675+
const optionsCopy = CopyOptions(options);
56845676
const addedDate = CalendarDateAdd(calendar, startDate, durationToAdd, options);
56855677
const addedDateFields = PrepareTemporalFields(addedDate, fieldNames, []);
56865678

@@ -6456,6 +6448,12 @@ export function CreateOnePropObject<K extends string, V>(propName: K, propValue:
64566448
return o;
64576449
}
64586450

6451+
function CopyOptions<T extends { [s in K]?: unknown }, K extends string & keyof T>(options: T | undefined) {
6452+
const optionsCopy = ObjectCreate(null) as T;
6453+
CopyDataProperties(optionsCopy, GetOptionsObject(options), []);
6454+
return optionsCopy;
6455+
}
6456+
64596457
type StringlyTypedKeys<T> = Exclude<keyof T, symbol | number>;
64606458
function GetOption<P extends StringlyTypedKeys<O>, O extends Partial<Record<P, unknown>>>(
64616459
options: O,

0 commit comments

Comments
 (0)