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

Adds support for es6 iterators #1074

Merged
merged 3 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I really don't like tracking this index. People should pass .entries if they want access to the key. i may not correspond to the real index. Just leave key undefined

Copy link
Collaborator

Choose a reason for hiding this comment

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

Some sort of index is needed for map(Series|Limit) to work.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point fine with this then

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