rampage
v0.1.0
Published
A generic way to split an array into pages, with optional callbacks to modify the structure of each page.
Downloads
10
Maintainers
Readme
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' }
];
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:
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