Skip to content

Commit

Permalink
Disallow formatRange() and formatRangeToParts() between different cal…
Browse files Browse the repository at this point in the history
…endars

In Intl.DateTimeFormat.formatRange() and formatRangeToParts(), check if
the calendars of the two operands are different. If they are different,
then throw. The previous behaviour was to throw only if both calendars
were non-ISO.

This brings the behaviour in line with that of difference().

Closes: #587
  • Loading branch information
ptomato committed Aug 28, 2020
1 parent c527d1b commit 05b0d41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function resolvedOptions() {

function adjustFormatterCalendar(formatter, calendar) {
const options = formatter.resolvedOptions();
if (!calendar || calendar === options.calendar || calendar === 'gregory' || calendar === 'iso8601') return formatter;
if (!calendar || calendar === options.calendar || calendar === 'iso8601') return formatter;
const locale = `${options.locale}-u-ca-${calendar}`;
return new IntlDateTimeFormat(locale, options);
}
Expand All @@ -68,8 +68,6 @@ function pickRangeCalendar(a, b) {
if (!a) return b;
if (!b) return a;
if (a === b) return a;
if (a === 'iso8601' || a === 'gregory') return b;
if (b === 'iso8601' || b === 'gregory') return a;
throw new RangeError(`cannot format range between two dates of ${a} and ${b} calendars`);
}

Expand Down
20 changes: 20 additions & 0 deletions polyfill/test/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ describe('Intl', () => {
});
it('should throw a TypeError when called with dissimilar types', () =>
throws(() => us.formatRange(Temporal.Absolute.from(t1), Temporal.DateTime.from(t2)), TypeError));
it('should throw a RangeError when called with different calendars', () => {
throws(
() => us.formatRange(Temporal.DateTime.from(t1), Temporal.DateTime.from(t2).withCalendar('japanese')),
RangeError
);
throws(
() => us.formatRange(Temporal.Date.from(t1), Temporal.Date.from(t2).withCalendar('japanese')),
RangeError
);
});
});
describe('formatRangeToParts', () => {
it('should work for Absolute', () => {
Expand Down Expand Up @@ -579,6 +589,16 @@ describe('Intl', () => {
});
it('should throw a TypeError when called with dissimilar types', () =>
throws(() => at.formatRangeToParts(Temporal.Absolute.from(t1), Temporal.DateTime.from(t2)), TypeError));
it('should throw a RangeError when called with different calendars', () => {
throws(
() => at.formatRangeToParts(Temporal.DateTime.from(t1), Temporal.DateTime.from(t2).withCalendar('japanese')),
RangeError
);
throws(
() => at.formatRangeToParts(Temporal.Date.from(t1), Temporal.Date.from(t2).withCalendar('japanese')),
RangeError
);
});
});
});
});
Expand Down

0 comments on commit 05b0d41

Please sign in to comment.