Skip to content

Instantly share code, notes, and snippets.

@daltonnyx
Created March 1, 2019 07:01
Show Gist options
  • Save daltonnyx/73c005b7635fe83e6f09e28ed784f8ae to your computer and use it in GitHub Desktop.
Save daltonnyx/73c005b7635fe83e6f09e28ed784f8ae to your computer and use it in GitHub Desktop.
const getInRangedValue = function(value, min, max) {
return value < min ? min : value > max ? max : value;
}
const pagination = function(current, visible, total) {
const middle = Math.ceil(visible / 2);
let startPage = current - middle + 1, endPage = startPage + visible;
startPage = getInRangedValue(startPage, 1, total - visible + 1);
endPage = getInRangedValue(endPage, visible, total);
let pages = [];
for(let i = startPage; i <= endPage && pages.length < visible; i++) {
pages.push(i);
}
return pages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment