Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created November 15, 2018 10:58
Show Gist options
  • Save munkacsitomi/b92e003eebf14d5df78c983e39cad195 to your computer and use it in GitHub Desktop.
Save munkacsitomi/b92e003eebf14d5df78c983e39cad195 to your computer and use it in GitHub Desktop.
Pagination algorithm
const pagination = (c, m) => {
let current = c,
last = m,
delta = 2,
left = current - delta,
right = current + delta + 1,
range = [],
rangeWithDots = [],
l;
for (let i = 1; i <= last; i++) {
if (i == 1 || i == last || i >= left && i < right) {
range.push(i);
}
}
for (let i of range) {
if (l) {
if (i - l === 2) {
rangeWithDots.push(l + 1);
} else if (i - l !== 1) {
rangeWithDots.push('...');
}
}
rangeWithDots.push(i);
l = i;
}
return rangeWithDots;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment