UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

33 lines (31 loc) • 1.27 kB
import { Temporal } from "../../internal/temporal.js"; import { plainYearMonthOf } from "../../internal/inputChecks.js"; // src/plain/compare/compareYearMonths.ts /** * Compare two year-month strings (`YYYY-MM`) in calendar order: year first, then month. * * - Accepts both year forms, so `2026-10` and `+002026-10` compare equal. * - Negative years sort before year 0: `-000001-12` is before `0000-01`. * - Returns `null` if either input is invalid or not in the ISO calendar. * * @param a year-month string * @param b year-month string * @returns -1, 0, or 1, or null on invalid input * * @example compareYearMonths("2026-09", "2026-10") // -1 * @example compareYearMonths("+002026-10", "2026-10") // 0 * @example compareYearMonths("-000001-12", "0000-01") // -1 * @example compareYearMonths("2026-13", "2026-10") // null * @example ["2026-10", "2025-12", "2026-01"].sort((a, b) => compareYearMonths(a, b) ?? 0) // ["2025-12", "2026-01", "2026-10"] (validate every entry first) */ function compareYearMonths(a, b) { const first = plainYearMonthOf(a); const second = plainYearMonthOf(b); if (first === null || second === null) return null; try { return Temporal.PlainYearMonth.compare(first, second); } catch { return null; } } export { compareYearMonths };