Code coverage report for helpers/calcPagination.js

Statements: 100% (25 / 25)      Branches: 100% (18 / 18)      Functions: 100% (2 / 2)      Lines: 100% (19 / 19)      Ignored: 1 branch     

All files » helpers/ » calcPagination.js
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                    1 10 10           10 2 8 2   10 3   10 10 10 10 8           8 2     8     10                
/**
 * calcPagination
 *
 * @module calcPagination
 *
 * @author Luca Pau <luca.pau82@gmail.com>
 */
 
import config from '../config';
 
export default function calcPagination(currentPage, totalItems, itemsPerPage) {
  itemsPerPage = parseInt(itemsPerPage);
  currentPage = parseInt(currentPage);
  //totalItems = parseInt(totalItems);
 
  //if(isNaN(totalItems))
  //  throw new TypeError('totalItems must be a valid number');
 
  if (isNaN(itemsPerPage)) {
    itemsPerPage = config.resultsPerPageDefault;
  } else if (itemsPerPage > config.resultsPerPageMax) {
    itemsPerPage = config.resultsPerPageMax;
  }
  if (isNaN(currentPage) || currentPage < 1) {
    currentPage = 1;
  }
  var limit = itemsPerPage;
  var skip = 0;
  var totalPages = 0;
  if (totalItems > 0) {
    totalPages = (
      totalItems % itemsPerPage === 0 ?
      totalItems / itemsPerPage :
      parseInt(totalItems / itemsPerPage) + 1
    );
 
    if (currentPage > totalPages) {
      currentPage = totalPages;
    }
 
    skip = (currentPage - 1) * limit;
  }
 
  return {
    skip: skip,
    limit: limit,
    totalPages: totalPages,
    currentPage: currentPage,
    itemsPerPage: itemsPerPage,
    totalItems: totalItems
  };
}