-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the `constructNow` function that creates the current date using the passed reference date's constructor.
- Loading branch information
1 parent
59c50f9
commit c1712d8
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters