From 1b9fcab2c1479f0295a5f867c6ec36a01fda2dfb Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Fri, 5 Jan 2024 19:42:41 +0100 Subject: [PATCH] fix: merge objects with `Module` type (#121) Co-authored-by: Pooya Parsa --- src/_utils.ts | 6 +++++- test/defu.test.ts | 18 ++++++++++++++++++ test/fixtures/index.ts | 1 + test/fixtures/nested.ts | 3 +++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/index.ts create mode 100644 test/fixtures/nested.ts diff --git a/src/_utils.ts b/src/_utils.ts index fd9f2f3..41e29b9 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -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; } diff --git a/test/defu.test.ts b/test/defu.test.ts index 19d490d..c2f1600 100644 --- a/test/defu.test.ts +++ b/test/defu.test.ts @@ -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]; @@ -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, + }, + }); + }); }); diff --git a/test/fixtures/index.ts b/test/fixtures/index.ts new file mode 100644 index 0000000..9a76003 --- /dev/null +++ b/test/fixtures/index.ts @@ -0,0 +1 @@ +export { default as exp } from "./nested.js"; diff --git a/test/fixtures/nested.ts b/test/fixtures/nested.ts new file mode 100644 index 0000000..2de8635 --- /dev/null +++ b/test/fixtures/nested.ts @@ -0,0 +1,3 @@ +export default { + nested: 1, +};