All files / src/walk andWalker.ts

100% Statements 23/23
100% Branches 6/6
100% Functions 3/3
100% Lines 23/23
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 41 42  15x 15x 15x 15x   605x 605x         601x 601x 601x 601x 1138x 1138x 1137x 67x 67x     1070x 1070x     600x 533x     67x       529x     529x     15x
import Problem from "../models/Problem";
import isProblem from "../utils/isProblem";
import clauseFromAlts from "../utils/clauseFromAlts";
 
export default function andWalker( clause, walkFn ) {
  var exprs = clause.opts.conformedExprs.map( clauseFromAlts );
 
  return {
    trailblaze: andTrailblaze,
    reconstruct: andReconstruct,
  };
 
  function andTrailblaze( data, walkOpts ) {
    var r = data;
    var conforms = [];
    var problems = [];
 
    for ( var i = 0; i < exprs.length; i += 1 ) {
      var currData = ( i === 0 ) ? data : conforms[ i - 1 ];
      r = walkFn( exprs[ i ], currData, walkOpts );
      if ( isProblem( r ) ) {
        problems.push( r );
        break;
      } else {
        var conformedR = walkFn( exprs[ i ], r, Object.assign( {}, walkOpts, { phase: 'reconstruct' } ) );
        conforms.push( conformedR );
      }
    }
 
    if ( problems.length === 0 ) {
      return { conforms };
    } else {
      return new Problem( data, exprs, problems, 'One or more expressions failed AND test' );
    }
  }
 
  function andReconstruct( { conforms } ) {
    //TODO: implement propagated conform. Perhaps as an option propagateConform
    // or as a separate clause construct such as "propagate"
    return conforms[ exprs.length - 1 ];
  }
}