UNPKG

ratio-lock

Version:

A TypeScript library for managing n numbers with locked ratios. When the ratio is locked, changing one value automatically adjusts all other values to maintain their proportional relationships.

32 lines 1.21 kB
import { parseNumber } from './parse-number.js'; /** * Syncs watched values with ratio lock, propagating changes when locked */ export function syncRatioValues(watchedValues, prevValues, names, ratioLock, setFormValue) { const newValues = watchedValues.map((v, i) => parseNumber(v, names[i])); let changedIndex = -1; for (let i = 0; i < newValues.length; i++) { if (newValues[i] !== prevValues[i]) { changedIndex = i; break; } } if (changedIndex !== -1 && ratioLock.isLocked()) { ratioLock.setValue(changedIndex, newValues[changedIndex]); const updatedValues = ratioLock.getValues(); for (let i = 0; i < names.length; i++) { if (i !== changedIndex) { const name = names[i]; const value = updatedValues[i]; if (name !== undefined && value !== undefined) { // eslint-disable-next-line @typescript-eslint/no-explicit-any setFormValue(name, value); } } } return updatedValues; } ratioLock.setValues(newValues); return newValues; } //# sourceMappingURL=sync-ratio-values.js.map