summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/internal/iterator.js51
1 files changed, 29 insertions, 22 deletions
diff --git a/lib/internal/iterator.js b/lib/internal/iterator.js
index 645dcbd..15c6fdd 100644
--- a/lib/internal/iterator.js
+++ b/lib/internal/iterator.js
@@ -2,33 +2,40 @@ import isArrayLike from 'lodash/isArrayLike';
import getIterator from './getIterator';
import keys from 'lodash/keys';
-export default function iterator(coll) {
+function createArrayIterator(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;
- };
+ var len = coll.length;
+ return function next() {
+ return ++i < len ? {value: coll[i], key: i} : null;
}
+}
- var iterate = getIterator(coll);
- if (iterate) {
- return function next() {
- var item = iterate.next();
- if (item.done)
- return null;
- i++;
- return {value: item.value, key: i};
- };
+function createES2015Iterator(iterator) {
+ var i = -1;
+ return function next() {
+ var item = iterator.next();
+ if (item.done)
+ return null;
+ i++;
+ return {value: item.value, key: i};
}
+}
- var okeys = keys(coll);
- len = okeys.length;
+function createObjectIterator(obj) {
+ var okeys = keys(obj);
+ var i = -1;
+ var len = okeys.length;
return function next() {
- i++;
- var key = okeys[i];
- return i < len ? {value: coll[key], key: key} : null;
+ var key = okeys[++i];
+ return i < len ? {value: obj[key], key: key} : null;
};
}
+
+export default function iterator(coll) {
+ if (isArrayLike(coll)) {
+ return createArrayIterator(coll);
+ }
+
+ var iterator = getIterator(coll);
+ return iterator ? createES2015Iterator(iterator) : createObjectIterator(coll);
+}