Last active
December 24, 2015 12:59
-
-
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"
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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
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