UNPKG

@selenite/graph-editor

Version:

A graph editor for visual programming, based on rete and svelte.

53 lines (52 loc) 1.53 kB
export var Vector; (function (Vector) { function zero() { return { x: 0, y: 0, z: 0 }; } Vector.zero = zero; function isVector(o) { return typeof o === 'object' && o !== null && 'x' in o && 'y' in o && 'z' in o; } Vector.isVector = isVector; function fromNumber(x) { return { x, y: x, z: x }; } Vector.fromNumber = fromNumber; })(Vector || (Vector = {})); function convertNumber(t, parser) { if (typeof t === 'boolean') return t ? 1 : 0; if (Vector.isVector(t)) return parser(String(t.x)); const attempt = parser(t); return isNaN(attempt) ? 0 : attempt; } export const valueConverters = { any: (t) => t, boolean: (t) => !!t, groupNameRef: (t) => String(t), integer: (t) => convertNumber(t, parseInt), number: (t) => convertNumber(t, parseFloat), string: (t) => (typeof t === 'string' ? t : JSON.stringify(t)), vector: (t) => { if (Vector.isVector(t)) return { x: t.x, y: t.y, z: t.z }; if (typeof t === 'string') { try { const parsed = JSON.parse(t); if (Vector.isVector(parsed)) return { x: parsed.x, y: parsed.y, z: parsed.z }; } catch { } } if (typeof t === 'number') return Vector.fromNumber(t); if (typeof t === 'boolean') return Vector.fromNumber(t ? 1 : 0); return Vector.zero(); } };