@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
35 lines • 1.31 kB
JavaScript
// src/nodes/string/interpolate.ts
import { Node } from '../../programmatic/node.js';
import { StringSchema } from '../../schemas/index.js';
import { annotatedDynamicInputs } from '../../annotations/index.js';
export default class StringInterpolationNode extends Node {
static title = 'Interpolation';
static type = 'studio.tokens.string.interpolate';
static description = 'Interpolates a string with provided values';
constructor(props) {
super(props);
this.annotations[annotatedDynamicInputs] = true;
this.addInput('template', {
type: StringSchema
});
this.addOutput('value', {
type: StringSchema
});
}
execute() {
const { template, ...variables } = this.getAllInputs();
try {
const interpolatedString = template.replace(/\{(\w+)\}/g, (match, key) => {
if (key in variables) {
return String(variables[key]);
}
return match; // If no replacement found, leave as is
});
this.outputs.value.set(interpolatedString);
}
catch (error) {
this.error = new Error(`Error during string interpolation: ${error.message}`);
}
}
}
//# sourceMappingURL=interpolate.js.map