Skip to content

Instantly share code, notes, and snippets.

@sajid-haniff
Created October 27, 2023 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajid-haniff/b275c88400be57af4714a3a391d308d4 to your computer and use it in GitHub Desktop.
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).
// 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