Skip to content

Commit

Permalink
Added Set#random()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed May 2, 2011
1 parent afc9cdc commit 9a66d6a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/objects/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ Set.prototype.pop = function(fn){
return this;
};

/**
* Return a random element from this set and callback `fn(err, str)`.
*
* @param {Function} fn
* @return {Set} for chaining
* @api public
*/

Set.prototype.random = function(fn){
this.client.srandmember(this.key, fn);
return this;
};

/**
* Get the members in this set and callback`fn(err, members)`.
*
Expand Down
15 changes: 15 additions & 0 deletions test/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,20 @@ module.exports = {
done();
});
});
},

'.random()': function(done){
var names = new Set('names');
names.add('tj');
names.add('aaron');
names.add('guillermo');
names.random(function(err, name){
name.should.be.a('string');
names.card(function(err, cardinality){
cardinality.should.equal(3);
names.client.end();
done();
});
});
}
};

0 comments on commit 9a66d6a

Please sign in to comment.