Skip to content

Commit

Permalink
fix #83: Correctly handle changing and shrinking object arrays in diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jul 7, 2017
1 parent 44cfc9f commit 5202a5a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/utils/Omnom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ export class Omnom {
let j = 0;

for (; i < original.length && j < modified.length; i++) {
if (this.almostEqual(original[i], modified[j])) j++;
const equalityDistance = this.almostEqual(original[i], modified[j])
if (equalityDistance == 1) j++;
else if(equalityDistance > 0)
// If we would need to both pull and $set on this array, we have to
// just use the $set operator.
return this.set(changePath, modified);
else pulls.push(original[i]);
}

Expand All @@ -143,7 +148,7 @@ export class Omnom {

// If we have a smaller target array than our source, we will need to re-create it
// regardless (if we want to do so in a single operation anyway)
else return this.set(changePath, modified);
return this.set(changePath, modified);
}

private onLargerArray(original: any[], modified: any[], changePath: string): void {
Expand Down
27 changes: 27 additions & 0 deletions test/Omnom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,32 @@ describe("Omnom",() => {

chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff);
});

it("should handle complex array objects which should be replaced in a smaller array", () => {
let oldObject = { a: [{x: 1, y: "a"}, {x: 2, y: "b"}, {x: 3, y: "c"}, {x: 4, y: "d"}]};
let newObject = { a: [{x: 1, y: "z"}, {x: 2, y: "y"}] };
let expectedDiff = {
$set: {
"a": [{x: 1, y: "z"}, {x: 2, y: "y"}]
}
};

chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff);
});

it("should handle complex array objects which should be replaced in a larger array", () => {
let oldObject = { a: [{x: 1, y: "a"}, {x: 2, y: "b"}]};
let newObject = { a: [{x: 1, y: "z"}, {x: 2, y: "y"}, {x: 3, y: "x"}, {x: 4, y: "w"}] };
let expectedDiff = {
$set: {
"a.0.y": "z",
"a.1.y": "y",
"a.2": {x: 3, y: "x"},
"a.3": {x: 4, y: "w"}
}
};

chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff);
});
});
});
17 changes: 16 additions & 1 deletion test/issues/#83.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Iridium from "../../iridium";
import {Omnom} from "../../lib/utils/Omnom";
import * as chai from "chai";

interface TestDocument {
Expand Down Expand Up @@ -31,7 +32,7 @@ describe("issues", () => {
});
}

describe.only("#83", () => {
describe("#83", () => {
let model = new Iridium.Model<TestDocument, Test>(core, Test);

it("track.save should update track accordingly", () => {
Expand All @@ -44,5 +45,19 @@ describe("issues", () => {
chai.expect(test.stuff).to.eql(build_dataset(20, 14));
});
});

it("should derive the correct change definition", () => {
const diff = Omnom.diff({
stuff: build_dataset(1, 19)
}, {
stuff: build_dataset(20, 14)
});

chai.expect(diff).to.eql({
"$set": {
stuff: build_dataset(20, 14)
}
});
});
});
});

0 comments on commit 5202a5a

Please sign in to comment.