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.
This commit is contained in:
Maciej Małecki 2012-02-20 18:59:56 +01:00 committed by Ben Noordhuis
parent 3f4062309e
commit c6c6f98f1c
3 changed files with 20 additions and 33 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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;
};