Compare diffs with user.id, not user_id

This commit is contained in:
James Allen 2014-03-12 14:04:33 +00:00
parent 575afae048
commit 9977a418c3
2 changed files with 21 additions and 18 deletions

View file

@ -42,14 +42,17 @@ module.exports = DiffGenerator =
newDiff = [] newDiff = []
for part in diff for part in diff
lastPart = newDiff[newDiff.length - 1] lastPart = newDiff[newDiff.length - 1]
if lastPart? and lastPart.i? and part.i? and lastPart.meta.user_id == part.meta.user_id if lastPart? and lastPart.meta?.user? and part.meta?.user?
lastPart.i += part.i if lastPart.i? and part.i? and lastPart.meta.user.id == part.meta.user.id
lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts) lastPart.i += part.i
lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts) lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
else if lastPart? and lastPart.d? and part.d? and lastPart.meta.user_id == part.meta.user_id lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
lastPart.d += part.d else if lastPart.d? and part.d? and lastPart.meta.user.id == part.meta.user.id
lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts) lastPart.d += part.d
lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts) lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
else
newDiff.push part
else else
newDiff.push part newDiff.push part
return newDiff return newDiff

View file

@ -91,18 +91,18 @@ describe "DiffGenerator", ->
describe "with adjacent inserts with the same user_id", -> describe "with adjacent inserts with the same user_id", ->
it "should create one update with combined meta data and min/max timestamps", -> it "should create one update with combined meta data and min/max timestamps", ->
diff = @DiffGenerator.compressDiff([ diff = @DiffGenerator.compressDiff([
{ i: "foo", meta: { start_ts: 10, end_ts: 20, user_id: @user_id }} { i: "foo", meta: { start_ts: 10, end_ts: 20, user: { id: @user_id } }}
{ i: "bar", meta: { start_ts: 5, end_ts: 15, user_id: @user_id }} { i: "bar", meta: { start_ts: 5, end_ts: 15, user: { id: @user_id } }}
]) ])
expect(diff).to.deep.equal([ expect(diff).to.deep.equal([
{ i: "foobar", meta: { start_ts: 5, end_ts: 20, user_id: @user_id }} { i: "foobar", meta: { start_ts: 5, end_ts: 20, user: { id: @user_id } }}
]) ])
describe "with adjacent inserts with different user_ids", -> describe "with adjacent inserts with different user_ids", ->
it "should leave the inserts unchanged", -> it "should leave the inserts unchanged", ->
input = [ input = [
{ i: "foo", meta: { start_ts: 10, end_ts: 20, user_id: @user_id }} { i: "foo", meta: { start_ts: 10, end_ts: 20, user: { id: @user_id } }}
{ i: "bar", meta: { start_ts: 5, end_ts: 15, user_id: @user_id_2 }} { i: "bar", meta: { start_ts: 5, end_ts: 15, user: { id: @user_id_2 } }}
] ]
output = @DiffGenerator.compressDiff(input) output = @DiffGenerator.compressDiff(input)
expect(output).to.deep.equal(input) expect(output).to.deep.equal(input)
@ -110,18 +110,18 @@ describe "DiffGenerator", ->
describe "with adjacent deletes with the same user_id", -> describe "with adjacent deletes with the same user_id", ->
it "should create one update with combined meta data and min/max timestamps", -> it "should create one update with combined meta data and min/max timestamps", ->
diff = @DiffGenerator.compressDiff([ diff = @DiffGenerator.compressDiff([
{ d: "foo", meta: { start_ts: 10, end_ts: 20, user_id: @user_id }} { d: "foo", meta: { start_ts: 10, end_ts: 20, user: { id: @user_id } }}
{ d: "bar", meta: { start_ts: 5, end_ts: 15, user_id: @user_id }} { d: "bar", meta: { start_ts: 5, end_ts: 15, user: { id: @user_id } }}
]) ])
expect(diff).to.deep.equal([ expect(diff).to.deep.equal([
{ d: "foobar", meta: { start_ts: 5, end_ts: 20, user_id: @user_id }} { d: "foobar", meta: { start_ts: 5, end_ts: 20, user: { id: @user_id } }}
]) ])
describe "with adjacent deletes with different user_ids", -> describe "with adjacent deletes with different user_ids", ->
it "should leave the deletes unchanged", -> it "should leave the deletes unchanged", ->
input = [ input = [
{ d: "foo", meta: { start_ts: 10, end_ts: 20, user_id: @user_id }} { d: "foo", meta: { start_ts: 10, end_ts: 20, user: { id: @user_id } }}
{ d: "bar", meta: { start_ts: 5, end_ts: 15, user_id: @user_id_2 }} { d: "bar", meta: { start_ts: 5, end_ts: 15, user: { id: @user_id_2 } }}
] ]
output = @DiffGenerator.compressDiff(input) output = @DiffGenerator.compressDiff(input)
expect(output).to.deep.equal(input) expect(output).to.deep.equal(input)