@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
40 lines • 1.47 kB
JavaScript
import { AnyArraySchema, AnySchema, NumberSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
export default class NodeDefinition extends Node {
static title = 'Replace Item';
static type = 'studio.tokens.array.replace';
static description = 'Replaces an item in an array at a specified index.';
constructor(props) {
super(props);
this.addInput('array', {
type: AnyArraySchema
});
this.addInput('item', {
type: AnySchema
});
this.addInput('index', {
type: NumberSchema
});
this.addOutput('array', {
type: AnyArraySchema
});
}
execute() {
const { item, index, array } = this.getAllInputs();
const arrayType = this.inputs.array.type;
this.inputs.item.setType(arrayType.items);
// Create a copy of the array value
const result = [...array];
if (index >= 0 && index < result.length) {
result[index] = item;
}
else if (index < 0 && Math.abs(index) <= result.length) {
// Handle negative indices by counting from the end
const actualIndex = result.length + index;
result[actualIndex] = item;
}
// Set the output using the modified result and the original array type
this.outputs.array.set(result, arrayType);
}
}
//# sourceMappingURL=replace.js.map