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

Quick Take

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

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


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.


rCrop(arr, strLen)
Input argument Type Obligatory? Description
arrOfRanges null or ranges - array of zero or more arrays yes Provide an array of ranges to invert. Ranges do not have to be sorted or merged.
strLen Natural number or zero yes Algorithm 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.


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-apply 6.0.1
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-ent-decode 5.0.1
Recursive HTML entity decoding for Ranges workflow
📦 ranges-sort 5.0.1
Sort string index ranges
📦 ranges-push 6.0.1
Gather string index ranges
📦 ranges-merge 8.0.1
Merge and sort string index ranges
📦 ranges-iterate 3.0.1
Iterate a string and any changes within given string index ranges
📦 ranges-is-index-within 3.0.1
Checks if index is within any of the given string index ranges