summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2016-11-14 09:40:37 -0500
committerGitHub <noreply@github.com>2016-11-14 09:40:37 -0500
commit56051dfb17cd3e5d97f796f9e516c22fc2316b6c (patch)
treeca454ba534f40067b35f3ab5a00df4b2eba0948d
parentb4d794c4b9c0c3406007297a06a08525d008ba51 (diff)
parente79952057cb1e439c396f0acc7dee5b635e40ab7 (diff)
downloadasync-56051dfb17cd3e5d97f796f9e516c22fc2316b6c.tar.gz
Merge pull request #1320 from caolan/pr/1297
Pr/1297
-rw-r--r--lib/internal/filter.js6
-rw-r--r--mocha_test/filter.js43
2 files changed, 45 insertions, 4 deletions
diff --git a/lib/internal/filter.js b/lib/internal/filter.js
index 35527e2..34cd722 100644
--- a/lib/internal/filter.js
+++ b/lib/internal/filter.js
@@ -10,8 +10,7 @@ export default function _filter(eachfn, arr, iteratee, callback) {
iteratee(x, function (err, v) {
if (err) {
callback(err);
- }
- else {
+ } else {
if (v) {
results.push({index: index, value: x});
}
@@ -21,8 +20,7 @@ export default function _filter(eachfn, arr, iteratee, callback) {
}, function (err) {
if (err) {
callback(err);
- }
- else {
+ } else {
callback(null, arrayMap(results.sort(function (a, b) {
return a.index - b.index;
}), property('value')));
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');