Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pagination-template

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pagination-template - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

README.md

154

lib/pagination.js
/**
Module for create a pagination for use in a templates
**/
module.exports = Pagination;

@@ -14,3 +15,3 @@ /**

options: {
maxPagination : 10, //Maximum number of indeces show
maxPagination : 10, //Maximum number of indexes show
//If for example we have a 20 pages and

@@ -25,87 +26,82 @@ //the maxPagination value is 10 the result is

**/
exports.pagination = function(urlOrigin, pageAct, totalResults, rowsPerPage, options){
if(options === undefined){
options = {};
function Pagination(urlOrigin, pageAct, totalResults, rowsPerPage, options){
this.options = options || {};
this.pageAct = pageAct;
this.urlOrigin = urlOrigin;
this.totalResults = totalResults;
this.rowsPerPage = rowsPerPage;
}
/**
Return an array with the objects for pagination
@return An array with objects {
url: /search/1/10
actualpage: true //indicate if is the actual page
index: 1 //if the number of page
}
**/
Pagination.prototype.getPagination = function(){
var maxPagination = 0;
var ini = 1;
var pages = Math.floor((this.totalResults / this.rowsPerPage)) + ((this.totalResults % this.rowsPerPage)>0 ? 1:0);
var last = pages;
var end = pages;
var paginationResult = [];
//check option maxPagination
if(this.options!==undefined && this.options.maxPagination>0){
maxPagination = this.options.maxPagination;
}
else{
options = options;
//check the begin of pagination
if(maxPagination>0 && this.pageAct>(maxPagination/2)){
ini = this.pageAct - (maxPagination/2);
}
//check the finish of pagination
if(maxPagination>0 && (maxPagination+ini) < last){
end = (ini + maxPagination) -1;
}
/**
Return an array with the objects for pagination
@return An array with objects {
url: /search/1/10
actualpage: true //indicate if is the actual page
index: 1 //if the number of page
}
**/
this.getPagination = function (){
var maxPages = totalResults / rowsPerPage;
var maxPagination = 0;
var ini = 1;
var pages = Math.floor((totalResults / rowsPerPage)) + ((totalResults % rowsPerPage)>0 ? 1:0);
var last = pages;
var end = pages;
var paginationResult = [];
//check option maxPagination
if(options!==undefined && options.maxPagination>0){
maxPagination = options.maxPagination;
console.log(ini, end, maxPagination);
if(maxPagination > 0 && (last - ini) <maxPagination){
ini = (end - maxPagination) + 1;
//check ini is negative
if(ini < 1){
ini = 1;
}
//check the begin of pagination
if(maxPagination>0 && pageAct>(maxPagination/2)){
ini = pageAct - (maxPagination/2);
}
}
console.log(ini, end, maxPagination);
//check domain option
var urlFinish = this.options.domain !== undefined ? (this.options.domain + this.urlOrigin) : (this.urlOrigin);
//check the finish of pagination
if(maxPagination>0 && (maxPagination+ini) < last){
end = (ini + maxPagination) -1;
}
console.log(ini, end, maxPagination);
if(maxPagination > 0 && (last - ini) <maxPagination){
ini = (end - maxPagination) + 1;
//check ini is negative
if(ini < 1){
ini = 1;
}
}
console.log(ini, end, maxPagination);
//check domain option
var urlFinish = options.domain !== undefined ? (options.domain + urlOrigin) : (urlOrigin);
//check go first button
if(this.options!==undefined && this.options.showBeginingEnd){
var pagina = {}
pagina.url = urlFinish + '/' + 1 + '/' + this.rowsPerPage;
pagina.actualPage = (i===1);
pagina.index = 1;
pagina.specialButton = "first";
paginationResult.push(pagina);
}
//console.log(ini, last, end);
for(var i = ini; i<= end; ++i){
var pagina = {};
//check go first button
if(options!==undefined && options.showBeginingEnd){
var pagina = {}
pagina.url = urlFinish + '/' + 1 + '/' + rowsPerPage;
pagina.actualPage = (i===1);
pagina.index = 1;
pagina.specialButton = "first";
paginationResult.push(pagina);
}
//console.log(ini, last, end);
for(var i = ini; i<= end; ++i){
var pagina = {};
pagina.url = urlFinish + '/' + i + '/' + this.rowsPerPage;
pagina.actualPage = (i===this.pageAct);
pagina.index = i;
pagina.url = urlFinish + '/' + i + '/' + rowsPerPage;
pagina.actualPage = (i===pageAct);
pagina.index = i;
paginationResult.push(pagina);
paginationResult.push(pagina);
};
//check go last button
if(options!==undefined && options.showBeginingEnd){
var pagina = {};
pagina.url = urlFinish + '/' + last + '/' + rowsPerPage;
pagina.actualPage = (last===pageAct);
pagina.index = last;
pagina.specialButton = "last";
paginationResult.push(pagina);
}
return paginationResult;
}
}
};
//check go last button
if(this.options!==undefined && this.options.showBeginingEnd){
var pagina = {};
pagina.url = urlFinish + '/' + last + '/' + this.rowsPerPage;
pagina.actualPage = (last===this.pageAct);
pagina.index = last;
pagina.specialButton = "last";
paginationResult.push(pagina);
}
return paginationResult;
}
{
"name": "pagination-template",
"version": "0.1.1",
"version": "0.1.2",
"description": "Pagination of results for use in a template",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,0 +0,0 @@ /**

@@ -1,5 +0,5 @@

var pag = require('../index.js');
var Pagination = require('../index.js');
describe('PAGINATION', function() {
it('Check basic pagination', function(done){
pag.pagination('search', 1, 100, 10);
var pag = new Pagination('search', 1, 100, 10);
var result = pag.getPagination();

@@ -14,3 +14,3 @@ expect(result).to.be.a('array');

it('Check void pagination', function(done){
pag.pagination('search', 1, 0, 10);
var pag = new Pagination('search', 1, 0, 10);
var result = pag.getPagination();

@@ -23,3 +23,3 @@ expect(result).to.be.a('array');

it('Check basic option maxPagination', function(done){
pag.pagination('search', 1, 150, 10, {maxPagination:10});
var pag = new Pagination('search', 1, 150, 10, {maxPagination:10});
var result = pag.getPagination();

@@ -31,3 +31,3 @@ //check the length of results

it('Check basic option with actual page = 3 maxPagination', function(done){
pag.pagination('search', 3, 150, 10, {maxPagination:10});
var pag = new Pagination('search', 3, 150, 10, {maxPagination:10});
var result = pag.getPagination();

@@ -41,3 +41,3 @@ //check the length of results

it('Check extended option maxPagination', function(done){
pag.pagination('search', 8, 150, 10, {maxPagination:10});
var pag = new Pagination('search', 8, 150, 10, {maxPagination:10});
var result = pag.getPagination();

@@ -52,3 +52,3 @@ //check the length of results

it('Check extended option maxPagination, the finish is near', function(done){
pag.pagination('search', 14, 150, 10, {maxPagination:10});
var pag = new Pagination('search', 14, 150, 10, {maxPagination:10});
var result = pag.getPagination();

@@ -62,3 +62,3 @@ //check the length of results

it('Check extended option maxPagination, the finish is here', function(done){
pag.pagination('search', 15, 150, 10, {maxPagination:10});
var pag = new Pagination('search', 15, 150, 10, {maxPagination:10});
var result = pag.getPagination();

@@ -72,3 +72,3 @@ //check the length of results

it('Check decimals for maxPagination results', function(done){
pag.pagination('search', 0, 4651, 10, {maxPagination:13, showBeginingEnd:true});
var pag = new Pagination('search', 0, 4651, 10, {maxPagination:13, showBeginingEnd:true});
var result = pag.getPagination();

@@ -85,3 +85,3 @@ console.log(result);

it('Check negative number for maxPagination index', function(done){
pag.pagination('search', 3, 141, 13, {maxPagination:13});
var pag = new Pagination('search', 3, 141, 13, {maxPagination:13});
var result = pag.getPagination();

@@ -96,3 +96,3 @@ if(result[0].index < 0){

it('Check first last button', function(done){
pag.pagination('search', 15, 150, 10, {showBeginingEnd:true});
var pag = new Pagination('search', 15, 150, 10, {showBeginingEnd:true});
var result = pag.getPagination();

@@ -107,3 +107,3 @@ //check the length of results

it('Check first last button with maxPagination', function(done){
pag.pagination('search', 15, 150, 10, {maxPagination:10, showBeginingEnd:true});
var pag = new Pagination('search', 15, 150, 10, {maxPagination:10, showBeginingEnd:true});
var result = pag.getPagination();

@@ -119,3 +119,3 @@ //check the length of results

var domain = 'http://www.google.es/';
pag.pagination('search', 15, 150, 10, {domain: domain});
var pag = new Pagination('search', 15, 150, 10, {domain: domain});
var result = pag.getPagination();

@@ -128,6 +128,2 @@ //check the length of results

});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc