Crop array of ranges when they go beyond the reference string's length

§ Quick Take

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

      [2, 3],
      [9, 10, "bad grey wolf"],
      [1, 2],
  [[1, 3]] // sorted, merged and cropped

§ Purpose

It crops the ranges, ensuring no range from an array goes beyond a given index.

Along the way, it will also merge and sort ranges.


crop(arr, strLen)

Input argumentTypeObligatory?Description
arrOfRangesnull or ranges - array of zero or more arraysyesProvide an array of ranges to invert. Ranges do not have to be sorted or merged.
strLenNatural number or zeroyesAlgorithm needs to know the length of the reference string to calculate the inverted last slice's ending index.

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. It creates and returns a new array with ranges cropped.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 ranges-process-outside 2.2.35
Iterate string considering ranges, as if they were already applied
📦 ranges-apply 3.2.3
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-invert 2.1.49
Invert string index ranges
📦 ranges-push 3.7.22
Gather string index ranges
📦 ranges-sort 3.13.3
Sort string index ranges
📦 ranges-merge 5.0.3
Merge and sort string index ranges
📦 ranges-iterate 1.1.48
Iterate a string and any changes within given string index ranges