From a3e50ef13b42c2b6d9874beb1a3e07ac767098b6 Mon Sep 17 00:00:00 2001 From: Eduardo Sorribas Date: Tue, 18 Mar 2014 21:24:06 +0100 Subject: [PATCH] Add the cursor.map function --- index.js | 7 +++++++ tests/test-cursor-map.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/test-cursor-map.js diff --git a/index.js b/index.js index efcf7ad..c7eba56 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/tests/test-cursor-map.js b/tests/test-cursor-map.js new file mode 100644 index 0000000..06dfce3 --- /dev/null +++ b/tests/test-cursor-map.js @@ -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(); + }); +}); +