From 346df2e98810b8781e95dd077721091c064ce6cd Mon Sep 17 00:00:00 2001 From: Ryan Seys Date: Fri, 29 Aug 2014 12:49:38 -0400 Subject: [PATCH] Reorder util.extend logic --- lib/common/util.js | 6 +++--- test/common/util.js | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/common/util.js b/lib/common/util.js index 994aae3bf92..e4f5384cf0c 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -35,14 +35,14 @@ function extend(from, to) { if (from === null || typeof from !== 'object') { return from; } - if (from.constructor !== Object && from.constructor !== Array) { - return from; - } if (from.constructor === Date || from.constructor === Function || from.constructor === String || from.constructor === Number || from.constructor === Boolean) { return new from.constructor(from); } + if (from.constructor !== Object && from.constructor !== Array) { + return from; + } to = to || new from.constructor(); for (var name in from) { to[name] = to[name] ? extend(from[name], null) : to[name]; diff --git a/test/common/util.js b/test/common/util.js index b1c2a8834c0..8c4942c6d81 100644 --- a/test/common/util.js +++ b/test/common/util.js @@ -26,6 +26,13 @@ describe('extend', function() { var copy = util.extend(null, {}); assert.strictEqual(copy, null); }); + + it('should return a new Date for Date input', function() { + var now = new Date(); + var copy = util.extend(now, {}); + assert.notStrictEqual(copy, now); + assert.strictEqual(copy.toString(), now.toString()); + }); }); describe('arrayize', function() {