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
Array Indexing #37
Array Indexing #37
Conversation
- This PR adds the array index operators so that hyperscript programs can not look up array values using this notation: "array[index]"
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.
Really good first effort! Impressed that you managed to make any sense of my code!
return _runtime.unifiedEval(this, function(context, rootVal){ | ||
return rootVal[index]; | ||
}, context, root) | ||
} |
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.
Close! The trick is to pass both root and index in to a single unifiedEval
:
return _runtime.unifiedEval(this, function(context, rootVal, index){
return rootVal[index];
}, context, root, indexExp)
It isn't documented at all, but the arguments that are passed in after the function are correctly resolved and then passed back to that function, in order. So you get context, root and indexExp all evaluated async-transparently and handed back to you.
Also, I don't think we need to parse an int here, we can rely on the user to supply the correct number type.
src/_hyperscript.js
Outdated
_parser.addGrammarElement("arrayIndex", function (parser, tokens, root) { | ||
|
||
if (tokens.matchOpToken("[")) { | ||
var indexExp = parser.parseElement("expression", tokens) |
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 have introduced a new function requireElement
that you probably want to use here, so it throws a parse error if no element is returned.
Now that I understand `_runtime.unifiedEval` a little better, I'm able to post this cleaner (probably more efficient) code. - Single call to `_runtime.unifiedEval` instead of two. - Clearer use of `op` function call should be easier to follow, now. - All tests passing
Updating this because I hadn't read all of your comments before I posted my update. I don't see |
I merged this manually to port to the new runtime, thank you! |
This PR adds the array index operators so that hyperscript programs can not look up array values using this notation: "array[index]"
There is more work to do on this, but it would be good to have another set of eyes on this first, before I try making any more additions to hyperscript.