What is ranges-sort?
The ranges-sort npm package is designed to sort and manage ranges of numbers or strings. It is particularly useful for tasks that involve handling multiple ranges and ensuring they are in a specific order.
What are ranges-sort's main functionalities?
Sorting Ranges
This feature allows you to sort an array of ranges. The ranges are sorted based on their starting values.
const rangesSort = require('ranges-sort');
const ranges = [[5, 10], [1, 3], [8, 12]];
const sortedRanges = rangesSort(ranges);
console.log(sortedRanges); // Output: [[1, 3], [5, 10], [8, 12]]
Handling Overlapping Ranges
This feature allows you to sort and merge overlapping ranges. The resulting array contains non-overlapping ranges.
const rangesSort = require('ranges-sort');
const ranges = [[5, 10], [1, 3], [8, 12]];
const sortedRanges = rangesSort(ranges, { mergeOverlapping: true });
console.log(sortedRanges); // Output: [[1, 3], [5, 12]]
Other packages similar to ranges-sort
interval-tree
The interval-tree package is used for managing and querying intervals. It supports efficient insertion, deletion, and querying of intervals. While it offers more advanced features for interval management compared to ranges-sort, it is more complex to use and may be overkill for simple range sorting tasks.
ranges-sort
Sort string index ranges
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 4.1.0 (npm i ranges-sort@4.1.0
).
npm i ranges-sort
Quick Take
import { strict as assert } from "assert";
import { rSort } from "ranges-sort";
assert.deepEqual(
rSort([
[2, 3],
[9, 10, "bad grey wolf"],
[1, 2],
]),
[
[1, 2],
[2, 3],
[9, 10, "bad grey wolf"],
]
);
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-2022 Roy Revelt and other contributors.