rampage

0.1.0 • Public • Published

Rampage.js npm Version Build Status Coverage Status

A generic way to split an array into pages, with optional callbacks to modify the structure of each page.

API

rampage(arr, numPerPage [, opts])

var arr = [
  { foo: 'bar' },
  { foo: 'baz' },
  { foo: 'qux' }
];
 
rampage(arr, 2);
/* [
 *   [ arr[0], arr[1] ],
 *   [ arr[2] ]
 * ]
 */

Pass in opts if you want to create previous/next links or customise the structure of each page:

var opts = {
  preProcess: function(pageItems, pageNum, totalPages) {
    return {
      pageItems: pageItems,
      pageNum: pageNum
    };
  },
  postProcess: function(currPage, prevPage, nextPage, pageNum, totalPages) {
    currPage.prevPage = prevPage;
    currPage.nextPage = nextPage;
    return currPage;
  }
};
 
var result = rampage(arr, 2, opts);
/* [
 *   {
 *     pageItems: [ arr[0], arr[1] ],
 *     pageNum: 0,
 *     prevPage: undefined,
 *     nextPage: result[1]
 *   },
 *   {
 *     pageItems: [ arr[2] ],
 *     pageNum: 1,
 *     prevPage: result[0],
 *     nextPage: undefined
 *   }
 * ]
 */

The opts.preProcess function maps over each slice of arr. It takes the following arguments:

  1. pageItems — The current slice of arr, which would have at most numPerPage number of items.
  2. pageNum — The current page number. Page numbers start from 0.
  3. totalPages — The total number of pages.

The opts.postProcess function maps over the result of opts.preProcess. It takes the following arguments:

  1. currPage — The current page.
  2. prevPage — A reference to the previous page, or undefined if there is no previous page.
  3. nextPage — A reference to the next page, or undefined if there is no next page.
  4. pageNum — The current page number. Page numbers start from 0.
  5. totalPages — The total number of pages.

Installation

Install via npm:

$ npm i --save rampage

Changelog

  • 0.1.0
    • Initial release

License

MIT license

Package Sidebar

Install

npm i rampage

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • yuanqing