-
Notifications
You must be signed in to change notification settings - Fork 0
<refactor> Refactor Node to standardised object #28
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
Conversation
Node class contains properties: - type - content - leftNode - rightNode - parent - commutative - precedence
Previously, expressionToComponentList wasn't able to use the new Node class, for a few reasons: - The Node class needs to be defined before it is accessed (see the Temporal Dead Zone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz) - Move Node class to a new file - Add private type and content properties, which are accessed by the Node getter and setters
- Change node type references to the enum, instead of string - Fix spelling errors
- Change references to node type to reference the enum
- Change references to node type to reference the enum
- Replace references to string literals in the function to use the NodeType and Operator enums
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.
Pull request overview
This pull request refactors the expression tree implementation to use a standardized Node class structure. Previously, nodes were created as plain JavaScript objects with inconsistent properties. The new implementation introduces enum-like classes (Operator and NodeType) and a Node class with defined properties and computed getters.
Changes:
- Introduces new
Node,Operator, andNodeTypeclasses to standardize node representation - Refactors all node creation and type checking throughout the codebase to use the new standardized objects
- Removes manual property assignment for precedence and commutativity in favor of computed getters
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| pages/index/tree.js | New file containing Operator, NodeType, and Node class definitions with enum-like patterns and computed properties |
| pages/index/script.js | Updates all node creation and type checking to use the new Node class and enum values instead of plain objects and string literals |
| index.html | Adds script tag to include the new tree.js file before script.js |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Adding some documentation as to what each property means would be beneficial. |
Returning in a switch-case statement terminates the statement. Hence, a break after the return is just dead code, and unneeded.
Previously, a missing break caused the setting of content for any Operator Node to fall into the function case. e.g: This would set its content as '+', instead of OPERATOR.ADDITION. Currently, these two things are the same thing, but this could deviate in the future.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Error objects return their stack trace, unlike a raw string.
Previously, nodes in the expression tree had no defined structure. Hence, it was hard to tell what a node needed, and what the correct syntax was for that property.
The standard Node class contains properties:
NON-COMPUTED:
COMPUTED:
All website functions now interact with nodes using instances of this Node object.
(*) - properties type and content (when dealing with operators) have enums that define the values they can take.