Skip to content

Instantly share code, notes, and snippets.

@hamzahamidi
Last active June 30, 2022 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamzahamidi/e0faa58c4f876db7b2159be009fd6700 to your computer and use it in GitHub Desktop.
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)
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