From c6c6f98f1c33e9569b885e89750278fc6a211080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Mon, 20 Feb 2012 18:59:56 +0100 Subject: [PATCH] util: add `util._extend` for extending objects There were 2 duplicates with such functionality in `cluster` and `child_process` modules which were replaced by this function. --- lib/child_process.js | 21 ++++----------------- lib/cluster.js | 20 ++++---------------- lib/util.js | 12 ++++++++++++ 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 9892c5cff26..6b4f78002a6 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -22,7 +22,7 @@ var EventEmitter = require('events').EventEmitter; var net = require('net'); var Process = process.binding('process_wrap').Process; -var inherits = require('util').inherits; +var util = require('util'); var constants; // if (!constants) constants = process.binding('constants'); var Pipe; @@ -53,19 +53,6 @@ function createSocket(pipe, readable) { return s; } -function mergeOptions(target, overrides) { - if (overrides) { - var keys = Object.keys(overrides); - for (var i = 0, len = keys.length; i < len; i++) { - var k = keys[i]; - if (overrides[k] !== undefined) { - target[k] = overrides[k]; - } - } - } - return target; -} - function setupChannel(target, channel) { target._channel = channel; @@ -244,7 +231,7 @@ exports.exec = function(command /*, options, callback */) { args = ['/s', '/c', '"' + command + '"']; // Make a shallow copy before patching so we don't clobber the user's // options object. - options = mergeOptions({}, options); + options = util._extend({}, options); options.windowsVerbatimArguments = true; } else { file = '/bin/sh'; @@ -281,7 +268,7 @@ exports.execFile = function(file /* args, options, callback */) { } // Merge optionArg into options - mergeOptions(options, optionArg); + util._extend(options, optionArg); var child = spawn(file, args, { cwd: options.cwd, @@ -430,7 +417,7 @@ function ChildProcess() { maybeExit(self); }; } -inherits(ChildProcess, EventEmitter); +util.inherits(ChildProcess, EventEmitter); function setStreamOption(name, index, options) { diff --git a/lib/cluster.js b/lib/cluster.js index 8a29f9ac6b7..556ab67fd86 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -29,18 +29,6 @@ function isObject(o) { return (typeof o === 'object' && o !== null); } -function extendObject(origin, add) { - // Don't do anything if add isn't an object - if (!add) return origin; - - var keys = Object.keys(add), - i = keys.length; - while (i--) { - origin[keys[i]] = add[keys[i]]; - } - return origin; -} - var debug; if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) { debug = function(x) { @@ -138,7 +126,7 @@ function isInternalMessage(message) { // Modyfi message object to be internal function internalMessage(inMessage) { - var outMessage = extendObject({}, inMessage); + var outMessage = util._extend({}, inMessage); // Add internal prefix to cmd outMessage.cmd = INTERNAL_PREFIX + (outMessage.cmd || ''); @@ -172,7 +160,7 @@ var messageHandingObject = {}; function handleMessage(worker, inMessage, inHandle) { //Remove internal prefix - var message = extendObject({}, inMessage); + var message = util._extend({}, inMessage); message.cmd = inMessage.cmd.substr(INTERNAL_PREFIX.length); var respondUsed = false; @@ -276,11 +264,11 @@ function Worker(customEnv) { // Create env object // first: copy and add uniqueID - var envCopy = extendObject({}, env); + var envCopy = util._extend({}, env); envCopy['NODE_UNIQUE_ID'] = this.uniqueID; // second: extend envCopy with the env argument if (isObject(customEnv)) { - envCopy = extendObject(envCopy, customEnv); + envCopy = util._extend(envCopy, customEnv); } // fork worker diff --git a/lib/util.js b/lib/util.js index 04e8b1331ae..8de905f4dc9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -506,3 +506,15 @@ exports.inherits = function(ctor, superCtor) { } }); }; + +exports._extend = function(origin, add) { + // Don't do anything if add isn't an object + if (!add) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; +};