Skip to content

Commit d51828d

Browse files
authored
Duration.toJSON() should not print unwanted zeroes (#1376)
Due to a missing argument, Temporal.Duration.from({ hours: 1 }).toJSON() would return "PT1H0.0S" instead of "PT1H". The same goes for toLocaleString() in the absence of Intl.DurationFormat. No change needed to the spec text or documentation, which are already correct.
1 parent 0949091 commit d51828d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ export const ES = ObjectAssign({}, ES2020, {
17021702
const timeZoneString = timeZone === undefined ? 'Z' : ES.GetOffsetStringFor(outputTimeZone, instant);
17031703
return `${year}-${month}-${day}T${hour}:${minute}${seconds}${timeZoneString}`;
17041704
},
1705-
TemporalDurationToString: (duration, precision, options = undefined) => {
1705+
TemporalDurationToString: (duration, precision = 'auto', options = undefined) => {
17061706
function formatNumber(num) {
17071707
if (num <= NumberMaxSafeInteger) return num.toString(10);
17081708
return bigInt(num).toString();

polyfill/test/duration.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ describe('Duration', () => {
327327
[{}, () => {}, undefined].forEach((options) => equal(d1.toString(options), 'PT15H23M'));
328328
});
329329
});
330+
describe('toJSON()', () => {
331+
it('is like toString but always with auto precision', () => {
332+
const d = Duration.from({ hours: 1 });
333+
equal(d.toJSON(), d.toString({ fractionalSecondDigits: 'auto' }));
334+
});
335+
});
330336
describe('toLocaleString()', () => {
331337
it('produces an implementation-defined string', () => {
332338
const duration = Duration.from({ hours: 12, minutes: 30 });

0 commit comments

Comments
 (0)