@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
23 lines (22 loc) • 754 B
JavaScript
import { LEAF } from "../+constants.js";
export function traverse(root, fn, comparator) {
const entries = root instanceof Map ? root.entries() : root.children.entries();
const stack = [...entries];
if (comparator)
stack.sort((l, r) => comparator(l[1], r[1]));
while (stack.length) {
const [key, node] = stack.shift();
if (node.kind === LEAF) {
fn(node, key);
}
else {
const continueWith = fn(node, key) ?? true;
if (continueWith) {
const children = [...node.children];
if (comparator)
children.sort((l, r) => comparator(l[1], r[1]));
stack.unshift(...children);
}
}
}
}