import { type Descendant, NodeApi, type NodeMatch, type NodeOf, type Path, } from '../../facade'; export type ApplyDeepToNodesOptions = { // Function to call on each node following the query. apply: ( node: NodeOf, source: (() => Record) | Record ) => void; // The destination node object. node: N; // Match nodes to update. match?: NodeMatch; // The source object. Can be a factory. source: (() => Record) | Record; path?: Path; }; /** Recursively apply a transform to child nodes matching a query. */ export const applyDeepToNodes = ({ apply, match, node, path = [], source, }: ApplyDeepToNodesOptions) => { if (!match || NodeApi.matches(node, match, path)) { if (typeof source === 'function') { apply(node, source()); } else { apply(node, source); } } if (!NodeApi.isAncestor(node)) return; for (const [child, childPath] of NodeApi.children(node, [])) { applyDeepToNodes({ apply, match, node: child as any, path: path.concat(childPath), source, }); } };