Rampage.js
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' ; ;/* [ * [ 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 = { return pageItems: pageItems pageNum: pageNum ; } { currPageprevPage = prevPage; currPagenextPage = nextPage; return currPage; }; var result = ;/* [ * { * 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:
pageItems
— The current slice ofarr
, which would have at mostnumPerPage
number of items.pageNum
— The current page number. Page numbers start from0
.totalPages
— The total number of pages.
The opts.postProcess
function maps over the result of opts.preProcess
. It takes the following arguments:
currPage
— The current page.prevPage
— A reference to the previous page, orundefined
if there is no previous page.nextPage
— A reference to the next page, orundefined
if there is no next page.pageNum
— The current page number. Page numbers start from0
.totalPages
— The total number of pages.
Installation
Install via npm:
$ npm i --save rampage
Changelog
- 0.1.0
- Initial release