parse.test.ts
This file
Path
examples/datekit/test/parse.test.ts
78 lines · 2.8 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–78why, one step back1import { describe, expect, test } from "bun:test";23import { isValid, parseISO } from "../src/index.ts";45const iso = (text: string) => parseISO(text).toISOString();67describe("parseISO", () => {8 test("reads a calendar date as the UTC midnight it starts at", () => {9 expect(iso("2026-10-07")).toBe("2026-10-07T00:00:00.000Z");10 expect(iso("20261007")).toBe("2026-10-07T00:00:00.000Z");11 });1213 test("reads a year and month as the month's first day, and a year as January 1", () => {14 expect(iso("2026-10")).toBe("2026-10-01T00:00:00.000Z");15 expect(iso("2026")).toBe("2026-01-01T00:00:00.000Z");16 });1718 test("reads an ordinal date as the nth day of the year", () => {19 expect(iso("2026-280")).toBe("2026-10-07T00:00:00.000Z");20 expect(iso("2026280")).toBe("2026-10-07T00:00:00.000Z");21 expect(iso("2028-366")).toBe("2028-12-31T00:00:00.000Z");22 });2324 test("reads a time with no offset as UTC", () => {25 expect(iso("2026-10-07T14:30")).toBe("2026-10-07T14:30:00.000Z");26 expect(iso("2026-10-07T14")).toBe("2026-10-07T14:00:00.000Z");27 expect(iso("2026-10-07T14:30:15.250")).toBe("2026-10-07T14:30:15.250Z");28 });2930 test("takes a UTC offset off the time", () => {31 expect(iso("2026-10-07T09:30:00+09:00")).toBe("2026-10-07T00:30:00.000Z");32 expect(iso("2026-10-07T09:30:00+0900")).toBe("2026-10-07T00:30:00.000Z");33 expect(iso("2026-10-06T20:30:00-03:30")).toBe("2026-10-07T00:00:00.000Z");34 expect(iso("2026-10-07T14:30:00Z")).toBe("2026-10-07T14:30:00.000Z");35 expect(iso("2026-10-07T00:00+05")).toBe("2026-10-06T19:00:00.000Z");36 });3738 test("drops digits past the millisecond", () => {39 expect(iso("2026-10-07T14:30:15.123456789Z")).toBe("2026-10-07T14:30:15.123Z");40 expect(iso("2026-10-07T14:30:15,5Z")).toBe("2026-10-07T14:30:15.500Z");41 });4243 test("reads February 29 only in a leap year", () => {44 expect(iso("2028-02-29")).toBe("2028-02-29T00:00:00.000Z");45 expect(isValid(parseISO("2026-02-29"))).toBe(false);46 expect(isValid(parseISO("2100-02-29"))).toBe(false);47 expect(iso("2000-02-29")).toBe("2000-02-29T00:00:00.000Z");48 });4950 test("reads the years before 100 as written", () => {51 expect(parseISO("0042-03-01").getUTCFullYear()).toBe(42);52 });5354 test("gives an invalid date for text it can't read, and never throws", () => {55 for (const text of [56 "",57 "next tuesday",58 "2026-13-01",59 "2026-10-32",60 "2026-00-10",61 "2026-367",62 "2026-10-07T24:00",63 "2026-10-07T14:60",64 "2026-10-07T14:30:00+24:00",65 "2026-10-07T14:30T15:00",66 "26-10-07",67 ]) {68 expect(isValid(parseISO(text)), text).toBe(false);69 }70 });71});7273describe("isValid", () => {74 test("tells a real instant from an invalid date", () => {75 expect(isValid(new Date(0))).toBe(true);76 expect(isValid(new Date(Number.NaN))).toBe(false);77 });78});