Skip to content

Commit

Permalink
feat: add RelativeTimeFormat to Intl API (#479)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl-Erik Kopseng <carlerik@gmail.com>
  • Loading branch information
sebastinez and fatso83 committed Oct 5, 2023
1 parent fc312b9 commit 9b2b474
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,24 @@ function withGlobal(_global) {
return mirrorDateProperties(ClockDate, NativeDate);
}

//eslint-disable-next-line jsdoc/require-jsdoc
/**
* Mirror Intl by default on our fake implementation
*
* Most of the properties are the original native ones,
* but we need to take control of those that have a
* dependency on the current clock.
*
* @returns {object} the partly fake Intl implementation
*/
function createIntl() {
const ClockIntl = { ...NativeIntl };
const ClockIntl = {};
/*
* All properties of Intl are non-enumerable, so we need
* to do a bit of work to get them out.
*/
Object.getOwnPropertyNames(NativeIntl).forEach(
(property) => (ClockIntl[property] = NativeIntl[property])
);

ClockIntl.DateTimeFormat = function (...args) {
const realFormatter = new NativeIntl.DateTimeFormat(...args);
Expand Down Expand Up @@ -1133,6 +1148,17 @@ function withGlobal(_global) {
return [secsSinceStart, remainderInNanos];
}

/**
* A high resolution timestamp in milliseconds.
*
* @typedef {number} DOMHighResTimeStamp
*/

/**
* performance.now()
*
* @returns {DOMHighResTimeStamp}
*/
function fakePerformanceNow() {
const hrt = hrtime();
const millis = hrt[0] * 1000 + hrt[1] / 1e6;
Expand Down
15 changes: 13 additions & 2 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4688,7 +4688,7 @@ describe("FakeTimers", function () {
* @template E
* @param {E[]} [list1]
* @param {E[]} [list2]
* @return {E[]}
* @returns {E[]}
*/
function getIntersection(list1, list2) {
return list1.filter((value) => list2.indexOf(value) !== -1);
Expand All @@ -4699,7 +4699,7 @@ describe("FakeTimers", function () {
*
* @function
* @param {string[]} [toFake]
* @return {{propertyName: string, originalValue: any}[]}
* @returns {{propertyName: string, originalValue: any}[]}
*/
function getOriginals(toFake) {
return toFake.map((propertyName) => ({
Expand Down Expand Up @@ -5300,4 +5300,15 @@ describe("Intl API", function () {
clock._Intl.DateTimeFormat.supportedLocalesOf()
);
});

it("Creates a RelativeTimeFormat like normal", function () {
if (typeof Intl?.RelativeTimeFormat === "undefined") {
this.skip();
}

const rtf = new Intl.RelativeTimeFormat("en-GB", {
numeric: "auto",
});
assert.equals(rtf.format(2, "day"), "in 2 days");
});
});

0 comments on commit 9b2b474

Please sign in to comment.