feat: add date comparison utils

This commit is contained in:
Lukas Obermann
2024-03-02 13:51:46 +01:00
parent 123bb30999
commit e9e1e5e0c1
2 changed files with 32 additions and 0 deletions
+26
View File
@@ -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,
)
})
})
+6
View File
@@ -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()