What is ranges-push?
The `ranges-push` npm package is a utility for managing and manipulating ranges of indexes. It is particularly useful for tasks that involve handling multiple ranges of text or data, such as text processing, data cleaning, or any scenario where you need to keep track of multiple index ranges.
What are ranges-push's main functionalities?
Adding Ranges
This feature allows you to add ranges to the `Ranges` instance. The `push` method takes two arguments, the start and end indexes of the range, and adds them to the list of ranges.
const { Ranges } = require('ranges-push');
const ranges = new Ranges();
ranges.push(0, 5);
ranges.push(10, 15);
console.log(ranges.current()); // [[0, 5], [10, 15]]
Merging Overlapping Ranges
This feature automatically merges overlapping ranges. When you add a new range that overlaps with an existing one, the ranges are merged into a single range.
const { Ranges } = require('ranges-push');
const ranges = new Ranges();
ranges.push(0, 5);
ranges.push(3, 10);
console.log(ranges.current()); // [[0, 10]]
Sorting Ranges
This feature ensures that the ranges are always sorted in ascending order. When you add new ranges, they are automatically placed in the correct order.
const { Ranges } = require('ranges-push');
const ranges = new Ranges();
ranges.push(10, 15);
ranges.push(0, 5);
console.log(ranges.current()); // [[0, 5], [10, 15]]
Deleting Ranges
This feature allows you to delete all ranges. The `wipe` method clears all the ranges that have been added.
const { Ranges } = require('ranges-push');
const ranges = new Ranges();
ranges.push(0, 5);
ranges.push(10, 15);
ranges.wipe();
console.log(ranges.current()); // []
Other packages similar to ranges-push
range-utils
The `range-utils` package provides a set of utilities for working with ranges of numbers. It includes functions for merging, intersecting, and subtracting ranges. Unlike `ranges-push`, it is more focused on mathematical operations on ranges rather than text index management.
text-range
The `text-range` package is designed for managing ranges within text. It provides utilities for creating, merging, and manipulating text ranges. It is similar to `ranges-push` but offers more specialized functions for text processing.
interval-tree
The `interval-tree` package implements an interval tree data structure, which is useful for efficiently querying overlapping intervals. While it is more complex and powerful than `ranges-push`, it serves a similar purpose in managing and querying ranges.
ranges-push
Gather string index ranges
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 5.1.0 (npm i ranges-push@5.1.0
).
npm i ranges-push
Quick Take
import { strict as assert } from "assert";
import { Ranges } from "ranges-push";
import { rApply } from "ranges-apply";
const gatheredRanges = new Ranges();
const oldString = "The quick brown fox jumps over the lazy dog.";
gatheredRanges.push(35, 43, "little Red Riding Hood");
gatheredRanges.push(4, 19, "bad grey wolf");
assert.deepEqual(gatheredRanges.current(), [
[4, 19, "bad grey wolf"],
[35, 43, "little Red Riding Hood"],
]);
assert.equal(
rApply(oldString, gatheredRanges.current()),
"The bad grey wolf jumps over the little Red Riding Hood.",
);
gatheredRanges.wipe();
assert.equal(gatheredRanges.current(), null);
Documentation
Please visit codsen.com for a full description of the API.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License.
Copyright © 2010-2024 Roy Revelt and other contributors.