From 7560e7b94cb57e500464ce0004a9f2c20a65b4d0 Mon Sep 17 00:00:00 2001 From: porkchop Date: Thu, 18 Sep 2014 22:49:53 -0700 Subject: [PATCH] Optionally override page size in list method --- README.md | 4 ++-- lib/leaderboard.js | 11 +++++++-- test/leaderboard.js | 54 +++++++++++++++++++++++++++++++-------------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a5150b5..6a5e5f4 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ Creates a new leaderboard or attaches to an existing leaderboard. // fall within the leaderboard }); - - `list([page], λ)` + - `list([page|options], λ)` - Retrieves a page of leaders from the leaderboard. + Retrieves a page of leaders from the leaderboard. Optionally, taking a page number or an object of the form {page: {Number}, pageSize: {Number}} board.list(function(err, list) { // list - list of leaders are ordered from diff --git a/lib/leaderboard.js b/lib/leaderboard.js index b6547b6..ac54382 100644 --- a/lib/leaderboard.js +++ b/lib/leaderboard.js @@ -162,6 +162,8 @@ proto.rmIn = function(name, member, cb) { * Retrieves a page of members from the leaderboard. * * @param {Number} page + * OR + * {Object} of the form {page: {Number}, pageSize: {Number}} * @param {Function} cb * @api public */ @@ -170,17 +172,22 @@ proto.list = function(page, cb) { }; proto.listIn = function(name, page, cb) { + var pageSize = this.pageSize; if (typeof(cb) === 'undefined' && page instanceof Function) { cb = page; } if (typeof(page) === 'undefined' || page instanceof Function) { page = 0; } + if (typeof(page) === 'object') { + pageSize = page.pageSize; + page = page.page; + } var req = [ name - , page * this.pageSize - , page * this.pageSize + this.pageSize - 1 + , page * pageSize + , page * pageSize + pageSize - 1 , 'WITHSCORES' ]; diff --git a/test/leaderboard.js b/test/leaderboard.js index 0d59969..a2a1da5 100644 --- a/test/leaderboard.js +++ b/test/leaderboard.js @@ -317,28 +317,50 @@ describe('Leaderboard', function() { }); }); - it('should return correct number of entries for the page 0', function(done) { - var board = new LB('general', {pageSize: 5}, {db: DBINDEX}); + describe('Using the pageSize member', function() { + it('should return correct number of entries for the page 0', function(done) { + var board = new LB('general', {pageSize: 5}, {db: DBINDEX}); - async.parallel([ - function(cb) { board.add('member4', 60, cb); }, - function(cb) { board.add('member5', 70, cb); }, - function(cb) { board.add('member6', 80, cb); }, - function(cb) { board.add('member7', 90, cb); } - ], function() { - board.list(function(err, list) { - assert.equal(list.length, 5); + async.parallel([ + function(cb) { board.add('member4', 60, cb); }, + function(cb) { board.add('member5', 70, cb); }, + function(cb) { board.add('member6', 80, cb); }, + function(cb) { board.add('member7', 90, cb); } + ], function() { + board.list(function(err, list) { + assert.equal(list.length, 5); + done(); + }); + }); + }); + + it('should return correct number of entries for the page 1', function(done) { + var board = new LB('general', {pageSize: 5}, {db: DBINDEX}); + + board.list(1, function(err, list) { + assert.equal(list.length, 2); done(); }); }); }); - it('should return correct number of entries for the page 1', function(done) { - var board = new LB('general', {pageSize: 5}, {db: DBINDEX}); - - board.list(1, function(err, list) { - assert.equal(list.length, 2); - done(); + describe('Overriding the pageSize member', function() { + it('should return correct number of entries for the page 0', function(done) { + var board = new LB('general', {pageSize: 2}, {db: DBINDEX}); + + board.list({page: 0, pageSize: 5}, function(err, list) { + assert.equal(list.length, 5); + done(); + }); + }); + + it('should return correct number of entries for the page 1', function(done) { + var board = new LB('general', {pageSize: 1}, {db: DBINDEX}); + + board.list({page: 1, pageSize: 5}, function(err, list) { + assert.equal(list.length, 2); + done(); + }); }); });