Skip to content

Instantly share code, notes, and snippets.

@yezz123
Forked from kottenator/simple-pagination.js
Last active July 10, 2021 19:20
Show Gist options
  • Save yezz123/b2b8281ce719409f4161d1ce9713697b to your computer and use it in GitHub Desktop.
Save yezz123/b2b8281ce719409f4161d1ce9713697b to your computer and use it in GitHub Desktop.
Simple pagination algorithm
// Implementation in ES6
function pagination(c, m) {
var 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