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.

190 lines (188 loc) • 6.21 kB
import { Temporal } from "./temporal.js"; import { plainDateTime } from "../regex/date-time.js"; import { isoStringBody } from "./isoStringBody.js"; import { isRememberedZone, rememberZone } from "./validatedZones.js"; import { hasSecond60 } from "../plain/validate/hasSecond60.js"; import { plainDate } from "../regex/date.js"; import { plainMonthDay } from "../regex/month-day.js"; import { plainTime } from "../regex/time.js"; import { timeZoneLike } from "../regex/time-zone-like.js"; import { plainYearMonth } from "../regex/year-month.js"; import { monthDayFrom, yearMonthFrom } from "./temporalCompat/isoCalendarFrom.js"; // src/internal/inputChecks.ts /** The cores of the `isValid*` functions, so a public function never calls another. */ /** * The check behind `isValidTimeZone`: an IANA name or a UTC offset to the minute. A pass is * remembered, so a repeat builds nothing. * * @param timeZone the candidate; a value that is not a string is not a zone * @returns whether it names a zone * * @example isZoneId("Europe/London") // true * @example isZoneId("Not/AZone") // false */ function isZoneId(timeZone) { if (typeof timeZone !== "string") return false; if (isRememberedZone(timeZone)) return true; try { Temporal.ZonedDateTime.from({ year: 2020, month: 2, day: 28, hour: 0, minute: 0, second: 0, timeZone }); if (!timeZoneLike.test(timeZone)) return false; rememberZone(timeZone); return true; } catch { return false; } } /** * The check behind `isValidTime`: a plain ISO 8601 time of day. * * @param value the candidate string * @returns whether Temporal reads it as a `PlainTime` and it has the strict shape * * @example isTimeString("17:00") // true * @example isTimeString("25:00") // false */ function isTimeString(value) { const body = isoStringBody(value); if (hasSecond60(body) || !plainTime.test(body)) return false; try { Temporal.PlainTime.from(value); return true; } catch { return false; } } /** * The check behind `isValidDate`: a plain ISO 8601 calendar date. * * @param value the candidate string * @returns whether Temporal reads it as an ISO calendar `PlainDate` and it has the strict shape * * @example isDateString("2024-02-29") // true * @example isDateString("2024-02-30") // false */ function isDateString(value) { const body = isoStringBody(value); if (hasSecond60(body) || !plainDate.test(body)) return false; try { return Temporal.PlainDate.from(value).calendarId === "iso8601"; } catch { return false; } } /** * The check behind `isValidDateTime`: the ISO calendar `PlainDateTime` a plain ISO 8601 * date-time string names, or `null`. * * @param value the candidate string * @returns the `PlainDateTime` Temporal reads when it has the strict shape and the ISO calendar, else `null` * * @example plainDateTimeOf("2024-02-29T12:00")?.day // 29 * @example plainDateTimeOf("2024-02-29T23:59:60") // null */ function plainDateTimeOf(value) { const body = isoStringBody(value); if (hasSecond60(body) || !plainDateTime.test(body)) return null; try { const dateTime = Temporal.PlainDateTime.from(value); return dateTime.calendarId === "iso8601" ? dateTime : null; } catch { return null; } } /** * The check behind `isValidMonthDay`: the ISO calendar `PlainMonthDay` a month-day string names, * or `null`. * * The part before the first annotation must match `plainMonthDay`, which refuses the basic form, * a year, a time, `Z`, an offset and a day no year has (`--02-30`) before Temporal is asked. * Temporal then reads the annotations (`monthDayFrom`, defect D20) and the calendar must be ISO. * * @param value the candidate * @returns the `PlainMonthDay` when the string is a month-day in the ISO calendar, else `null` * * @example plainMonthDayOf("--02-29")?.day // 29 * @example plainMonthDayOf("--02-30") // null * @example plainMonthDayOf(null) // null */ function plainMonthDayOf(value) { if (typeof value !== "string" || !plainMonthDay.test(isoStringBody(value))) return null; try { const monthDay = monthDayFrom(value); return monthDay.calendarId === "iso8601" ? monthDay : null; } catch { return null; } } /** * The check behind `isValidYearMonth`: the ISO calendar `PlainYearMonth` a year-month string * names, or `null`. * * The part before the first annotation must match `plainYearMonth`, which refuses the basic form, * a date, a time, `Z` and an offset before Temporal is asked. Temporal then enforces the range * (`-271821-04` to `+275760-09`) and reads the annotations (`yearMonthFrom`, defect D20), and the * calendar must be ISO. * * @param value the candidate * @returns the `PlainYearMonth` when the string is a year-month in the ISO calendar, else `null` * * @example plainYearMonthOf("2026-10")?.month // 10 * @example plainYearMonthOf("2026-10-01") // null * @example plainYearMonthOf(null) // null */ function plainYearMonthOf(value) { if (typeof value !== "string" || !plainYearMonth.test(isoStringBody(value))) return null; try { const yearMonth = yearMonthFrom(value); return yearMonth.calendarId === "iso8601" ? yearMonth : null; } catch { return null; } } /** * The check behind `isValidDuration`: an ISO 8601 duration string. * * @param value the candidate * @returns whether it is a string Temporal reads as a `Duration` * * @example isDurationString("P1DT2H") // true * @example isDurationString("2 days") // false */ function isDurationString(value) { if (typeof value !== "string") return false; try { Temporal.Duration.from(value); return true; } catch { return false; } } const ROLL_CONVENTIONS = [ "following", "modifiedFollowing", "preceding", "modifiedPreceding", "endOfMonth", "none" ]; /** * The check behind `isValidRollConvention`: membership in the roll set. * * @param convention the candidate * @returns whether it is one of the six conventions * * @example isRollConventionName("following") // true * @example isRollConventionName("nearest") // false */ function isRollConventionName(convention) { return typeof convention === "string" && ROLL_CONVENTIONS.includes(convention); } export { isDateString, isDurationString, isRollConventionName, isTimeString, isZoneId, plainDateTimeOf, plainMonthDayOf, plainYearMonthOf };