From 694fd982fca79365e8f46e5f15ee6effe47c75ef Mon Sep 17 00:00:00 2001 From: Brian Broll Date: Fri, 21 Aug 2020 09:01:41 -0500 Subject: [PATCH 1/2] Reverse sort array keys in changeset. Closes #10 --- index.js | 4 ++-- test/index.js | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ce55325..ae8bf6e 100644 --- a/index.js +++ b/index.js @@ -26,8 +26,8 @@ function compare(path, old, new_) { !_.contains(comparing, old)) { comparing.push(old); - var oldKeys = Object.keys(old); - var newKeys = Object.keys(new_); + var oldKeys = Object.keys(old).reverse(); + var newKeys = Object.keys(new_).reverse(); var sameKeys = _.intersection(oldKeys, newKeys); sameKeys.forEach(function (k) { diff --git a/test/index.js b/test/index.js index df6dc9e..20a2c49 100644 --- a/test/index.js +++ b/test/index.js @@ -40,18 +40,18 @@ describe('changeset', function () { var changes = diff(a, b); expect(changes).to.deep.equal([ - { type: 'put', key: ['name'], value: 'Susan' }, - { type: 'put', key: ['number'], value: 43 }, - { type: 'put', key: ['tags', '1'], value: 'tag4' }, - { type: 'del', key: ['tags', '2'] }, + { type: 'put', key: ['self'], value: b }, { type: 'put', key: [ 'scores', 'someArray', '1' ], value: 'three' }, { type: 'del', key: [ 'scores', 'someArray', '2' ] }, { type: 'del', key: ['scores', 'tetris'] }, { type: 'put', key: ['scores', 'zelda'], value: 3000 }, - { type: 'put', key: ['self'], value: b }, + { type: 'put', key: ['tags', '1'], value: 'tag4' }, + { type: 'del', key: ['tags', '2'] }, + { type: 'put', key: ['number'], value: 43 }, + { type: 'put', key: ['name'], value: 'Susan' }, { type: 'del', key: ['scoresAgain'], }, + { type: 'put', key: ['friend'], value: a }, { type: 'put', key: ['age'], value: 37 }, - { type: 'put', key: ['friend'], value: a } ]); done(); @@ -243,4 +243,18 @@ describe('changeset', function () { ]); done(); }); + + it('should sort array deletions from end', function() { + const a = ['a', 'b', 'c', 'd', 'e']; + const b = ['f', 'g']; + const changes = diff(a, b); + const delKeys = changes + .filter(change => change.type === 'del') + .map(change => change.key[0]); + + delKeys.reduce((prev, next) => { + expect(+prev).to.be.above(+next); + return next; + }); + }); }); From 625eda61cd13783d6cc412f16570e046d3e7fa98 Mon Sep 17 00:00:00 2001 From: Brian Broll Date: Fri, 21 Aug 2020 11:28:59 -0500 Subject: [PATCH 2/2] Only reverse del changes --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ae8bf6e..27ea3e9 100644 --- a/index.js +++ b/index.js @@ -26,8 +26,8 @@ function compare(path, old, new_) { !_.contains(comparing, old)) { comparing.push(old); - var oldKeys = Object.keys(old).reverse(); - var newKeys = Object.keys(new_).reverse(); + var oldKeys = Object.keys(old); + var newKeys = Object.keys(new_); var sameKeys = _.intersection(oldKeys, newKeys); sameKeys.forEach(function (k) { @@ -36,7 +36,7 @@ function compare(path, old, new_) { }); var delKeys = _.difference(oldKeys, newKeys); - delKeys.forEach(function (k) { + delKeys.reverse().forEach(function (k) { changes.push({ type: 'del', key: path.concat(k) }); });