Created
October 27, 2023 00:42
-
-
Save sajid-haniff/b275c88400be57af4714a3a391d308d4 to your computer and use it in GitHub Desktop.
This code provides utilities for handling and searching through operator data in JavaScript (ES6).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample operator data for demonstration purposes | |
const operators = { | |
'+': { name: '_ADD', infix: { name: '_PLUS' }, prefix: null }, | |
'-': { name: '_SUB', infix: { name: '_MINUS' }, prefix: { name: '_NEGATE' } }, | |
'*': { name: '_MUL', infix: null, prefix: null }, | |
'/': { name: '_DIV', infix: null, prefix: null }, | |
// ... potentially other operators ... | |
}; | |
// Create a set of all operator names | |
const operatorNamesSet = new Set( | |
Object.values(operators) | |
.flatMap(({name, infix, prefix}) => [name, infix?.name, prefix?.name]) | |
.filter(Boolean) | |
); | |
console.log(operatorNamesSet); // Output: Set { '_ADD', '_PLUS', '_SUB', '_MINUS', '_NEGATE', '_MUL', '_DIV' } | |
// Utility function to find an operator object by its name | |
const findOperatorObject = (name) => | |
Object.entries(operators).find( | |
([, value]) => value.name === name || value.infix?.name === name || value.prefix?.name === name | |
); | |
const [operatorSymbol, operatorObject] = findOperatorObject('_ADD') || []; | |
console.log(operatorSymbol, operatorObject); // Output: '+' { name: '_ADD', infix: { name: '_PLUS' }, prefix: null } | |
// Log all operator entries | |
console.log(Object.entries(operators)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment