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

fix: merge objects with Module type #121

Merged
merged 7 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/defu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function _defu<T>(
if (!_isPlainObject(defaults)) {
return _defu(baseObject, {}, namespace, merger);
}

const object = Object.assign({}, defaults);

for (const key in baseObject) {
Expand Down Expand Up @@ -57,8 +56,7 @@ function _isPlainObject(value: unknown): boolean {
(prototype === null ||
prototype === Object.prototype ||
Object.getPrototypeOf(prototype) === null) &&
!(Symbol.toStringTag in value) &&
!(Symbol.iterator in value)
(!(Symbol.toStringTag in value) || !(Symbol.iterator in value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it is purely for Date merging fix, using instance of Date would be faster

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it isn't. See the linked issue. It also applies to Module (import via *)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see! With this PR want to merge Module instances!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking, this logic currently basically bypasses entire L59 as iterator symbol does not exist in any of tested values (removing the line also all the tests pass). Wondering how we can cover it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see! With this PR want to merge Module instances!

yes indeed πŸ‘πŸ»

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking, this logic currently basically bypasses entire L59 as iterator symbol does not exist in any of tested values (removing the line also all the tests pass). Wondering how we can cover it

Oh, I see πŸ€” That's a bit tricky. I also wonder how to cover it then...

But - do we need to? Dates are correctly replaced (not merged) like that too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have ported test suite of is-plain-object (see commits to main) and also merged in with this PR. Intrestingly we will be breaking two more tests at least (Math and native arguments will be wrongly detected as pure object)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with a logic that precisely works for Module to reduce (more!) regression chances

);
}

Expand Down
25 changes: 22 additions & 3 deletions test/defu.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectTypeOf } from "expect-type";
import { it, describe, expect } from "vitest";
import { defu, createDefu, defuFn, defuArrayFn } from "../src/defu";
import * as asteriskImport from "./fixtures/";

// Part of tests brought from jonschlinkert/defaults-deep (MIT)
const nonObject = [null, undefined, [], false, true, 123];
Expand Down Expand Up @@ -50,7 +51,7 @@ describe("defu", () => {
}>();
});

it.skip("should avoid merging objects with custom constructor", () => {
it("should avoid merging objects with custom constructor", () => {
class Test {
// eslint-disable-next-line no-useless-constructor
constructor(public value: string) {}
Expand All @@ -59,11 +60,12 @@ describe("defu", () => {
expect(result).toEqual({ test: new Test("a") });
});

it.skip("should assign date properly", () => {
it("should not merge dates but override", () => {
const date1 = new Date("2020-01-01");
const date2 = new Date("2020-01-02");
const result = defu({ date: date1 }, { date: date2 });
expect(result).toEqual({ date: date2 });
// Ensure to override, not merge
expect(result).toEqual({ date: date1 });
});

it("should correctly merge different object types", () => {
Expand Down Expand Up @@ -232,4 +234,21 @@ describe("defu", () => {
foo: { bar: { modules: "foo.bar:X,Y" } },
});
});

it("works with asterisk-import", () => {
expect(
defu(asteriskImport, {
a: 2,
exp: {
anotherNested: 2,
},
}),
).toStrictEqual({
a: 2,
exp: {
anotherNested: 2,
nested: 1,
},
});
});
});
1 change: 1 addition & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as exp } from "./nested.js";
3 changes: 3 additions & 0 deletions test/fixtures/nested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
nested: 1,
};