@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
39 lines • 1.36 kB
JavaScript
import { AnyArraySchema, AnySchema, NumberSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
export default class NodeDefinition extends Node {
static title = 'Inject Item';
static type = 'studio.tokens.array.inject';
static description = 'Injects an item into 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) {
result.splice(index, 0, item);
}
else {
const insertIndex = Math.max(0, result.length + index + 1);
result.splice(insertIndex, 0, item);
}
// Set the output using the modified result and the original array type
this.outputs.array.set(result, arrayType);
}
}
//# sourceMappingURL=inject.js.map