Skip to content

Commit

Permalink
fix: merge objects with Module type (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
TheAlexLichter and pi0 authored Jan 5, 2024
1 parent 7c7a9a4 commit 1b9fcab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export function isPlainObject(value: unknown): boolean {
return false;
}

if (Symbol.toStringTag in value || Symbol.iterator in value) {
if (Symbol.iterator in value) {
return false;
}

if (Symbol.toStringTag in value) {
return Object.prototype.toString.call(value) === "[object Module]";
}

return true;
}
18 changes: 18 additions & 0 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 @@ -232,4 +233,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,
};

0 comments on commit 1b9fcab

Please sign in to comment.