nodedb-json
Version:
A lightweight JSON-based database for Node.js with TypeScript support, indexing, and complex query capabilities
32 lines • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePagination = validatePagination;
exports.applyPagination = applyPagination;
function validatePagination(pagination) {
const { page, pageSize } = pagination;
if (!Number.isInteger(page) || !Number.isInteger(pageSize) || page < 1 || pageSize < 1) {
throw new Error('Pagination page and pageSize must be positive integers');
}
}
function applyPagination(data, pagination) {
const { page, pageSize } = pagination;
validatePagination(pagination);
const totalItems = data.length;
const totalPages = Math.ceil(totalItems / pageSize);
const currentPage = page;
const startIndex = (currentPage - 1) * pageSize;
const endIndex = startIndex + pageSize;
const paginatedData = data.slice(startIndex, endIndex);
return {
data: paginatedData,
pagination: {
currentPage,
pageSize,
totalItems,
totalPages,
hasPrevious: currentPage > 1,
hasNext: currentPage < totalPages
}
};
}
//# sourceMappingURL=paginator.js.map