diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100755 index 00000000..569b15cf --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,9 @@ +{ + "ExpandedNodes": [ + "", + "\\lib", + "\\test" + ], + "SelectedNode": "\\lib\\deep-equal.js", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/hoek/v16/.suo b/.vs/hoek/v16/.suo new file mode 100755 index 00000000..af37cb33 Binary files /dev/null and b/.vs/hoek/v16/.suo differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100755 index 00000000..0328cf9c Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/lib/index.js b/lib/index.js index 571d64ad..8fc832ab 100755 --- a/lib/index.js +++ b/lib/index.js @@ -52,14 +52,20 @@ exports.clone = function (obj, options = {}, _seen = null) { newObj = new RegExp(obj); } else { - const proto = Object.getPrototypeOf(obj); - if (proto && - proto.isImmutable) { + if (options.prototype !== false) { // Defaults to true + const proto = Object.getPrototypeOf(obj); + if (proto && + proto.isImmutable) { - newObj = obj; + newObj = obj; + } + else { + newObj = Object.create(proto); + cloneDeep = true; + } } else { - newObj = Object.create(proto); + newObj = {}; cloneDeep = true; } } diff --git a/test/index.js b/test/index.js index 2e27015a..2dd9b97b 100755 --- a/test/index.js +++ b/test/index.js @@ -247,6 +247,30 @@ describe('clone()', () => { expect(a).to.equal(b); }); + it('clones an object without a prototype', () => { + + const Obj = function () { + + this.a = 5; + }; + + Obj.prototype.b = function () { + + return 'c'; + }; + + const a = new Obj(); + a.x = 123; + + const b = Hoek.clone(a, { prototype: false }); + + expect(a).to.equal(b); + expect(a).to.not.equal(b, { prototype: true }); + expect(b.a).to.equal(5); + expect(b.b).to.not.exist(); + expect(b.x).to.equal(123); + }); + it('reuses cloned Date object', () => { const obj = {