Skip to content

Commit

Permalink
Normative: Copy options object in Plain{Date,MonthDay,YearMonth}.{fro…
Browse files Browse the repository at this point in the history
…m,p.with}

Also following the precedent set in #2447, this preserves consistency with
analogous from/with operations in {Plain,Zoned}DateTime that need to read
the overflow option twice in order to deal with time and date units
separately.
  • Loading branch information
ptomato committed Aug 11, 2023
1 parent c8e4dab commit dc7a33b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
9 changes: 6 additions & 3 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ export function ToTemporalTimeRecord(bag, completeness = 'complete') {
}

export function ToTemporalDate(item, options) {
if (options !== undefined) options = SnapshotOwnProperties(GetOptionsObject(options), null);
if (Type(item) === 'Object') {
if (IsTemporalDate(item)) return item;
if (IsTemporalZonedDateTime(item)) {
Expand All @@ -1180,12 +1181,12 @@ export function ToTemporalDate(item, options) {
const fields = PrepareTemporalFields(item, fieldNames, []);
return CalendarDateFromFields(calendar, fields, options);
}
ToTemporalOverflow(options); // validate and ignore
let { year, month, day, calendar, z } = ParseTemporalDateString(RequireString(item));
if (z) throw new RangeError('Z designator not supported for PlainDate');
if (!calendar) calendar = 'iso8601';
if (!IsBuiltinCalendar(calendar)) throw new RangeError(`invalid calendar identifier ${calendar}`);
calendar = ASCIILowercase(calendar);
ToTemporalOverflow(options); // validate and ignore
return CreateTemporalDate(year, month, day, calendar);
}

Expand Down Expand Up @@ -1290,6 +1291,7 @@ export function ToTemporalInstant(item) {
}

export function ToTemporalMonthDay(item, options) {
if (options !== undefined) options = SnapshotOwnProperties(GetOptionsObject(options), null);
if (Type(item) === 'Object') {
if (IsTemporalMonthDay(item)) return item;
let calendar, calendarAbsent;
Expand All @@ -1313,11 +1315,11 @@ export function ToTemporalMonthDay(item, options) {
return CalendarMonthDayFromFields(calendar, fields, options);
}

ToTemporalOverflow(options); // validate and ignore
let { month, day, referenceISOYear, calendar } = ParseTemporalMonthDayString(RequireString(item));
if (calendar === undefined) calendar = 'iso8601';
if (!IsBuiltinCalendar(calendar)) throw new RangeError(`invalid calendar identifier ${calendar}`);
calendar = ASCIILowercase(calendar);
ToTemporalOverflow(options); // validate and ignore

if (referenceISOYear === undefined) {
RejectISODate(1972, month, day);
Expand Down Expand Up @@ -1364,6 +1366,7 @@ export function ToTemporalTime(item, overflow = 'constrain') {
}

export function ToTemporalYearMonth(item, options) {
if (options !== undefined) options = SnapshotOwnProperties(GetOptionsObject(options), null);
if (Type(item) === 'Object') {
if (IsTemporalYearMonth(item)) return item;
const calendar = GetTemporalCalendarSlotValueWithISODefault(item);
Expand All @@ -1372,11 +1375,11 @@ export function ToTemporalYearMonth(item, options) {
return CalendarYearMonthFromFields(calendar, fields, options);
}

ToTemporalOverflow(options); // validate and ignore
let { year, month, referenceISODay, calendar } = ParseTemporalYearMonthString(RequireString(item));
if (calendar === undefined) calendar = 'iso8601';
if (!IsBuiltinCalendar(calendar)) throw new RangeError(`invalid calendar identifier ${calendar}`);
calendar = ASCIILowercase(calendar);
ToTemporalOverflow(options); // validate and ignore

if (referenceISODay === undefined) {
RejectISODate(year, month, 1);
Expand Down
4 changes: 2 additions & 2 deletions polyfill/lib/plaindate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class PlainDate {
throw new TypeError('invalid argument');
}
ES.RejectTemporalLikeObject(temporalDateLike);
options = ES.GetOptionsObject(options);
const resolvedOptions = ES.SnapshotOwnProperties(ES.GetOptionsObject(options), null);

const calendar = GetSlot(this, CALENDAR);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
Expand All @@ -104,7 +104,7 @@ export class PlainDate {
fields = ES.CalendarMergeFields(calendar, fields, partialDate);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);

return ES.CalendarDateFromFields(calendar, fields, options);
return ES.CalendarDateFromFields(calendar, fields, resolvedOptions);
}
withCalendar(calendar) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
Expand Down
4 changes: 2 additions & 2 deletions polyfill/lib/plainmonthday.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class PlainMonthDay {
throw new TypeError('invalid argument');
}
ES.RejectTemporalLikeObject(temporalMonthDayLike);
options = ES.GetOptionsObject(options);
const resolvedOptions = ES.SnapshotOwnProperties(ES.GetOptionsObject(options), null);

const calendar = GetSlot(this, CALENDAR);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
Expand All @@ -44,7 +44,7 @@ export class PlainMonthDay {
fields = ES.CalendarMergeFields(calendar, fields, partialMonthDay);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);

return ES.CalendarMonthDayFromFields(calendar, fields, options);
return ES.CalendarMonthDayFromFields(calendar, fields, resolvedOptions);
}
equals(other) {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
Expand Down
4 changes: 2 additions & 2 deletions polyfill/lib/plainyearmonth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class PlainYearMonth {
throw new TypeError('invalid argument');
}
ES.RejectTemporalLikeObject(temporalYearMonthLike);
options = ES.GetOptionsObject(options);
const resolvedOptions = ES.SnapshotOwnProperties(ES.GetOptionsObject(options), null);

const calendar = GetSlot(this, CALENDAR);
const fieldNames = ES.CalendarFields(calendar, ['month', 'monthCode', 'year']);
Expand All @@ -70,7 +70,7 @@ export class PlainYearMonth {
fields = ES.CalendarMergeFields(calendar, fields, partialYearMonth);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);

return ES.CalendarYearMonthFromFields(calendar, fields, options);
return ES.CalendarYearMonthFromFields(calendar, fields, resolvedOptions);
}
add(temporalDurationLike, options = undefined) {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
Expand Down
7 changes: 4 additions & 3 deletions spec/plaindate.html
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ <h1>Temporal.PlainDate.prototype.with ( _temporalDateLike_ [ , _options_ ] )</h1
1. If Type(_temporalDateLike_) is not Object, then
1. Throw a *TypeError* exception.
1. Perform ? RejectTemporalLikeObject(_temporalDateLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _resolvedOptions_ be ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. Let _calendar_ be _temporalDate_.[[Calendar]].
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"day"*, *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_temporalDate_, _fieldNames_, «»).
1. Let _partialDate_ be ? PrepareTemporalFields(_temporalDateLike_, _fieldNames_, ~partial~).
1. Set _fields_ to ? CalendarMergeFields(_calendar_, _fields_, _partialDate_).
1. Set _fields_ to ? PrepareTemporalFields(_fields_, _fieldNames_, «»).
1. Return ? CalendarDateFromFields(_calendar_, _fields_, _options_).
1. Return ? CalendarDateFromFields(_calendar_, _fields_, _resolvedOptions_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -740,6 +740,7 @@ <h1>ToTemporalDate ( _item_ [ , _options_ ] )</h1>
<emu-alg>
1. If _options_ is not present, set _options_ to *undefined*.
1. Assert: Type(_options_) is Object or Undefined.
1. If _options_ is not *undefined*, set _options_ to ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalDate]] internal slot, then
1. Return _item_.
Expand All @@ -755,14 +756,14 @@ <h1>ToTemporalDate ( _item_ [ , _options_ ] )</h1>
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"day"*, *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_item_, _fieldNames_, «»).
1. Return ? CalendarDateFromFields(_calendar_, _fields_, _options_).
1. Perform ? ToTemporalOverflow(_options_).
1. If _item_ is not a String, throw a *TypeError* exception.
1. Let _result_ be ? ParseTemporalDateString(_item_).
1. Assert: IsValidISODate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]]) is *true*.
1. Let _calendar_ be _result_.[[Calendar]].
1. If _calendar_ is *undefined*, set _calendar_ to *"iso8601"*.
1. If IsBuiltinCalendar(_calendar_) is *false*, throw a *RangeError* exception.
1. Set _calendar_ to the ASCII-lowercase of _calendar_.
1. Perform ? ToTemporalOverflow(_options_).
1. Return ? CreateTemporalDate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]], _calendar_).
</emu-alg>
</emu-clause>
Expand Down
7 changes: 4 additions & 3 deletions spec/plainmonthday.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ <h1>Temporal.PlainMonthDay.prototype.with ( _temporalMonthDayLike_ [ , _options_
1. If Type(_temporalMonthDayLike_) is not Object, then
1. Throw a *TypeError* exception.
1. Perform ? RejectTemporalLikeObject(_temporalMonthDayLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _resolvedOptions_ be ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. Let _calendar_ be _monthDay_.[[Calendar]].
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"day"*, *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_monthDay_, _fieldNames_, «»).
1. Let _partialMonthDay_ be ? PrepareTemporalFields(_temporalMonthDayLike_, _fieldNames_, ~partial~).
1. Set _fields_ to ? CalendarMergeFields(_calendar_, _fields_, _partialMonthDay_).
1. Set _fields_ to ? PrepareTemporalFields(_fields_, _fieldNames_, «»).
1. Return ? CalendarMonthDayFromFields(_calendar_, _fields_, _options_).
1. Return ? CalendarMonthDayFromFields(_calendar_, _fields_, _resolvedOptions_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -360,6 +360,7 @@ <h1>ToTemporalMonthDay ( _item_ [ , _options_ ] )</h1>
<emu-alg>
1. If _options_ is not present, set _options_ to *undefined*.
1. Assert: Type(_options_) is Object or Undefined.
1. If _options_ is not *undefined*, set _options_ to ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. Let _referenceISOYear_ be 1972 (the first leap year after the Unix epoch).
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalMonthDay]] internal slot, then
Expand All @@ -382,13 +383,13 @@ <h1>ToTemporalMonthDay ( _item_ [ , _options_ ] )</h1>
1. If _calendarAbsent_ is *true*, and _month_ is not *undefined*, and _monthCode_ is *undefined* and _year_ is *undefined*, then
1. Perform ! CreateDataPropertyOrThrow(_fields_, *"year"*, 𝔽(_referenceISOYear_)).
1. Return ? CalendarMonthDayFromFields(_calendar_, _fields_, _options_).
1. Perform ? ToTemporalOverflow(_options_).
1. If _item_ is not a String, throw a *TypeError* exception.
1. Let _result_ be ? ParseTemporalMonthDayString(_item_).
1. Let _calendar_ be _result_.[[Calendar]].
1. If _calendar_ is *undefined*, set _calendar_ to *"iso8601"*.
1. If IsBuiltinCalendar(_calendar_) is *false*, throw a *RangeError* exception.
1. Set _calendar_ to the ASCII-lowercase of _calendar_.
1. Perform ? ToTemporalOverflow(_options_).
1. If _result_.[[Year]] is *undefined*, then
1. Return ? CreateTemporalMonthDay(_result_.[[Month]], _result_.[[Day]], _calendar_, _referenceISOYear_).
1. Set _result_ to ? CreateTemporalMonthDay(_result_.[[Month]], _result_.[[Day]], _calendar_, result.[[year]]).
Expand Down
7 changes: 4 additions & 3 deletions spec/plainyearmonth.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ <h1>Temporal.PlainYearMonth.prototype.with ( _temporalYearMonthLike_ [ , _option
1. If Type(_temporalYearMonthLike_) is not Object, then
1. Throw a *TypeError* exception.
1. Perform ? RejectTemporalLikeObject(_temporalYearMonthLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _resolvedOptions_ be ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. Let _calendar_ be _yearMonth_.[[Calendar]].
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_yearMonth_, _fieldNames_, «»).
1. Let _partialYearMonth_ be ? PrepareTemporalFields(_temporalYearMonthLike_, _fieldNames_, ~partial~).
1. Set _fields_ to ? CalendarMergeFields(_calendar_, _fields_, _partialYearMonth_).
1. Set _fields_ to ? PrepareTemporalFields(_fields_, _fieldNames_, «»).
1. Return ? CalendarYearMonthFromFields(_calendar_, _fields_, _options_).
1. Return ? CalendarYearMonthFromFields(_calendar_, _fields_, _resolvedOptions_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -489,20 +489,21 @@ <h1>ToTemporalYearMonth ( _item_ [ , _options_ ] )</h1>
<emu-alg>
1. If _options_ is not present, set _options_ to *undefined*.
1. Assert: Type(_options_) is Object or Undefined.
1. If _options_ is not *undefined*, set _options_ to ? SnapshotOwnProperties(? GetOptionsObject(_options_), *null*).
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalYearMonth]] internal slot, then
1. Return _item_.
1. Let _calendar_ be ? GetTemporalCalendarSlotValueWithISODefault(_item_).
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_item_, _fieldNames_, «»).
1. Return ? CalendarYearMonthFromFields(_calendar_, _fields_, _options_).
1. Perform ? ToTemporalOverflow(_options_).
1. If _item_ is not a String, throw a *TypeError* exception.
1. Let _result_ be ? ParseTemporalYearMonthString(_item_).
1. Let _calendar_ be _result_.[[Calendar]].
1. If _calendar_ is *undefined*, set _calendar_ to *"iso8601"*.
1. If IsBuiltinCalendar(_calendar_) is *false*, throw a *RangeError* exception.
1. Set _calendar_ to the ASCII-lowercase of _calendar_.
1. Perform ? ToTemporalOverflow(_options_).
1. Set _result_ to ? CreateTemporalYearMonth(_result_.[[Year]], _result_.[[Month]], _calendar_, _result_.[[Day]]).
1. NOTE: The following operation is called without _options_, in order for the calendar to store a canonical value in the [[ISODay]] internal slot of the result.
1. Return ? CalendarYearMonthFromFields(_calendar_, _result_).
Expand Down

0 comments on commit dc7a33b

Please sign in to comment.