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) • 1.17 kB
import { Temporal } from "./temporal.js"; // src/internal/durationFrom.ts /** * `Temporal.Duration.from`, except that a string must write its designators in upper case. * * ISO 8601-1 writes the designators P, Y, M, W, D, T, H and S in upper case; Temporal's * `ParseTemporalDurationString` also reads them in lower case (`p1d`, `pt1h`), which GMT never * copies. A lower-case letter has no other place in a duration string (the grammar has no * exponent), so any of them is refused. The decimal comma (ISO 8601-1) stays. * * @param value an ISO 8601 duration string, a `Temporal.Duration` or a duration-like object * @returns the duration * @throws RangeError when the string has a lower-case letter, or when Temporal refuses the value * * @example durationFrom("PT1H30M").toString() // "PT1H30M" * @example durationFrom("PT0,5H").toString() // "PT30M" * @example durationFrom("pt1h") // throws RangeError * @example durationFrom("P1d") // throws RangeError */ function durationFrom(value) { if (typeof value === "string" && /[a-z]/.test(value)) throw new RangeError(`Invalid duration: ${value}`); return Temporal.Duration.from(value); } export { durationFrom };