Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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'
];

Expand Down
54 changes: 38 additions & 16 deletions test/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down