Created
March 27, 2019 19:59
-
-
Save adamdehaven/161882e49fd06abc9c55dd8962268bb2 to your computer and use it in GitHub Desktop.
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
pagination(currentPage, pageCount) { | |
let delta = 1, | |
left = currentPage - delta, | |
right = currentPage + delta + 1, | |
result = [] | |
result = Array.from({ length: pageCount }, (v, k) => k + 1) | |
.filter(i => i && i >= left && i < right) | |
if (result.length > 1) { | |
// Add first page and dots | |
if (result[0] > 1) { | |
if (result[0] > 2) { | |
// Add dots | |
result.unshift('...') | |
} | |
// Add first page | |
result.unshift(1) | |
} | |
// Add dots and last page | |
if (result[result.length - 1] < pageCount) { | |
if (result[result.length - 1] !== pageCount - 1) { | |
// Add dots | |
result.push('...') | |
} | |
// Add last page | |
result.push(pageCount) | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment