From 7ce7a9f5828bc4581b3b71dd4747f8f06f508ae6 Mon Sep 17 00:00:00 2001 From: Brian Huf Date: Tue, 28 Oct 2025 07:05:58 -0700 Subject: [PATCH] support objects with prototypes --- src/index.spec.ts | 29 +++++++++++++++++++++++++++++ src/index.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index 0370039..b112ee9 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -824,6 +824,35 @@ describe('a generate json patch function', () => { expect(patch).to.eql([]); }); }); + + describe('with object prototypes', () => { + it('handles objects', () => { + class MySubObject { + c = ''; + d = 0; + } + + class MyObject { + a = new MySubObject(); + b = ''; + + method() { + return 'hello'; + } + } + + const before = new MyObject(); + const after = new MyObject(); + after.b = 'changed'; + after.a.d = 42; + + const patch = generateJSONPatch(before, after); + expect(patch).to.eql([ + { op: 'replace', path: '/a/d', value: 42 }, + { op: 'replace', path: '/b', value: 'changed' }, + ]); + }); + }); }); function doPatch(json: JsonValue, patch: Patch) { diff --git a/src/index.ts b/src/index.ts index 9cc3859..1c5ea51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -248,7 +248,7 @@ function isPrimitiveValue(value: JsonValue): value is JsonValue { } function isJsonObject(value: JsonValue): value is JsonObject { - return value?.constructor === Object; + return value !== null && typeof value === 'object' && !Array.isArray(value); } function deepEqual(objA: any, objB: any) {