From 8f592d9d3786e1fec9b22dc317b14070869d9a14 Mon Sep 17 00:00:00 2001 From: Nick Baugh Date: Tue, 11 Nov 2014 18:51:40 -0500 Subject: [PATCH] Added semicolons, renamed functions --- .jshintrc | 6 +++- index.js | 81 ++++++++++++++++++++++------------------------ test/index-test.js | 2 +- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/.jshintrc b/.jshintrc index b9e4a58..f587642 100644 --- a/.jshintrc +++ b/.jshintrc @@ -5,6 +5,8 @@ "it", "console", "describe", + "after", + "before", "beforeEach", "waits", "waitsFor", @@ -46,5 +48,7 @@ "sub": true, "strict": false, "white": false, - "asi": true + "asi": false, + + "expr": true } diff --git a/index.js b/index.js index 19c98dd..0e06f78 100644 --- a/index.js +++ b/index.js @@ -8,72 +8,67 @@ // * Author: [@niftylettuce](https://twitter.com/#!/niftylettuce) // * Source: -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(); - } + }; -} +}; diff --git a/test/index-test.js b/test/index-test.js index 8b77e7e..1c7a558 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -34,7 +34,7 @@ describe('paginate', function(){ }).should.throw(/\> 0/); }); - }) + }); });