-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1041 from caolan/predicate-standardization
Predicate standardization
- Loading branch information
Showing
8 changed files
with
443 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
|
||
describe("detect", function () { | ||
|
||
function detectIterator(call_order, x, callback) { | ||
setTimeout(function(){ | ||
call_order.push(x); | ||
callback(null, x == 2); | ||
}, x*25); | ||
} | ||
|
||
it('detect', function(done){ | ||
var call_order = []; | ||
async.detect([3,2,1], detectIterator.bind(this, call_order), function(err, result){ | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([1,2,'callback',3]); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('detect - mulitple matches', function(done){ | ||
var call_order = []; | ||
async.detect([3,2,2,1,2], detectIterator.bind(this, call_order), function(err, result){ | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([1,2,'callback',2,2,3]); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('detect error', function(done){ | ||
async.detect([3,2,1], function(x, callback) { | ||
setTimeout(function(){callback('error');}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal('error'); | ||
expect(result).to.not.exist; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('detectSeries', function(done){ | ||
var call_order = []; | ||
async.detectSeries([3,2,1], detectIterator.bind(this, call_order), function(err, result){ | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([3,2,'callback']); | ||
done(); | ||
}, 200); | ||
}); | ||
|
||
it('detectSeries - multiple matches', function(done){ | ||
var call_order = []; | ||
async.detectSeries([3,2,2,1,2], detectIterator.bind(this, call_order), function(err, result){ | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([3,2,'callback']); | ||
done(); | ||
}, 200); | ||
}); | ||
|
||
it('detectSeries - ensure stop', function (done) { | ||
async.detectSeries([1, 2, 3, 4, 5], function (num, cb) { | ||
if (num > 3) throw new Error("detectSeries did not stop iterating"); | ||
cb(null, num === 3); | ||
}, function (err, result) { | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('detectLimit', function(done){ | ||
var call_order = []; | ||
async.detectLimit([3, 2, 1], 2, detectIterator.bind(this, call_order), function(err, result) { | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function() { | ||
expect(call_order).to.eql([2, 'callback', 3]); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('detectLimit - multiple matches', function(done){ | ||
var call_order = []; | ||
async.detectLimit([3,2,2,1,2], 2, detectIterator.bind(this, call_order), function(err, result){ | ||
call_order.push('callback'); | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(2); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([2, 'callback', 3]); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('detectLimit - ensure stop', function (done) { | ||
async.detectLimit([1, 2, 3, 4, 5], 2, function (num, cb) { | ||
if (num > 4) throw new Error("detectLimit did not stop iterating"); | ||
cb(null, num === 3); | ||
}, function (err, result) { | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(3); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
|
||
describe("every", function () { | ||
|
||
it('everyLimit true', function(done){ | ||
async.everyLimit([3,1,2], 1, function(x, callback){ | ||
setTimeout(function(){callback(null, x >= 1);}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('everyLimit false', function(done){ | ||
async.everyLimit([3,1,2], 2, function(x, callback){ | ||
setTimeout(function(){callback(null, x === 2);}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('everyLimit short-circuit', function(done){ | ||
var calls = 0; | ||
async.everyLimit([3,1,2], 1, function(x, callback){ | ||
calls++; | ||
callback(null, x === 1); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(false); | ||
expect(calls).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
it('true', function(done){ | ||
async.every([1,2,3], function(x, callback){ | ||
setTimeout(function(){callback(null, true);}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('false', function(done){ | ||
async.every([1,2,3], function(x, callback){ | ||
setTimeout(function(){callback(null, x % 2);}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('early return', function(done){ | ||
var call_order = []; | ||
async.every([1,2,3], function(x, callback){ | ||
setTimeout(function(){ | ||
call_order.push(x); | ||
callback(null, x === 1); | ||
}, x*25); | ||
}, function(){ | ||
call_order.push('callback'); | ||
}); | ||
setTimeout(function(){ | ||
expect(call_order).to.eql([1,2,'callback',3]); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('error', function(done){ | ||
async.every([1,2,3], function(x, callback){ | ||
setTimeout(function(){callback('error');}, 0); | ||
}, function(err, result){ | ||
expect(err).to.equal('error'); | ||
expect(result).to.not.exist; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('all alias', function(){ | ||
expect(async.all).to.equal(async.every); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
|
||
function filterIterator(x, callback) { | ||
setTimeout(function(){ | ||
callback(null, x % 2); | ||
}, x*25); | ||
} | ||
|
||
function testLimit(arr, limitFunc, limit, iter, done) { | ||
var args = []; | ||
|
||
limitFunc(arr, limit, function(x) { | ||
args.push(x); | ||
iter.apply(this, arguments); | ||
}, function() { | ||
expect(args).to.eql(arr); | ||
done.apply(this, arguments); | ||
}); | ||
} | ||
|
||
describe("filter", function () { | ||
|
||
it('filter', function(done){ | ||
async.filter([3,1,2], filterIterator, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([3,1]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('filter original untouched', function(done){ | ||
var a = [3,1,2]; | ||
async.filter(a, function(x, callback){ | ||
callback(null, x % 2); | ||
}, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([3,1]); | ||
expect(a).to.eql([3,1,2]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('filter error', function(done){ | ||
async.filter([3,1,2], function(x, callback){ | ||
callback('error'); | ||
} , function(err, results){ | ||
expect(err).to.equal('error'); | ||
expect(results).to.not.exist; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('filterSeries', function(done){ | ||
async.filterSeries([3,1,2], filterIterator, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([3,1]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('select alias', function(){ | ||
expect(async.select).to.equal(async.filter); | ||
}); | ||
|
||
it('selectSeries alias', function(){ | ||
expect(async.selectSeries).to.equal(async.filterSeries); | ||
}); | ||
|
||
it('filterLimit', function(done) { | ||
testLimit([5, 4, 3, 2, 1], async.filterLimit, 2, function(v, next) { | ||
next(null, v % 2); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.eql([5, 3, 1]); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
describe("reject", function () { | ||
|
||
it('reject', function(done){ | ||
async.reject([3,1,2], filterIterator, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([2]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('reject original untouched', function(done){ | ||
var a = [3,1,2]; | ||
async.reject(a, function(x, callback){ | ||
callback(null, x % 2); | ||
}, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([2]); | ||
expect(a).to.eql([3,1,2]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('reject error', function(done){ | ||
async.reject([3,1,2], function(x, callback){ | ||
callback('error'); | ||
} , function(err, results){ | ||
expect(err).to.equal('error'); | ||
expect(results).to.not.exist; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('rejectSeries', function(done){ | ||
async.rejectSeries([3,1,2], filterIterator, function(err, results){ | ||
expect(err).to.equal(null); | ||
expect(results).to.eql([2]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('rejectLimit', function(done) { | ||
testLimit([5, 4, 3, 2, 1], async.rejectLimit, 2, function(v, next) { | ||
next(null, v % 2); | ||
}, function(err, result){ | ||
expect(err).to.equal(null); | ||
expect(result).to.eql([4, 2]); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--compilers js:babel-core/register | ||
--recursive | ||
--growl | ||
--ui bdd |
Oops, something went wrong.