get contiguous ranges in pre-sorted array of numbers

Usage no npm install needed!

<script type="module">
  import getRanges from '';


Extracts contiguous ranges from a pre-sorted array of numbers.

From this question: "How to convert sequence of numbers in an array to range of numbers"

This is CMS's answer, except negative numbers are in parenthesis. NOTE: you may need to sort the array beforehand. One way is npm i --save sort-numbers then require('sort-numbers')(array);

var getRanges = require('get-ranges');
// returns ["2-5", "10", "18-20"]

To return an array of numeric ranges instead of strings describing ranges:

var getRanges = require('get-ranges');
// returns [[2,5], 10, [18,20]]