-
Notifications
You must be signed in to change notification settings - Fork 30
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
fix: favor extended array toStringTag where available #50
Conversation
Hey @dvlsg, thanks for this. Looks great! Tests would definitely be ideal before we merge. We have toStringTag tests here in test/index.js#L227-261. If you could add Arrays there, that'd be great. Thanks! |
Can do. |
Is it safe for me to assume that Considering adding checks like found here. Are those worthwhile in this case, or just extra noise? |
@dvlsg yes indeed. The tests will be run in IE9, PhantomJS, etc. classes are not a given. Feel free to wrap it in the style we have done, such as in the example you linked. |
Hm. Thoughts on the Node v4.3 case? It's coming out as |
@@ -95,7 +95,9 @@ module.exports = function typeDetect(obj) { | |||
* array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled) | |||
*/ | |||
if (isArrayExists && Array.isArray(obj)) { | |||
return 'array'; | |||
if (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined') { |
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.
These ifs could be combined for terseness.
All makes sense to me, here's another swing at it, might not be 100% there yet. Couple notes about what I did:
|
I believe the (symbolExists === false || typeof obj[Symbol.toStringTag] === 'undefined') ...ends up meaning this: (symbolExists === false || typeof obj['undefined'] === 'undefined') ...which can lead to some funky behavior, especially when the |
I agree, it's definitely odd. As far as I can tell, // test/index.js
// using node v4.3
// index.js utilizing `symbolToStringTagExists`
console.log(Symbol.toStringTag); //=> '__@@toStringTag__'
console.log(Object.prototype.toString.call(extendedArray); //=> '[object ExtendedArray]'
console.log(type(extendedArray)); //=> 'array' We could technically swap the if statement to be something like this: // ...
if (
isArrayExists &&
Array.isArray(obj) &&
(typeof Symbol === 'undefined' || typeof Symbol.toStringTag === 'undefined' || typeof obj[Symbol.toStringTag] === 'undefined')
) {
return 'array';
} But then we're running those I agree that I don't like the |
isArrayExists && | ||
Array.isArray(obj) && | ||
(symbolExists === false || typeof obj[Symbol.toStringTag] === 'undefined') | ||
) { |
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.
Typically the precedent is that if we have long conditions, they get assigned to a var which is more readable. For example here:
var symbolToStringTagExists = (symbolExists === false || typeof obj[Symbol.toStringTag] === 'undefined');
if (isArrayExists && Array.isArray(obj) && symbolToStringTagExists)
Would be ideal. Both lines <120 and the if is more readable as the conditions are given names with semantic meaning.
Yes... this makes things more complicated. You could sham Symbol.toStringTag before requiring the main lib from inside the tests, which would get around this problem - but it might make other tests fail. I guess give it a try 😄 |
Now that I think about it, I ran into this same problem when working on #25. An argument could be made that shamming stuff in the tests is making things more difficult than they need to be. Instead, there could be separate tests depending on if |
When I get back to my laptop (hopefully later tonight), I'll try lifting to toStringTag sham stuff toward the top of the file / before the main I also like the idea of splitting the tests into separate environments based on |
Hah. This is turning into way more trouble than I thought it would be! Here's where it's at: Lifting the Considering invalidating the require cache? That would technically work, I think. |
Highly recommend that we hold on this PR, create a new PR to refactor tests so that no shamming is involved, then rebase this PR with the changes, thus making writing tests significantly easier (as well as positioning us better for the future). |
Invalidating the require cache in a |
Well, cludge number one (the Also, this commit log is getting fairly noisy, since I keep pushing partial failures. :\ Sounds like we may be holding off on a merge until a test refactor? But it's probably worth squashing to reduce the noise. Github allows squashing before merging, right? Otherwise, I can do it manually. |
Yeah, let's pause progress on this PR for a while, and we'll get the tests reliable in a different PR. If you have some ideas on how to get the tests reliable, please feel free to raise a new PR, otherwise I'll investigate over next weekend. |
@dvlsg if you would like to revisit this issue, I think it'd be much simpler to solve with the latest |
Sweet! I got very distracted by other work (apologies!), so I'm glad to see it's making it in! Thank you for picking it up. |
Discussion in #49
Still pending any specific tests to cover this scenario. If you'd like me to add tests as part of this pull request, let me know where you'd like them. (
new-ecmascript-types.js
?)