Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ranges-is-index-withinnext

ranges-is-index-within4.2.0

Checks if index is within any of the given string index ranges

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • API — ISINDEXWITHIN…
  • API — DEFAULTS
  • API — VERSION
  • API — TYPES
  • EXAMPLE
  • THE ALGORITH…
  • Changelog

Installation

Quick Take

Examples

  • Include both range endpoints in the match
  • Return false when an index is outside every range
  • Return the matched range instead of true

API — isIndexWithin()

The main function isIndexWithin() is imported like this:

It’s a function which takes three input arguments:

Input argumentTypeObligatoryDescription
index
Type: Natural number
Obligatory: yes
indexNatural numberyesThe natural number index you’re checking
rangesArr
Type: Array of zero or more arrays or null
Obligatory: yes
rangesArrArray of zero or more arrays or nullyesArray of ranges, for example, [ [1, 5], [10, 20] ]
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object.

The Optional Options Object has the following shape:

KeyTypeDefaultDescription
inclusiveRangeEnds
Type: Boolean
Default: false
inclusiveRangeEndsBooleanfalseThat is, are 1 or 5 considered to be within range [1, 5]? The default answer is no, but if set to true, the answer would be yes.
returnMatchedRangeInsteadOfTrue
Type: Boolean
Default: false
returnMatchedRangeInsteadOfTrueBooleanfalseIf set to true, instead of result true it will return the matched range. false is still used as a negative answer. It’s handy when you want to know which range it matched.

Here are all defaults in one place for copying:

The function will return either a boolean, or the matched range (opts.returnMatchedRangeInsteadOfTrue).

If opts.returnMatchedRangeInsteadOfTrue is set to true, positive result will be the range which was matched. Negative result would be still false.

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

API — version

You can import version:

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Opts
Type: Opts
OptsThe Optional Options Object of isIndexWithin(), documented above.
Range
Type: Range
RangeA single range: a two- or three-element array.
Ranges
Type: Ranges
RangesZero or more Ranges, or null.
import type { Opts, Range, Ranges } from "ranges-is-index-within";

Example

Simple encoding using default settings:

import { isIndexWithin } from "ranges-is-index-within";
let res1 = isIndexWithin(79, [
  [5, 10],
  [15, 20],
  [25, 30],
  [35, 40],
  [45, 50],
  [55, 60],
  [65, 70],
  [75, 80], // <-- "true", - "79" would be within this range, answer is "true"
  [85, 90],
  [95, 100],
  [105, 110],
  [115, 120],
  [125, 130],
]);
console.log(res1);
// > true

let res2 = isIndexWithin(31, [
  [5, 10],
  [15, 20],
  [25, 30], // <-- "false" because "31" falls in between this and next range. It's not within.
  [35, 40],
  [45, 50],
  [55, 60],
  [65, 70],
  [75, 80],
  [85, 90],
  [95, 100],
  [105, 110],
  [115, 120],
  [125, 130],
]);
console.log(res2);
// > false

let res3 = isIndexWithin(
  30,
  [
    [5, 10],
    [15, 20],
    [25, 30], // <-- "true" because opts.inclusiveRangeEnds=true and "30" is on the edge of the range.
    [35, 40],
    [45, 50],
    [55, 60],
    [65, 70],
    [75, 80],
    [85, 90],
    [95, 100],
    [105, 110],
    [115, 120],
    [125, 130],
  ],
  { inclusiveRangeEnds: true },
);
console.log(res3);
// > true

let res4 = isIndexWithin(
  30,
  [
    [5, 10],
    [15, 20],
    [25, 30], // <-- "true" because opts.inclusiveRangeEnds=true and "30" is on the edge of the range.
    [35, 40],
    [45, 50],
    [55, 60],
    [65, 70],
    [75, 80],
    [85, 90],
    [95, 100],
    [105, 110],
    [115, 120],
    [125, 130],
  ],
  { inclusiveRangeEnds: true, returnMatchedRangeInsteadOfTrue: true },
);
console.log(res4);
// > [25, 30]  <------ ! not Boolean, but the range itself.

The algorithm

We tried Binary Search algorithmopens in a new tab but native Array.prototype.find()/Array.prototype.some() are around 85x faster.

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab