Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,28 @@ console.log(paginateCollection)
data: [...]
}
```
## Override default labels

```js

const paginate = require("paginate-array");
const collection = [...];

const labels = {
currentPage: 'current_page',
perPage: 'per_page',
data: 'docs'
}

const paginateCollection = paginate(collection[,pageNumber, numItemsPerPage, labels]);

console.log(paginateCollection)

{
current_page: 1,
per_page: 10,
total: 20,
totalPages: 2,
docs: [...]
}
```
26 changes: 20 additions & 6 deletions lib/paginate-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.default = function (collection) {
var page = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
var numItems = arguments.length <= 2 || arguments[2] === undefined ? 10 : arguments[2];
var page = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var numItems = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
var labels = arguments[3];

if (!Array.isArray(collection)) {
throw "Expect array and got " + (typeof collection === "undefined" ? "undefined" : _typeof(collection));
Expand All @@ -18,14 +19,27 @@ exports.default = function (collection) {
var offset = (page - 1) * perPage;
var paginatedItems = collection.slice(offset, offset + perPage);

return {
var res = {
currentPage: currentPage,
perPage: perPage,
total: collection.length,
totaPages: Math.ceil(collection.length / perPage),
totalPages: Math.ceil(collection.length / perPage),
data: paginatedItems
};

if (labels) {
var keys = Object.keys(res);
keys.forEach(function (item) {
if (labels[item]) {
var temp = res[item];
delete res[item];

var newKey = labels[item];
res[newKey] = temp;
}
});
}
return res;
};

module.exports = exports["default"];
module.exports = exports["default"];
Loading