Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down