-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat!: clone support #92
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Caceresenzo <[email protected]> use structuredClone when available
var structuredClone: (x: any) => any; | ||
value = structuredClone | ||
? structuredClone(value) | ||
: JSON.parse(JSON.stringify(value)); |
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.
this would remove unserializable objects like functions, won't it?
my object may have an array of functions!
if (clone) { | ||
// eslint-disable-next-line no-var | ||
var structuredClone: (x: any) => any; | ||
value = structuredClone |
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 the condition we're looking for it
value = structuredClone | |
value = typeof structuredClone !== undefined |
still, the comments in https://github.com/unjs/defu/pull/92/files#r1541894626 stand as structuredClone
wouldn't support all types as well. :(
i'm not sure we should actually do this defu already clones nested structures (plain objects, arrays), which leaves instances (RegExp, Date, etc) uncloned structuredClone and JSON will result in us losing functions and other complex objects (or will throw, in the case of structuredClone) IMO we should leave cloning up to existing battle tested libraries (e.g. klona) and keep defu focused on dealing with applying defaults |
Also noticed that this PR says
Here's an example: const obj1 = { prop1: { a: 1, b: 2 } };
const obj2 = {};
const obj3 = defu(obj2, obj1);
obj3.prop2 = 'hello' // doesn't affect obj2 or obj1
obj3.prop1.c = 3 // affects obj1.prop1 Imo, I agree that we should extend |
ah i understand now, thanks for the example 🙏 we iterate through the target object's properties in order to find what needs merging. however, before we do any of that, we do so in your example, we copy the entirety of if we did have it still feels like we'd end up needing a deep clone algorithm there 🤔 |
Hi, this sould fix #90
Because structuredClone is not available in all environments:
If structuredClone is available, we use it otherwise we use JSON.parse(JSON.stringify(value)).
To have a full support, we need to add polyfills.
Thank @Caceresenzo for his help