Skip to content

Commit

Permalink
Polyfill: Implement DurationFormat.p.format{,ToParts} accepting Duration
Browse files Browse the repository at this point in the history
An additional modification to ECMA-402 that Temporal makes, is for
DurationFormat.p.format and formatToParts to accept a Temporal.Duration
object as input. This monkeypatches those methods to conform to that
requirement.
  • Loading branch information
ptomato committed Feb 13, 2025
1 parent 298ef27 commit 0b11e62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IntlDateTimeFormatPrototypeResolvedOptions,
IntlDurationFormatPrototype,
IntlDurationFormatPrototypeFormat,
IntlDurationFormatPrototypeFormatToParts,
IntlDurationFormatPrototypeResolvedOptions,
ObjectAssign,
ObjectCreate,
Expand Down Expand Up @@ -667,3 +668,13 @@ export function ModifiedIntlDurationFormatPrototypeFormat(durationLike) {
const record = temporalDurationToCompatibilityRecord(duration);
return ES.Call(IntlDurationFormatPrototypeFormat, this, [record]);
}

if (IntlDurationFormatPrototype) {
IntlDurationFormatPrototype.format = ModifiedIntlDurationFormatPrototypeFormat;
IntlDurationFormatPrototype.formatToParts = function formatToParts(durationLike) {
ES.Call(IntlDurationFormatPrototypeResolvedOptions, this); // brand check
const duration = ES.ToTemporalDuration(durationLike);
const record = temporalDurationToCompatibilityRecord(duration);
return ES.Call(IntlDurationFormatPrototypeFormatToParts, this, [record]);
};
}
1 change: 1 addition & 0 deletions polyfill/lib/primordials.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const {
export const IntlDurationFormatPrototype = IntlDurationFormat?.prototype ?? ObjectCreate(null);
export const {
format: IntlDurationFormatPrototypeFormat,
formatToParts: IntlDurationFormatPrototypeFormatToParts,
resolvedOptions: IntlDurationFormatPrototypeResolvedOptions
} = IntlDurationFormatPrototype;
export const { stringify: JSONStringify } = JSON;
Expand Down

0 comments on commit 0b11e62

Please # to comment.