§ 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 Sourcehut.
§ Licence
Copyright © 2010–2020 Roy Revelt and other contributors
Related packages:
📦 ranges-push 4.0.2
Gather string index ranges
📦 ranges-apply 4.0.2
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-merge 6.2.0
Merge and sort string index ranges
📦 ranges-offset 1.0.3
Increment or decrement each index in every range
📦 string-range-expander 1.12.0
Expands string index ranges within whitespace boundaries until letters are met
📦 ranges-is-index-within 1.16.0
Checks if index is within any of the given string index ranges
📦 ranges-ent-decode 3.0.3
Recursive HTML entity decoding for Ranges workflow