Skip to content

Commit

Permalink
Clone without prototype. Closes #290
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 27, 2019
1 parent 5bfe5b6 commit f2eb7fd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ExpandedNodes": [
"",
"\\lib",
"\\test"
],
"SelectedNode": "\\lib\\deep-equal.js",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/hoek/v16/.suo
Binary file not shown.
Binary file added .vs/slnx.sqlite
Binary file not shown.
16 changes: 11 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit f2eb7fd

Please sign in to comment.