Skip to main content

isInRange

Determines whether a given value falls within the range specified by min and max.

You can configure whether to include boundary values via inclusiveMin/inclusiveMax. By default, the minimum value is inclusive and the maximum value is exclusive.


Code

🔗 View source code

Benchmark

  • hz: operations per second
  • mean: average response time (ms)
NamehzmeanPerformance
modern-kit/isInRange24,435,490.550.0000fastest
lodash/inRange9,373,021.300.0001slowest
  • modern-kit/isInRange
    • 2.61x faster than lodash/inRange

Interface

typescript
interface IsInRangeProps {
value: number;
min: number;
max: number;
inclusiveMin?: boolean;
inclusiveMax?: boolean;
}

function isInRange({
value,
min,
max,
inclusiveMin,
inclusiveMax,
}: IsInRangeProps): boolean;

Usage

typescript
import { isInRange } from '@modern-kit/utils';

isInRange({ value: 5, min: 0, max: 10 }) // true
isInRange({ value: 0, min: 0, max: 10 }) // true
isInRange({ value: 10, min: 0, max: 10, inclusiveMin: true }) // true
isInRange({ value: 10, min: 0, max: 10, inclusiveMin: false, inclusiveMax: true }) // true
isInRange({ value: 10, min: 0, max: 10, inclusiveMin: true, inclusiveMax: true }) // true

// Error
isInRange({ value: 5, min: 10, max: 0 }) // Error('min must be less than max.')