Created
June 12, 2022 22:14
-
-
Save babacarMbengue12/18deffb5018011dbb0a75050d939a3f2 to your computer and use it in GitHub Desktop.
Paginate data using lodash support typescript and javascript
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
import _ from "lodash"; | |
function paginate( | |
items, | |
page=1, | |
pageSize = 100 | |
){ | |
const offset = (page - 1) * pageSize; | |
const pagedItems = _.drop(items, offset).slice(0, pageSize); | |
return { | |
page: page, | |
pageSize: pageSize, | |
total: items.length, | |
total_pages: Math.ceil(items.length / pageSize), | |
data: pagedItems, | |
}; | |
} |
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
import _ from "lodash"; | |
type PaginatedResults<T> = { | |
page: number; | |
pageSize: number; | |
total: number; | |
total_pages: number; | |
data: T[]; | |
}; | |
function paginate<T>( | |
items: T[], | |
page: number = 1, | |
pageSize: number = 100 | |
): PaginatedResults<T> { | |
const offset = (page - 1) * pageSize; | |
const pagedItems = _.drop(items, offset).slice(0, pageSize); | |
return { | |
page: page, | |
pageSize: pageSize, | |
total: items.length, | |
total_pages: Math.ceil(items.length / pageSize), | |
data: pagedItems, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment