All files / modules/pagination index.js

100% Statements 11/11
100% Branches 4/4
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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