Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f2eb7fd

Browse files
committedMar 27, 2019
Clone without prototype. Closes #290
1 parent 5bfe5b6 commit f2eb7fd

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed
 

‎.vs/VSWorkspaceState.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"ExpandedNodes": [
3+
"",
4+
"\\lib",
5+
"\\test"
6+
],
7+
"SelectedNode": "\\lib\\deep-equal.js",
8+
"PreviewInSolutionExplorer": false
9+
}

‎.vs/hoek/v16/.suo

16 KB
Binary file not shown.

‎.vs/slnx.sqlite

10.4 MB
Binary file not shown.

‎lib/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ exports.clone = function (obj, options = {}, _seen = null) {
5252
newObj = new RegExp(obj);
5353
}
5454
else {
55-
const proto = Object.getPrototypeOf(obj);
56-
if (proto &&
57-
proto.isImmutable) {
55+
if (options.prototype !== false) { // Defaults to true
56+
const proto = Object.getPrototypeOf(obj);
57+
if (proto &&
58+
proto.isImmutable) {
5859

59-
newObj = obj;
60+
newObj = obj;
61+
}
62+
else {
63+
newObj = Object.create(proto);
64+
cloneDeep = true;
65+
}
6066
}
6167
else {
62-
newObj = Object.create(proto);
68+
newObj = {};
6369
cloneDeep = true;
6470
}
6571
}

‎test/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ describe('clone()', () => {
247247
expect(a).to.equal(b);
248248
});
249249

250+
it('clones an object without a prototype', () => {
251+
252+
const Obj = function () {
253+
254+
this.a = 5;
255+
};
256+
257+
Obj.prototype.b = function () {
258+
259+
return 'c';
260+
};
261+
262+
const a = new Obj();
263+
a.x = 123;
264+
265+
const b = Hoek.clone(a, { prototype: false });
266+
267+
expect(a).to.equal(b);
268+
expect(a).to.not.equal(b, { prototype: true });
269+
expect(b.a).to.equal(5);
270+
expect(b.b).to.not.exist();
271+
expect(b.x).to.equal(123);
272+
});
273+
250274
it('reuses cloned Date object', () => {
251275

252276
const obj = {

0 commit comments

Comments
 (0)
Please sign in to comment.