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
npm i ranges-merge
Quick Take
import { strict as assert } from "assert";
import mergeR from "ranges-merge";
assert.deepEqual(
mergeR([
[1, 2],
[2, 3],
[9, 10],
]),
[
[1, 3],
[9, 10],
]
);
assert.deepEqual(
mergeR([
[1, 5],
[2, 10],
]),
[[1, 10]]
);
Documentation
Please visit codsen.com for a full description of the API and examples.
Licence
MIT License
Copyright (c) 2010-2020 Roy Revelt and other contributors