Skip to content

Commit 6fd8d28

Browse files
committed
Use singular unit names in polyfill
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
1 parent f6b3312 commit 6fd8d28

File tree

9 files changed

+218
-417
lines changed

9 files changed

+218
-417
lines changed

lib/calendar.mjs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ export class Calendar {
7979
one = ES.ToTemporalDate(one);
8080
two = ES.ToTemporalDate(two);
8181
options = ES.GetOptionsObject(options);
82-
const largestUnit = ES.ToLargestTemporalUnit(options, 'days', [
83-
'hours',
84-
'minutes',
85-
'seconds',
86-
'milliseconds',
87-
'microseconds',
88-
'nanoseconds'
89-
]);
82+
const largestUnit = ES.ToLargestTemporalUnit(
83+
options,
84+
'auto',
85+
['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'],
86+
'day'
87+
);
9088
const { years, months, weeks, days } = impl[GetSlot(this, CALENDAR_ID)].dateUntil(one, two, largestUnit);
9189
const Duration = GetIntrinsic('%Temporal.Duration%');
9290
return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
@@ -793,22 +791,22 @@ const nonIsoHelperBase = {
793791
let months = 0;
794792
let years = 0;
795793
switch (largestUnit) {
796-
case 'days':
794+
case 'day':
797795
days = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
798796
break;
799-
case 'weeks': {
797+
case 'week': {
800798
const totalDays = this.calendarDaysUntil(calendarOne, calendarTwo, cache);
801799
days = totalDays % 7;
802800
weeks = (totalDays - days) / 7;
803801
break;
804802
}
805-
case 'months':
806-
case 'years': {
803+
case 'month':
804+
case 'year': {
807805
const diffYears = calendarTwo.year - calendarOne.year;
808806
const diffMonths = calendarTwo.month - calendarOne.month;
809807
const diffDays = calendarTwo.day - calendarOne.day;
810808
const sign = this.compareCalendarDates(calendarTwo, calendarOne);
811-
if (largestUnit === 'years' && diffYears) {
809+
if (largestUnit === 'year' && diffYears) {
812810
const isOneFurtherInYear = diffMonths * sign < 0 || (diffMonths === 0 && diffDays * sign < 0);
813811
years = isOneFurtherInYear ? diffYears - sign : diffYears;
814812
}
@@ -898,7 +896,7 @@ const nonIsoHelperBase = {
898896
twoIso.year,
899897
twoIso.month,
900898
twoIso.day,
901-
'days'
899+
'day'
902900
);
903901
return duration.days;
904902
},

lib/duration.mjs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,14 @@ export class Duration {
323323
nanoseconds
324324
);
325325
options = ES.GetOptionsObject(options);
326-
let smallestUnit = ES.ToSmallestTemporalDurationUnit(options, undefined);
326+
let smallestUnit = ES.ToSmallestTemporalUnit(options, undefined);
327327
let smallestUnitPresent = true;
328328
if (!smallestUnit) {
329329
smallestUnitPresent = false;
330-
smallestUnit = 'nanoseconds';
330+
smallestUnit = 'nanosecond';
331331
}
332-
defaultLargestUnit = ES.LargerOfTwoTemporalDurationUnits(defaultLargestUnit, smallestUnit);
333-
let largestUnit = ES.ToLargestTemporalDurationUnit(options);
332+
defaultLargestUnit = ES.LargerOfTwoTemporalUnits(defaultLargestUnit, smallestUnit);
333+
let largestUnit = ES.ToLargestTemporalUnit(options, undefined);
334334
let largestUnitPresent = true;
335335
if (!largestUnit) {
336336
largestUnitPresent = false;
@@ -342,19 +342,7 @@ export class Duration {
342342
}
343343
ES.ValidateTemporalUnitRange(largestUnit, smallestUnit);
344344
const roundingMode = ES.ToTemporalRoundingMode(options, 'halfExpand');
345-
const maximumIncrements = {
346-
years: undefined,
347-
months: undefined,
348-
weeks: undefined,
349-
days: undefined,
350-
hours: 24,
351-
minutes: 60,
352-
seconds: 60,
353-
milliseconds: 1000,
354-
microseconds: 1000,
355-
nanoseconds: 1000
356-
};
357-
const roundingIncrement = ES.ToTemporalRoundingIncrement(options, maximumIncrements[smallestUnit], false);
345+
const roundingIncrement = ES.ToTemporalDateTimeRoundingIncrement(options, smallestUnit);
358346
let relativeTo = ES.ToRelativeTemporalObject(options);
359347

360348
({ years, months, weeks, days } = ES.UnbalanceDurationRelative(
@@ -495,7 +483,8 @@ export class Duration {
495483
toString(options = undefined) {
496484
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
497485
options = ES.GetOptionsObject(options);
498-
const { precision, unit, increment } = ES.ToDurationSecondsStringPrecision(options);
486+
const { precision, unit, increment } = ES.ToSecondsStringPrecision(options);
487+
if (precision === 'minute') throw new RangeError('smallestUnit must not be "minute"');
499488
const roundingMode = ES.ToTemporalRoundingMode(options, 'trunc');
500489
return ES.TemporalDurationToString(this, precision, { unit, increment, roundingMode });
501490
}
@@ -559,8 +548,8 @@ export class Duration {
559548
const shift1 = ES.CalculateOffsetShift(relativeTo, y1, mon1, w1, d1, h1, min1, s1, ms1, µs1, ns1);
560549
const shift2 = ES.CalculateOffsetShift(relativeTo, y2, mon2, w2, d2, h2, min2, s2, ms2, µs2, ns2);
561550
if (y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0) {
562-
({ days: d1 } = ES.UnbalanceDurationRelative(y1, mon1, w1, d1, 'days', relativeTo));
563-
({ days: d2 } = ES.UnbalanceDurationRelative(y2, mon2, w2, d2, 'days', relativeTo));
551+
({ days: d1 } = ES.UnbalanceDurationRelative(y1, mon1, w1, d1, 'day', relativeTo));
552+
({ days: d2 } = ES.UnbalanceDurationRelative(y2, mon2, w2, d2, 'day', relativeTo));
564553
}
565554
ns1 = ES.TotalDurationNanoseconds(d1, h1, min1, s1, ms1, µs1, ns1, shift1);
566555
ns2 = ES.TotalDurationNanoseconds(d2, h2, min2, s2, ms2, µs2, ns2, shift2);

0 commit comments

Comments
 (0)