-
-
Save ecmel/4ea45d808fa3f8022acb2e5f2b7cdad6 to your computer and use it in GitHub Desktop.
Simple pagination algorithm
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 pagination(c, m) { | |
const current = c, | |
last = m, | |
delta = 2, | |
left = current - delta + 1, | |
right = current + delta + 2, | |
range = [], | |
rangeWithDots = [] | |
let l | |
for (let i = 1; i <= last; i++) { | |
if (i == 1 || i == last || i >= left && i < right) range.push(i) | |
} | |
for (let i = 0; i < range.length; i++) { | |
if (l) { | |
if (range[i] - l === 2) rangeWithDots.push(l + 1) | |
else if (range[i] - l !== 1) rangeWithDots.push('...') | |
} | |
rangeWithDots.push(range[i]) | |
l = range[i] | |
} | |
return rangeWithDots | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make it symmetric.