diff --git a/source/server/utils/merge/diff.test.ts b/source/server/utils/merge/diff.test.ts index 6c4603bc..234a5f34 100644 --- a/source/server/utils/merge/diff.test.ts +++ b/source/server/utils/merge/diff.test.ts @@ -83,11 +83,19 @@ describe("merge.diff()", function(){ it("throws when diffing an array with an object", function(){ expect(()=>diff({a:[]}, {a:{}})).to.throw("Can't diff an array with an object"); }); + + it("handles null in arrays", function(){ + expect(diff({a:[0, null, 0]}, {a:[0,0, null]})).to.deep.equal({a:{1:0, 2:null}}); + }); }); describe("objects", function(){ it("deep merge objects", function(){ expect(diff({v: {a: 1 }}, {v: {a:1, b:2}})).to.deep.equal({v:{b:2}}); }); + + it("handles object creation", function(){ + expect(diff({name:"A", articles: null}, {name:"A", articles: {id:"1"}})).to.deep.equal({articles: {id:"1"}}); + }) }); }); diff --git a/source/server/utils/merge/diff.ts b/source/server/utils/merge/diff.ts index a6a2ff8b..c06f5a90 100644 --- a/source/server/utils/merge/diff.ts +++ b/source/server/utils/merge/diff.ts @@ -38,6 +38,12 @@ export default function diff>(from :T, to :T) :Diff continue; } + if(from[key] == null){ + //We already verified that to[key] is not null + r[key] = to[key]; + continue; + } + const d = diff(from[key] as any, to[key] as any); if(Object.keys(d).length){ //console.log("Diffing", key, from[key], to[key]);