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.
31 lines • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.treeCollect = void 0;
/**
* @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}.
*/
function treeCollect(node, collected, callback) {
callback(collected, node, null, 0);
treeRecurseImpl(collected, node, node.getChildren(), 1, callback);
return collected;
}
exports.treeCollect = treeCollect;
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