paginationator
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -43,4 +43,5 @@ /*! | ||
var pages = new Pages(), page = new Page(); | ||
var pages = new Pages(); | ||
while (i < total) { | ||
var page = new Page(); | ||
var start = i * limit; | ||
@@ -51,3 +52,2 @@ var end = start + limit; | ||
i++; | ||
page = new Page(); | ||
} | ||
@@ -54,0 +54,0 @@ return pages; |
'use strict'; | ||
var define = require('define-property'); | ||
/** | ||
@@ -18,55 +16,74 @@ * Page constructor | ||
if (!page) page = {}; | ||
for (var key in page) { | ||
this[key] = page[key]; | ||
for (var key in page) this[key] = page[key]; | ||
if (!this.hasOwnProperty('idx')) this.idx = 0; | ||
if (!this.hasOwnProperty('total')) this.total = 1; | ||
if (!this.hasOwnProperty('current')) { | ||
this.current = this.total; | ||
} | ||
this.initPage(); | ||
} | ||
/** | ||
* Initialize default properties | ||
* Page getters | ||
*/ | ||
Page.prototype.initPage = function() { | ||
this.idx = (typeof this.idx === 'undefined') ? 0 : this.idx; | ||
this.total = (typeof this.total === 'undefined') ? 1 : this.total; | ||
this.current = (typeof this.current === 'undefined') ? this.total : this.current; | ||
}; | ||
Object.defineProperties(Page.prototype, { | ||
/** | ||
* Helper property to determine if this is the first page in a list. | ||
*/ | ||
/** | ||
* Helper property to determine if this is the first page in a list. | ||
*/ | ||
define(Page.prototype, 'isFirst', { | ||
get: function() { | ||
return this.idx === 0; | ||
} | ||
}); | ||
isFirst: { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { | ||
return this.idx === 0; | ||
} | ||
}, | ||
/** | ||
* Helper property to determine if this is the last page in a list. | ||
*/ | ||
/** | ||
* Helper property to determine if this is the last page in a list. | ||
*/ | ||
define(Page.prototype, 'isLast', { | ||
get: function() { | ||
return this.idx === (this.total - 1); | ||
} | ||
}); | ||
isLast: { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { | ||
return this.idx === (this.total - 1); | ||
} | ||
}, | ||
/** | ||
* Helper property to determine if this is there is a page before this one in a list. | ||
*/ | ||
/** | ||
* Helper property to determine if this is there is a page before this one in a list. | ||
*/ | ||
define(Page.prototype, 'hasPrevious', { | ||
get: function() { | ||
return !this.isFirst; | ||
} | ||
}); | ||
hasPrevious: { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { | ||
return !this.isFirst; | ||
} | ||
}, | ||
/** | ||
* Helper property to determine if this is there is a page after this one in a list. | ||
*/ | ||
/** | ||
* Helper property to determine if this is there is a page before this one in a list. | ||
*/ | ||
define(Page.prototype, 'hasNext', { | ||
get: function() { | ||
return !this.isLast; | ||
hasPrev: { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { | ||
return !this.isFirst; | ||
} | ||
}, | ||
/** | ||
* Helper property to determine if this is there is a page after this one in a list. | ||
*/ | ||
hasNext: { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { | ||
return !this.isLast; | ||
} | ||
} | ||
@@ -73,0 +90,0 @@ }); |
143
lib/pages.js
'use strict'; | ||
var define = require('define-property'); | ||
var Page = require('./page'); | ||
@@ -19,11 +18,7 @@ | ||
this.pages = []; | ||
if (!pages) return; | ||
if (typeof pages === 'undefined') return; | ||
if (!Array.isArray(pages)) { | ||
throw new TypeError('expected pages to be an Array'); | ||
} | ||
pages.forEach(function(page) { | ||
this.addPage(page); | ||
}.bind(this)); | ||
this.addPages(pages); | ||
} | ||
@@ -39,3 +34,3 @@ | ||
* @param {Object} `page` Plain object or instance of a `Page` | ||
* @return {Object} Returns `this` for chaining | ||
* @return {Object} Returns the instance for chaining | ||
* @api public | ||
@@ -45,6 +40,23 @@ */ | ||
Pages.prototype.addPage = function(page) { | ||
if (!(page instanceof Page)) { | ||
page = new Page(page); | ||
if (!(page instanceof Page)) page = new Page(page); | ||
this.pages.push(decorate(this, page)); | ||
return this; | ||
}; | ||
/** | ||
* Add an array of pages to the list. | ||
* | ||
* ```js | ||
* pages.addPages([...]); | ||
* ``` | ||
* | ||
* @param {Object} `pages` Array of page objects | ||
* @return {Object} Returns the instance for chaining | ||
* @api public | ||
*/ | ||
Pages.prototype.addPages = function(pages) { | ||
for (var i = 0; i < pages.length; i++) { | ||
this.addPage(pages[i]); | ||
} | ||
this.pages.push(this.decoratePage(page)); | ||
return this; | ||
@@ -57,41 +69,42 @@ }; | ||
* @param {Object} `page` Instance of page to decorate | ||
* @return {Object} Returns the decoracted page to be added to the list | ||
* @return {Object} Returns the decorated page to be added to the list | ||
*/ | ||
Pages.prototype.decoratePage = function(page) { | ||
var self = this; | ||
define(page, 'first', { | ||
enumerable: true, | ||
get: function() { | ||
return self.first && self.first.current; | ||
function decorate(pages, page) { | ||
Object.defineProperties(page, { | ||
first: { | ||
enumerable: true, | ||
set: function() {}, | ||
get: function() { | ||
return pages.first && pages.first.current; | ||
} | ||
}, | ||
set: function() {} | ||
}); | ||
define(page, 'current', { | ||
enumerable: true, | ||
get: function() { | ||
return this.idx + 1; | ||
current: { | ||
enumerable: true, | ||
set: function() {}, | ||
get: function() { | ||
return this.idx + 1; | ||
} | ||
}, | ||
set: function() {} | ||
}); | ||
define(page, 'last', { | ||
enumerable: true, | ||
get: function() { | ||
return self.last && self.last.current; | ||
last: { | ||
enumerable: true, | ||
set: function() {}, | ||
get: function() { | ||
return pages.last && pages.last.current; | ||
} | ||
}, | ||
set: function() {} | ||
}); | ||
define(page, 'total', { | ||
enumerable: true, | ||
get: function() { | ||
return self.total; | ||
}, | ||
set: function() {} | ||
total: { | ||
enumerable: true, | ||
set: function() {}, | ||
get: function() { | ||
return pages.total; | ||
} | ||
} | ||
}); | ||
var prev = this.last; | ||
var idx = this.total; | ||
var prev = pages.last; | ||
var idx = pages.total; | ||
page.idx = idx; | ||
@@ -102,33 +115,39 @@ if (prev) { | ||
} | ||
return page; | ||
}; | ||
} | ||
/** | ||
* Helper property to calculate the total pages in the array. | ||
* Getters | ||
*/ | ||
define(Pages.prototype, 'total', { | ||
get: function() { | ||
return this.pages.length; | ||
} | ||
}); | ||
Object.defineProperties(Pages.prototype, { | ||
/** | ||
* Helper property to get the first page from the array. | ||
*/ | ||
/** | ||
* Helper property to calculate the total pages in the array. | ||
*/ | ||
define(Pages.prototype, 'first', { | ||
get: function() { | ||
return this.total > 0 ? this.pages[0] : null; | ||
} | ||
}); | ||
total: { | ||
get: function() { | ||
return this.pages.length; | ||
} | ||
}, | ||
/** | ||
* Helper property to get the last page from the array. | ||
*/ | ||
/** | ||
* Helper property to get the first page from the array. | ||
*/ | ||
define(Pages.prototype, 'last', { | ||
get: function() { | ||
return this.total > 0 ? this.pages[this.total - 1] : null; | ||
first: { | ||
get: function() { | ||
return this.total > 0 ? this.pages[0] : null; | ||
} | ||
}, | ||
/** | ||
* Helper property to get the last page from the array. | ||
*/ | ||
last: { | ||
get: function() { | ||
return this.total > 0 ? this.pages[this.total - 1] : null; | ||
} | ||
} | ||
@@ -135,0 +154,0 @@ }); |
{ | ||
"name": "paginationator", | ||
"description": "Paginate an array into pages of items.", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"homepage": "https://github.com/doowb/paginationator", | ||
@@ -14,3 +14,5 @@ "author": "Brian Woodward (https://github.com/doowb)", | ||
"index.js", | ||
"lib/" | ||
"lib", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
@@ -24,14 +26,21 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"define-property": "^0.2.5" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
"gulp-format-md": "^0.1.9", | ||
"mocha": "^2.5.3" | ||
}, | ||
"keywords": [ | ||
"paginationator" | ||
], | ||
"verb": { | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"related": { | ||
"list": [ | ||
"assemble", | ||
"base-methods", | ||
"templates", | ||
@@ -41,6 +50,10 @@ "verb" | ||
}, | ||
"plugins": [ | ||
"gulp-format-md" | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"verb-generate-readme" | ||
] | ||
} | ||
} |
@@ -1,15 +0,15 @@ | ||
# paginationator [![NPM version](https://img.shields.io/npm/v/paginationator.svg)](https://www.npmjs.com/package/paginationator) [![Build Status](https://img.shields.io/travis/doowb/paginationator.svg)](https://travis-ci.org/doowb/paginationator) | ||
# paginationator [![NPM version](https://img.shields.io/npm/v/paginationator.svg?style=flat)](https://www.npmjs.com/package/paginationator) [![NPM downloads](https://img.shields.io/npm/dm/paginationator.svg?style=flat)](https://npmjs.org/package/paginationator) [![Build Status](https://img.shields.io/travis/doowb/paginationator.svg?style=flat)](https://travis-ci.org/doowb/paginationator) | ||
> Paginate an array into pages of items. | ||
Paginate an array into pages of items. | ||
![image](https://cloud.githubusercontent.com/assets/995160/9802527/ca15f300-57e8-11e5-96db-523ea5a0572e.png) | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i paginationator --save | ||
$ npm install --save paginationator | ||
``` | ||
![image](https://cloud.githubusercontent.com/assets/995160/9802527/ca15f300-57e8-11e5-96db-523ea5a0572e.png) | ||
## Usage | ||
@@ -45,3 +45,3 @@ | ||
### [Page](lib/page.js#L16) | ||
### [Page](lib/page.js#L14) | ||
@@ -60,3 +60,3 @@ Page constructor | ||
### [Pages](lib/pages.js#L17) | ||
### [Pages](lib/pages.js#L16) | ||
@@ -75,3 +75,3 @@ Pages constructor | ||
### [.addPage](lib/pages.js#L42) | ||
### [.addPage](lib/pages.js#L37) | ||
@@ -83,3 +83,3 @@ Add a page to the list. | ||
* `page` **{Object}**: Plain object or instance of a `Page` | ||
* `returns` **{Object}**: Returns `this` for chaining | ||
* `returns` **{Object}**: Returns the instance for chaining | ||
@@ -92,35 +92,61 @@ **Example** | ||
## Related projects | ||
### [.addPages](lib/pages.js#L55) | ||
* [assemble](https://www.npmjs.com/package/assemble): Static site generator for Grunt.js, Yeoman and Node.js. Used by Zurb Foundation, Zurb Ink, H5BP/Effeckt,… [more](https://www.npmjs.com/package/assemble) | [homepage](http://assemble.io) | ||
* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods) | ||
* [templates](https://www.npmjs.com/package/templates): System for creating and managing template collections, and rendering templates with any node.js template engine.… [more](https://www.npmjs.com/package/templates) | [homepage](https://github.com/jonschlinkert/templates) | ||
* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://www.npmjs.com/package/verb) | [homepage](https://github.com/verbose/verb) | ||
Add an array of pages to the list. | ||
## Running tests | ||
**Params** | ||
Install dev dependencies: | ||
* `pages` **{Object}**: Array of page objects | ||
* `returns` **{Object}**: Returns the instance for chaining | ||
**Example** | ||
```js | ||
pages.addPages([...]); | ||
``` | ||
## About | ||
### Related projects | ||
* [assemble](https://www.npmjs.com/package/assemble): Get the rocks out of your socks! Assemble makes you fast at creating web projects… [more](https://github.com/assemble/assemble) | [homepage](https://github.com/assemble/assemble "Get the rocks out of your socks! Assemble makes you fast at creating web projects. Assemble is used by thousands of projects for rapid prototyping, creating themes, scaffolds, boilerplates, e-books, UI components, API documentation, blogs, building websit") | ||
* [templates](https://www.npmjs.com/package/templates): System for creating and managing template collections, and rendering templates with any node.js template engine… [more](https://github.com/jonschlinkert/templates) | [homepage](https://github.com/jonschlinkert/templates "System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.") | ||
* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://github.com/verbose/verb) | [homepage](https://github.com/verbose/verb "Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used on hundreds of projects of all sizes to generate everything from API docs to readmes.") | ||
### Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Building docs | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
```sh | ||
$ npm i -d && npm test | ||
$ npm install -g verb verb-generate-readme && verb | ||
``` | ||
## Contributing | ||
### Running tests | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/paginationator/issues/new). | ||
Install dev dependencies: | ||
## Author | ||
```sh | ||
$ npm install -d && npm test | ||
``` | ||
### Author | ||
**Brian Woodward** | ||
* [github/doowb](https://github.com/doowb) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/doowb](http://twitter.com/doowb) | ||
## License | ||
### License | ||
Copyright © 2015 [Brian Woodward](https://github.com/doowb) | ||
Released under the MIT license. | ||
Copyright © 2016, [Brian Woodward](https://github.com/doowb). | ||
Released under the [MIT license](https://github.com/doowb/paginationator/blob/master/LICENSE). | ||
*** | ||
_This file was generated by [verb](https://github.com/verbose/verb) on December 20, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on July 19, 2016._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13025
0
267
147
0
- Removeddefine-property@^0.2.5
- Removeddefine-property@0.2.5(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-accessor-descriptor@1.0.1(transitive)
- Removedis-data-descriptor@1.0.1(transitive)
- Removedis-descriptor@0.1.7(transitive)