Skip to content

Commit

Permalink
Merge pull request #1224 from caolan/whilst-test-params
Browse files Browse the repository at this point in the history
Fix test function arguments for whilst/until/during
  • Loading branch information
aearly authored Jul 10, 2016
2 parents ca22b96 + d2764c2 commit c148bbe
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 47 deletions.
31 changes: 22 additions & 9 deletions lib/doDuring.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import during from './during';
import noop from 'lodash/noop';
import rest from 'lodash/rest';
import onlyOnce from './internal/onlyOnce';

/**
* The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
Expand All @@ -15,17 +17,28 @@ import during from './during';
* The function is passed a `callback(err)`, which must be called once it has
* completed with an optional `err` argument. Invoked with (callback).
* @param {Function} test - asynchronous truth test to perform before each
* execution of `fn`. Invoked with (callback).
* execution of `fn`. Invoked with (...args, callback), where `...args` are the
* non-error args from the previous callback of `fn`.
* @param {Function} [callback] - A callback which is called after the test
* function has failed and repeated execution of `fn` has stopped. `callback`
* will be passed an error and any arguments passed to the final `fn`'s
* callback. Invoked with (err, [results]);
* will be passed an error if one occured, otherwise `null`.
*/
export default function doDuring(fn, test, callback) {
var calls = 0;
callback = onlyOnce(callback || noop);

var next = rest(function(err, args) {
if (err) return callback(err);
args.push(check);
test.apply(this, args);
});

function check(err, truth) {
if (err) return callback(err);
if (!truth) return callback(null);
fn(next);
}

check(null, true);

during(function(next) {
if (calls++ < 1) return next(null, true);
test.apply(this, arguments);
}, fn, callback);
}

36 changes: 21 additions & 15 deletions lib/doWhilst.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import whilst from './whilst';
import noop from 'lodash/noop';
import rest from 'lodash/rest';

import onlyOnce from './internal/onlyOnce';

/**
* The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
* the order of operations, the arguments `test` and `fn` are switched.
* the order of operations, the arguments `test` and `iteratee` are switched.
*
* `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
*
Expand All @@ -12,20 +15,23 @@ import whilst from './whilst';
* @method
* @see [async.whilst]{@link module:ControlFlow.whilst}
* @category Control Flow
* @param {Function} fn - A function which is called each time `test` passes.
* The function is passed a `callback(err)`, which must be called once it has
* completed with an optional `err` argument. Invoked with (callback).
* @param {Function} iteratee - A function which is called each time `test`
* passes. The function is passed a `callback(err)`, which must be called once
* it has completed with an optional `err` argument. Invoked with (callback).
* @param {Function} test - synchronous truth test to perform after each
* execution of `fn`. Invoked with Invoked with the non-error callback results
* of `fn`.
* execution of `iteratee`. Invoked with Invoked with the non-error callback
* results of `iteratee`.
* @param {Function} [callback] - A callback which is called after the test
* function has failed and repeated execution of `fn` has stopped. `callback`
* will be passed an error and any arguments passed to the final `fn`'s
* callback. Invoked with (err, [results]);
* function has failed and repeated execution of `iteratee` has stopped.
* `callback` will be passed an error and any arguments passed to the final
* `iteratee`'s callback. Invoked with (err, [results]);
*/
export default function doWhilst(fn, test, callback) {
var calls = 0;
whilst(function() {
return ++calls <= 1 || test.apply(this, arguments);
}, fn, callback);
export default function doWhilst(iteratee, test, callback) {
callback = onlyOnce(callback || noop);
var next = rest(function(err, args) {
if (err) return callback(err);
if (test.apply(this, args)) return iteratee(next);
callback.apply(null, [null].concat(args));
});
iteratee(next);
}
23 changes: 9 additions & 14 deletions lib/during.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import noop from 'lodash/noop';
import rest from 'lodash/rest';
import onlyOnce from './internal/onlyOnce';

/**
* Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that
Expand All @@ -20,8 +20,7 @@ import rest from 'lodash/rest';
* completed with an optional `err` argument. Invoked with (callback).
* @param {Function} [callback] - A callback which is called after the test
* function has failed and repeated execution of `fn` has stopped. `callback`
* will be passed an error and any arguments passed to the final `fn`'s
* callback. Invoked with (err, [results]);
* will be passed an error, if one occured, otherwise `null`.
* @example
*
* var count = 0;
Expand All @@ -40,22 +39,18 @@ import rest from 'lodash/rest';
* );
*/
export default function during(test, fn, callback) {
callback = callback || noop;
callback = onlyOnce(callback || noop);

var next = rest(function(err, args) {
if (err) {
callback(err);
} else {
args.push(check);
test.apply(this, args);
}
});
function next(err) {
if (err) return callback(err);
test(check);
}

var check = function(err, truth) {
function check(err, truth) {
if (err) return callback(err);
if (!truth) return callback(null);
fn(next);
};
}

test(check);
}
6 changes: 4 additions & 2 deletions lib/whilst.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import noop from 'lodash/noop';
import rest from 'lodash/rest';

import onlyOnce from './internal/onlyOnce';

/**
* Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when
* stopped, or an error occurs.
Expand Down Expand Up @@ -37,11 +39,11 @@ import rest from 'lodash/rest';
* );
*/
export default function whilst(test, iteratee, callback) {
callback = callback || noop;
callback = onlyOnce(callback || noop);
if (!test()) return callback(null);
var next = rest(function(err, args) {
if (err) return callback(err);
if (test.apply(this, args)) return iteratee(next);
if (test()) return iteratee(next);
callback.apply(null, [null].concat(args));
});
iteratee(next);
Expand Down
7 changes: 4 additions & 3 deletions mocha_test/during.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('during', function() {
function (cb) {
call_order.push(['iteratee', count]);
count++;
cb();
cb(null, count);
},
function (err) {
assert(err === null, err + " passed instead of 'null'");
Expand All @@ -42,9 +42,10 @@ describe('during', function() {
function (cb) {
call_order.push(['iteratee', count]);
count++;
cb();
cb(null, count);
},
function (cb) {
function (c, cb) {
expect(c).to.equal(count);
call_order.push(['test', count]);
cb(null, count < 5);
},
Expand Down
6 changes: 4 additions & 2 deletions mocha_test/until.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('until', function(){
var call_order = [];
var count = 0;
async.until(
function () {
function (c) {
expect(c).to.equal(undefined);
call_order.push(['test', count]);
return (count == 5);
},
Expand Down Expand Up @@ -42,7 +43,8 @@ describe('until', function(){
count++;
cb(null, count);
},
function () {
function (c) {
expect(c).to.equal(count);
call_order.push(['test', count]);
return (count == 5);
},
Expand Down
6 changes: 4 additions & 2 deletions mocha_test/whilst.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('whilst', function(){

var count = 0;
async.whilst(
function () {
function (c) {
expect(c).to.equal(undefined);
call_order.push(['test', count]);
return (count < 5);
},
Expand Down Expand Up @@ -57,7 +58,8 @@ describe('whilst', function(){
count++;
cb(null, count);
},
function () {
function (c) {
expect(c).to.equal(count);
call_order.push(['test', count]);
return (count < 5);
},
Expand Down

0 comments on commit c148bbe

Please sign in to comment.