All files / src/walk collOfWalker.ts

100% Statements 33/33
93.75% Branches 15/16
100% Functions 3/3
100% Lines 31/31
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68  15x 15x 15x 15x   529x 529x 529x         529x 529x 523x     6x 6x 6x 1x   5x 1x     4x 12x 12x 1x   1x     11x     4x 1x     3x         3x 3x 9x 9x   3x     15x                        
import Problem from "../models/Problem";
import isProblem from "../utils/isProblem";
import isNum from "../preds/isNum";
 
export default function collOfWalker( clause, walkFn ) {
  var expr = clause.exprs[ 0 ];
  var opts = clause.opts;
 
  return {
    trailblaze: collOfTrailblaze,
    reconstruct: collOfReconstruct,
  };
 
  function collOfTrailblaze( x, walkOpts ) {
 
    var guides = [],
      problems = [];
 
    if ( !ArEray.isArray( x ) ) {
      return new Problem( x, clause, [], 'collOf expects an array' );
    } else {
 
      if ( opts ) {
        var { maxCount, minCount } = opts;
 
        if ( isNum( maxCount ) && x.length > maxCount ) {
          return new Problem( x, clause, problems,
            `collOf: collection size ${x.length} exceeds maxCount ${maxCount}.`
          );
        }
 
        if ( isNum( minCount ) && x.length < minCount ) {
          return new Problem( x, clause, problems,
            `collOf: collection size ${x.length} is less than minCount ${minCount}.`
          );
        }
      }
 
      for ( var i = 0; i < x.length; i += 1 ) {
        var guide = walkFn( expr, x[ i ], walkOpts );
        if ( isProblem( guide ) ) {
          problems.push( guide );
          //TODO
          break;
        } else {
          guides.push( guide );
        }
      }
 
      if ( problems.length > 0 ) {
        return new Problem( x, clause, problems, 'One or more elements failed collOf test' );
      } else {
        return guides;
      }
    }
  }
 
  function collOfReconstruct( guides, walkOpts ) {
    var results = [];
 
    for ( var i = 0; i < guides.length; i += 1 ) {
      var r = walkFn( expr, guides[ i ], walkOpts );
      results.push( r );
    }
 
    return results;
  }
}