-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
iteratorToArray Support #2894
iteratorToArray Support #2894
Changes from all 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,7 @@ | ||||||||||||||||
function isIterable(obj) { | ||||||||||||||||
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. Also, you forgot |
||||||||||||||||
// checks for null and undefined | ||||||||||||||||
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. This is an unnecessary comment. |
||||||||||||||||
if (obj == null) { | ||||||||||||||||
return false; | ||||||||||||||||
} | ||||||||||||||||
return typeof obj[Symbol.iterator] === 'function'; | ||||||||||||||||
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. We can't safely assume that A similar, but more subtle, consideration applies to So this line should have read like this:
Suggested change
Comment on lines
+3
to
+6
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. Given my previous comment, the entire body of this function would actually fit into a single expression:
Suggested change
Though |
||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function iteratorToArray(iterator) { | ||
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. Like with |
||
var data, | ||
result = []; | ||
|
||
while (!(data = iterator.next()).done) { | ||
result.push(data.value); | ||
} | ||
return result; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ import isArrayLike from './_isArrayLike.js'; | |
import map from './map.js'; | ||
import identity from './identity.js'; | ||
import values from './values.js'; | ||
|
||
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. This blank line should stay. |
||
import isIterable from './isIterable.js' | ||
import iteratorToArray from './iteratorToArray' | ||
// Safely create a real, live array from anything iterable. | ||
var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g; | ||
export default function toArray(obj) { | ||
|
@@ -16,5 +17,6 @@ export default function toArray(obj) { | |
return obj.match(reStrSymbol); | ||
} | ||
if (isArrayLike(obj)) return map(obj, identity); | ||
else if (isIterable(obj)) return iteratorToArray(obj[Symbol.iterator]) | ||
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. When adding a feature to Underscore, you always also have to add tests to verify that the feature works as intended. This is especially important because this enables us to notice when a later change breaks a pre-existing feature. I'm mentioning this here because |
||
return values(obj); | ||
} |
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.
If you intended
isIterable
to be a public function (i.e., mixed into the_
object), then you should list it inmodules/index.js
. There should also be a comment before the function to explain its purpose. Take a look at a few other public modules to get a taste of how we write these comments.If you intended
isIterable
to be only an internal function instead, then you should start the name of the module with a leading underscore, i.e.,modules/_isIterable.js
. In this case it would still be desirable to have a comment that explains the purpose of the function, but I'll readily admit that there are currently more internal functions where this is missing.