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 689210d commit b1a5eb5
Show file tree
Hide file tree
Showing 28 changed files with 577 additions and 1,664 deletions.
2,066 changes: 435 additions & 1,631 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
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
3 changes: 2 additions & 1 deletion src/formatDistanceToNow/index.ts
@@ -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
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { FormatDistanceFn } from "../locale/types.js";
import { formatDistanceToNow } from "./index.js";

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

it("respects date extensions", () => {
const result = formatDistanceToNow(
new UTCDate(+new Date(1986, 3, 4, 9, 32, 0)),
);
expect(result).toBe("about 1 hour");
});
});
3 changes: 2 additions & 1 deletion src/formatDistanceToNowStrict/index.ts
@@ -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);
}
14 changes: 12 additions & 2 deletions src/formatDistanceToNowStrict/test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { FormatDistanceFn } from "../locale/types.js";
import { formatDistanceToNowStrict } from "./index.js";

Expand Down Expand Up @@ -366,6 +367,15 @@ describe("formatDistanceToNowStrict", () => {
});

it("throws `RangeError` if the date is `Invalid Date`", () => {
expect(formatDistanceToNowStrict.bind(null, new Date(NaN))).toThrow(RangeError);
expect(formatDistanceToNowStrict.bind(null, new Date(NaN))).toThrow(
RangeError,
);
});

it("respects date extensions", () => {
const result = formatDistanceToNowStrict(
new UTCDate(+new Date(1986, 3, 4, 10, 32, 5)),
);
expect(result).toBe("5 seconds");
});
});
3 changes: 2 additions & 1 deletion src/isThisHour/index.ts
@@ -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));
}
9 changes: 8 additions & 1 deletion src/isThisHour/test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isThisHour } from "./index.js";

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

it("respects date extensions", () => {
expect(
isThisHour(new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 45))),
).toBe(true);
});
});
3 changes: 2 additions & 1 deletion src/isThisISOWeek/index.ts
@@ -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));
}
9 changes: 8 additions & 1 deletion src/isThisISOWeek/test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isThisISOWeek } from "./index.js";

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

it("respects date extensions", () => {
expect(isThisISOWeek(new UTCDate(+new Date(2014, 8 /* Sep */, 25)))).toBe(
true,
);
});
});
3 changes: 2 additions & 1 deletion src/isThisMinute/index.ts
@@ -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
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isThisMinute } from "./index.js";

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

it("respects date extensions", () => {
expect(
isThisMinute(
new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500)),
),
).toBe(true);
});
});
3 changes: 2 additions & 1 deletion src/isThisMonth/index.ts
@@ -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));
}
11 changes: 9 additions & 2 deletions src/isThisMonth/test.ts
@@ -1,11 +1,12 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, 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 @@ -26,4 +27,10 @@ describe("isThisMonth", () => {
const date = new Date(2014, 8 /* Sep */, 30).getTime();
expect(isThisMonth(date)).toBe(true);
});

it("respects date extensions", () => {
expect(isThisMonth(new UTCDate(+new Date(2014, 8 /* Sep */, 1)))).toBe(
true,
);
});
});
3 changes: 2 additions & 1 deletion src/isThisQuarter/index.ts
@@ -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));
}
11 changes: 9 additions & 2 deletions src/isThisQuarter/test.ts
@@ -1,11 +1,12 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, 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 @@ -26,4 +27,10 @@ describe("isThisQuarter", () => {
const date = new Date(2014, 6 /* Jul */, 2).getTime();
expect(isThisQuarter(date)).toBe(true);
});

it("respects date extensions", () => {
expect(isThisQuarter(new UTCDate(+new Date(2014, 6 /* Jul */, 1)))).toBe(
true,
);
});
});
3 changes: 2 additions & 1 deletion src/isThisSecond/index.ts
@@ -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
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isThisSecond } from "./index.js";

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

it("respects date extensions", () => {
expect(
isThisSecond(
new UTCDate(+new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500)),
),
).toBe(true);
});
});
3 changes: 2 additions & 1 deletion src/isThisWeek/index.ts
@@ -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);
}
15 changes: 11 additions & 4 deletions src/isThisWeek/test.ts
@@ -1,11 +1,12 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, 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 @@ -23,12 +24,18 @@ describe("isThisWeek", () => {
});

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

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

it("respects date extensions", () => {
expect(isThisWeek(new UTCDate(+new Date(2014, 8 /* Sep */, 21)))).toBe(
true,
);
});
});
3 changes: 2 additions & 1 deletion src/isThisYear/index.ts
@@ -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));
}
9 changes: 7 additions & 2 deletions src/isThisYear/test.ts
@@ -1,11 +1,12 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isThisYear } from "./index.js";

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

afterEach(() => {
Expand All @@ -26,4 +27,8 @@ describe("isThisYear", () => {
const date = new Date(2014, 6 /* Jul */, 2).getTime();
expect(isThisYear(date)).toBe(true);
});

it("respects date extensions", () => {
expect(isThisYear(new UTCDate(+new Date(2014, 0 /* Jan */, 1)))).toBe(true);
});
});
3 changes: 2 additions & 1 deletion src/isToday/index.ts
@@ -1,3 +1,4 @@
import { constructNow } from "../constructNow/index.js";
import { isSameDay } from "../isSameDay/index.js";

/**
Expand All @@ -23,5 +24,5 @@ import { isSameDay } from "../isSameDay/index.js";
export function isToday<DateType extends Date>(
date: DateType | number | string,
): boolean {
return isSameDay(date, Date.now());
return isSameDay(date, constructNow(date));
}
7 changes: 6 additions & 1 deletion src/isToday/test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { isToday } from "./index.js";

describe("isToday", () => {
Expand All @@ -26,4 +27,8 @@ describe("isToday", () => {
const result = isToday(new Date(2014, 8 /* Sep */, 25).getTime());
expect(result).toBe(true);
});

it("respects date extensions", () => {
expect(isToday(new UTCDate(+new Date(2014, 8 /* Sep */, 25)))).toBe(true);
});
});

0 comments on commit b1a5eb5

Please sign in to comment.