@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.
26 lines (25 loc) • 1.25 kB
JavaScript
// src/regex/year-month.ts
/**
* RegExp matching an ISO 8601 year-month in the extended form: `YYYY-MM` or `±YYYYYY-MM`.
*
* - The year is the `year` regex: Temporal's `DateYear`, four digits or a sign and six digits,
* with `-000000` refused (its early error).
* - The extended form only. `202610` does not match, as `plainDate` refuses `20241225`.
* - The shape only: the range (`-271821-04` to `+275760-09`) is Temporal's, checked by
* `isValidYearMonth`.
*
* Capture groups: none.
*
* The whole string, with no RFC 9557 annotation of any kind: `isValidYearMonth` matches this against the
* entire value, so a time zone, a calendar (`"…[u-ca=iso8601]"`) or an elective (`"…[foo=bar]"`)
* annotation makes the value invalid.
*
* @example plainYearMonth.test("2026-10") // true
* @example plainYearMonth.test("+275760-09") // true
* @example plainYearMonth.test("+999999-12") // true (shape only; outside Temporal's range)
* @example plainYearMonth.test("202610") // false (basic format)
* @example plainYearMonth.test("2026-13") // false (month 13)
* @example plainYearMonth.test("2026-10-01") // false (a full date)
*/
const plainYearMonth = /^(?:\d{4}|\+\d{6}|-(?!0{6})\d{6})-(?:0[1-9]|1[0-2])$/;
export { plainYearMonth };