All files / src/walk collOfWalker.js

0% Statements 0/31
0% Branches 0/16
0% Functions 0/3
0% Lines 0/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 69 70 71                                                                                                                                             
var Problem = require( '../models/Problem' );
var isProblem = require( '../utils/isProblem' );
var isNum = require( '../preds/isNum' );
 
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 ( !Array.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;
  }
}
 
module.exports = collOfWalker;