vremel
Version:
JavaScript date utility library for Temporal API
23 lines • 880 B
JavaScript
import { assertSameType, assertValidInterval } from "../assert.js";
import { compare } from "./_compare.js";
/**
* Returns a datetime object clamped within the given interval.
* * When the given datetime is earlier than the start of the interval, the start will be returned.
* * When the given datetime is later than the end of the interval, the end will be returned.
* * Otherwise the given datetime will be returned.
* @param dateTime datetime object
* @param interval interval
* @returns clamped datetime object
*/
export function clamp(dateTime, interval) {
assertValidInterval(interval);
assertSameType(dateTime, interval.start);
if (compare(dateTime, interval.start) === -1) {
return interval.start;
}
if (compare(dateTime, interval.end) === 1) {
return interval.end;
}
return dateTime;
}
//# sourceMappingURL=clamp.js.map