summaryrefslogtreecommitdiff
path: root/mocha_test/filter.js
diff options
context:
space:
mode:
Diffstat (limited to 'mocha_test/filter.js')
-rw-r--r--mocha_test/filter.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/mocha_test/filter.js b/mocha_test/filter.js
index 8b4b268..8f13620 100644
--- a/mocha_test/filter.js
+++ b/mocha_test/filter.js
@@ -41,6 +41,49 @@ describe("filter", function () {
});
});
+ it('filter collection', function(done){
+ var a = {a: 3, b: 1, c: 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({a: 3, b: 1, c: 2});
+ done();
+ });
+ });
+
+ if (typeof Symbol === 'function' && Symbol.iterator) {
+ function makeIterator(array){
+ var nextIndex;
+ let iterator = {
+ next: function(){
+ return nextIndex < array.length ?
+ {value: array[nextIndex++], done: false} :
+ {done: true};
+ }
+ };
+ iterator[Symbol.iterator] = function() {
+ nextIndex = 0; // reset iterator
+ return iterator;
+ };
+ return iterator;
+ }
+
+ it('filter iterator', function(done){
+ var a = makeIterator([500, 20, 100]);
+ async.filter(a, function(x, callback) {
+ setTimeout(function() {
+ callback(null, x > 20);
+ }, x);
+ }, function(err, results){
+ expect(err).to.equal(null);
+ expect(results).to.eql([500, 100]);
+ done();
+ });
+ });
+ }
+
it('filter error', function(done){
async.filter([3,1,2], function(x, callback){
callback('error');