Skip to content

Commit

Permalink
optimize PR #35, redo TypeScript; write test. Performance not visibly…
Browse files Browse the repository at this point in the history
… affected because of the optimization
  • Loading branch information
warpech committed Jan 5, 2015
1 parent 1ea71b9 commit c404e06
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/json-patch-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ var jsonpatch;
var oldVal = mirror[key];
if (obj.hasOwnProperty(key)) {
var newVal = obj[key];
if (oldVal instanceof Object && newVal instanceof Object) {
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
_generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key));
} else {
if (oldVal != newVal) {
Expand Down
2 changes: 1 addition & 1 deletion src/json-patch-duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ module jsonpatch {
var oldVal = mirror[key];
if (obj.hasOwnProperty(key)) {
var newVal = obj[key];
if (oldVal instanceof Object) {
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
_generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key));
}
else {
Expand Down
18 changes: 18 additions & 0 deletions test/spec/duplexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ describe("duplex", function () {
{op: "replace", path: "/user/lastName", value: "Collins"}
]);
});

it('should replace null with object', function () {
var objA = {user: null};
var objB = {user: {}};

expect(jsonpatch.compare(objA, objB)).toEqual([
{op: "replace", path: "/user", value: {}}
]);
});

it('should replace object with null', function () {
var objA = {user: {}};
var objB = {user: null};

expect(jsonpatch.compare(objA, objB)).toEqual([
{op: "replace", path: "/user", value: null}
]);
});
});


Expand Down

0 comments on commit c404e06

Please sign in to comment.