Can insert and remove array of docs

pull/2/head
Louis Chatriot 12 years ago
parent af0e9fcccb
commit 775b7c0b78
  1. 8
      lib/indexes.js
  2. 16
      test/indexes.test.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);

@ -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' ==== //

Loading…
Cancel
Save