Skip to content

Commit

Permalink
Allow r.do with one argument - Fix neumino#344
Browse files Browse the repository at this point in the history
  • Loading branch information
neumino committed Aug 3, 2017
1 parent 1d119dc commit af1f9c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ r.prototype.http = function() {
}
r.prototype.do = function() {
var _len = arguments.length;var _args = new Array(_len); for(var _i = 0; _i < _len; _i++) {_args[_i] = arguments[_i];}
Term.prototype._arityRange(_args, 2, Infinity, 'r.do', this);
Term.prototype._arityRange(_args, 1, Infinity, 'r.do', this);

var term = new Term(this).expr(_args[0]);
return term.do.apply(term, _args.slice(1));
Expand Down
6 changes: 4 additions & 2 deletions lib/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -2255,11 +2255,13 @@ Term.prototype.args = function() {
}
Term.prototype.do = function() {
var _len = arguments.length;var _args = new Array(_len); for(var _i = 0; _i < _len; _i++) {_args[_i] = arguments[_i];}
this._arityRange(_args, 1, Infinity, 'do', this);

var term = new Term(this._r);
term._query.push(termTypes.FUNCALL);
var args = [new Term(this._r).expr(_args[_args.length-1])._wrap()._query];
var args = [];
if (_args.length > 0) {
args.push(new Term(this._r).expr(_args[_args.length-1])._wrap()._query);
}
args.push(this);
for(var i=0; i<_args.length-1; i++) {
args.push(new Term(this._r).expr(_args[i]))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"bugs": {
"url": "https://github.com/neumino/rethinkdbdash/issues"
},
"dependencies":{
"dependencies": {
"bluebird": ">= 3.0.1"
},
"devDependencies": {
Expand Down
27 changes: 12 additions & 15 deletions test/control-structures.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ It('`r.do` should work', function* (done) {
result = yield r.do(1, 2, function(a, b) { return b }).run();
assert.equal(result, 2);

result = yield r.do(3).run();
assert.equal(result, 3);

result = yield r.expr(4).do().run();
assert.equal(result, 4);

result = yield r.do(1, 2).run()
assert.deepEqual(result, 2);

result = yield r.do(r.args([ r.expr(3), r.expr(4) ])).run()
assert.deepEqual(result, 3);

done();
}
catch(e) {
Expand All @@ -36,21 +48,6 @@ It('`r.do` should work', function* (done) {
})


It('`do` should throw if no argument has been given', function* (done) {
try{
var result = yield r.expr(1).do().run();
}
catch(e) {
if (e.message.match(/^`do` takes at least 1 argument, 0 provided after:/)) {
done()
}
else {
done(e);
}
}
})


It('`branch` should work', function* (done) {
try {
var result = yield r.branch(true, 1, 2).run();
Expand Down

0 comments on commit af1f9c6

Please sign in to comment.