Skip to content

Instantly share code, notes, and snippets.

@ecmel
Forked from kottenator/simple-pagination.js
Last active March 24, 2017 20:42
Show Gist options
  • Select an option

  • Save ecmel/4ea45d808fa3f8022acb2e5f2b7cdad6 to your computer and use it in GitHub Desktop.

Select an option

Save ecmel/4ea45d808fa3f8022acb2e5f2b7cdad6 to your computer and use it in GitHub Desktop.
Simple pagination algorithm
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
}
@ecmel
Copy link
Copy Markdown
Author

ecmel commented Feb 28, 2017

Make it symmetric.

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