@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.
41 lines (39 loc) • 1.61 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
// src/plain/calculate/clampDate.ts
/**
* Restrict `value` to the range [`min`, `max`].
*
* - 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 PlainDate string to clamp
* @param min ISO PlainDate string for the lower bound (inclusive)
* @param max ISO PlainDate string for the upper bound (inclusive)
* @returns The clamped date string, or "" on invalid input
*
* @example clampDate("2024-03-15", "2024-03-01", "2024-03-31") // "2024-03-15"
* @example clampDate("2024-02-01", "2024-03-01", "2024-03-31") // "2024-03-01"
* @example clampDate("2024-05-01", "2024-03-01", "2024-03-31") // "2024-03-31"
* @example clampDate("2024-03-15", "2024-03-31", "2024-03-01") // ""
* @example clampDate("invalid", "2024-03-01", "2024-03-31") // ""
*/
function clampDate(value, min, max) {
if (!isValidDate(value) || !isValidDate(min) || !isValidDate(max)) return "";
try {
const v = Temporal.PlainDate.from(value);
const mn = Temporal.PlainDate.from(min);
const mx = Temporal.PlainDate.from(max);
if (Temporal.PlainDate.compare(mn, mx) === 1) return "";
const cmpMin = Temporal.PlainDate.compare(v, mn);
const cmpMax = Temporal.PlainDate.compare(v, mx);
if (cmpMin < 0) return mn.toString();
if (cmpMax > 0) return mx.toString();
return v.toString();
} catch {
return "";
}
}
export { clampDate };