-
-
Notifications
You must be signed in to change notification settings - Fork 267
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
chore(suite): use structuredClone for deep copying #6071
Changes from all commits
208ba56
6e3d10d
c21c178
da91863
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const deepClone = <T>(value: T): T => { | ||
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 would suggest little bit restrict values that can be passed, because you can pass anything now I think, but I am not sure how JSON will behave if you will pass object with BigInt or other special types. It will work for structuredClone but JSON could mess it up, so we should limit it only types that will work 100% for both solutions. 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. And now we are back to square 1 from which the conversation has started hehe :D Yeah, makes sense. 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 agree that we should consider that objects are going to be stringified when using const appointment = {
location: 'Prague',
day: new Date(),
}
const whenStructuredClone = structuredClone(appointment)
whenStructuredClone.day.getDate()
// result -> 25
const whenStringified = JSON.parse(JSON.stringify(appointment))
whenStringified.day.getDate()
// result -> Uncaught TypeError: whenStringified.day.getDate is not a function And the same for nested objects. And the same behavior can be expected from other objects like Restricting the use to types that will work 100% for both could be a solution but if I am not mistaken I think it should iterate over the whole object and nested potential objects to make sure there is no nested 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 think type like this should do the job: import { JsonValue } from 'type-fest' |
||
if (value === undefined) { | ||
return value; | ||
} | ||
|
||
let clone; | ||
|
||
if (typeof structuredClone === 'function') { | ||
clone = structuredClone(value); | ||
} else { | ||
clone = JSON.parse(JSON.stringify(value)); | ||
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. We should probably use some better polyfill than this. There are already available. 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. Never heard of it before, core-js has a polyfill. (I know we are cautious about included new dependencies, but just sharing). |
||
} | ||
|
||
return clone; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { deepClone } from '../src/deepClone'; | ||
|
||
describe('deepClone', () => { | ||
it('deepClone works', () => { | ||
const original = { | ||
a: 1, | ||
b: '2', | ||
c: { | ||
a: 1, | ||
b: '2', | ||
c: { | ||
a: [1, 2, 3], | ||
b: '2', | ||
}, | ||
}, | ||
}; | ||
|
||
const copy = deepClone(original); | ||
const shallowCopy = { ...original }; | ||
|
||
expect(copy.c.c.a === original.c.c.a).toBeFalsy(); | ||
expect(shallowCopy.c.c.a === original.c.c.a).toBeTruthy(); | ||
}); | ||
}); |
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 are ~ 12 more places where
JSON.parse(JSON.stringify...
is used, I guess it would be nice to use this util on all those places?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.
da91863