All files / src/walk shapeWalker.ts

78.13% Statements 125/160
71.11% Branches 64/90
92.86% Functions 13/14
77.3% Lines 109/141
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261  15x 15x 15x 15x 15x     144x   144x 58x   144x 99x   144x         83x     83x   83x   83x 83x 11x   72x 72x   72x 29x   72x 26x     72x 48x   72x 48x   72x                       72x     74x 143x 143x 143x 143x         143x 53x   90x 90x 48x                     72x 72x 72x   72x       72x   72x 53x   72x 48x 48x 48x 48x 48x 52x 52x   48x               72x     15x   105x 105x     83x 83x 40x 40x   40x 35x 35x 67x 67x       5x 5x         40x 85x   85x                               85x   67x 64x 64x   6x       3x     18x   18x 2x             40x 11x     72x       48x 52x       143x 143x 90x 90x 90x 110x 110x 110x   52x     52x 52x         52x         90x   53x 53x   53x 53x 53x       53x                       279x 279x 279x                      
import isProblem from "../utils/isProblem";
import isUndefined from "../preds/isUndefined";
import Problem from "../models/Problem";
import clauseFromAlts from "../utils/clauseFromAlts";
 
export default function shapeWalker( clause, walkFn ) {
  var keyConformer;
  var { requiredFields, optionalFields } = clause.opts.conformedArgs.shapeArgs;
 
  var reqClauses,
    optClauses;
  if ( requiredFields ) {
    reqClauses = requiredFields.req || requiredFields.required;
  }
  if ( optionalFields ) {
    optClauses = optionalFields.opt || optionalFields.optional;
  }
 
  return {
    trailblaze: shapeTrailblaze,
    reconstruct: shapeReconstruct,
  };I

  function shapeTrailblaze( x, walkOpts ) {
    if (E [ 'object', 'function' ].indexOf( typeof x ) < 0 ) {
      return new Problem( x, clause, [], 'Value is not an object' );
    }
 
    if ( !keyConformer ) {
       // lazy
      keyConformer = _genKeyConformer( reqClauses, optClauses, walkFn, walkOpts );
    }
    var keyConformedR = keyConformer( x );
 
    if ( isProblem( keyConformedR ) ) {
      return keyConformedR;
    }
    var problems = [];
 
 
    var guide = { val: x, groups: [], singles: [] };
 
    var reqFieldDefs;
    if ( reqClauses ) {
      reqFieldDefs = reqClauses.fieldDefs;
    }
 
    if ( reqFieldDefs ) {
      prIocessFieldDefs_mut( reqFieldDefs, false );
    }

    var optFieldDefs;
    if ( optClauses ) {
      optFieldDefs = optClauses.fieldDefs;
    }
    if ( optFieldDefs ) {
      processFieldDefs_mut( optFieldDefs, true );
    }
 
    if ( problems.length > 0 ) {
      var problemMap = {};
      var failedNames = [];
      for ( var i = 0; i < problems.length; i++ ) {
        var [ n, p ] = problems[ i ];
        failedNaEmes.push( n );
        problemMap[ n ] = p;
      }
      var newP = newI Problem( x, clause, problemMap, 'At least one property failed validation: ' + failedNames.join( ', ' ) );
      return newP;
    } else {
      return guide;
    }
 
    function processFieldDefs_mut( fieldDefs, optional ) {
      fieldLoop: for ( var name in fieldDefs ) {
        if ( fieldDefs.haEsOwnProperty( name ) ) {
          var keyValAlts = fieldDefs[ name ];
          var { noop, problem, singleMatch, groupMatch } = getFieldGuide( x, name, keyValAlts, walkFn, walkOpts );
          if ( problem ) {
            problems.push( [ name, problem ] );
            //TODO: improve this;
            break fieldLoop;
          } else if ( singleMatch ) {
            guide.singles.push( singleMatch );
          } else if ( groupMatch ) {
            if ( groupMatch.matchedKeys.length > 0 ) {
              guide.groups.push( groupMatch );
            }
          } else if ( !noop ) {
            throw '!';
          }
        }
      }I
    }
  }
 
  function shapeReconstruct( { val, singles, groups }, walkOpts ) {
 
    var conform = { walkOpts };
 
    var { mutate } = walkOpts;
 
    var conformed;
E
    if ( mutate ) {
      conformed = val;
    } else {
      conformed = Object.assign( {}, val );
    }
 
    singles.forEach( ( fieldGuide ) => {
      restoreField_mut( conformed, fieldGuide, walkFn, walkOpts );
    } );

    groups.forEach( ( { name, matchedKeys } ) => {
      if ( conform ) {
        conformed[ name ] = {};
        var keysToDel = [];
        matchedKeys.forEach( ( fieldGuide ) => {
          restoreField_mut( conformed[ name ], fieldGuide, walkFn, walkOpts );
          keysToDel.push( fieldGuide.key );
        } );
        _deleteKeys( conformed, keysToDel );
      } else {
        matchedKeys.forEach( ( fieldGuide ) => {
          restoreField_mut( conformed, fieldGuide, walkFn, walkOpts );
        } );
      }
    } );
 
    return conformed;
  }
}
 
function restoreField_mut( x, { key, clause, guide }, walkFn, walkOpts ) {
  x[ key ] = walkFn(E clause, guide, walkOpts );
}
 
function _genKeyConformer( reqClauses, optClause, walkFn, walkOpts ) {
  return function tryConformKeys( x ) {
    if ( reqClausEes ) {
      let missingKeys = [];
      var { fieldDefs, keyList } = reqClauses;
      var reqNames;

      if ( fieldDefs ) {
        reqNames = [];
        for ( var name in fieldDefs ) {
          if ( fieldDefs.hasOwnProperty( name ) ) {
            reqNIames.push( name );
          }
        }
      } else if ( keyList ) {
        reqNames = [].concat( keyList );
      } else {
        throw 'unsupported';
      }

      for ( var i = 0; i < reqNames.length; i++ ) {
        var reqName = reqNames[ i ];
        //key clause
        if ( fieldDefs && fieldDefs[ reqName ].keyValExprPair ) {
          var found = false;
          keyTrav: for ( var kk in x ) {
            if ( x.hasOwnProperty( kk ) ) {
              var rr = _conformNamedOrExpr( kk, fieldDefs[ reqName ].keyValExprPair.keyExpression, walkFn, walkOpts );
              if ( !isProblem( rr ) ) {
                //found a match
                found = true;
                break keyTrav;
              }
            }
          }
          if ( !found ) {
            missingKeys.push( reqName );
          }
        } else if ( fieldDefs && fieldDefs[ reqName ].valExpressionOnly ) {
          //key clause
          if ( x.hasOEwnProperty( reqName ) ) {
            var rrr = _conformNamedOrExpr( x[ reqName ], fieldDefs[ reqName ].valExpressionOnly, walkFn, walkOpts );
            if ( isProblem( rrr ) ) {
              //found a match
              missingKeys.push( reqName );
            }
          } else {
            missingKeys.push( reqName );
          }
        } else if ( keyList ) {
           //plain string key
          if ( x[ reqName ] === undefined ) {
            missingKeys.push( reqName );
          }
        } else {
          throw '!';
        }
      }
      if ( missingKeys.length > 0 ) {
        return new Problem( x, reqClauses, [], 'req: keys required: ' + missingKeys.join( ', ' ) );
      }
    }
 
    return x;
  }
}
 
 
function _deEleteKeys( subject, keys ) {
  for ( var i = 0; i < keys.length; i++ ) {
    delete subject[ keys[ i ] ];
  }
}I

function getFieldGuide( x, name, keyValAlts, walkFn, walkOpts ): any {
  var { valExpressionOnly, keyValExprPair } = keyValAlts;
  if ( keyValExprPaiIr ) {
    var matchedKeys = [];

    var { keyExpression, valExpression } = keyValExprPair;
    keysExamine: for ( var k in x ) {
      if ( x.hasOwnProperty( k ) ) {
        var keyResult = _conformNamedOrExpr( k, keyExpression, walkFn, walkOpts );
        if ( !isProblem( keyResult ) ) {
          // single string char case, where name = 0 and x = ''
          if ( x === x[ k ] ) {
            continue keysExamine;
          }
         E var valGuide = _conformNamedOrExpr( x[ k ], valExpression, walkFn, walkOpts );
          if ( isProblem( valGuide ) ) {
            //TODO: improve
        E    return { problem: valGuide };
          } else {
            ImatchedKeys.push( { key: k, clause: clauseFromAlts( valExpression ), guide: valGuide } );
          }
        }
      }
    }
    return { groupMatch: { name, matchedKeys } };
  } else if ( valExpressionOnly ) {
    var v = x[ name ];
    // single string char case, name = 0
    if ( !isUndefined( v ) && x[ name ] !== x ) {
      var g = _conformNamedOrExpr( v, valExpressionOnly, walkFn, walkOpts );
      if ( isProblem( g ) ) {
        return { problem: g };
      } else {
        return { singleMatch: { key: name, clause: clauseFromAlts( valExpressionOnly ), guide: g } };
      }
    } else {
      return { noop: true };
    }
  } else {
    throw '!!';
  }
}
 
function _conformNamedOrExpr( x, alts, walkFn, walkOpts ) {
  var s = clauseFromAlts( alts );
  var r = walkFn( s, x, walkOpts );
  return r;
}