UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

65 lines 2.03 kB
// src/nodes/math/dataMapping.ts import { BooleanSchema, NumberSchema } from '../../schemas/index.js'; import { Node } from '../../programmatic/node.js'; export default class RangeMappingNode extends Node { static title = 'Range Mapping'; static type = 'studio.tokens.math.dataMapping'; static description = 'Maps a value from one range to another'; constructor(props) { super(props); this.addInput('inputValue', { type: { ...NumberSchema, default: 5 } }); this.addInput('inputMin', { type: { ...NumberSchema, default: 0 } }); this.addInput('inputMax', { type: { ...NumberSchema, default: 100 } }); this.addInput('outputMin', { type: { ...NumberSchema, default: 0 } }); this.addInput('outputMax', { type: { ...NumberSchema, default: 1 } }); this.addInput('clamp', { type: { ...BooleanSchema, default: true } }); this.addOutput('mappedValue', { type: NumberSchema }); } execute() { const { inputValue, inputMin, inputMax, outputMin, outputMax, clamp } = this.getAllInputs(); // Avoid division by zero if (inputMin === inputMax) { throw new Error('inputMin and inputMax cannot be the same value'); } let mappedValue = ((inputValue - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin) + outputMin; if (clamp) { mappedValue = Math.min(Math.max(mappedValue, Math.min(outputMin, outputMax)), Math.max(outputMin, outputMax)); } this.outputs.mappedValue.set(mappedValue); } } //# sourceMappingURL=rangeMapping.js.map