Skip to content

Commit

Permalink
Make types consistent that Calendar methods are be called with
Browse files Browse the repository at this point in the history
Now that we have a fast-path conversion from PlainDateTime to PlainDate,
it's no longer necessary to check for an [[ISOYear]] internal slot. Make
sure it is consistent whether a particular Calendar method can be called
with a Temporal.PlainYearMonth or Temporal.PlainMonthDay without
triggering a conversion, if appropriate.

The policy is that these Calendar methods can all be called with a
PlainDate, and additionally some of them can be called with a
PlainYearMonth and/or PlainMonthDay. Anything else gets passed through
ToTemporalDate, which includes the fast path for PlainDateTime.

See: #1428
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent 983e314 commit 88ebc41
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 152 deletions.
26 changes: 14 additions & 12 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,71 +88,73 @@ export class Calendar {
}
year(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].year(date);
}
month(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (ES.IsTemporalMonthDay(date)) throw new TypeError('use monthCode on PlainMonthDay instead');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].month(date);
}
monthCode(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date) && !ES.IsTemporalMonthDay(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].monthCode(date);
}
day(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalMonthDay(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].day(date);
}
era(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].era(date);
}
eraYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].eraYear(date);
}
dayOfWeek(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].dayOfWeek(date);
}
dayOfYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].dayOfYear(date);
}
weekOfYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].weekOfYear(date);
}
daysInWeek(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInWeek(date);
}
daysInMonth(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInMonth(date);
}
daysInYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInYear(date);
}
monthsInYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].monthsInYear(date);
}
inLeapYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].inLeapYear(date);
}
toString() {
Expand Down
Loading

0 comments on commit 88ebc41

Please sign in to comment.