adonis-api-resources
Version:
API resources for AdonisJS
20 lines (19 loc) • 642 B
JavaScript
export const paginate = (data, page, limit) => {
const lastPage = Math.max(Math.ceil(data.length / limit), 1);
const meta = {
total: data.length,
perPage: Math.floor(limit),
currentPage: Math.floor(page),
lastPage: lastPage,
firstPage: 1,
firstPageUrl: '/?page=1',
lastPageUrl: '/?page=' + lastPage,
nextPageUrl: page < lastPage ? `/?page=${page + 1}` : null,
previousPageUrl: page > 1 ? `/?page=${page - 1}` : null,
};
const collection = data.slice((page - 1) * limit, page * limit);
return {
meta: meta,
data: collection,
};
};