From 1ebaf34210647a4227eb19a93bc91f8d41bee875 Mon Sep 17 00:00:00 2001 From: "Pedro J. Aramburu" Date: Thu, 23 Jun 2016 16:21:23 -0300 Subject: [PATCH 1/4] Adds next and prev return values While it is fairly easy to implement this functionality outside the library it is convenient to provide it with the other return values so it encapsulates everything needed on the pagination template on a single object. Although most template engines let you do the math on the template side there are some that don't or require writing extra helpers. --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 8af68dd..9c60409 100644 --- a/index.js +++ b/index.js @@ -75,6 +75,16 @@ function paginate(query, options, callback) { if (page !== undefined) { result.page = page; result.pages = Math.ceil(data.count / limit) || 1; + var prev = page - 1; + var next = page + 1; + + if (prev > 0) { + result.prev = prev; + } + + if (next <= result.pages) { + result.next = next; + } } if (typeof callback === 'function') { return callback(null, result); From 755e828a9331a80b73ab95291f140c9e669b586c Mon Sep 17 00:00:00 2001 From: "Pedro J. Aramburu" Date: Thu, 23 Jun 2016 16:23:34 -0300 Subject: [PATCH 2/4] Use let instead of var --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9c60409..ed1c520 100644 --- a/index.js +++ b/index.js @@ -75,8 +75,8 @@ function paginate(query, options, callback) { if (page !== undefined) { result.page = page; result.pages = Math.ceil(data.count / limit) || 1; - var prev = page - 1; - var next = page + 1; + let prev = page - 1; + let next = page + 1; if (prev > 0) { result.prev = prev; From 814d82acb636f5ff93e0339e85bf2e458f72acec Mon Sep 17 00:00:00 2001 From: "Pedro J. Aramburu" Date: Thu, 23 Jun 2016 16:26:26 -0300 Subject: [PATCH 3/4] Add prev and next return value to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 59f49fc..bb11694 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Promise fulfilled with object having properties: * `limit` {Number} - Limit that was used * `[page]` {Number} - Only if specified or default `page`/`offset` values were used * `[pages]` {Number} - Only if `page` specified or default `page`/`offset` values were used +* `[prev]` {Number} - Only if there is a previous page to go to. +* `[next]` {Number} - Only if there is a next page to go to. * `[offset]` {Number} - Only if specified or default `page`/`offset` values were used ### Examples From 0c71a5eddb1caf354ee5d0ebaab035df7301a672 Mon Sep 17 00:00:00 2001 From: "Pedro J. Aramburu" Date: Thu, 23 Jun 2016 16:36:08 -0300 Subject: [PATCH 4/4] Added test cases for prev and next --- tests/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/index.js b/tests/index.js index 7a1a8d2..a09ba68 100644 --- a/tests/index.js +++ b/tests/index.js @@ -113,9 +113,35 @@ describe('mongoose-paginate', function() { expect(result.limit).to.equal(20); expect(result.page).to.equal(1); expect(result.pages).to.equal(5); + expect(result.next).to.equal(2); + expect(result.prev).to.be.undefined; expect(result).to.not.have.property('offset'); }); }); + it('on the first page', function() { + return Book.paginate({}, { page: 1, limit: 20 }).then(function(result) { + expect(result.next).to.equal(2); + expect(result).to.not.have.property('prev'); + }); + }); + it('on the last page', function() { + return Book.paginate({}, { page: 5, limit: 20 }).then(function(result) { + expect(result.prev).to.equal(4); + expect(result).to.not.have.property('next'); + }); + }); + it('on the middle page', function() { + return Book.paginate({}, { page: 3, limit: 20 }).then(function(result) { + expect(result.prev).to.equal(2); + expect(result.next).to.equal(4); + }); + }); + it('with page 1 and limit', function() { + return Book.paginate({}, { page: 1, limit: 20 }).then(function(result) { + expect(result.next).to.equal(2); + expect(result).to.not.have.property('prev'); + }); + }); it('with zero limit', function() { return Book.paginate({}, { page: 1, limit: 0 }).then(function(result) { expect(result.docs).to.have.length(0);