Skip to content

Commit

Permalink
feat(core): add alter option on deepMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 6, 2024
1 parent c4dc273 commit 35a5ff7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/core/src/utils/objects/deepMerge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,26 @@ describe("deepMerge", () => {
});
});
it("should merge data (4)", () => {
const alter = vi.fn().mockImplementation((key, value) => {
return value;
});
expect(
deepMerge(
{
prop: {}
},
{
prop: ""
},
{
alter
}
)
).toEqual({
prop: {}
});

expect(alter).toHaveBeenCalledWith("prop", {});
});
it("should merge data and prevent prototype pollution", () => {
const obj = JSON.parse('{"__proto__": {"a": "vulnerable"}, "security": [{"1": "o"}, {"2": "o1"}]}');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/objects/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface DeepMergeOptions {
reducers?: Record<string, DeepMergeReducerCB>;
parentKey?: string;
cleanUndefinedProps?: boolean;
alter?: (key: string, value: any) => any;
}

export function mergeReducerBuilder(cb: DeepMergeComparatorCB) {
Expand Down Expand Up @@ -77,7 +78,7 @@ export function deepMerge<T = any, C = any>(source: T & any, obj: C & any, optio
return out;
}

out[key] = value;
out[key] = options.alter?.(key, value) ?? value;

return out;
}, newObj);
Expand Down

0 comments on commit 35a5ff7

Please sign in to comment.