UNPKG

@ministryofjustice/probation-search-frontend

Version:

A shared UI component to search for probation cases from within your Express/Nunjucks application

33 lines 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getPaginationLinks; function getPaginationLinks(currentPage, totalPages, totalResults, pathFn, pageSize = 10, maxPagesToShow = 7) { const firstPage = firstPageToShow(currentPage, maxPagesToShow); const lastPage = lastPageToShow(currentPage, maxPagesToShow, totalPages); return { from: ((currentPage - 1) * pageSize + 1).toLocaleString('en-GB'), to: Math.min(currentPage * pageSize, totalResults).toLocaleString('en-GB'), total: totalResults.toLocaleString('en-GB'), next: currentPage < totalPages ? pathFn(currentPage + 1) : undefined, prev: currentPage > 1 ? pathFn(currentPage - 1) : undefined, items: getPageLinks(firstPage, lastPage, currentPage, totalPages, pathFn), }; } function firstPageToShow(currentPage, maxPagesToShow) { return Math.max(currentPage - Math.floor(maxPagesToShow / 2), 1); } function lastPageToShow(currentPage, maxPagesToShow, totalPages) { return Math.min(currentPage + Math.floor(maxPagesToShow / 2), totalPages); } function getPageLinks(firstPage, lastPage, currentPage, totalPages, pathFn) { return [ ...(firstPage > 1 ? [{ ellipsis: true }] : []), ...Array.from({ length: lastPage - firstPage + 1 }, (_, i) => firstPage + i).map(pageNumber => ({ number: pageNumber, current: currentPage === pageNumber, href: pathFn(pageNumber), })), ...(lastPage < totalPages ? [{ ellipsis: true }] : []), ]; } //# sourceMappingURL=pagination.js.map