@ssense/node-paginator
Advanced tools
Comparing version 1.0.5 to 1.1.0
{ | ||
"name": "@ssense/node-paginator", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Paginator", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -20,52 +20,61 @@ # Paginator | ||
$ npm install --save @ssense/node-paginator | ||
```bash | ||
npm install --save @ssense/node-paginator | ||
``` | ||
### Usage | ||
import { Paginator } from '@ssense/node-paginator'; | ||
const items = ['item1', 'item2', 'item3']; // from your DB returned collection... | ||
const perPage = 1; | ||
const currentPage = 2; | ||
const paginator: Paginator = new Paginator(items, items.length, perPage, currentPage); | ||
// Transform paginator to object, use this as rour REST response | ||
const paginated = paginator.transform(); | ||
console.log(paginated); | ||
/* | ||
output | ||
{ | ||
total: 3, | ||
per_page: 1, | ||
current_page: 2, | ||
last_page: 3, | ||
from: 2, | ||
to: 4, | ||
links:{ | ||
previous: '/?page=1&foo=bar#foobar', | ||
current: '/?page=2&foo=bar#foobar', | ||
next: '/?page=3&foo=bar#foobar' | ||
}, | ||
data: [ 'item1', 'item2', 'item3' ] | ||
} | ||
*/ | ||
```js | ||
import { Paginator } from '@ssense/node-paginator'; | ||
const items = ['item1', 'item2', 'item3']; // from your DB returned collection... | ||
const perPage = 1; | ||
const currentPage = 2; | ||
const paginator: Paginator = new Paginator(items, items.length, perPage, currentPage); | ||
// Transform paginator to object, use this as rour REST response | ||
const paginated = paginator.transform(); | ||
console.log(paginated); | ||
/* | ||
output | ||
{ | ||
total: 3, | ||
per_page: 1, | ||
current_page: 2, | ||
last_page: 3, | ||
from: 2, | ||
to: 4, | ||
links:{ | ||
previous: '/?page=1&foo=bar#foobar', | ||
current: '/?page=2&foo=bar#foobar', | ||
next: '/?page=3&foo=bar#foobar' | ||
}, | ||
data: [ 'item1', 'item2', 'item3' ] | ||
} | ||
*/ | ||
``` | ||
### Tips and tricks | ||
#### Append queries string to URL | ||
// Append one query string to URL | ||
paginator.append('key', 'value'); | ||
// Appends multiple queries to URL | ||
paginator.appends({ | ||
foo: 'bar' | ||
}); | ||
```js | ||
// Append one query string to URL | ||
paginator.append('key', 'value'); | ||
// Appends multiple queries to URL | ||
paginator.appends({ | ||
foo: 'bar' | ||
}); | ||
``` | ||
#### Add fragment to URL | ||
// Add foobar fragment to URL | ||
paginator.fragment('foobar'); | ||
```js | ||
// Add foobar fragment to URL | ||
paginator.fragment('foobar'); | ||
``` | ||
### License | ||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
@@ -0,1 +1,2 @@ | ||
export * from './PaginatorMeta'; | ||
export * from './Paginator'; |
@@ -5,3 +5,4 @@ "use strict"; | ||
} | ||
__export(require("./PaginatorMeta")); | ||
__export(require("./Paginator")); | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
import { PaginatorMeta } from './PaginatorMeta'; | ||
export declare type PaginatorInterface = { | ||
@@ -22,9 +23,4 @@ total: number; | ||
}; | ||
export declare class Paginator { | ||
protected _hasMore: boolean; | ||
export declare class Paginator extends PaginatorMeta { | ||
protected _items: any[]; | ||
protected _total: number; | ||
protected _lastPage: number; | ||
protected _perPage: number; | ||
protected _currentPage: number; | ||
protected _options: PaginatorOptions; | ||
@@ -40,18 +36,5 @@ protected _defaultOptions: PaginatorOptions; | ||
items(): any[]; | ||
total(): number; | ||
lastPage(): number; | ||
firstItem(): number; | ||
lastItem(): number; | ||
perPage(): number; | ||
onFirstPage(): boolean; | ||
currentPage(): number; | ||
hasPages(): boolean; | ||
hasMorePages(): boolean; | ||
isEmpty(): boolean; | ||
count(): number; | ||
url(page?: number): string; | ||
previousPageUrl(): string; | ||
nextPageUrl(): string; | ||
protected setCurrentPage(currentPage: number): number; | ||
protected isValidPageNumber(page: number): boolean; | ||
getPageName(): string; | ||
@@ -58,0 +41,0 @@ setPageName(name: string): Paginator; |
"use strict"; | ||
const querystring = require("querystring"); | ||
class Paginator { | ||
const PaginatorMeta_1 = require("./PaginatorMeta"); | ||
class Paginator extends PaginatorMeta_1.PaginatorMeta { | ||
constructor(items, total, perPage, currentPage, options) { | ||
super(total, perPage, currentPage); | ||
this._defaultOptions = { | ||
@@ -13,6 +15,2 @@ path: '/', | ||
this._items = items; | ||
this._total = total; | ||
this._perPage = perPage; | ||
this._lastPage = Math.ceil(total / perPage); | ||
this._currentPage = this.setCurrentPage(currentPage); | ||
this._options = options || this._defaultOptions; | ||
@@ -49,41 +47,2 @@ this.setPageName(this._options.pageName || 'page'); | ||
} | ||
total() { | ||
return this._total; | ||
} | ||
lastPage() { | ||
return this._lastPage; | ||
} | ||
firstItem() { | ||
if (this._items.length === 0) { | ||
return; | ||
} | ||
return (this._currentPage - 1) * this._perPage + 1; | ||
} | ||
lastItem() { | ||
if (this._items.length === 0) { | ||
return; | ||
} | ||
return this.firstItem() + this.count() - 1; | ||
} | ||
perPage() { | ||
return this._perPage; | ||
} | ||
onFirstPage() { | ||
return this.currentPage() <= 1; | ||
} | ||
currentPage() { | ||
return this._currentPage; | ||
} | ||
hasPages() { | ||
return !(this.currentPage() === 0 && !this.hasMorePages()); | ||
} | ||
hasMorePages() { | ||
return this.currentPage() < this.lastPage(); | ||
} | ||
isEmpty() { | ||
return this._items.length === 0; | ||
} | ||
count() { | ||
return this._items.length; | ||
} | ||
url(page) { | ||
@@ -113,9 +72,2 @@ page = (page <= 0) ? 1 : page; | ||
} | ||
setCurrentPage(currentPage) { | ||
currentPage = currentPage || 1; | ||
return this.isValidPageNumber(currentPage) ? currentPage : 1; | ||
} | ||
isValidPageNumber(page) { | ||
return page >= 1; | ||
} | ||
getPageName() { | ||
@@ -122,0 +74,0 @@ return this._pageName; |
@@ -0,1 +1,2 @@ | ||
export * from './PaginatorMeta'; | ||
export * from './Paginator'; |
import * as querystring from 'querystring'; | ||
import {PaginatorMeta} from './PaginatorMeta'; | ||
@@ -50,10 +51,4 @@ export type PaginatorInterface = { | ||
export class Paginator { | ||
export class Paginator extends PaginatorMeta { | ||
/** | ||
* Determine if there are more items in the data source. | ||
*/ | ||
protected _hasMore: boolean; // tslint:disable-line variable-name | ||
/** | ||
* All of the items being paginated. | ||
@@ -66,30 +61,2 @@ * | ||
/** | ||
* The total number of items before slicing. | ||
* | ||
* @var int | ||
*/ | ||
protected _total: number; // tslint:disable-line variable-name | ||
/** | ||
* The last available page. | ||
* | ||
* @var int | ||
*/ | ||
protected _lastPage: number; // tslint:disable-line variable-name | ||
/** | ||
* The number of items to be shown per page. | ||
* | ||
* @var int | ||
*/ | ||
protected _perPage: number; // tslint:disable-line variable-name | ||
/** | ||
* The current page being "viewed". | ||
* | ||
* @var int | ||
*/ | ||
protected _currentPage: number; // tslint:disable-line variable-name | ||
/** | ||
* Paginator options | ||
@@ -130,7 +97,4 @@ * | ||
constructor(items: any[], total: number, perPage: number, currentPage: number, options?: PaginatorOptions) { | ||
super(total, perPage, currentPage); | ||
this._items = items; | ||
this._total = total; | ||
this._perPage = perPage; | ||
this._lastPage = Math.ceil(total / perPage); | ||
this._currentPage = this.setCurrentPage(currentPage); | ||
this._options = options || this._defaultOptions; | ||
@@ -214,107 +178,2 @@ | ||
/** | ||
* Get the total number of items being paginated. | ||
* @returns {number} | ||
*/ | ||
public total(): number { | ||
return this._total; | ||
} | ||
/** | ||
* Get the last page. | ||
* | ||
* @returns {number} | ||
*/ | ||
public lastPage(): number { | ||
return this._lastPage; | ||
} | ||
/** | ||
* Get the number of the first item in the slice. | ||
* | ||
* @returns {number} | ||
*/ | ||
public firstItem(): number { // number|void | ||
if (this._items.length === 0) { | ||
return; | ||
} | ||
return (this._currentPage - 1) * this._perPage + 1; | ||
} | ||
/** | ||
* Get the number of the last item in the slice. | ||
* | ||
* @returns {number} | ||
*/ | ||
public lastItem(): number { // number|void | ||
if (this._items.length === 0) { | ||
return; | ||
} | ||
return this.firstItem() + this.count() - 1; | ||
} | ||
/** | ||
* Get the number of items shown per page. | ||
* | ||
* @returns {number} | ||
*/ | ||
public perPage(): number { | ||
return this._perPage; | ||
} | ||
/** | ||
* Determine if the paginator is on the first page. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
public onFirstPage(): boolean { | ||
return this.currentPage() <= 1; | ||
} | ||
/** | ||
* Get the current page. | ||
* | ||
* @returns {number} | ||
*/ | ||
public currentPage(): number { | ||
return this._currentPage; | ||
} | ||
/** | ||
* Determine if there are enough items to split into multiple pages. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
public hasPages(): boolean { | ||
return ! (this.currentPage() === 0 && ! this.hasMorePages()); | ||
} | ||
/** | ||
* Determine if there are more items in the data source. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
public hasMorePages(): boolean { | ||
return this.currentPage() < this.lastPage(); | ||
} | ||
/** | ||
* Determine if the list of items is empty or not. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
public isEmpty(): boolean { | ||
return this._items.length === 0; | ||
} | ||
/** | ||
* Get the number of items for the current page. | ||
* | ||
* @returns {number} | ||
*/ | ||
public count(): number { | ||
return this._items.length; | ||
} | ||
/** | ||
* Get the URL for a given page number. | ||
@@ -367,24 +226,2 @@ * | ||
/** | ||
* Get the current page for the request. | ||
* | ||
* @param currentPage | ||
* @returns {number} | ||
*/ | ||
protected setCurrentPage(currentPage: number): number { | ||
currentPage = currentPage || 1; | ||
return this.isValidPageNumber(currentPage) ? currentPage : 1; | ||
} | ||
/** | ||
* Determine if the given value is a valid page number. | ||
* | ||
* @param page | ||
* @returns {boolean} | ||
*/ | ||
protected isValidPageNumber(page: number): boolean { | ||
return page >= 1; | ||
} | ||
/** | ||
* Get the query string variable used to store the page. | ||
@@ -391,0 +228,0 @@ * |
Sorry, the diff of this file is not supported yet
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
103404
22
656
80