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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/defu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ function _defu<T>(
baseObject: T,
defaults: any,
namespace = ".",
merger?: Merger
merger?: Merger,
clone?: boolean
): T {
if (!isObject(defaults)) {
return _defu(baseObject, {}, namespace, merger);
return _defu(baseObject, {}, namespace, merger, clone);
}

const object = Object.assign({}, defaults);
Expand All @@ -22,7 +23,7 @@ function _defu<T>(
continue;
}

const value = baseObject[key];
let value = baseObject[key];

if (value === null || value === undefined) {
continue;
Expand All @@ -39,9 +40,17 @@ function _defu<T>(
value,
object[key],
(namespace ? `${namespace}.` : "") + key.toString(),
merger
merger,
clone
);
} else {
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. :(

? 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!

}
object[key] = value;
}
}
Expand All @@ -50,10 +59,14 @@ function _defu<T>(
}

// Create defu wrapper with optional merger and multi arg support
export function createDefu(merger?: Merger): DefuFunction {
export function createDefu(
merger?: Merger,
options?: { clone?: boolean }
): DefuFunction {
const clone = options?.clone || false;
return (...arguments_) =>
// eslint-disable-next-line unicorn/no-array-reduce
arguments_.reduce((p, c) => _defu(p, c, "", merger), {} as any);
arguments_.reduce((p, c) => _defu(p, c, "", merger, clone), {} as any);
}

// Standard version
Expand Down
8 changes: 8 additions & 0 deletions test/defu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,12 @@ describe("defu", () => {
foo: { bar: { modules: "foo.bar:X,Y" } },
});
});

it("should clone the defaults object's values", () => {
const ext = createDefu(undefined, { clone: true });
const source = { a: [1, 2] };
const defaults = { a: { b: 1 } };
const result = ext(source, defaults);
expect(result.a).not.toBe(source.a);
});
});