@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
88 lines • 2.79 kB
JavaScript
import { Node } from '../../programmatic/node.js';
import { NumberSchema } from '../../schemas/index.js';
import { arrayOf } from '../../schemas/utils.js';
import { setToPrecision } from '../../utils/precision.js';
export default class NodeDefinition extends Node {
static title = 'Arithmetic Series';
static type = 'studio.tokens.series.arithmetic';
static description = 'Generates an arithmetic f(n)= c + (f(n-1)) series of numbers based on the base value, steps down, steps and increment.';
constructor(props) {
super(props);
this.addInput('base', {
type: {
...NumberSchema,
default: 16
},
visible: true
});
this.addInput('stepsDown', {
type: {
...NumberSchema,
default: 0
},
visible: true
});
this.addInput('stepsUp', {
type: {
...NumberSchema,
default: 1
},
visible: true
});
this.addInput('increment', {
type: {
...NumberSchema,
default: 1
}
});
this.addInput('precision', {
type: {
...NumberSchema,
default: 2
}
});
this.addOutput('array', {
type: arrayOf(NumberSchema)
});
this.addOutput('indexed', {
type: {
$id: `https://schemas.tokens.studio/studio.tokens.series.arithmetic/indexed.json`,
type: 'object',
properties: {
index: {
type: NumberSchema
},
value: {
type: NumberSchema
}
}
},
visible: false
});
}
execute() {
const { base, precision, increment, stepsDown, stepsUp } = this.getAllInputs();
const values = [];
for (let i = Math.abs(stepsDown); i > 0; i--) {
const value = setToPrecision(base - increment * i, precision);
values.push({
index: 0 - i,
value: value
});
}
values.push({
index: 0,
value: setToPrecision(base, precision)
});
for (let i = 0; i < Math.abs(stepsUp); i++) {
const value = setToPrecision(base + increment * (i + 1), precision);
values.push({
index: i + 1,
value: value
});
}
this.outputs.array.set(values.map(x => x.value));
this.outputs.indexed.set(values);
}
}
//# sourceMappingURL=arithmetic.js.map