Files
DiceCloud/app/imports/api/engine/computation/utility/walkdown.ts
2024-03-23 16:02:28 +02:00

15 lines
446 B
TypeScript

import { TreeDoc } from '/imports/api/parenting/ChildSchema';
import { TreeNode } from '/imports/api/parenting/parentingFunctions';
export default function walkDown<T extends TreeDoc>(
trees: TreeNode<T>[], callback: (node: TreeNode<T>, stack: TreeNode<T>[]) => any
) {
const stack = [...trees];
while (stack.length) {
const node = stack.pop();
if (!node) return;
callback(node, stack);
stack.push(...node.children);
}
}