Skip to content

Commit

Permalink
fix: handle Object assign within class constructors (#31)
Browse files Browse the repository at this point in the history
* Fixed class Object assign.

Removed unnecessary hasOwnProperty.

* Undo auto-formatting.

* Undo organize-imports.

* Check if a property exists in the original object

* Update test/suites/class.js

Co-authored-by: Luke Edwards <[email protected]>
  • Loading branch information
tripodsgames and lukeed authored Oct 27, 2021
1 parent 28565c3 commit 7650274
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function klona(x) {
if (x.constructor !== Object && typeof x.constructor === 'function') {
tmp = new x.constructor();
for (k in x) {
if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
if (x.hasOwnProperty(k) && tmp[k] !== x[k]) {
tmp[k] = klona(x[k]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function klona(x) {
if (x.constructor !== Object && typeof x.constructor === 'function') {
tmp = new x.constructor();
for (k in x) {
if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
if (x.hasOwnProperty(k) && tmp[k] !== x[k]) {
tmp[k] = klona(x[k]);
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/suites/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,19 @@ export default function (klona) {
);
});

Classes('constructor properties :: Object.assign', () => {
class Foobar {
constructor(data) {
Object.assign(this, data);
}
}
const input = new Foobar({ test: 123 });
const output = klona(input);

assert.deepEqual(input, output);
assert.equal(input.constructor, output.constructor);
assert.equal(output.constructor.name, 'Foobar');
});

Classes.run();
}

0 comments on commit 7650274

Please sign in to comment.