Skip to content

Instantly share code, notes, and snippets.

@screeny05
Created January 19, 2018 15:10
Show Gist options
  • Save screeny05/e2c65fc85867babc54126f6d5635477f to your computer and use it in GitHub Desktop.
Save screeny05/e2c65fc85867babc54126f6d5635477f to your computer and use it in GitHub Desktop.
const parsePagination = s =>
s
.split(',')
.map(s => {
if(s.match(/^\d+$/)){
return parseInt(s);
}
if(s.match(/^\d+-\d+$/)){
s = s.split('-');
return Array(s[1] - s[0] + 1)
.fill(0)
.map((v, i) => i + parseInt(s[0]));
}
throw new SyntaxError('Cannot parse string ' + s);
})
.reduce((acc, v) => acc.concat(v), []);
@screeny05
Copy link
Author

parsePagination('1,2,3,5-7,20,22');
// [1,2,3,5,6,7,20,22]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment