oa_based/src/helper/paginate/create-pagination.ts

28 lines
557 B
TypeScript
Raw Normal View History

2024-02-28 08:32:35 +08:00
import { IPaginationMeta } from './interface'
import { Pagination } from './pagination'
export function createPaginationObject<T>({
items,
totalItems,
currentPage,
limit,
}: {
items: T[]
totalItems?: number
currentPage: number
limit: number
}): Pagination<T> {
const totalPages
= totalItems !== undefined ? Math.ceil(totalItems / limit) : undefined
const meta: IPaginationMeta = {
totalItems,
itemCount: items.length,
itemsPerPage: limit,
totalPages,
currentPage,
}
return new Pagination<T>(items, meta)
}