Created
September 23, 2022 03:54
-
-
Save Alii-isk/ac0d154fc5aec44d2cf92ae902371be8 to your computer and use it in GitHub Desktop.
A simple vanilla javascript pagination function NO DOM
This file contains 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
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