gremlin-v3
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -14,2 +14,3 @@ 'use strict'; | ||
var EdgeWrapper = require('./edge-wrapper'); | ||
var IteratorWrapper = require('./iterator-wrapper'); | ||
@@ -275,1 +276,28 @@ var Gremlin = module.exports = function (opts) { | ||
// Applies *process* to each item returned by the *javaIterator*. | ||
// *javaIterator* may be a java.util.iterator, or an IteratorWrapper, or a TraversalWrapper, | ||
// or any type that type that implements hasNext() and next() as methods returning promises, | ||
// with the semantics of those methods of java.util.iterator. | ||
// *process* is function(item) {}, and may be either a synchronous function or async function returning a promise. | ||
// Each item is processed completely before the next item is retrieved. | ||
// Returns a promise if callback is omitted, else calls callback asynchronously when all items processed. | ||
Gremlin.prototype.forEach = function (javaIterator, process, callback) { | ||
function _eachIterator(javaIterator, promisedProcess) { | ||
return javaIterator.hasNext() | ||
.then(function (hasNext) { | ||
if (!hasNext) | ||
return null; | ||
return promisedProcess(javaIterator.next()) | ||
.then(function () { | ||
return _eachIterator(javaIterator, promisedProcess); | ||
}); | ||
}); | ||
} | ||
if (_.isUndefined(javaIterator.unwrap)) { | ||
if (this.java.instanceOf(javaIterator, 'java.util.Iterator')) | ||
javaIterator = new IteratorWrapper(javaIterator); | ||
} | ||
return _eachIterator(javaIterator, Q.promised(process)).nodeify(callback); | ||
}; |
@@ -594,1 +594,9 @@ 'use strict'; | ||
}; | ||
// Fully iterates the traversal, applying *process* to each element returned. | ||
// *process* is a function(elem) {} returning and may be either synchronous or return a promise. | ||
// Returns a promise if callback is omitted, otherwise calls callback when all elements are processed. | ||
TraversalWrapper.prototype.forEach = function (process, callback) { | ||
return this.gremlin.forEach(this, process, callback); | ||
}; | ||
{ | ||
"name": "gremlin-v3", | ||
"description": "Gremlin for graph databases which implement the TinkerPop3 property graph data model.", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "gremlin", |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var VertexWrapper = require('../lib/vertex-wrapper'); | ||
var EdgeWrapper = require('../lib/edge-wrapper'); | ||
@@ -139,2 +140,29 @@ suite('gremlin', function () { | ||
test('gremlin.forEach() ArrayList', function (done) { | ||
var list = gremlin.toListSync(['a', 'b', 'c']); | ||
var iterator = list.listIteratorSync(); | ||
var count = 0; | ||
gremlin.forEach(iterator, function (elem) { | ||
assert.ok(_.isString(elem)); | ||
++count; | ||
}) | ||
.then(function () { | ||
assert.strictEqual(count, 3); | ||
}) | ||
.done(done); | ||
}); | ||
test('gremlin.forEach() Traversal', function (done) { | ||
var iterator = g.E(); | ||
var count = 0; | ||
gremlin.forEach(iterator, function (elem) { | ||
assert.ok(elem instanceof EdgeWrapper); | ||
++count; | ||
}) | ||
.then(function () { | ||
assert.strictEqual(count, 6); | ||
}) | ||
.done(done); | ||
}); | ||
}); |
@@ -1002,2 +1002,14 @@ 'use strict'; | ||
test('forEach()', function (done) { | ||
var count = 0; | ||
g.E().forEach(function (elem) { | ||
assert.ok(elem instanceof EdgeWrapper); | ||
++count; | ||
}) | ||
.then(function () { | ||
assert.strictEqual(count, 6); | ||
}) | ||
.done(done); | ||
}); | ||
}); |
111844
20
2707