From 5cea07602a86fcef244b769912e224f9f6a036c5 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 17 Jan 2015 21:29:07 -0800 Subject: [PATCH] Extract function to DRY up code --- lib/robot.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 2d46660..b62ba0c 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -172,11 +172,7 @@ Robot.prototype.initConnections = function(opts) { Logger.warn(str); - _.forEach(opts.connections, function(conn, key) { - var name = _.isString(key) ? key : conn.name; - this.connection(name, conn); - }, this); - + this.performArraySetup(opts.connections, "connection"); return this.connections; } @@ -269,11 +265,7 @@ Robot.prototype.initDevices = function(opts) { Logger.warn(str); - _.forEach(opts.devices, function(device, key) { - var name = _.isString(key) ? key : device.name; - this.device(name, device); - }, this); - + this.performArraySetup(opts.devices, "device"); return this.devices; } @@ -421,3 +413,10 @@ Robot.prototype.halt = function(callback) { Robot.prototype.toString = function() { return "[Robot name='" + this.name + "']"; }; + +Robot.prototype.performArraySetup = function(things, typeOfThing) { + _.forEach(things, function(t, key) { + var name = _.isString(key) ? key : t.name; + this[typeOfThing](name, t); + }, this); +};