Skip to content

Instantly share code, notes, and snippets.

@inoyakaigor
Created October 8, 2019 13:47
Show Gist options
  • Save inoyakaigor/06a0794d6fe039211ed5b5b8e0733d04 to your computer and use it in GitHub Desktop.
Save inoyakaigor/06a0794d6fe039211ed5b5b8e0733d04 to your computer and use it in GitHub Desktop.
Get range function
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