Quick Take

import { strict as assert } from "assert";
import { rOffset } from "ranges-offset";

      [3, 5],
      [8, 7],
    [13, 15],
    [18, 17],

// ranges are empty, nothing happens:
assert.deepEqual(rOffset(null, 10), null);

// if input does not resemble ranges, nothing happens:
assert.deepEqual(rOffset(true, 10), true);


rOffset(rangesArr, offset)
Input argument Type Obligatory? Description
arr null or Array of one or more arrays (Ranges) no Provide an array of ranges to invert. Ranges do not have to be sorted or merged.
offset Integer number no This number will be added to each index of every range.


  • if not ranges were passed, array of one or more arrays, same input will be returned
  • ELSE, offset value will be added to each index of every range

Inputs are not mutated.


For example, when working on parsed tokens, we have a value, chunk of string, but we know it starts at index X, not at zero. When we return zero-based result, receiving parties need to increment every index by X. The catch is, empty Ranges are null so Array.prototype.map() can't be used. Hence this library.


See it in the monorepo opens in a new tab, on GitHub.


To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ranges-push 6.0.1
Gather string index ranges
📦 ranges-apply 6.0.1
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-merge 8.0.1
Merge and sort string index ranges
📦 ranges-sort 5.0.1
Sort string index ranges
📦 string-range-expander 3.0.1
Expands string index ranges within whitespace boundaries until letters are met
📦 ranges-crop 5.0.1
Crop array of ranges when they go beyond the reference string's length
📦 ranges-process-outside 5.0.1
Iterate string considering ranges, as if they were already applied