All files / src find-diffed-object.js

83.33% Statements 15/18
75% Branches 9/12
100% Functions 1/1
83.33% Lines 15/18

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 35 36 37 38 39 40              26x   26x 106x   106x 40x 40x 40x       66x 46x 46x       20x 8x 8x           12x       26x    
import { isParentNode, isElement } from './parse5-utils';
 
/**
 * @param {ASTNode | ASTNode[]} root
 * @param {string[]} path
 */
export function findDiffedObject(root, path) {
  let node = root;
 
  for (let i = 0; i < path.length; i += 1) {
    const step = path[i];
 
    if (Array.isArray(node)) {
      const intStep = parseFloat(step);
      Eif (Number.isInteger(intStep)) {
        node = node[intStep];
      } else {
        throw new Error(`Non-integer step: ${step} for array node.`);
      }
    } else if (step === 'childNodes') {
      Eif (isParentNode(node)) {
        node = node.childNodes;
      } else {
        throw new Error('Cannot read childNodes from non-parent node.');
      }
    } else if (step === 'attrs') {
      Eif (isElement(node)) {
        node = node.attrs;
      } else {
        throw new Error('Cannot read attributes from non-element node.');
      }
    } else {
      // For all other steps we don't walk further
      break;
    }
  }
 
  return node;
}