Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active December 24, 2015 12:59
Show Gist options
  • Save latentflip/6801756 to your computer and use it in GitHub Desktop.
Save latentflip/6801756 to your computer and use it in GitHub Desktop.
Convert array to string with ranges, ie: [1,2,3,5,6,8,10,11,13] -> "1-3,5-6,8,10-11,13"
function play(a,p) {
s=''
for (c in a)
y=p==a[c]-1 ? s=s.slice(0,y ? -3 : -1)+'-' : 0,
s+=(p=a[c])+','
return s.slice(0,-1)
}
play=function p(a,m) {
a=(a+'').replace(/(-\d+-)|((\d+),(\d+))/g, function(a,x,y,b,c) {
return x ? m='-' : c-b-1 ? y : m=b+'-'+c
})
return m ? p(a) : a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment