1.0.0 • Published 9 years ago

simple-pagination v1.0.0

Weekly downloads
403
License
MIT
Repository
github
Last release
9 years ago

simple-pagination

npm version Build Status

Just provides only a simple pagination logic

Installation

npm install --save simple-pagination

API

/*
 * Returns pagination result
 *
 * @param {number} totalCount    - Number of items that will be paginated
 * @param {number} perPage       - Number of items per page
 * @param {number} specifiedPage - Page number you wanted to get, it is started by 1
 */
function paginate(totalCount, perPage, specifiedPage) {
};

Examples

var assert = require('assert');
var paginate = require('simple-pagination');

assert.deepEqual(
  paginate(25, 10, 1), {
    totalCount: 25,
    perPage: 10,
    specifiedPage: 1,
    pageCount: 3,
    firstPage: 1,
    lastPage: 3,
    currentPage: 1,
    isFirstPage: true,
    isLastPage: false,
    previousPage: null,
    nextPage: 2,
    fromCount: 1,
    toCount: 10
  }
);

assert.deepEqual(
  paginate(25, 10, 3), {
    totalCount: 25,
    perPage: 10,
    specifiedPage: 3,
    pageCount: 3,
    firstPage: 1,
    lastPage: 3,
    currentPage: 3,
    isFirstPage: false,
    isLastPage: true,
    previousPage: 2,
    nextPage: null,
    fromCount: 21,
    toCount: 25
  }
);

// Specified page number is out of range
assert.deepEqual(
  paginate(25, 10, 4), {
    totalCount: 25,
    perPage: 10,
    specifiedPage: 4,
    pageCount: 3,
    firstPage: 1,
    lastPage: 3,
    // currentPage is different from specifiedPage
    currentPage: 3,
    isFirstPage: false,
    isLastPage: true,
    previousPage: 2,
    nextPage: null,
    fromCount: 21,
    toCount: 25
  }
);

// If page count is 0, then props are mostly null
assert.deepEqual(
  paginate(0, 1, 1), {
    totalCount: 0,
    perPage: 1,
    specifiedPage: 1,
    pageCount: 0,
    firstPage: null,
    lastPage: null,
    currentPage: null,
    isFirstPage: null,
    isLastPage: null,
    previousPage: null,
    nextPage: null,
    fromCount: null,
    toCount: null
  }
);