Skip to content

Commit

Permalink
Make date extensions work with now functions
Browse files Browse the repository at this point in the history
Fixed functions that use current date internally and made them work with date extensions like `UTCDate`.
  • Loading branch information
kossnocorp committed Mar 14, 2024
1 parent c1712d8 commit 572db9f
Show file tree
Hide file tree
Showing 28 changed files with 133 additions and 33 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6092,6 +6092,7 @@
"@babel/preset-env": "^7.22.10",
"@babel/preset-typescript": "^7.22.5",
"@date-fns/docs": "^0.29.0",
"@date-fns/utc": "^1.2.0",
"@octokit/core": "^3.2.5",
"@size-limit/esbuild": "^11.0.1",
"@size-limit/file": "^11.0.1",
Expand Down Expand Up @@ -6123,4 +6124,4 @@
"typescript": "^5.3.2",
"vitest": "^0.34.6"
}
}
}
3 changes: 2 additions & 1 deletion src/formatDistanceToNow/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FormatDistanceOptions } from "../formatDistance/index.js";
import { formatDistance } from "../formatDistance/index.js";
import { constructNow } from "../index.js";

/**
* The {@link formatDistanceToNow} function options.
Expand Down Expand Up @@ -93,5 +94,5 @@ export function formatDistanceToNow<DateType extends Date>(
date: DateType | number | string,
options?: FormatDistanceToNowOptions,
): string {
return formatDistance(date, Date.now(), options);
return formatDistance(date, constructNow(date), options);
}
10 changes: 9 additions & 1 deletion src/formatDistanceToNow/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import type { FormatDistanceFn } from "../locale/types.js";
import { formatDistanceToNow } from "./index.js";

Expand Down Expand Up @@ -192,4 +193,11 @@ describe("formatDistanceToNow", () => {
it("throws RangeError if the passed date is `Invalid Date`", function () {
assert.throws(formatDistanceToNow.bind(null, new Date(NaN)), RangeError);
});

it("respects date extensions", () => {
const result = formatDistanceToNow(
new UTCDate(+new Date(1986, 3, 4, 9, 32, 0)),
);
assert(result === "about 1 hour");
});
});
3 changes: 2 additions & 1 deletion src/formatDistanceToNowStrict/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FormatDistanceStrictOptions } from "../formatDistanceStrict/index.js";
import { formatDistanceStrict } from "../formatDistanceStrict/index.js";
import { constructNow } from "../index.js";

/**
* The {@link formatDistanceToNowStrict} function options.
Expand Down Expand Up @@ -84,5 +85,5 @@ export function formatDistanceToNowStrict<DateType extends Date>(
date: DateType | number | string,
options?: FormatDistanceToNowStrictOptions,
): string {
return formatDistanceStrict(date, Date.now(), options);
return formatDistanceStrict(date, constructNow(date), options);
}
10 changes: 9 additions & 1 deletion src/formatDistanceToNowStrict/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { afterEach, beforeEach, describe, it } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import type { FormatDistanceFn } from "../locale/types.js";
import { formatDistanceToNowStrict } from "./index.js";

Expand Down Expand Up @@ -374,4 +375,11 @@ describe("formatDistanceToNowStrict", () => {
RangeError,
);
});

it("respects date extensions", () => {
const result = formatDistanceToNowStrict(
new UTCDate(+new Date(1986, 3, 4, 10, 32, 5)),
);
assert(result === "5 seconds");
});
});
3 changes: 2 additions & 1 deletion src/isThisHour/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameHour } from "../isSameHour/index.js";

/**
Expand All @@ -24,5 +25,5 @@ import { isSameHour } from "../isSameHour/index.js";
export function isThisHour<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameHour(Date.now(), date);
return isSameHour(date, constructNow(date));
}
7 changes: 6 additions & 1 deletion src/isThisHour/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { afterEach, beforeEach, describe, it } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisHour } from "./index.js";

describe("isThisHour", () => {
Expand Down Expand Up @@ -31,4 +32,8 @@ describe("isThisHour", () => {
const date = new Date(2014, 8 /* Sep */, 25, 18, 45).getTime();
assert(isThisHour(date) === true);
});

it("respects date extensions", () => {
assert(isThisHour(new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 45))));
});

Check failure on line 38 in src/isThisHour/test.ts

View workflow job for this annotation

GitHub Actions / tests

AssertionError: false == true

at /home/runner/work/date-fns/date-fns/src/isThisHour/test.ts:38:1

Check failure on line 38 in src/isThisHour/test.ts

View workflow job for this annotation

GitHub Actions / tests

AssertionError: false == true

at /home/runner/work/date-fns/date-fns/src/isThisHour/test.ts:38:1
});
3 changes: 2 additions & 1 deletion src/isThisISOWeek/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameISOWeek } from "../isSameISOWeek/index.js";

/**
Expand Down Expand Up @@ -26,5 +27,5 @@ import { isSameISOWeek } from "../isSameISOWeek/index.js";
export function isThisISOWeek<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameISOWeek(date, Date.now());
return isSameISOWeek(date, constructNow(date));
}
7 changes: 6 additions & 1 deletion src/isThisISOWeek/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisISOWeek } from "./index.js";

describe("isSameISOWeek", () => {
Expand All @@ -29,4 +30,8 @@ describe("isSameISOWeek", () => {
const date = new Date(2014, 8 /* Sep */, 29).getTime();
assert(isThisISOWeek(date) === false);
});

it("respects date extensions", () => {
assert(isThisISOWeek(new UTCDate(+new Date(2014, 8 /* Sep */, 25))));
});
});
3 changes: 2 additions & 1 deletion src/isThisMinute/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameMinute } from "../isSameMinute/index.js";

/**
Expand Down Expand Up @@ -25,5 +26,5 @@ import { isSameMinute } from "../isSameMinute/index.js";
export function isThisMinute<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameMinute(Date.now(), date);
return isSameMinute(date, constructNow(date));
}
11 changes: 10 additions & 1 deletion src/isThisMinute/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisMinute } from "./index.js";

describe("isThisMinute", () => {
Expand Down Expand Up @@ -31,4 +32,12 @@ describe("isThisMinute", () => {
const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 30).getTime();
assert(isThisMinute(date) === true);
});

it("respects date extensions", () => {
assert(
isThisMinute(
new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500)),
),
);
});
});
3 changes: 2 additions & 1 deletion src/isThisMonth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameMonth } from "../isSameMonth/index.js";

/**
Expand All @@ -24,5 +25,5 @@ import { isSameMonth } from "../isSameMonth/index.js";
export function isThisMonth<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameMonth(Date.now(), date);
return isSameMonth(date, constructNow(date));
}
9 changes: 7 additions & 2 deletions src/isThisMonth/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisMonth } from "./index.js";

describe("isThisMonth", () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime());
clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 1).getTime());
});

afterEach(() => {
Expand All @@ -29,4 +30,8 @@ describe("isThisMonth", () => {
const date = new Date(2014, 8 /* Sep */, 30).getTime();
assert(isThisMonth(date) === true);
});

it("respects date extensions", () => {
assert(isThisMonth(new UTCDate(+new Date(2014, 8 /* Sep */, 1))));
});
});
3 changes: 2 additions & 1 deletion src/isThisQuarter/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameQuarter } from "../isSameQuarter/index.js";

/**
Expand All @@ -23,5 +24,5 @@ import { isSameQuarter } from "../isSameQuarter/index.js";
export function isThisQuarter<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameQuarter(Date.now(), date);
return isSameQuarter(date, constructNow(date));
}
9 changes: 7 additions & 2 deletions src/isThisQuarter/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisQuarter } from "./index.js";

describe("isThisQuarter", () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime());
clock = sinon.useFakeTimers(new Date(2014, 6 /* Jul */, 1).getTime());
});

afterEach(() => {
Expand All @@ -29,4 +30,8 @@ describe("isThisQuarter", () => {
const date = new Date(2014, 6 /* Jul */, 2).getTime();
assert(isThisQuarter(date) === true);
});

it("respects date extensions", () => {
assert(isThisQuarter(new UTCDate(+new Date(2014, 6 /* Jul */, 1))));
});
});
3 changes: 2 additions & 1 deletion src/isThisSecond/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameSecond } from "../isSameSecond/index.js";

/**
Expand All @@ -24,5 +25,5 @@ import { isSameSecond } from "../isSameSecond/index.js";
export function isThisSecond<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameSecond(Date.now(), date);
return isSameSecond(date, constructNow(date));
}
11 changes: 10 additions & 1 deletion src/isThisSecond/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { afterEach, beforeEach, describe, it } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisSecond } from "./index.js";

describe("isThisSecond", () => {
Expand Down Expand Up @@ -31,4 +32,12 @@ describe("isThisSecond", () => {
const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 250).getTime();
assert(isThisSecond(date) === true);
});

it("respects date extensions", () => {
assert(
isThisSecond(
new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500)),
),
);
});
});
3 changes: 2 additions & 1 deletion src/isThisWeek/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameWeek } from "../isSameWeek/index.js";
import type { LocalizedOptions, WeekOptions } from "../types.js";

Expand Down Expand Up @@ -39,5 +40,5 @@ export function isThisWeek<DateType extends Date>(
date: DateType | number | string,
options?: IsThisWeekOptions,
): boolean {
return isSameWeek(date, Date.now(), options);
return isSameWeek(date, constructNow(date), options);
}
13 changes: 9 additions & 4 deletions src/isThisWeek/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-env mocha */

import { UTCDate } from "@date-fns/utc";
import assert from "node:assert";
import { describe, it, beforeEach, afterEach } from "vitest";
import sinon from "sinon";
import { afterEach, beforeEach, describe, it } from "vitest";
import { isThisWeek } from "./index.js";

describe("isThisWeek", () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime());
clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 21).getTime());
});

afterEach(() => {
Expand All @@ -26,12 +27,16 @@ describe("isThisWeek", () => {
});

it("allows to specify which day is the first day of the week", () => {
const date = new Date(2014, 8 /* Sep */, 28);
assert(isThisWeek(date, { weekStartsOn: 1 }) === true);
const date = new Date(2014, 8 /* Sep */, 22);
assert(isThisWeek(date, { weekStartsOn: 1 }) === false);
});

it("accepts a timestamp", () => {
const date = new Date(2014, 8 /* Sep */, 21).getTime();
assert(isThisWeek(date) === true);
});

it("respects date extensions", () => {
assert(isThisWeek(new UTCDate(+new Date(2014, 8 /* Sep */, 21))));
});
});
3 changes: 2 additions & 1 deletion src/isThisYear/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constructNow } from "../index.js";
import { isSameYear } from "../isSameYear/index.js";

/**
Expand All @@ -23,5 +24,5 @@ import { isSameYear } from "../isSameYear/index.js";
export function isThisYear<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameYear(date, Date.now());
return isSameYear(date, constructNow(date));
}

0 comments on commit 572db9f

Please sign in to comment.