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

Fix test function arguments for whilst/until/during #1224

Merged
merged 4 commits into from
Jul 10, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 21 additions & 9 deletions lib/doDuring.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import during from './during';
import noop from 'lodash/noop';
import rest from 'lodash/rest';

/**
* The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
Expand All @@ -15,17 +16,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 = 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);
}

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

/**
* 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 +13,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 = callback || noop;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets once wrap this

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);
}
20 changes: 7 additions & 13 deletions lib/during.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import noop from 'lodash/noop';
import rest from 'lodash/rest';

/**
* Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that
Expand All @@ -20,8 +19,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 @@ -42,20 +40,16 @@ import rest from 'lodash/rest';
export default function during(test, fn, callback) {
callback = 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);
}
2 changes: 1 addition & 1 deletion lib/whilst.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function whilst(test, iteratee, callback) {
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