-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cloneDeep): maintain prototype when cloning class instances #794
Changes from 2 commits
ced0bb2
91cf525
da58522
75cd7d4
8671626
8174579
c7ebc81
0745767
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { getSymbols } from '../compat/_internal/getSymbols.ts'; | ||
import { isPrototype } from '../compat/_internal/isPrototype.ts'; | ||
import { isPrimitive } from '../predicate/isPrimitive.ts'; | ||
import { isTypedArray } from '../predicate/isTypedArray.ts'; | ||
|
||
|
@@ -184,7 +185,9 @@ function cloneDeepImpl<T>(obj: T, stack = new Map<any, any>()): T { | |
} | ||
|
||
if (typeof obj === 'object' && obj !== null) { | ||
const result = {}; | ||
const result = | ||
typeof obj.constructor === 'function' && !isPrototype(obj) ? Object.create(Object.getPrototypeOf(obj)) : {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m curious why we check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed that calling |
||
|
||
stack.set(obj, result); | ||
|
||
copyProperties(result, obj, stack); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test code is added in
clone
, notcloneDeep
. Let me add a test case forcloneDeep
, too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There already is a
should clone instance
test incloneDeep.spec.ts
, so that's why I only added a single test covering this case.