Use _.result to get commands from options.

This commit is contained in:
Andrew Stewart 2014-12-17 15:07:03 -08:00
parent 6cbf2db122
commit c9e16fe8c2
2 changed files with 7 additions and 13 deletions

View File

@ -84,20 +84,14 @@ var Robot = module.exports = function Robot(opts) {
}, this);
if (opts.commands) {
var cmds = opts.commands;
var cmds = _.result(opts, "commands");
if (_.isObject(cmds)) {
if (_.isObject(cmds) && !_.isArray(cmds)) {
this.commands = cmds;
}
if (_.isFunction(cmds)) {
var result = cmds.call(this, this);
if (_.isObject(result) && !_.isArray(result)) {
this.commands = result;
} else {
throw new Error("#commands function must return an object");
}
var err = "#commands must be an object ";
err += "or a function that returns an object";
throw new Error(err);
}
}

View File

@ -139,7 +139,7 @@ describe("Robot", function() {
it("throws an error", function() {
expect(fn).to.throw(
Error,
"#commands function must return an object"
"#commands must be an object or a function that returns an object"
);
});
});