Skip to content

Commit

Permalink
Added semicolons, renamed functions
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Nov 11, 2014
1 parent 722d5a9 commit 8f592d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
6 changes: 5 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"it",
"console",
"describe",
"after",
"before",
"beforeEach",
"waits",
"waitsFor",
Expand Down Expand Up @@ -46,5 +48,7 @@
"sub": true,
"strict": false,
"white": false,
"asi": true
"asi": false,

"expr": true
}
81 changes: 38 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,67 @@
// * Author: [@niftylettuce](https://twitter.com/#!/niftylettuce)
// * Source: <https://github.com/niftylettuce/express-paginate>

var querystring = require('querystring')
var url = require('url')
var _ = require('lodash')
var querystring = require('querystring');
var url = require('url');
var _ = require('lodash');
var util = require('util');

exports = module.exports
exports = module.exports;

exports.href = function paginate(req) {
exports.href = function href(req) {
return function(prev) {
var query = _.clone(req.query)
prev = (typeof prev === 'boolean') ? prev : false
query.page = prev ? query.page-= 1 : query.page += 1
query.page = (query.page < 1) ? 1 : query.page
return url.parse(req.originalUrl).pathname + '?' + querystring.stringify(query)
}
}


exports.hasNextPages = function(req) {

var query = _.clone(req.query);
prev = (typeof prev === 'boolean') ? prev : false;
query.page = prev ? query.page-= 1 : query.page += 1;
query.page = (query.page < 1) ? 1 : query.page;
return url.parse(req.originalUrl).pathname + '?' + querystring.stringify(query);
};
};

exports.hasNextPages = function hasNextPages(req) {
return function(pageCount) {

if (typeof pageCount !== 'number' || pageCount < 0)
throw new Error('express-paginate: `pageCount` is not a number > 0')

return req.query.page < pageCount

}

}
throw new Error('express-paginate: `pageCount` is not a number > 0');
return req.query.page < pageCount;
};
};

exports.middleware = function(limit, maxLimit) {
exports.middleware = function middleware(limit, maxLimit) {

var that = this
var that = this;

that.limit = (typeof limit === 'number') ? parseInt(limit, 10) : 10
that.limit = (typeof limit === 'number') ? parseInt(limit, 10) : 10;

that.maxLimit = (typeof maxLimit === 'number') ? parseInt(maxLimit, 10) : 50
that.maxLimit = (typeof maxLimit === 'number') ? parseInt(maxLimit, 10) : 50;

if (that.limit < 1)
throw new Error('express-paginate: `limit` cannot be less than 1')
throw new Error('express-paginate: `limit` cannot be less than 1');

if (that.maxLimit < 1)
throw new Error('express-paginate: `maxLimit` cannot be less than 1')
throw new Error('express-paginate: `maxLimit` cannot be less than 1');

return function middleware(req, res, next) {
return function _middleware(req, res, next) {

req.query.page = (typeof req.query.page === 'string') ? parseInt(req.query.page, 10) : 1
req.query.page = (typeof req.query.page === 'string') ? parseInt(req.query.page, 10) : 1;

req.query.limit = (typeof req.query.limit === 'string') ? parseInt(req.query.limit, 10) : that.limit
req.query.limit = (typeof req.query.limit === 'string') ? parseInt(req.query.limit, 10) : that.limit;

if (req.query.limit > that.maxLimit)
req.query.limit = that.maxLimit
req.query.limit = that.maxLimit;

if (req.query.page < 1)
req.query.page = 1
req.query.page = 1;

if (req.query.limit < 1)
req.query.limit = 1
req.query.limit = 1;

res.locals.paginate = {}
res.locals.paginate.href = exports.href(req)
res.locals.paginate.hasPreviousPages = req.query.page > 1
res.locals.paginate.hasNextPages = exports.hasNextPages(req)
res.locals.paginate = {};
res.locals.paginate.href = exports.href(req);
res.locals.paginate.hasPreviousPages = req.query.page > 1;
res.locals.paginate.hasNextPages = exports.hasNextPages(req);

next()
next();

}
};

}
};
2 changes: 1 addition & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('paginate', function(){
}).should.throw(/\> 0/);
});

})
});

});

Expand Down

0 comments on commit 8f592d9

Please sign in to comment.