-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
import isArrayLike from 'lodash/isArrayLike'; | ||
import keys from 'lodash/keys'; | ||
import isMap from 'lodash/isMap'; | ||
|
||
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](); | ||
var map = isMap(coll); | ||
|
||
return function next() { | ||
var item = iterate.next(); | ||
if (item.done) | ||
return null; | ||
i++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't like tracking this index. People should pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some sort of index is needed for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point fine with this then |
||
return map ? {value: item.value[1], key: item.value[0]} : {value: item.value, key: i}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is map the only time we'd like to do this switch? Also not sure if tracking indexs even makes sense for a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a valid thing to do. I think it's odd to iterate over a map and get |
||
}; | ||
} | ||
|
||
var okeys = keys(coll); | ||
len = okeys.length; | ||
return function next() { | ||
i++; | ||
var key = okeys[i]; | ||
return i < len ? {value: coll[key], key: key} : null; | ||
}; | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note,
isMap
doesn't work for all forms of maps (WeakMap
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought
WeakMap
s were not iterable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, I just am against a special case for maps