diff --git a/src/index.js b/src/index.js index 014d59c..6903821 100644 --- a/src/index.js +++ b/src/index.js @@ -4,17 +4,26 @@ export default function klona(x) { var k, tmp, str=Object.prototype.toString.call(x); if (str === '[object Object]') { - tmp = {}; - for (k in x) { - if (k === '__proto__') { - Object.defineProperty(tmp, k, { - value: klona(x[k]), - configurable: 1, - enumerable: 1, - writable: 1, - }); - } else { - tmp[k] = klona(x[k]); + if (x.constructor !== Object && typeof x.constructor === 'function') { + tmp = new x.constructor(); + for (k in x) { + if (tmp[k] === void 0) { + tmp[k] = klona(x[k]); + } + } + } else { + tmp = {}; // null + for (k in x) { + if (k === '__proto__') { + Object.defineProperty(tmp, k, { + value: klona(x[k]), + configurable: true, + enumerable: true, + writable: true, + }); + } else { + tmp[k] = klona(x[k]); + } } } return tmp; diff --git a/test/index.js b/test/index.js index dc1e57b..1c5e723 100644 --- a/test/index.js +++ b/test/index.js @@ -65,39 +65,30 @@ Classes('class', () => { const output = klona(input); assert.deepEqual(input, output); + assert.equal(input.constructor, output.constructor); + assert.equal(output.constructor.name, 'Foobar'); output.foobar = 123; // @ts-ignore assert.notEqual(input.foobar, 123); }); -Classes.run(); - -// --- - -const Constructor = suite('constructor'); - -Constructor('hijack', () => { - let count = 0; - - class Foo {} - function CustomArray() { - count++; - } - - const input = new Foo(); - assert.equal(input.constructor.name, 'Foo'); - - input.constructor = CustomArray; - assert.equal(input.constructor.name, 'CustomArray'); +// @see https://github.com/lukeed/klona/issues/14 +Classes('prototype', () => { + function Test () {} + Test.prototype.val = 42; + const input = new Test(); const output = klona(input); + assert.deepEqual(input, output); - assert.equal(count, 0, '~> did not call constructor'); + assert.deepEqual(output.constructor, Test); + assert.deepEqual(output.__proto__, { val: 42 }); + assert.deepEqual(output, {}); }); -Constructor.run(); +Classes.run(); // ---