@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
37 lines • 1.24 kB
JavaScript
import { AnyArraySchema, AnySchema, NumberSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
export default class NodeDefinition extends Node {
static title = 'Index Array';
static type = 'studio.tokens.array.index';
static description = 'Extracts a value from an array at a given index. ';
constructor(props) {
super(props);
this.addInput('array', {
type: AnyArraySchema
});
this.addInput('index', {
type: {
...NumberSchema,
default: 0
}
});
this.addOutput('value', {
type: AnySchema
});
}
execute() {
const array = this.inputs.array;
const { index } = this.getAllInputs();
//Get the value
const calculated = array.value.at(index);
//Extract the type
//We assume that the array has a single defined item
// Infer the item type from the array schema
let itemType = AnySchema;
if (array.type && array.type.items) {
itemType = array.type.items;
}
this.outputs.value.set(calculated, itemType);
}
}
//# sourceMappingURL=indexArray.js.map