@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
37 lines • 1.35 kB
JavaScript
import { AnyArraySchema, NumberSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
export default class NodeDefinition extends Node {
static title = 'Remove Item';
static type = 'studio.tokens.array.remove';
static description = 'Removes an item from an array at a specified index and returns both the modified array and the removed item.';
constructor(props) {
super(props);
this.addInput('array', {
type: AnyArraySchema
});
this.addInput('index', {
type: NumberSchema
});
this.addOutput('array', {
type: AnyArraySchema
});
this.addOutput('item', {
type: AnyArraySchema.items
});
}
execute() {
const { index, array } = this.getAllInputs();
const arrayType = this.inputs.array.type;
// Create a copy and remove item if index is valid
const result = [...array];
const [removedItem] = index >= -result.length && index < result.length
? result.splice(index, 1)
: [];
// Set the outputs
this.outputs.array.set(result, arrayType);
if (removedItem !== undefined) {
this.outputs.item.set(removedItem, arrayType.items);
}
}
}
//# sourceMappingURL=remove.js.map