Last active
November 24, 2021 10:01
-
-
Save OtayNacef/345be3db0ef1bc8c6121fc1a5fe9e6b6 to your computer and use it in GitHub Desktop.
Javascript pagination that returns currentPage , totalItems , totalPages and items
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 formatPaginatedResponse = (rows: unknown[], count: number, page: string) => { | |
const itemsPerPage = Number(process.env.ITEMS_PER_PAGE); | |
const currentPage = Math.max(Number(page), 1) || 1; | |
return { | |
page: currentPage, | |
totalItems: count, | |
hasNextPage: itemsPerPage * displayPage < count, | |
totalPages: Math.max(Math.floor(count / itemsPerPage) + (count % itemsPerPage === 0 ? 0 : 1), 1), | |
items: rows, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment