-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add iterator support #27
Conversation
while (!done) { | ||
iterableResponse = arrayLike.next(); | ||
if ( | ||
iterableResponse.hasOwnProperty('value') |
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.
let's use the "has" module rather than trusting Object#hasOwnProperty
@ljharb Addressed all comments raised. |
@@ -1,9 +1,88 @@ | |||
'use strict'; | |||
var ES = require('es-abstract/es6'); | |||
var supportsDescriptors = require('define-properties').supportsDescriptors; | |||
var has = require('has'); | |||
var global = require('global-object'); |
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.
an alternative to this module would be https://www.npmjs.com/package/system.global which follows the official language spec proposal.
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.
+1 to using system.global
instead.
What’s with the failing builds? |
Most of the new tests in this PR are missing explicit end or plan calls. |
All passing now :-) |
@ljharb This is ready for another review when you have the time. Thanks for helping with this feature by the way, I really appreciate the feedback you have given :-) |
}()); | ||
|
||
if (supportsStrIterator) { | ||
iteratorSymbol = '@@iterator'; |
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.
edit: now fixed I don't think that the above check actually indicates that it supports a string iterator - that can only be tested by setting one, and asserting that for..of
invokes it.
if (iteratorSymbol && isCallable(items[iteratorSymbol])) { | ||
return items[iteratorSymbol](); | ||
} else if ('Set' in global && 'Map' in global && isCallable(items.entries) && isCallable(items.values)) { | ||
if (items.constructor === Set) { |
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.
anything with ===
won't work cross-realm (like iframes, or web workers, etc) - to be robust, I think you'd need to stash a copy of the Set.prototype.size
getter (and Map) and assert that .call
ing on items
did not throw.
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 made a JSBin to test this out, it seems Set
s and Map
s are detectable across realms (I tested web workers) via instanceof
. Is this what you expected?
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.
Another, the following test will work on many more browsers.
var iframe = document.createElement('iframe');
iframe.src = 'about:blank';
document.body.appendChild(iframe);
console.log(isMap(new iframe.contentWindow.Map()));
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 that's the case with instanceof
, it's a violation of the spec - the .call
you're using is the correct way to make it work across realms.
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.
Yes, it is assumed to use .call
.
Addressed all comments, all passing on CI except for 3 allowed failures -- node 0.9, node 0.6, node 0.4 I'm currently running this test suite against a multitude of browsers, this usually takes a very long time. I will add a comment Wednesday morning (UK time). So far, it looks like there are errors on ios 8.2 :-/ |
if (has(items, iteratorSymbol)) { | ||
return items[iteratorSymbol](); | ||
} else if ('Set' in global && 'Map' in global && isCallable(items.entries) && isCallable(items.values)) { | ||
if (items.constructor === Set) { |
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.
Won't work cross realm, need to resolve this.
All browsers not finished running yet, results so far for browsers which failed:
|
return done ? tempArray : false; | ||
}; | ||
|
||
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'; |
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.
typeof Symbol.iterator === 'symbol'
doesn't work with Symbol polyfills.
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.
That's because Symbol
isn't truly polyfillable - it's a native-only feature. We should be supporting shims, but not go out of our way to support shams.
I refined this support in #30. It would work better. Could you review it? |
I ran the tests against the master using my list of browsers, it seems to have the same 4 failing tests. |
|
I've commented out the test which is failing on master and am now rerunning against all the browsers on SauceLabs. I have high hopes it will work :-) |
Hmm - I'd like to fix those tests on master before merging this. |
🎉 🎉 🎉 🎉 🎉 🎉 🎉
|
@mathiasbynens @ljharb I believe this is ready for it's final review :-) |
@mathiasbynens btw would you mind enabling required PR approvals on this repo, in settings? :-) @JakeChampion thanks! I'll take a final look and merge tonight. |
Thanks! I'm going to give this a couple days to bake, and for me to test and play around, before releasing it. |
Iterators still don't work in |
Or use https://github.com/studio-b12/array-from until a new version is published to NPM |
Should solve #4