From 47c45160602e8d443622f36401654ac2480e01a7 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 22 May 2015 14:46:20 +0200 Subject: [PATCH] Index & UniqueIndex: Correctly updates even if the hash has not changed. --- packages/xo-collection/src/index.js | 3 --- packages/xo-collection/src/index.spec.js | 25 +++++++++++++++++++ packages/xo-collection/src/unique-index.js | 3 --- .../xo-collection/src/unique-index.spec.js | 20 +++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/xo-collection/src/index.js b/packages/xo-collection/src/index.js index 5b507da33..76c1af5d7 100644 --- a/packages/xo-collection/src/index.js +++ b/packages/xo-collection/src/index.js @@ -109,9 +109,6 @@ export default class Index { const prev = keysToHash[key] const hash = computeHash(value, key) - // Same hash, nothing to do. - if (hash === prev) continue - // Removes item from the previous hash's list if any. if (prev != null) delete itemsByHash[prev][key] diff --git a/packages/xo-collection/src/index.spec.js b/packages/xo-collection/src/index.spec.js index 2587f4a19..715025102 100644 --- a/packages/xo-collection/src/index.spec.js +++ b/packages/xo-collection/src/index.spec.js @@ -7,6 +7,7 @@ chai.use(dirtyChai) import sourceMapSupport from 'source-map-support' sourceMapSupport.install() +import eventToPromise from 'event-to-promise' import forEach from 'lodash.foreach' // ------------------------------------------------------------------- @@ -143,6 +144,30 @@ describe('Index', function () { }) }) + it('correctly updates the value even the same object has the same hash', function () { + const item1bis = { + id: item1.id, + group: item1.group, + newProp: true + } + + col.update(item1bis) + + return eventToPromise(col, 'finish').then(() => { + expect(col.indexes).to.eql({ + byGroup: { + foo: { + [item1.id]: item1bis, + [item3.id]: item3 + }, + bar: { + [item2.id]: item2 + } + } + }) + }) + }) + describe('#sweep()', function () { it('removes empty items lists', function () { col.remove(item2) diff --git a/packages/xo-collection/src/unique-index.js b/packages/xo-collection/src/unique-index.js index eb70ef71c..6153bbe61 100644 --- a/packages/xo-collection/src/unique-index.js +++ b/packages/xo-collection/src/unique-index.js @@ -90,9 +90,6 @@ export default class UniqueIndex { const prev = keysToHash[key] const hash = computeHash(value, key) - // Same hash, nothing to do. - if (hash === prev) continue - // Removes item from the previous hash's list if any. if (prev != null) delete itemByHash[prev] diff --git a/packages/xo-collection/src/unique-index.spec.js b/packages/xo-collection/src/unique-index.spec.js index a4000ff60..c4fb09ec6 100644 --- a/packages/xo-collection/src/unique-index.spec.js +++ b/packages/xo-collection/src/unique-index.spec.js @@ -7,6 +7,7 @@ chai.use(dirtyChai) import sourceMapSupport from 'source-map-support' sourceMapSupport.install() +import eventToPromise from 'event-to-promise' import forEach from 'lodash.foreach' // ------------------------------------------------------------------- @@ -117,4 +118,23 @@ describe('UniqueIndex', function () { }) }) }) + + it('correctly updates the value even the same object has the same hash', function () { + const item1bis = { + id: item1.id, + key: item1.key, + newProp: true + } + + col.update(item1bis) + + return eventToPromise(col, 'finish').then(() => { + expect(col.indexes).to.eql({ + byKey: { + [item1.key]: item1bis, + [item2.key]: item2 + } + }) + }) + }) })