Skip to content

Commit

Permalink
Merge pull request #66 from chdanielmueller/offset-greater-0-smaller-…
Browse files Browse the repository at this point in the history
…limit

Offset greater than 0 smaller than limit
  • Loading branch information
aravindnc committed Mar 27, 2020
2 parents efb8eea + e9afd2a commit 2a2a70e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ function paginate(query, options, callback) {
if (page > 1) {
meta[labelHasPrevPage] = true;
meta[labelPrevPage] = (page - 1);
} else if (page == 1 && offset !== 0) {
meta[labelHasPrevPage] = true;
meta[labelPrevPage] = 1;
} else {
meta[labelPrevPage] = null;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ describe('mongoose-paginate', function () {
expect(result.totalPages).to.equal(10);
});
});

it('with offset and limit (not page) condition: offset > 0 < limit', function () {
var query = {
title: {
$in: [/Book/i]
}
};

var options = {
limit: 10,
offset: 5,
sort: {
_id: 1
},
lean: true
};

return Book.paginate(query, options).then((result) => {

expect(result.docs).to.have.length(10);
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(1);
expect(result.pagingCounter).to.equal(1);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.prevPage).to.equal(1);
expect(result.nextPage).to.equal(2);
expect(result.totalPages).to.equal(10);
});
});

it('with limit=0 (metadata only)', function () {
var query = {
Expand Down

0 comments on commit 2a2a70e

Please sign in to comment.