Source: pagination/index.js

  1. /**
  2. * @module pagination
  3. */
  4. const {calculateLimitAndOffset, paginate} = require('paginate-info');
  5. /**
  6. * Get page data according to input page number and
  7. * desired page size.
  8. * Returns paginated data and metadata.
  9. * @param {Number} currentPageIn current page number
  10. * @param {Number} pageSizeIn desired page size
  11. * @param {Array} data
  12. * @return {Object} data and meta as object
  13. */
  14. const getPageData = (currentPageIn, pageSizeIn, data) => {
  15. const currentPage = currentPageIn ? currentPageIn : 1;
  16. const pageSize = pageSizeIn ? pageSizeIn : 10;
  17. const {limit, offset} = calculateLimitAndOffset(currentPage, pageSize);
  18. const count = data.length;
  19. const paginatedData = data.slice(offset, offset + limit);
  20. const paginationInfo = paginate(currentPage, count, data, pageSize);
  21. paginationInfo.pageSize = parseInt(pageSize, 10);
  22. return {
  23. meta: paginationInfo,
  24. data: paginatedData,
  25. };
  26. };
  27. module.exports = {
  28. getPageData,
  29. };