§ 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 argumentTypeObligatory?Description
arrOfRangesArrayyesArray of zero or more arrays meaning natural number ranges (2 elements each)
optsPlain objectnoOptional 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 keyTypeObligatory?DefaultDescription
strictlyTwoElementsInRangeArraysBooleannofalseIf 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).
progressFnFunctionnonullIf 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
}

§ Licence

MITopens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 ranges-push 3.7.22
Gather string index ranges
📦 ranges-apply 3.2.3
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-merge 5.0.3
Merge and sort string index ranges
📦 string-range-expander 1.11.11
Expands string index ranges within whitespace boundaries until letters are met
📦 ranges-regex 2.1.3
Integrate regex operations into Ranges workflow
📦 ranges-process-outside 2.2.35
Iterate string considering ranges, as if they were already applied
📦 ranges-crop 2.1.3
Crop array of ranges when they go beyond the reference string's length