import { describe, expect, test } from "bun:test"; import { comparePartialDates, parsePartialDate } from "../src/partial-date.ts"; describe("parsePartialDate", () => { const table: [string | null | undefined, { value: string; precision: string } | null][] = [ ["2019", { value: "2019", precision: "year" }], ["2019-03", { value: "2019-03", precision: "month" }], ["2019-03-04", { value: "2019-03-04", precision: "day" }], ["2019-03-04T10:20:30Z", { value: "2019-03-04T10:20:30Z", precision: "day" }], ["2019-03-04T10:20:30.123+02:00", { value: "2019-03-04T10:20:30.123+02:00", precision: "day" }], ["03/04/2019", null], ["", null], [null, null], [undefined, null], ["2019-13", null], ["2019-02-30", null], ["garbage", null], ]; for (const [input, expected] of table) { test(`${JSON.stringify(input)}`, () => { expect(parsePartialDate(input)).toEqual(expected as never); }); } }); describe("comparePartialDates", () => { test("orders by value", () => { const y = { value: "2019", precision: "year" as const }; const m = { value: "2019-03", precision: "month" as const }; const d = { value: "2019-03-04", precision: "day" as const }; expect(comparePartialDates(y, m)).toBeLessThan(0); expect(comparePartialDates(d, m)).toBeGreaterThan(0); expect(comparePartialDates(d, d)).toBe(0); }); });