Skip to content

Commit

Permalink
replace deepmerge-ts with defu
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Jul 31, 2024
1 parent 4305f07 commit c28f30b
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 373 deletions.
2 changes: 1 addition & 1 deletion code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
"@types/express": "^4.17.21",
"@types/node": "^18.0.0",
"browser-assert": "^1.2.1",
"deepmerge-ts": "^7.0.3",
"defu": "^6.1.4",
"esbuild": "^0.18.0 || ^0.19.0 || ^0.20.0 || ^0.21.0 || ^0.22.0 || ^0.23.0",
"esbuild-register": "^3.5.0",
"express": "^4.19.2",
Expand Down
15 changes: 15 additions & 0 deletions code/core/src/manager-api/lib/merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test, describe } from "vitest";

import merge from "./merge";

describe(merge.name, () => {
test("applies entries not in the source", ({ expect }) => {
const merged = merge({ a: 1 }, { a: 2, b: 3, c: 4 });
console.log({ merged });
expect(merged).toStrictEqual({
a: 1,
b: 3,
c: 4
});
});
});
15 changes: 11 additions & 4 deletions code/core/src/manager-api/lib/merge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { deepmerge } from "deepmerge-ts";
import { defu, defuArrayFn } from 'defu';

import { logger } from '@storybook/core/client-logger';

export default <TObj = any>(a: TObj, b: Partial<TObj>) => {
if (Array.isArray(a) && !Array.isArray(b)) {
export default <TObj = any>(a: TObj, b: Partial<TObj> & any) => {
if (Array.isArray(a)) {
if (Array.isArray(b)) {
return defuArrayFn(a, b);
}

logger.log(['the types mismatch, picking', a]);
return a;
}

return deepmerge(a, b);
// TODO: Ask for what is prefered:
// 1. type guard (safe at runtime, but potential performance slow-down)
// 2. or type assertion (unsafe at runtime)
return defu(a as object, b);
};
Loading

0 comments on commit c28f30b

Please sign in to comment.