From 44862959fac42bb047b652aed921c48a713a33e9 Mon Sep 17 00:00:00 2001 From: Chathura Buddhika Date: Wed, 11 Sep 2024 08:26:44 +0530 Subject: [PATCH] fix: handle the possible errors when object has a property called hasOwnProperty --- src/merge.ts | 2 +- test/merge.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/merge.ts b/src/merge.ts index b15ea88..bf532e6 100644 --- a/src/merge.ts +++ b/src/merge.ts @@ -2,7 +2,7 @@ export const MergeJson = (target: Record, source: Record> = { ...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]; diff --git a/test/merge.test.ts b/test/merge.test.ts index 720a1c0..cbfa3c3 100644 --- a/test/merge.test.ts +++ b/test/merge.test.ts @@ -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 }); + }); });