Skip to content

Commit

Permalink
Add the cursor.map function
Browse files Browse the repository at this point in the history
  • Loading branch information
sorribas committed Mar 18, 2014
1 parent 5d49b11 commit a3e50ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ Cursor.prototype.destroy = function() {
this.push(null);
};

Cursor.prototype.map = function(mapfn, callback) {
this.toArray(function(err, arr) {
if (err) return callback(err);
callback(null, arr.map(mapfn))
});
};

Cursor.prototype._apply = function(fn, args) {
this._get(function(err, cursor) {
if (err) return getCallback(args)(err);
Expand Down
18 changes: 18 additions & 0 deletions tests/test-cursor-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var assert = require('assert');
var insert = require('./insert');

insert([{
hello:'world1'
},{
hello:'world2'
}], function(db, done) {
var cursor = db.a.find();
cursor.map(function(x) {
return x.hello
}, function(err, res) {
assert.equal(res[0], 'world1');
assert.equal(res[1], 'world2');
done();
});
});

0 comments on commit a3e50ef

Please sign in to comment.