Skip to content

Commit

Permalink
fix: handle the possible errors when object has a property called has…
Browse files Browse the repository at this point in the history
…OwnProperty
  • Loading branch information
chathurabuddi committed Sep 11, 2024
1 parent a6e70bc commit 2c29ece
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const MergeJson = (target: Record<string, any>, source: Record<string, an
const result: Partial<Record<string, any>> = { ...target };

for (const key in source) {
if (source.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
const targetValue = result[key];
const sourceValue = source[key];

Expand Down
7 changes: 7 additions & 0 deletions test/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@ describe('MergeJson', () => {
const result = MergeJson(target, source);
expect(result).toEqual({ a: { x: 2 } });
});

test('should handle objects with a property named hasOwnProperty', () => {
const target = { a: 1, hasOwnProperty: 2 };
const source = { b: 3, hasOwnProperty: 4 };
const result = MergeJson(target, source);
expect(result).toEqual({ a: 1, b: 3, hasOwnProperty: 4 });
});
});

0 comments on commit 2c29ece

Please sign in to comment.