Skip to content

Instantly share code, notes, and snippets.

@Alii-isk
Created September 23, 2022 03:54
Show Gist options
  • Save Alii-isk/ac0d154fc5aec44d2cf92ae902371be8 to your computer and use it in GitHub Desktop.
Save Alii-isk/ac0d154fc5aec44d2cf92ae902371be8 to your computer and use it in GitHub Desktop.
A simple vanilla javascript pagination function NO DOM
export const paginate = ({lastPage,currentPage}) => {
const _links = Array.from({ length: lastPage}, (_, i) => i + 1);
return _links.reduce((acc, link) => {
if (
link === 1 ||
link === _links.length ||
(link > currentPage - 2 && link < currentPage + 2)
) {
acc.push(link);
} else if (
link === currentPage - 2 ||
link === currentPage + 2
) {
acc.push("...");
}
return acc;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment