Skip to content

Commit

Permalink
Use singular unit names in polyfill
Browse files Browse the repository at this point in the history
This is the implementation of the normative changes in #1509 in the
polyfill, accompanied by test262 tests to ensure that passing plural and
singular values for largestUnit, smallestUnit, and unit behaves the same;
and also that Calendar.dateUntil() is only ever called with options bags
that contain singular values for largestUnit.

See: #1491
  • Loading branch information
ptomato committed May 25, 2021
1 parent 8590b9b commit 64da90c
Show file tree
Hide file tree
Showing 59 changed files with 1,857 additions and 417 deletions.
26 changes: 12 additions & 14 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ export class Calendar {
one = ES.ToTemporalDate(one);
two = ES.ToTemporalDate(two);
options = ES.GetOptionsObject(options);
const largestUnit = ES.ToLargestTemporalUnit(options, 'days', [
'hours',
'minutes',
'seconds',
'milliseconds',
'microseconds',
'nanoseconds'
]);
const largestUnit = ES.ToLargestTemporalUnit(
options,
'auto',
['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'],
'day'
);
const { years, months, weeks, days } = impl[GetSlot(this, CALENDAR_ID)].dateUntil(one, two, largestUnit);
const Duration = GetIntrinsic('%Temporal.Duration%');
return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
Expand Down Expand Up @@ -793,22 +791,22 @@ const nonIsoHelperBase = {
let months = 0;
let years = 0;
switch (largestUnit) {
case 'days':
case 'day':
days = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
break;
case 'weeks': {
case 'week': {
const totalDays = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
days = totalDays % 7;
weeks = (totalDays - days) / 7;
break;
}
case 'months':
case 'years': {
case 'month':
case 'year': {
const diffYears = calendarTwo.year - calendarOne.year;
const diffMonths = calendarTwo.month - calendarOne.month;
const diffDays = calendarTwo.day - calendarOne.day;
const sign = this.compareCalendarDates(calendarTwo, calendarOne);
if (largestUnit === 'years' && diffYears) {
if (largestUnit === 'year' && diffYears) {
const isOneFurtherInYear = diffMonths * sign < 0 || (diffMonths === 0 && diffDays * sign < 0);
years = isOneFurtherInYear ? diffYears - sign : diffYears;
}
Expand Down Expand Up @@ -898,7 +896,7 @@ const nonIsoHelperBase = {
twoIso.year,
twoIso.month,
twoIso.day,
'days'
'day'
);
return duration.days;
},
Expand Down
29 changes: 9 additions & 20 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ export class Duration {
nanoseconds
);
options = ES.GetOptionsObject(options);
let smallestUnit = ES.ToSmallestTemporalDurationUnit(options, undefined);
let smallestUnit = ES.ToSmallestTemporalUnit(options, undefined);
let smallestUnitPresent = true;
if (!smallestUnit) {
smallestUnitPresent = false;
smallestUnit = 'nanoseconds';
smallestUnit = 'nanosecond';
}
defaultLargestUnit = ES.LargerOfTwoTemporalDurationUnits(defaultLargestUnit, smallestUnit);
let largestUnit = ES.ToLargestTemporalDurationUnit(options);
defaultLargestUnit = ES.LargerOfTwoTemporalUnits(defaultLargestUnit, smallestUnit);
let largestUnit = ES.ToLargestTemporalUnit(options, undefined);
let largestUnitPresent = true;
if (!largestUnit) {
largestUnitPresent = false;
Expand All @@ -342,19 +342,7 @@ export class Duration {
}
ES.ValidateTemporalUnitRange(largestUnit, smallestUnit);
const roundingMode = ES.ToTemporalRoundingMode(options, 'halfExpand');
const maximumIncrements = {
years: undefined,
months: undefined,
weeks: undefined,
days: undefined,
hours: 24,
minutes: 60,
seconds: 60,
milliseconds: 1000,
microseconds: 1000,
nanoseconds: 1000
};
const roundingIncrement = ES.ToTemporalRoundingIncrement(options, maximumIncrements[smallestUnit], false);
const roundingIncrement = ES.ToTemporalDateTimeRoundingIncrement(options, smallestUnit);
let relativeTo = ES.ToRelativeTemporalObject(options);

({ years, months, weeks, days } = ES.UnbalanceDurationRelative(
Expand Down Expand Up @@ -495,7 +483,8 @@ export class Duration {
toString(options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
options = ES.GetOptionsObject(options);
const { precision, unit, increment } = ES.ToDurationSecondsStringPrecision(options);
const { precision, unit, increment } = ES.ToSecondsStringPrecision(options);
if (precision === 'minute') throw new RangeError('smallestUnit must not be "minute"');
const roundingMode = ES.ToTemporalRoundingMode(options, 'trunc');
return ES.TemporalDurationToString(this, precision, { unit, increment, roundingMode });
}
Expand Down Expand Up @@ -559,8 +548,8 @@ export class Duration {
const shift1 = ES.CalculateOffsetShift(relativeTo, y1, mon1, w1, d1, h1, min1, s1, ms1, µs1, ns1);
const shift2 = ES.CalculateOffsetShift(relativeTo, y2, mon2, w2, d2, h2, min2, s2, ms2, µs2, ns2);
if (y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0) {
({ days: d1 } = ES.UnbalanceDurationRelative(y1, mon1, w1, d1, 'days', relativeTo));
({ days: d2 } = ES.UnbalanceDurationRelative(y2, mon2, w2, d2, 'days', relativeTo));
({ days: d1 } = ES.UnbalanceDurationRelative(y1, mon1, w1, d1, 'day', relativeTo));
({ days: d2 } = ES.UnbalanceDurationRelative(y2, mon2, w2, d2, 'day', relativeTo));
}
ns1 = ES.TotalDurationNanoseconds(d1, h1, min1, s1, ms1, µs1, ns1, shift1);
ns2 = ES.TotalDurationNanoseconds(d2, h2, min2, s2, ms2, µs2, ns2, shift2);
Expand Down
Loading

0 comments on commit 64da90c

Please sign in to comment.