@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
31 lines • 1.19 kB
JavaScript
import { Node } from '../../programmatic/node.js';
import { NumberSchema, StringSchema } from '../../schemas/index.js';
import { Parser } from 'expr-eval';
import { annotatedDynamicInputs } from '../../annotations/index.js';
export default class NodeDefinition extends Node {
static title = 'Evaluate math';
static type = 'studio.tokens.math.eval';
static description = 'Allows you to evaluate arbitrary math expressions';
constructor(props) {
super(props);
this.annotations[annotatedDynamicInputs] = true;
this.addInput('expression', {
type: StringSchema
});
//We expect users to expose the variables they want to use in the expression as inputs
this.addOutput('value', {
type: NumberSchema
});
this.addOutput('expression', {
type: StringSchema
});
}
execute() {
const { expression, ...inputs } = this.getAllInputs();
const parser = new Parser();
const output = parser.evaluate(expression, inputs);
this.outputs.expression.set(expression);
this.outputs.value.set(output);
}
}
//# sourceMappingURL=eval.js.map