inRange
Checks if a number falls within a given range. Start is inclusive, end is exclusive. If start is greater than end, the bounds are swapped automatically.
Try it
Section titled “Try it”Import
Section titled “Import”import { inRange } from "1o1-utils";import { inRange } from "1o1-utils/in-range";Signature
Section titled “Signature”function inRange(params: { value: number; start: number; end: number }): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| value | number | Yes | The number to check |
| start | number | Yes | The start of the range (inclusive) |
| end | number | Yes | The end of the range (exclusive) |
Returns
Section titled “Returns”boolean — true if value is within [start, end), otherwise false.
Examples
Section titled “Examples”inRange({ value: 3, start: 1, end: 5 }); // trueinRange({ value: 5, start: 1, end: 5 }); // false — end is exclusiveinRange({ value: 1, start: 1, end: 5 }); // true — start is inclusiveinRange({ value: -1, start: -5, end: 5 }); // true// Bounds are swapped automatically when start > endinRange({ value: 3, start: 5, end: 1 }); // true// Floating-point valuesinRange({ value: 1.5, start: 1, end: 2 }); // trueEdge Cases
Section titled “Edge Cases”- Throws if any of
value,start, orendis not a number or isNaN. - A zero-width range (
start === end) always returnsfalse. - Works with
Infinitybounds — e.g.{ value: 1e10, start: 0, end: Infinity }returnstrue. - When
start > end, the bounds are swapped silently. The inclusive side follows the lower bound.
Also known as
Section titled “Also known as”between, range check, bounds check, within range, number range
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use inRange to check if a number is within a bounded range.