/** * LUMINA DEEP UTILS * Provides immutable operations for complex nested data structures. * * Design Principles: * 1. Immutability: All operations return a NEW object reference for the modified branch. * 2. Safety: Path traversal checks for existence. * 3. Performance: Structural sharing is preserved for unchanged branches. */ /** * Retrieves a value from a nested object using dot notation. * @param obj - The source object. * @param path - Dot-separated path (e.g., "slides.0.elements.1"). * @returns The value at path or undefined. */ export function getByPath(obj: any, path: string): any { if (!obj || !path) return undefined; const parts = path.split('.'); let current = obj; for (const part of parts) { if (current === null || current === undefined) return undefined; current = current[part]; } return current; } /** * Sets a value deep in a nested object, returning a new object (immutable). * Handles array updates efficiently. * * @param obj - The source object. * @param path - Dot-separated path. * @param value - The new value to set. * @returns A new object with the updated value. */ export function setByPath(obj: any, path: string, value: any): any { if (!obj) return obj; if (!path) return value; // Root replacement logic could go here, but usually assume obj is root. const parts = path.split('.'); const head = parts[0]; const tail = parts.slice(1).join('.'); // Handle Array if (Array.isArray(obj)) { const index = parseInt(head, 10); if (isNaN(index)) return obj; // Invalid array usage const newArray = [...obj]; if (parts.length === 1) { newArray[index] = value; } else { newArray[index] = setByPath(obj[index], tail, value); } return newArray; } // Handle Object const newObj = { ...obj }; if (parts.length === 1) { newObj[head] = value; } else { newObj[head] = setByPath(obj[head] || {}, tail, value); } return newObj; } /** * Inserts an item into an array at a specific deep path. * * @param obj - The root object. * @param arrayPath - Path to the array itself (e.g., "slides.0.elements"). * @param index - Index to insert at. * @param item - The item to insert. * @returns New root object. */ export function insertByPath(obj: any, arrayPath: string, index: number, item: any): any { const targetArray = getByPath(obj, arrayPath); if (!Array.isArray(targetArray)) { console.warn(`[Lumina Deep] Insert failed: Target at '${arrayPath}' is not an array.`); return obj; } const newArray = [...targetArray]; newArray.splice(index, 0, item); return setByPath(obj, arrayPath, newArray); } /** * Removes an item from an array at a specific deep path. * * @param obj - The root object. * @param arrayPath - Path to the array. * @param index - Index to remove. * @returns New root object. */ export function removeByPath(obj: any, arrayPath: string, index: number): any { const targetArray = getByPath(obj, arrayPath); if (!Array.isArray(targetArray)) { console.warn(`[Lumina Deep] Remove failed: Target at '${arrayPath}' is not an array.`); return obj; } const newArray = [...targetArray]; newArray.splice(index, 1); return setByPath(obj, arrayPath, newArray); } /** * Moves an item within a specific array (reordering). * * @param obj - Root object * @param arrayPath - Path to the array * @param fromIndex - Source index * @param toIndex - Destination index */ export function moveByPath(obj: any, arrayPath: string, fromIndex: number, toIndex: number): any { const targetArray = getByPath(obj, arrayPath); if (!Array.isArray(targetArray)) return obj; const newArray = [...targetArray]; const [movedItem] = newArray.splice(fromIndex, 1); newArray.splice(toIndex, 0, movedItem); return setByPath(obj, arrayPath, newArray); }