UNPKG

1.27 kBTypeScriptView Raw
1import type { Interval } from "./types.js";
2/**
3 * @name clamp
4 * @category Interval Helpers
5 * @summary Return a date bounded by the start and the end of the given interval
6 *
7 * @description
8 * Clamps a date to the lower bound with the start of the interval and the upper
9 * bound with the end of the interval.
10 *
11 * - When the date is less than the start of the interval, the start is returned.
12 * - When the date is greater than the end of the interval, the end is returned.
13 * - Otherwise the date is returned.
14 *
15 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
16 *
17 * @param date - The date to be bounded
18 * @param interval - The interval to bound to
19 *
20 * @returns The date bounded by the start and the end of the interval
21 *
22 * @example
23 * // What is Mar, 21, 2021 bounded to an interval starting at Mar, 22, 2021 and ending at Apr, 01, 2021
24 * const result = clamp(new Date(2021, 2, 21), {
25 * start: new Date(2021, 2, 22),
26 * end: new Date(2021, 3, 1),
27 * })
28 * //=> Mon Mar 22 2021 00:00:00
29 */
30export declare function clamp<DateType extends Date>(
31 date: DateType | number | string,
32 interval: Interval,
33): DateType | Date;