Skip to content

Commit 7ce7a9f

Browse files
author
Brian Huf
committed
support objects with prototypes
1 parent 507fe16 commit 7ce7a9f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/index.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,35 @@ describe('a generate json patch function', () => {
824824
expect(patch).to.eql([]);
825825
});
826826
});
827+
828+
describe('with object prototypes', () => {
829+
it('handles objects', () => {
830+
class MySubObject {
831+
c = '';
832+
d = 0;
833+
}
834+
835+
class MyObject {
836+
a = new MySubObject();
837+
b = '';
838+
839+
method() {
840+
return 'hello';
841+
}
842+
}
843+
844+
const before = new MyObject();
845+
const after = new MyObject();
846+
after.b = 'changed';
847+
after.a.d = 42;
848+
849+
const patch = generateJSONPatch(before, after);
850+
expect(patch).to.eql([
851+
{ op: 'replace', path: '/a/d', value: 42 },
852+
{ op: 'replace', path: '/b', value: 'changed' },
853+
]);
854+
});
855+
});
827856
});
828857

829858
function doPatch(json: JsonValue, patch: Patch) {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function isPrimitiveValue(value: JsonValue): value is JsonValue {
248248
}
249249

250250
function isJsonObject(value: JsonValue): value is JsonObject {
251-
return value?.constructor === Object;
251+
return value !== null && typeof value === 'object' && !Array.isArray(value);
252252
}
253253

254254
function deepEqual(objA: any, objB: any) {

0 commit comments

Comments
 (0)