Skip to content

Commit

Permalink
Merge pull request #1074 from ex1st/master
Browse files Browse the repository at this point in the history
Adds support for es6 iterators
  • Loading branch information
aearly committed Mar 23, 2016
2 parents ee68eeb + 4c94979 commit 92df0ca
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 28 deletions.
10 changes: 5 additions & 5 deletions lib/internal/eachOfLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import noop from 'lodash/noop';
import once from 'lodash/once';

import keyIterator from './keyIterator';
import iterator from './iterator';
import onlyOnce from './onlyOnce';

export default function _eachOfLimit(limit) {
return function (obj, iteratee, callback) {
callback = once(callback || noop);
obj = obj || [];
var nextKey = keyIterator(obj);
var nextElem = iterator(obj);
if (limit <= 0) {
return callback(null);
}
Expand All @@ -24,16 +24,16 @@ export default function _eachOfLimit(limit) {
}

while (running < limit && !errored) {
var key = nextKey();
if (key === null) {
var elem = nextElem();
if (elem === null) {
done = true;
if (running <= 0) {
callback(null);
}
return;
}
running += 1;
iteratee(obj[key], key, onlyOnce(function (err) {
iteratee(elem.value, elem.key, onlyOnce(function (err) {
running -= 1;
if (err) {
callback(err);
Expand Down
38 changes: 38 additions & 0 deletions lib/internal/iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

import isArrayLike from 'lodash/isArrayLike';
import keys from 'lodash/keys';

var iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator;

export default function iterator(coll) {
var i = -1;
var len;
if (isArrayLike(coll)) {
len = coll.length;
return function next() {
i++;
return i < len ? {value: coll[i], key: i} : null;
};
}

if (iteratorSymbol && coll[iteratorSymbol]) {
var iterate = coll[iteratorSymbol]();

return function next() {
var item = iterate.next();
if (item.done)
return null;
i++;
return {value: item.value, key: i};
};
}

var okeys = keys(coll);
len = okeys.length;
return function next() {
i++;
var key = okeys[i];
return i < len ? {value: coll[key], key: key} : null;
};
}
23 changes: 0 additions & 23 deletions lib/internal/keyIterator.js

This file was deleted.

89 changes: 89 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,36 @@ exports['forEachOf with array'] = function(test){
});
};

exports['forEachOf with Set (iterators)'] = function(test){
if (typeof Set !== 'function')
return test.done();

var args = [];
var set = new Set();
set.add("a");
set.add("b");
async.forEachOf(set, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, "a", 1, "b"]);
test.done();
});
};

exports['forEachOf with Map (iterators)'] = function(test){
if (typeof Map !== 'function')
return test.done();

var args = [];
var map = new Map();
map.set(1, "a");
map.set(2, "b");
async.forEachOf(map, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, [1, "a"], 1, [2, "b"]]);
test.done();
});
};

exports['eachSeries'] = function(test){
var args = [];
async.eachSeries([1,3,2], eachIteratee.bind(this, args), function(err){
Expand Down Expand Up @@ -1116,6 +1146,35 @@ exports['forEachOfSeries with array'] = function(test){
});
};

exports['forEachOfSeries with Set (iterators)'] = function(test){
if (typeof Set !== 'function')
return test.done();

var args = [];
var set = new Set();
set.add("a");
set.add("b");
async.forEachOfSeries(set, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, "a", 1, "b"]);
test.done();
});
};

exports['forEachOfSeries with Map (iterators)'] = function(test){
if (typeof Map !== 'function')
return test.done();

var args = [];
var map = new Map();
map.set(1, "a");
map.set(2, "b");
async.forEachOfSeries(map, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, [1, "a"], 1, [2, "b"]]);
test.done();
});
};

exports['eachOfLimit alias'] = function(test){
test.equals(async.eachOfLimit, async.forEachOfLimit);
Expand Down Expand Up @@ -1233,6 +1292,36 @@ exports['forEachOfLimit with array'] = function(test){
});
};

exports['forEachOfLimit with Set (iterators)'] = function(test){
if (typeof Set !== 'function')
return test.done();

var args = [];
var set = new Set();
set.add("a");
set.add("b");
async.forEachOfLimit(set, 1, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, "a", 1, "b"]);
test.done();
});
};

exports['forEachOfLimit with Map (iterators)'] = function(test){
if (typeof Map !== 'function')
return test.done();

var args = [];
var map = new Map();
map.set(1, "a");
map.set(2, "b");
async.forEachOfLimit(map, 1, forEachOfIteratee.bind(this, args), function(err){
if (err) throw err;
test.same(args, [0, [1, "a"], 1, [2, "b"]]);
test.done();
});
};

exports['map'] = {

'basic': function(test){
Expand Down

0 comments on commit 92df0ca

Please sign in to comment.