@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
63 lines • 2.33 kB
JavaScript
import { BooleanSchema, NumberSchema, StringSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
import { Operator } from '../../schemas/operators.js';
import { arrayOf } from '../../schemas/utils.js';
export default class NodeDefinition extends Node {
static title = 'Find First Match';
static type = 'studio.tokens.search.findFirst';
static description = 'Finds the first array element that matches a comparison condition with a target value';
constructor(props) {
super(props);
this.addInput('array', {
type: {
...arrayOf(NumberSchema),
description: 'Array of numbers to search through'
}
});
this.addInput('target', {
type: {
...NumberSchema,
description: 'Target value to compare against'
}
});
this.addInput('operator', {
type: {
...StringSchema,
enum: [Operator.GREATER_THAN, Operator.LESS_THAN],
default: Operator.GREATER_THAN,
description: 'Comparison operator to use (greater than or less than)'
}
});
this.addOutput('value', {
type: {
...NumberSchema,
description: 'The first matching value found (null if no match)'
}
});
this.addOutput('index', {
type: {
...NumberSchema,
description: 'Index of the first matching value (-1 if no match)'
}
});
this.addOutput('found', {
type: {
...BooleanSchema,
description: 'Whether a matching value was found'
}
});
}
execute() {
const { array, target, operator } = this.getAllInputs();
const comparisonFn = operator === Operator.GREATER_THAN
? (value) => value > target
: (value) => value < target;
const index = array.findIndex(comparisonFn);
const found = index !== -1;
const value = found ? array[index] : undefined;
this.outputs.value.set(value);
this.outputs.index.set(index);
this.outputs.found.set(found);
}
}
//# sourceMappingURL=findFirst.js.map