all files / src/ deep-iterator.js

100% Statements 51/51
100% Branches 33/33
100% Functions 5/5
100% Lines 18/18
7 statements, 5 branches Ignored     
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   127× 50×         56×     50×   50×   50×   50×             49× 49×      
import * as search from './search';
import RootNode from './root-node';
import makeSeen from './seen';
 
export default function* deepIterator(rootElement, options = {}) {
  const config = {
    onlyLeaves: false,
    circularReference: 'leaf',
    search: 'dfsPreOrder',
    iterateOverObject: true,
    skipIteration: () => false
  };
 
  if (options.onlyLeaves !== undefined) {
    config.onlyLeaves = options.onlyLeaves;
  }
  if (options.circularReference !== undefined) {
    config.circularReference = options.circularReference;
  }
  if (options.iterateOverObject !== undefined) {
    config.iterateOverObject = options.iterateOverObject;
  }
  if (options.skipIteration !== undefined) {
    config.skipIteration = options.skipIteration;
  }
  if (options.search !== undefined) {
    if (!(options.search in search)) {
      throw new Error(`The search algorithm ${options.search} is incorrect.`);
    }
    config.search = options.search;
  }
  const node = new RootNode(rootElement, config);
  const seen = makeSeen(config.circularReference);
  yield* search[config.search](node, config.onlyLeaves, seen);
}