parse.test.ts
This file
Path
test/parse.test.ts
117 lines · 4.5 KiB · TypeScript
Last changed by
parse-iso-weekanswered by oscarbf199ee
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import { describe, expect, test } from "bun:test";
import { isValid, parseISO } from "../src/index.ts";
const iso = (text: string) => parseISO(text).toISOString();
describe("parseISO", () => { test("reads a calendar date as the UTC midnight it starts at", () => { expect(iso("2026-10-07")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("20261007")).toBe("2026-10-07T00:00:00.000Z"); });
test("reads a year and month as the month's first day, and a year as January 1", () => { expect(iso("2026-10")).toBe("2026-10-01T00:00:00.000Z"); expect(iso("2026")).toBe("2026-01-01T00:00:00.000Z"); });
test("reads an ordinal date as the nth day of the year", () => { expect(iso("2026-280")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("2026280")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("2028-366")).toBe("2028-12-31T00:00:00.000Z"); });
test("reads a week date as the UTC midnight of its day, and a week as its Monday", () => { // Week 41 of 2026 is Monday, October 5 to Sunday, October 11. expect(iso("2026-W41-3")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("2026W413")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("2026-W41")).toBe("2026-10-05T00:00:00.000Z"); expect(iso("2026W41")).toBe("2026-10-05T00:00:00.000Z"); expect(iso("2026-W41-7")).toBe("2026-10-11T00:00:00.000Z"); });
test("puts week 1 around the year's first Thursday, so it can start in the year before", () => { expect(iso("2026-W01-1")).toBe("2025-12-29T00:00:00.000Z"); expect(iso("2021-W01-1")).toBe("2021-01-04T00:00:00.000Z"); // 2020 starts on a Wednesday and is a leap year, so it has 53 weeks. expect(iso("2020-W53-7")).toBe("2021-01-03T00:00:00.000Z"); });
test("gives a week date the same time and offset as a calendar date", () => { expect(iso("2026-W41-3T14:30Z")).toBe("2026-10-07T14:30:00.000Z"); expect(iso("2026-W41-3T09:30:00+02:00")).toBe("2026-10-07T07:30:00.000Z"); expect(iso("2026-W41T14:30")).toBe("2026-10-05T14:30:00.000Z"); });
test("reads week 53 only in a year with 53 weeks", () => { // 2026 starts on a Thursday, so it has 53 weeks; 2027 starts on a Friday and has 52. expect(iso("2026-W53-7")).toBe("2027-01-03T00:00:00.000Z"); expect(isValid(parseISO("2027-W53-1"))).toBe(false); expect(isValid(parseISO("2027-W53"))).toBe(false); expect(isValid(parseISO("2026-W54-1"))).toBe(false); });
test("reads a time with no offset as UTC", () => { expect(iso("2026-10-07T14:30")).toBe("2026-10-07T14:30:00.000Z"); expect(iso("2026-10-07T14")).toBe("2026-10-07T14:00:00.000Z"); expect(iso("2026-10-07T14:30:15.250")).toBe("2026-10-07T14:30:15.250Z"); });
test("takes a UTC offset off the time", () => { expect(iso("2026-10-07T09:30:00+09:00")).toBe("2026-10-07T00:30:00.000Z"); expect(iso("2026-10-07T09:30:00+0900")).toBe("2026-10-07T00:30:00.000Z"); expect(iso("2026-10-06T20:30:00-03:30")).toBe("2026-10-07T00:00:00.000Z"); expect(iso("2026-10-07T14:30:00Z")).toBe("2026-10-07T14:30:00.000Z"); expect(iso("2026-10-07T00:00+05")).toBe("2026-10-06T19:00:00.000Z"); });
test("drops digits past the millisecond", () => { expect(iso("2026-10-07T14:30:15.123456789Z")).toBe("2026-10-07T14:30:15.123Z"); expect(iso("2026-10-07T14:30:15,5Z")).toBe("2026-10-07T14:30:15.500Z"); });
test("reads February 29 only in a leap year", () => { expect(iso("2028-02-29")).toBe("2028-02-29T00:00:00.000Z"); expect(isValid(parseISO("2026-02-29"))).toBe(false); expect(isValid(parseISO("2100-02-29"))).toBe(false); expect(iso("2000-02-29")).toBe("2000-02-29T00:00:00.000Z"); });
test("reads the years before 100 as written", () => { expect(parseISO("0042-03-01").getUTCFullYear()).toBe(42); });
test("gives an invalid date for text it can't read, and never throws", () => { for (const text of [ "", "next tuesday", "2026-13-01", "2026-10-32", "2026-00-10", "2026-367", "2026-W00-1", "2026-W41-0", "2026-W41-8", "2026-W41-", "2026-W41-33", "2026-W4-3", "2026-w41-3", "2026-W413", "2026W41-3", "2026-10-07T24:00", "2026-10-07T14:60", "2026-10-07T14:30:00+24:00", "2026-10-07T14:30T15:00", "26-10-07", ]) { expect(isValid(parseISO(text)), text).toBe(false); } });});
describe("isValid", () => { test("tells a real instant from an invalid date", () => { expect(isValid(new Date(0))).toBe(true); expect(isValid(new Date(Number.NaN))).toBe(false); });});