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.

26 lines (24 loc) • 853 B
import { Temporal } from "./temporal.js"; // src/internal/gtfsServiceDate.ts /** GTFS § Field Types, Date. */ const GTFS_DATE = /^[0-9]{8}$/; /** GMT's plain-date form, with a four-digit year only. */ const PLAIN_DATE_FOUR_DIGIT_YEAR = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/; /** * The service date as canonical "YYYY-MM-DD", or null. Never throws. * * @example gtfsServiceDateOf("20240130") // "2024-01-30" * @example gtfsServiceDateOf("2024-01-30") // "2024-01-30" * @example gtfsServiceDateOf("20240230") // null * @example gtfsServiceDateOf("+002024-01-30") // null */ function gtfsServiceDateOf(value) { if (typeof value !== "string") return null; if (!GTFS_DATE.test(value) && !PLAIN_DATE_FOUR_DIGIT_YEAR.test(value)) return null; try { return Temporal.PlainDate.from(value).toString(); } catch { return null; } } export { gtfsServiceDateOf };