unjs / defu

🌊 Assign default properties recursively
MIT License
1.06k stars 22 forks source link

feat!: clone support #92

Open thegostisdead opened 1 year ago

thegostisdead commented 1 year ago

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

43081j commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 🤔