What is ranges-merge?
The 'ranges-merge' npm package is used to merge and sort ranges of numbers or strings. It is particularly useful for tasks that involve handling multiple ranges and ensuring they are combined and ordered correctly.
What are ranges-merge's main functionalities?
Merging overlapping ranges
This feature allows you to merge overlapping ranges into a single range. In the example, the ranges [1, 5] and [3, 7] overlap and are merged into [1, 7].
const { mergeRanges } = require('ranges-merge');
const ranges = [[1, 5], [3, 7], [8, 10]];
const merged = mergeRanges(ranges);
console.log(merged); // Output: [[1, 7], [8, 10]]
Sorting ranges
This feature sorts the ranges in ascending order. In the example, the ranges are sorted to be in the order [1, 3], [5, 7], and [8, 10].
const { mergeRanges } = require('ranges-merge');
const ranges = [[5, 7], [1, 3], [8, 10]];
const merged = mergeRanges(ranges);
console.log(merged); // Output: [[1, 3], [5, 7], [8, 10]]
Handling non-overlapping ranges
This feature ensures that non-overlapping ranges are kept separate. In the example, the ranges do not overlap and are returned as they are.
const { mergeRanges } = require('ranges-merge');
const ranges = [[1, 2], [3, 4], [5, 6]];
const merged = mergeRanges(ranges);
console.log(merged); // Output: [[1, 2], [3, 4], [5, 6]]
Other packages similar to ranges-merge
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. While it does not specifically focus on merging ranges, it offers functions like '_.union' and '_.sortBy' that can be used to achieve similar results with more general-purpose utilities.
interval-tree
The 'interval-tree' package provides a data structure for efficiently storing and querying intervals. It is more specialized for interval operations and can handle more complex queries and operations compared to 'ranges-merge'.
range-merge
The 'range-merge' package is another library specifically designed for merging ranges. It offers similar functionality to 'ranges-merge' but may have different performance characteristics or additional features.
ranges-merge
Merge and sort string index ranges
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 7.1.0 (npm i ranges-merge@7.1.0
).
npm i ranges-merge
Quick Take
import { strict as assert } from "assert";
import { rMerge } from "ranges-merge";
assert.deepEqual(
rMerge([
[1, 2],
[2, 3],
[9, 10],
]),
[
[1, 3],
[9, 10],
],
);
assert.deepEqual(
rMerge([
[1, 5],
[2, 10],
]),
[[1, 10]],
);
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-2023 Roy Revelt and other contributors.