import type { Path } from '../../index'; export const copyPath = (path: Path): Path => { const copy = new Array(path.length); let index = 0; while (index < path.length) { const pathValue = path[index]; if (pathValue === undefined) { throw new Error('Cannot copy an invalid path.'); } copy[index] = pathValue; index += 1; } return copy; }; export const parentPath = (path: Path): Path => { const parent = new Array(Math.max(0, path.length - 1)); let index = 0; while (index < parent.length) { const pathValue = path[index]; if (pathValue === undefined) { throw new Error('Cannot get a parent path for an invalid path.'); } parent[index] = pathValue; index += 1; } return parent; }; export const nextPath = (path: Path): Path => { const lastIndex = path.length - 1; const index = path[lastIndex]; if (index === undefined) { throw new Error('Cannot get a next path for the root.'); } const next = new Array(path.length); let pathIndex = 0; while (pathIndex < lastIndex) { const pathValue = path[pathIndex]; if (pathValue === undefined) { throw new Error('Cannot get a next path for an invalid path.'); } next[pathIndex] = pathValue; pathIndex += 1; } next[lastIndex] = index + 1; return next; };