Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
util: make _extend() more robust
Browse files Browse the repository at this point in the history
Add a better 'is object?' check, the old one let values like true slip through.
  • Loading branch information
bnoordhuis committed May 10, 2012
1 parent 5979f09 commit 928d28a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/util.js
Expand Up @@ -509,7 +509,7 @@ exports.inherits = function(ctor, superCtor) {

exports._extend = function(origin, add) {
// Don't do anything if add isn't an object
if (!add) return origin;
if (!add || typeof add !== 'object') return origin;

var keys = Object.keys(add);
var i = keys.length;
Expand Down
9 changes: 9 additions & 0 deletions test/simple/test-util.js
Expand Up @@ -69,3 +69,12 @@ assert.equal(false, util.isError({}));
assert.equal(false, util.isError({ name: 'Error', message: '' }));
assert.equal(false, util.isError([]));
assert.equal(false, util.isError(Object.create(Error.prototype)));

// _extend
assert.deepEqual(util._extend({a:1}), {a:1});
assert.deepEqual(util._extend({a:1}, []), {a:1});
assert.deepEqual(util._extend({a:1}, null), {a:1});
assert.deepEqual(util._extend({a:1}, true), {a:1});
assert.deepEqual(util._extend({a:1}, false), {a:1});
assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});

0 comments on commit 928d28a

Please sign in to comment.