feat: add date comparison utils
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import assert from "node:assert/strict"
|
||||
import { describe, it } from "node:test"
|
||||
import { compareDate } from "./date.ts"
|
||||
|
||||
describe("compareDate", () => {
|
||||
it("returns a negative value if the first date is earlier than the second date", () => {
|
||||
assert.equal(
|
||||
compareDate(new Date(2000, 0, 1, 12, 0, 0, 100), new Date(2000, 0, 1, 12, 0, 0, 200)),
|
||||
-100,
|
||||
)
|
||||
})
|
||||
|
||||
it("returns a positive value if the first date is later than the second date", () => {
|
||||
assert.equal(
|
||||
compareDate(new Date(2000, 0, 1, 12, 0, 0, 300), new Date(2000, 0, 1, 12, 0, 0, 200)),
|
||||
100,
|
||||
)
|
||||
})
|
||||
|
||||
it("returns zero if the first date is equal to the second date", () => {
|
||||
assert.equal(
|
||||
compareDate(new Date(2000, 0, 1, 12, 0, 0, 200), new Date(2000, 0, 1, 12, 0, 0, 200)),
|
||||
0,
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Compare } from "./compare.ts"
|
||||
|
||||
/**
|
||||
* A comparator function for {@link Date} objects in ascending order.
|
||||
*/
|
||||
export const compareDate: Compare<Date> = (a, b) => a.getTime() - b.getTime()
|
||||
Reference in New Issue
Block a user