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.

42 lines (40 loc) • 2.12 kB
import { Temporal } from "../../internal/temporal.js"; import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js"; import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js"; // src/zoned/calculate/clampZoned.ts /** * Restrict `value` to the range [`min`, `max`] in zoned datetime space. * * - Returns `value` if it falls within the bounds (inclusive on both ends). * - Returns `min` if `value` is before `min`. * - Returns `max` if `value` is after `max`. * - Returns `""` if any input is invalid or `min > max`. * * @param value ISO ZonedDateTime string to clamp * @param min ISO ZonedDateTime string for the lower bound (inclusive) * @param max ISO ZonedDateTime string for the upper bound (inclusive) * @returns The clamped zoned datetime string, or "" on invalid input * * @example clampZoned("2024-03-15T12:00:00+00:00[UTC]", "2024-03-01T00:00:00+00:00[UTC]", "2024-03-31T23:59:59+00:00[UTC]") // "2024-03-15T12:00:00+00:00[UTC]" * @example clampZoned("2024-02-01T12:00:00+00:00[UTC]", "2024-03-01T00:00:00+00:00[UTC]", "2024-03-31T23:59:59+00:00[UTC]") // "2024-03-01T00:00:00+00:00[UTC]" * @example clampZoned("2024-05-01T12:00:00+00:00[UTC]", "2024-03-01T00:00:00+00:00[UTC]", "2024-03-31T23:59:59+00:00[UTC]") // "2024-03-31T23:59:59+00:00[UTC]" * @example clampZoned("2024-03-15T12:00:00+00:00[UTC]", "2024-03-31T00:00:00+00:00[UTC]", "2024-03-01T00:00:00+00:00[UTC]") // "" * @example clampZoned("invalid", "2024-03-01T00:00:00+00:00[UTC]", "2024-03-31T23:59:59+00:00[UTC]") // "" */ function clampZoned(value, min, max) { if (!isValidZonedDateTime(value) || !isValidZonedDateTime(min) || !isValidZonedDateTime(max)) return ""; try { const v = zonedDateTimeFrom(value); const mn = zonedDateTimeFrom(min); const mx = zonedDateTimeFrom(max); if (Temporal.ZonedDateTime.compare(mn, mx) === 1) return ""; const cmpMin = Temporal.ZonedDateTime.compare(v, mn); const cmpMax = Temporal.ZonedDateTime.compare(v, mx); if (cmpMin < 0) return mn.toString(); if (cmpMax > 0) return mx.toString(); return v.toString(); } catch { return ""; } } export { clampZoned };