All files / src/utils traversal.ts

100% Statements 12/12
87.5% Branches 7/8
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                        4x 66x 18x     48x 48x 48x         4x 36x 1x   35x 16x   19x    
/*!
 * Copyright 2020 Cognite AS
 */
 
interface WithChildren<T> {
  readonly children: T[];
}
 
interface WithParent<T> {
  readonly parent?: T;
}
 
export function traverseDepthFirst<T extends WithChildren<T>>(root: T, visitor: (element: T) => boolean): void {
  if (!visitor(root)) {
    return;
  }
 
  Eif (root.children) {
    for (const child of root.children) {
      traverseDepthFirst(child, visitor);
    }
  }
}
 
export function traverseUpwards<T extends WithParent<T>>(node: T, callback: (element: T) => boolean) {
  if (!callback(node)) {
    return;
  }
  if (!node.parent) {
    return;
  }
  traverseUpwards(node.parent, callback);
}