UNPKG

typedash

Version:

modern, type-safe collection of utility functions

35 lines (34 loc) 1.41 kB
//#region src/functions/inRange/inRange.ts /** * Checks if a number is within a specified range. * @param value The number to check. * @param range The range to check against, as a tuple of start and end values. * @param options Optional parameters. * @param options.inclusive Whether the range is inclusive of the start and/or end value. * - If `true`, the range is inclusive of both start and end. * - If `false`, the range is exclusive of both start and end. * - If `'start'`, the range is inclusive of the start value but exclusive of the end value. * - If `'end'`, the range is exclusive of the start value but inclusive of the end value. * @returns `true` if the number is within the range, `false` otherwise. * @example * ```typescript * inRange(3, [1, 5]); // true * inRange(5, [1, 5]); // false * inRange(5, [1, 5], { inclusive: true }); // true * inRange(1, [1, 5], { inclusive: 'end' }); // false * ``` */ function inRange(value, range, options) { const [start, end] = range; const { inclusive = "start" } = options ?? {}; if (start > end) throw new RangeError(`Invalid range: [${start},${end}]`); return { true: () => value >= start && value <= end, false: () => value > start && value < end, start: () => value >= start && value < end, end: () => value > start && value <= end }[inclusive](); } //#endregion export { inRange as t }; //# sourceMappingURL=inRange-DDsoQjG6.js.map