diff --git a/lib/indexes.js b/lib/indexes.js index e1788a2..27f98ef 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -40,7 +40,7 @@ function Index (options) { Index.prototype.insert = function (doc) { var key, self = this; - if (util.isArray(doc)) { doc.forEach(function (d) { self.insert(d); }); } + if (util.isArray(doc)) { doc.forEach(function (d) { self.insert(d); }); return; } key = model.getDotValue(doc, this.fieldName); @@ -59,7 +59,11 @@ Index.prototype.insert = function (doc) { * O(log(n)) */ Index.prototype.remove = function (doc) { - var key = model.getDotValue(doc, this.fieldName); + var key, self = this + + if (util.isArray(doc)) { doc.forEach(function (d) { self.remove(d); }); return; } + + key = model.getDotValue(doc, this.fieldName); if (key === undefined && this.sparse) { this.nonindexedDocs = _.without(this.nonindexedDocs, doc); diff --git a/test/indexes.test.js b/test/indexes.test.js index b5f8195..e59d739 100644 --- a/test/indexes.test.js +++ b/test/indexes.test.js @@ -174,6 +174,22 @@ describe('Indexing', function () { idx.tree.search('world')[0].should.equal(doc4); }); + it('Can remove an array of documents', function () { + var idx = new Index({ fieldName: 'tf' }) + , doc1 = { a: 5, tf: 'hello' } + , doc2 = { a: 8, tf: 'world' } + , doc3 = { a: 2, tf: 'bloup' } + ; + + idx.insert([doc1, doc2, doc3]); + idx.tree.getNumberOfKeys().should.equal(3); + idx.remove([doc1, doc3]); + idx.tree.getNumberOfKeys().should.equal(1); + assert.deepEqual(idx.tree.search('hello'), []); + assert.deepEqual(idx.tree.search('world'), [doc2]); + assert.deepEqual(idx.tree.search('bloup'), []); + }); + }); // ==== End of 'Removal' ==== //