Last active
June 30, 2022 23:34
-
-
Save hamzahamidi/e0faa58c4f876db7b2159be009fd6700 to your computer and use it in GitHub Desktop.
creates a pagination object (useful to format the result of a DB query)
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 pagination = (paginationParams = {}) => ({ | |
...paginationParams, | |
limit: paginationParams.limit || 20, | |
offset: paginationParams.offset || 0, | |
totalCount: paginationParams.totalCount || 0, | |
get currentPage() { | |
return Math.floor(this.offset / this.limit) + 1; | |
}, | |
get hasNext() { | |
return this.currentPage * this.limit < this.totalCount; | |
}, | |
get hasPrevious() { | |
return !!this.offset; | |
}, | |
get totalPage() { | |
if (!this.totalCount) { | |
return 1; | |
} | |
return Math.ceil(this.totalCount / this.limit); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment