Skip to content

Commit

Permalink
Add constructNow
Browse files Browse the repository at this point in the history
Added the `constructNow` function that creates the current date using the passed reference date's constructor.
  • Loading branch information
kossnocorp committed Mar 14, 2024
1 parent 59c50f9 commit c1712d8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@
"default": "./constructFrom.mjs"
}
},
"./constructNow": {
"require": {
"types": "./constructNow.d.ts",
"default": "./constructNow.js"
},
"import": {
"types": "./constructNow.d.mts",
"default": "./constructNow.mjs"
}
},
"./daysToWeeks": {
"require": {
"types": "./daysToWeeks.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/constructFrom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { GenericDateConstructor } from "../types.js";
* date and the given value. It helps to build generic functions that accept
* date extensions.
*
* It defaults to `Date` if the passed reference date is a number or a string.
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param date - The reference date to take constructor from
Expand Down
37 changes: 37 additions & 0 deletions src/constructNow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { constructFrom } from "../index.js";

/**
* @name constructNow
* @category Generic Helpers
* @summary Constructs a new current date using the passed value constructor.
* @pure false
*
* @description
* The function constructs a new current date using the constructor from
* the reference date. It helps to build generic functions that accept date
* extensions and use the current date.
*
* It defaults to `Date` if the passed reference date is a number or a string.
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param date - The reference date to take constructor from
*
* @returns Current date initialized using the given date constructor
*
* @example
* import { constructNow, isSameDay } from 'date-fns'
*
* function isToday<DateType extends Date>(
* date: DateType | number | string,
* ): boolean {
* // If we were to use `new Date()` directly, the function would behave
* // differently in different timezones and return false for the same date.
* return isSameDay(date, constructNow(date));
* }
*/
export function constructNow<DateType extends Date>(
date: DateType | number | string,
): DateType {
return constructFrom(date, Date.now());
}
38 changes: 38 additions & 0 deletions src/constructNow/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import assert from "assert";
import { describe, it } from "vitest";
import { constructNow } from ".";

describe("constructNow", () => {
it("creates a new Date instance using the constructor from the reference date", () => {
const result = constructNow(new Date("2023-10-25T12:00:00"));

assert.ok(result instanceof Date);
assert(+result - Date.now() < 10); // Give 10 ms of slack
assert.strictEqual(result.constructor, Date);
});

it("creates a new Date instance using a number as the reference date", () => {
const result = constructNow(1635158400000);

assert.ok(result instanceof Date);
assert(+result - Date.now() < 10); // Give 10 ms of slack
assert.strictEqual(result.constructor, Date);
});

it("creates a new Date instance using a string as the reference date", () => {
const result = constructNow("2023-10-25T12:00:00");

assert.ok(result instanceof Date);
assert(+result - Date.now() < 10); // Give 10 ms of slack
assert.strictEqual(result.constructor, Date);
});

it("creates a new custom Date instance using the constructor from the reference date", () => {
class CustomDate extends Date {}
const result = constructNow(new CustomDate("2023-10-26T12:00:00"));

assert.ok(result instanceof CustomDate);
assert(+result - Date.now() < 10); // Give 10 ms of slack
assert.strictEqual(result.constructor, CustomDate);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from "./closestTo/index.js";
export * from "./compareAsc/index.js";
export * from "./compareDesc/index.js";
export * from "./constructFrom/index.js";
export * from "./constructNow/index.js";
export * from "./daysToWeeks/index.js";
export * from "./differenceInBusinessDays/index.js";
export * from "./differenceInCalendarDays/index.js";
Expand Down
1 change: 1 addition & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"./src/compareAsc/index.ts",
"./src/compareDesc/index.ts",
"./src/constructFrom/index.ts",
"./src/constructNow/index.ts",
"./src/daysToWeeks/index.ts",
"./src/differenceInBusinessDays/index.ts",
"./src/differenceInCalendarDays/index.ts",
Expand Down

0 comments on commit c1712d8

Please sign in to comment.