Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In response iterator, fix handling of falsy values for view results. #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion lib/cradle/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ this.basePrototype = {
this.collectionPrototype = {
forEach: function (f) {
for (var i = 0, value; i < this.length; i++) {
value = this[i].doc || this[i].json || this[i].value || this[i];
value = this[i].doc;
if (value === undefined)
value = this[i].json;
if (value === undefined)
value = this[i].value;
if (value === undefined)
value = this[i];

if (f.length === 1) {
f.call(this[i], value);
} else {
Expand Down
26 changes: 26 additions & 0 deletions test/response-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var path = require('path'),

var cradle = require('../lib/cradle');
var document = { _rev: '2-76be', _id: 'f6av8', name: 'buzz', age: 99 };
var view = { rows: [ { key: 'key1', value: 10 }, { key: 'key2', value: 0 }, { key: 'key3', value: false } ] };

vows.describe('cradle/response').addBatch({
'A cradle.Response instance': {
Expand Down Expand Up @@ -66,6 +67,31 @@ vows.describe('cradle/response').addBatch({
assert.equal(json.hair, 'blue');
}
}
},
'from a view': {
topic: new(cradle.Response)(view),

'should correctly handle rows with falsy values in iterator': function (topic) {
var values = topic.toArray();

assert.lengthOf(values, 3);

for (var i=0; i < values.length; i++)
{
var value = values[i];
switch (i) {
case 0:
assert.equal(value, 10);
break;
case 1:
assert.equal(value, 0);
break;
case 2:
assert.equal(value, false);
break;
}
}
}
}
}
}).export(module);
Expand Down