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.

§ Changelog

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

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 ranges-invert 3.0.2
Invert string index ranges
📦 ranges-regex 3.0.3
Integrate regex operations into Ranges workflow
📦 ranges-push 4.0.2
Gather string index ranges
📦 ranges-iterate 1.2.2
Iterate a string and any changes within given string index ranges
📦 ranges-merge 6.2.0
Merge and sort string index ranges
📦 ranges-offset 1.0.3
Increment or decrement each index in every range
📦 ranges-process-outside 3.0.2
Iterate string considering ranges, as if they were already applied