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
1import { 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 week date as the UTC midnight of its day, and a week as its Monday", () => {25 // Week 41 of 2026 is Monday, October 5 to Sunday, October 11.26 expect(iso("2026-W41-3")).toBe("2026-10-07T00:00:00.000Z");27 expect(iso("2026W413")).toBe("2026-10-07T00:00:00.000Z");28 expect(iso("2026-W41")).toBe("2026-10-05T00:00:00.000Z");29 expect(iso("2026W41")).toBe("2026-10-05T00:00:00.000Z");30 expect(iso("2026-W41-7")).toBe("2026-10-11T00:00:00.000Z");31 });3233 test("puts week 1 around the year's first Thursday, so it can start in the year before", () => {34 expect(iso("2026-W01-1")).toBe("2025-12-29T00:00:00.000Z");35 expect(iso("2021-W01-1")).toBe("2021-01-04T00:00:00.000Z");36 // 2020 starts on a Wednesday and is a leap year, so it has 53 weeks.37 expect(iso("2020-W53-7")).toBe("2021-01-03T00:00:00.000Z");38 });3940 test("gives a week date the same time and offset as a calendar date", () => {41 expect(iso("2026-W41-3T14:30Z")).toBe("2026-10-07T14:30:00.000Z");42 expect(iso("2026-W41-3T09:30:00+02:00")).toBe("2026-10-07T07:30:00.000Z");43 expect(iso("2026-W41T14:30")).toBe("2026-10-05T14:30:00.000Z");44 });4546 test("reads week 53 only in a year with 53 weeks", () => {47 // 2026 starts on a Thursday, so it has 53 weeks; 2027 starts on a Friday and has 52.48 expect(iso("2026-W53-7")).toBe("2027-01-03T00:00:00.000Z");49 expect(isValid(parseISO("2027-W53-1"))).toBe(false);50 expect(isValid(parseISO("2027-W53"))).toBe(false);51 expect(isValid(parseISO("2026-W54-1"))).toBe(false);52 });5354 test("reads a time with no offset as UTC", () => {55 expect(iso("2026-10-07T14:30")).toBe("2026-10-07T14:30:00.000Z");56 expect(iso("2026-10-07T14")).toBe("2026-10-07T14:00:00.000Z");57 expect(iso("2026-10-07T14:30:15.250")).toBe("2026-10-07T14:30:15.250Z");58 });5960 test("takes a UTC offset off the time", () => {61 expect(iso("2026-10-07T09:30:00+09:00")).toBe("2026-10-07T00:30:00.000Z");62 expect(iso("2026-10-07T09:30:00+0900")).toBe("2026-10-07T00:30:00.000Z");63 expect(iso("2026-10-06T20:30:00-03:30")).toBe("2026-10-07T00:00:00.000Z");64 expect(iso("2026-10-07T14:30:00Z")).toBe("2026-10-07T14:30:00.000Z");65 expect(iso("2026-10-07T00:00+05")).toBe("2026-10-06T19:00:00.000Z");66 });6768 test("drops digits past the millisecond", () => {69 expect(iso("2026-10-07T14:30:15.123456789Z")).toBe("2026-10-07T14:30:15.123Z");70 expect(iso("2026-10-07T14:30:15,5Z")).toBe("2026-10-07T14:30:15.500Z");71 });7273 test("reads February 29 only in a leap year", () => {74 expect(iso("2028-02-29")).toBe("2028-02-29T00:00:00.000Z");75 expect(isValid(parseISO("2026-02-29"))).toBe(false);76 expect(isValid(parseISO("2100-02-29"))).toBe(false);77 expect(iso("2000-02-29")).toBe("2000-02-29T00:00:00.000Z");78 });7980 test("reads the years before 100 as written", () => {81 expect(parseISO("0042-03-01").getUTCFullYear()).toBe(42);82 });8384 test("gives an invalid date for text it can't read, and never throws", () => {85 for (const text of [86 "",87 "next tuesday",88 "2026-13-01",89 "2026-10-32",90 "2026-00-10",91 "2026-367",92 "2026-W00-1",93 "2026-W41-0",94 "2026-W41-8",95 "2026-W41-",96 "2026-W41-33",97 "2026-W4-3",98 "2026-w41-3",99 "2026-W413",100 "2026W41-3",101 "2026-10-07T24:00",102 "2026-10-07T14:60",103 "2026-10-07T14:30:00+24:00",104 "2026-10-07T14:30T15:00",105 "26-10-07",106 ]) {107 expect(isValid(parseISO(text)), text).toBe(false);108 }109 });110});111112describe("isValid", () => {113 test("tells a real instant from an invalid date", () => {114 expect(isValid(new Date(0))).toBe(true);115 expect(isValid(new Date(Number.NaN))).toBe(false);116 });117});