Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,9 @@ export namespace Temporal {
readonly [Symbol.toStringTag]: 'Temporal.Instant';
}

type EitherYearOrEraAndEraYear = { era: string; eraYear: number } | { year: number };
type EitherMonthCodeOrMonthAndYear = (EitherYearOrEraAndEraYear & { month: number }) | { monthCode: string };
type YearOrEraAndEraYear = { era: string; eraYear: number } | { year: number };
type MonthCodeOrMonthAndYear = (YearOrEraAndEraYear & { month: number }) | { monthCode: string };
type MonthOrMonthCode = { month: number } | { monthCode: string };

export interface CalendarProtocol {
id?: string;
Expand Down Expand Up @@ -648,15 +649,15 @@ export namespace Temporal {
date: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.PlainYearMonth | PlainDateLike | string
): boolean;
dateFromFields(
fields: EitherMonthCodeOrMonthAndYear & { day: number },
fields: YearOrEraAndEraYear & MonthOrMonthCode & { day: number },
options?: AssignmentOptions
): Temporal.PlainDate;
yearMonthFromFields(
fields: EitherYearOrEraAndEraYear & ({ month: number } | { monthCode: string }),
fields: YearOrEraAndEraYear & MonthOrMonthCode,
options?: AssignmentOptions
): Temporal.PlainYearMonth;
monthDayFromFields(
fields: EitherMonthCodeOrMonthAndYear & { day: number },
fields: MonthCodeOrMonthAndYear & { day: number },
options?: AssignmentOptions
): Temporal.PlainMonthDay;
dateAdd(
Expand Down Expand Up @@ -726,15 +727,15 @@ export namespace Temporal {
date: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.PlainYearMonth | PlainDateLike | string
): boolean;
dateFromFields(
fields: EitherMonthCodeOrMonthAndYear & { day: number },
fields: YearOrEraAndEraYear & MonthOrMonthCode & { day: number },
options?: AssignmentOptions
): Temporal.PlainDate;
yearMonthFromFields(
fields: EitherYearOrEraAndEraYear & ({ month: number } | { monthCode: string }),
fields: YearOrEraAndEraYear & MonthOrMonthCode,
options?: AssignmentOptions
): Temporal.PlainYearMonth;
monthDayFromFields(
fields: EitherMonthCodeOrMonthAndYear & { day: number },
fields: MonthCodeOrMonthAndYear & { day: number },
options?: AssignmentOptions
): Temporal.PlainMonthDay;
dateAdd(
Expand Down
22 changes: 11 additions & 11 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
const ArrayIncludes = Array.prototype.includes;
const ArrayPrototypePush = Array.prototype.push;
const IntlDateTimeFormat = globalThis.Intl.DateTimeFormat;
const ArraySort = Array.prototype.sort;
const MathAbs = Math.abs;
const MathFloor = Math.floor;
const ObjectAssign = Object.assign;
Expand Down Expand Up @@ -418,7 +419,7 @@ class OneObjectCache {
this.hits = 0;
this.misses = 0;
if (cacheToClone !== undefined) {
let i = cacheToClone.length;
let i = 0;
for (const entry of cacheToClone.map.entries()) {
if (++i > OneObjectCache.MAX_CACHE_ENTRIES) break;
this.map.set(...entry);
Expand Down Expand Up @@ -509,7 +510,7 @@ const nonIsoHelperBase = {
return this.formatter;
},
isoToCalendarDate(isoDate, cache) {
let { year: isoYear, month: isoMonth, day: isoDay } = isoDate;
const { year: isoYear, month: isoMonth, day: isoDay } = isoDate;
const key = JSON.stringify({ func: 'isoToCalendarDate', isoYear, isoMonth, isoDay, id: this.id });
const cached = cache.get(key);
if (cached) return cached;
Expand Down Expand Up @@ -575,8 +576,7 @@ const nonIsoHelperBase = {
result.era = era;
result.eraYear = eraYear;
}
if (this.checkIcuBugs) this.checkIcuBugs(result, isoDate);

if (this.checkIcuBugs) this.checkIcuBugs(isoDate);
const calendarDate = this.adjustCalendarDate(result, cache, 'constrain', true);
if (calendarDate.year === undefined) throw new RangeError(`Missing year converting ${JSON.stringify(isoDate)}`);
if (calendarDate.month === undefined) throw new RangeError(`Missing month converting ${JSON.stringify(isoDate)}`);
Expand All @@ -597,7 +597,7 @@ const nonIsoHelperBase = {
return calendarDate;
},
validateCalendarDate(calendarDate) {
let { era, month, year, day, eraYear, monthCode, monthExtra } = calendarDate;
const { era, month, year, day, eraYear, monthCode, monthExtra } = calendarDate;
// When there's a suffix (e.g. "5bis" for a leap month in Chinese calendar)
// the derived class must deal with it.
if (monthExtra !== undefined) throw new RangeError('Unexpected `monthExtra` value');
Expand Down Expand Up @@ -1283,7 +1283,7 @@ const helperIndian = ObjectAssign({}, nonIsoHelperBase, {
// expected.
vulnerableToBceBug:
new Date('0000-01-01T00:00Z').toLocaleDateString('en-US-u-ca-indian', { timeZone: 'UTC' }) !== '10/11/-79 Saka',
checkIcuBugs(calendarDate, isoDate) {
checkIcuBugs(isoDate) {
if (this.vulnerableToBceBug && isoDate.year < 1) {
throw new RangeError(
`calendar '${this.id}' is broken for ISO dates before 0001-01-01` +
Expand Down Expand Up @@ -1384,7 +1384,7 @@ function adjustEras(eras) {
// Ensure that the latest epoch is first in the array. This lets us try to
// match eras in index order, with the last era getting the remaining older
// years. Any reverse-signed era must be at the end.
eras.sort((e1, e2) => {
ArraySort.call(eras, (e1, e2) => {
if (e1.reverseOf) return 1;
if (e2.reverseOf) return -1;
return e2.isoEpoch.year - e1.isoEpoch.year;
Expand Down Expand Up @@ -1521,7 +1521,7 @@ const makeHelperGregorian = (id, originalEras) => {
.toLocaleDateString('en-US-u-ca-japanese', { timeZone: 'UTC' })
.startsWith('12'),
calendarIsVulnerableToJulianBug: false,
checkIcuBugs(calendarDate, isoDate) {
checkIcuBugs(isoDate) {
if (this.calendarIsVulnerableToJulianBug && this.v8IsVulnerableToJulianBug) {
const beforeJulianSwitch = ES.CompareISODate(isoDate.year, isoDate.month, isoDate.day, 1582, 10, 15) < 0;
if (beforeJulianSwitch) {
Expand Down Expand Up @@ -1793,7 +1793,7 @@ const helperChinese = ObjectAssign({}, nonIsoHelperBase, {
if (
month === undefined &&
monthCode.endsWith('L') &&
!['M01L', 'M12L', 'M13L'].includes(monthCode) &&
!ArrayIncludes.call(['M01L', 'M12L', 'M13L'], monthCode) &&
overflow === 'constrain'
) {
let withoutML = monthCode.slice(1, -1);
Expand Down Expand Up @@ -1831,7 +1831,7 @@ const helperChinese = ObjectAssign({}, nonIsoHelperBase, {
const months = this.getMonthList(year, cache);
let numberPart = monthCode.replace('L', 'bis').slice(1);
if (numberPart[0] === '0') numberPart = numberPart.slice(1);
let monthInfo = months[numberPart];
const monthInfo = months[numberPart];
if (!monthInfo) throw new RangeError(`Unmatched monthCode ${monthCode} in Chinese year ${year}`);
if (month !== monthInfo.monthIndex) {
throw new RangeError(`monthCode ${monthCode} doesn't correspond to month ${month} in Chinese year ${year}`);
Expand Down Expand Up @@ -1909,7 +1909,7 @@ const nonIsoGeneralImpl = {
return result;
},
fields(fields) {
if (fields.includes('year')) fields = [...fields, 'era', 'eraYear'];
if (ArrayIncludes.call(fields, 'year')) fields = [...fields, 'era', 'eraYear'];
return fields;
},
mergeFields(fields, additionalFields) {
Expand Down