All files / src/utils match.js

0% Statements 0/7
0% Branches 0/4
0% Functions 0/1
0% Lines 0/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                                           
/**
 * convenient method used in conjunction with "and" and "or" conformation to handle labelled cases
 *
 */
 
function match( alts, handlerMap, unknownCaseHandler ) {
  for ( var label in alts ) {
    // should iterate only once
    if ( alts.hasOwnProperty( label ) && handlerMap.hasOwnProperty( label ) ) {
      return handlerMap[ label ]( alts[ label ] );
    } else {
      return unknownCaseHandler( alts[ label ] );
    }
  }
 
  // only reach here if alts is empty
  console.error( alts );
  throw new Error( 'No cases present in the given object' );
}
 
module.exports = match;