diff --git a/lib/utils.js b/lib/utils.js index 846b68ee0ac..fd3589d2442 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -199,6 +199,10 @@ exports.clone = function clone(obj, options, isArrayChild) { return obj.toObject(options); } + if (typeof obj.cloneOf === 'function') { + return obj.cloneOf(); + } + if (obj.constructor) { switch (exports.getFunctionName(obj.constructor)) { case 'Object': diff --git a/test/utils.test.js b/test/utils.test.js index 9431e463ea2..618c9986912 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -254,6 +254,22 @@ describe('utils', function() { return Promise.resolve(); }); + + it('invokes cloneOf method on object before valueOf if exists', function(done) { + const o = Object.create({ + cloneOf() { + this.cloneOfCalled = true; + return this; + }, + }); + + const out = utils.clone(o); + assert.deepEqual(out, o); + assert.equal(o.cloneOfCalled, true); + assert.equal(out.valueOfCalled, undefined); + + done(); + }); }); it('array.flatten', function(done) {