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

§ 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.


rCrop(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 GitHub.

§ Contributing

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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ranges-is-index-within 2.0.14
Checks if index is within any of the given string index ranges
📦 ranges-ent-decode 4.0.14
Recursive HTML entity decoding for Ranges workflow
📦 ranges-push 5.0.14
Gather string index ranges
📦 ranges-sort 4.0.14
Sort string index ranges
📦 ranges-merge 7.0.14
Merge and sort string index ranges
📦 ranges-regex 4.0.14
Integrate regex operations into Ranges workflow
📦 ranges-offset 2.0.14
Increment or decrement each index in every range