Created
October 8, 2019 13:47
-
-
Save inoyakaigor/06a0794d6fe039211ed5b5b8e0733d04 to your computer and use it in GitHub Desktop.
Get range function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getRanges = arr => | |
arr | |
.reduceRight( | |
(prev, curr) => ( | |
prev.length | |
? ( | |
prev[0][0] === curr + 1 | |
? prev[0].unshift(curr) | |
: prev.unshift([curr]) | |
) && prev | |
: [[curr]] | |
), | |
[] | |
) | |
.map( | |
a => a.join('-') | |
) | |
.join(',') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment