Code coverage report for es6/lib/dispatcher.js

Statements: 100% (31 / 31)      Branches: 100% (14 / 14)      Functions: 100% (3 / 3)      Lines: 100% (11 / 11)      Ignored: 1 statement, 2 branches     

All files » es6/lib/ » dispatcher.js
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                      15 2 49   49 191   189 44       47 3     44          
import isUndefined from 'lodash/lang/isUndefined';
 
/**
 * Recursive dispatcher, returns a function which iterates a series of commands
 * looking for one to handle the target and return a value. If the target
 * cannot be handled by a command the command returns undefined. If none of the
 * supplied commands handle the target, an error is thrown.
 *
 * @param commands
 * @returns {Function}
 */
function dispatcher(...commands) {
  return function dispatch(target) {
    let result;
 
    for (let command of commands) {
      result = command(target, dispatch);
 
      if (!isUndefined(result)) {
        break;
      }
    }
 
    if (isUndefined(result)) {
      throw new TypeError(`Encountered unexpected target`);
    }
 
    return result;
  };
}
 
export default dispatcher;