5.0.0 • Published 7 years ago

get-ranges v5.0.0

Weekly downloads
77
License
ISC
Repository
github
Last release
7 years ago

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

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

http://stackoverflow.com/questions/2270910/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');
getRanges([2,3,4,5,10,18,19,20]);
// returns ["2-5", "10", "18-20"]

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

var getRanges = require('get-ranges');
getRanges([2,3,4,5,10,18,19,20],{array:true});
// returns [[2,5], 10, [18,20]]
5.0.0

7 years ago

4.0.0

7 years ago

3.0.0

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago