diff --git a/polyfill/index.d.ts b/polyfill/index.d.ts index d8a202c788..39e2784ee7 100644 --- a/polyfill/index.d.ts +++ b/polyfill/index.d.ts @@ -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; @@ -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( @@ -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( diff --git a/polyfill/lib/calendar.mjs b/polyfill/lib/calendar.mjs index 92942d8cad..b2b68efee2 100644 --- a/polyfill/lib/calendar.mjs +++ b/polyfill/lib/calendar.mjs @@ -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; @@ -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); @@ -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; @@ -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)}`); @@ -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'); @@ -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` + @@ -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; @@ -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) { @@ -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); @@ -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}`); @@ -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) {