cressie176/power-merge

View on GitHub
lib/commands/invoke.js

Summary

Maintainability
A
0 mins
Test Coverage
const debug = require('debug')('power-merge:commands:invoke');
const R = require('ramda');

module.exports = function __invoke(fn) {

  return function _invoke(context) {

    return R.is(Function, fn) ? inline : named;

    function inline(facts) {
      return invoke(fn.name || 'anon', fn, facts);
    }

    function named(facts) {
      return invoke(fn, context.get('namedCommands')[fn], facts);
    }

    function invoke(name, command, facts) {
      debug('command: %s, facts: %o', command, facts);
      if (!command) throw new Error('No such command: ' + name);
      if (!R.is(Function, command)) throw new Error(name + ' is not a function');
      const result = command(facts);
      debug('return: %o', result);
      return result;
    }
  };
};