Skip to content
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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

thegostisdead
Copy link

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

Co-authored-by: Caceresenzo <caceresenzo1502@gmail.com>

use structuredClone  when available
@manniL manniL requested a review from pi0 August 17, 2023 13:39
var structuredClone: (x: any) => any;
value = structuredClone
? structuredClone(value)
: JSON.parse(JSON.stringify(value));

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

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

Suggested change
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. :(

@pi0 pi0 marked this pull request as draft March 28, 2024 10:15
@43081j
Copy link

43081j commented Mar 31, 2024

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

@ineshbose
Copy link

Also noticed that this PR says feat!, but I don't think this would be a breaking change 😄

defu already clones nested structures (plain objects, arrays)

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 createDefu to take a second prop mergerOptions: { clone?: boolean }, and within the _defu function, somewhere we will only need to use if (baseObject[key] === undefined) baseObject[key] = Object.assign({}, object[key]) right before we check if both baseObject[key] and object[key] are JS objects and we recursively call _defu again... something like this. This way, we can also export a defuCopy function from this library... that's my idea, but I trust Pooya to give a well-thought-out solution!

@43081j
Copy link

43081j commented Mar 31, 2024

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 Object.assign({}, defaults) to catch new properties our target doesn't have (at the root).

so in your example, we copy the entirety of prop1 across by reference since our target doesn't have a prop1

if we did have clone, though, that still means we'd have to initially clone the defaults deeply. we can't do it as part of the loop since that is iterating over properties our target has, not properties the defaults have (or we'd need two loops)

it still feels like we'd end up needing a deep clone algorithm there 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clone support
3 participants