rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
27 lines • 909 B
JavaScript
/**
* @public
* Similar to a reducer, but without the return requirement. It otherwise behaves like {@link treeIterate}.
*
* @param node - The start point.
* @param collected - The reduced value.
* @param callback - Called for each node, including the start point.
*
* @remarks
* See {@link treeCollect}.
*/
export function treeCollect(node, collected, callback) {
callback(collected, node, null, 0);
treeRecurseImpl(collected, node, node.getChildren(), 1, callback);
return collected;
}
function treeRecurseImpl(collected, parent, children, depth, callback) {
if (children == null) {
return;
}
for (let i = 0, iEnd = children.length; i < iEnd; i++) {
const node = children[i];
callback(collected, node, parent, depth);
treeRecurseImpl(collected, node, node.getChildren(), depth + 1, callback);
}
}
//# sourceMappingURL=tree-collect.js.map