§ Quick Take

import { strict as assert } from "assert";
import invert from "ranges-invert";

assert.deepEqual(
  invert(
    [
      [3, 5],
      [5, 7],
    ],
    9 // string length needed to set the boundary
  ),
  [
    [0, 3],
    [7, 9],
  ]
);

§ API

invert(arr, strLen, [opts])

Input argumentTypeObligatory?Description
arrOfRangesArray of zero or more arraysyesProvide an array of ranges to invert. Ranges do not have to be sorted or merged.
strLenInteger numberyesAlgorithm needs to know the length of the reference string to calculate the inverted last slice's ending index.
optsPlain objectnoOptional options go here.

Output: array of zero or more arrays (so-called ranges) where each consists of two or more natural number (or zero) indexes.

This package does not mutate the input array, instead it creates and returns a new array with ranges inverted.

§ Options object

options object's keyTypeObligatory?DefaultDescription
strictlyTwoElementsInRangeArraysBooleannofalseIf set to true, all ranges must have two and only two elements, otherwise an error will be thrown. For example, input being [ [1, 2, 'zzz'] ] would throw (because of 3 elements), as well as [ ['a'] ] (1 element).
skipChecksBooleannofalseIf set to true, no checks will be performed. It's handy to cut corners for perf reasons when you know input ranges are clean.

§ Licence

MITopens in a new tab

Copyright © 2015–2020 Roy Revelt and other contributors

Related packages:

📦 ranges-push 3.7.21
Gather string index ranges
📦 ranges-apply 3.2.2
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-ent-decode 2.1.2
Recursive HTML entity decoding for Ranges workflow
📦 ranges-is-index-within 1.15.1
Checks if index is within any of the given string index ranges
📦 ranges-iterate 1.1.47
Iterate a string and any changes within given string index ranges
📦 ranges-crop 2.1.2
Crop array of ranges when they go beyond the reference string's length
📦 ranges-regex 2.1.2
Integrate regex operations into Ranges workflow