Skip to content
This repository has been archived by the owner on Mar 11, 2018. It is now read-only.

yuanqing/rampage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Usage

var arr = [
  { foo: 'bar' },
  { foo: 'baz' },
  { foo: 'qux' }
];

rampage(arr, 2);
/* [
 *   [ arr[0], arr[1] ],
 *   [ arr[2] ]
 * ]
 */

Note that a new array is returned; the original arr is unchanged.

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
 *   }
 * ]
 */

API

rampage(arr, numPerPage [, opts])

  • arr — An array.

  • numPerPage — A positive integer.

  • opts — Used to define the preProcess and preProcess callbacks.

    • opts.preProcess maps over each slice of arr. It defaults to the identity function, and 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.
    • opts.postProcess 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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published