Quick Take
import { strict as assert } from "assert";
import { rSort } from "ranges-sort";
// Ranges (see codsen.com/ranges/) are sorted:
assert.deepEqual(
rSort([
[2, 3],
[9, 10, "bad grey wolf"],
[1, 2],
]),
[
[1, 2],
[2, 3],
[9, 10, "bad grey wolf"],
]
);
API
rSort(arr, [opts])
In other words, this library gives you a function and you must feed an array into its first argument and also if you wish, you can feed a second argument, the Optional Options Object (bracket in [, opts]
means "optional").
Input argument | Type | Obligatory? | Description |
---|---|---|---|
arrOfRanges |
Array | yes | Array of zero or more arrays meaning natural number ranges (2 elements each) |
opts |
Plain object | no | Optional options go here. |
For example,
[ [5, 9], [5, 3] ] => [ [5, 3], [5, 9] ]
This library does not mutate the inputs. In theory, a function in JavaScript could mutate its arguments, but only if they are on an "object" primitive type (an array or a plain object, for example).
Options object
options object's key |
Type | Obligatory? | Default | Description |
---|---|---|---|---|
strictlyTwoElementsInRangeArrays |
Boolean | no | false |
If set to true , all ranges must have two and only elements, otherwise error is thrown. For example, input being [ [1, 2, 'zzz'] ] would throw (3 elements), as well as [ ['a'] ] (1 element). |
progressFn |
Function | no | null |
If a function is given, it will be called with natural number meaning percentage of the total work done. It's approximate and used in worker setups. |
Output: Sorted input array. First, we sort by the first argument of each child range array, then by second.
Here is whole Optional Options Object in one place, with all defaults, in case you want to copy it:
{
strictlyTwoElementsInRangeArrays: false,
progressFn: null
}
Changelog
See it in the monorepo , on GitHub.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
Licence
Copyright © 2010–2021 Roy Revelt and other contributors