Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions inverted-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ InvertedIndex.prototype.get = function(query) {
InvertedIndex.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;

for (var i = 0, l = this.documents.length; i < l; i++)
callback.call(scope, this.documents[i], i, this);
for (var i = 0, l = this.items.length; i < l; i++)
callback.call(scope, this.items[i], i, this);
};

/**
Expand Down
37 changes: 36 additions & 1 deletion test/inverted-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,47 @@ describe('InvertedIndex', function() {
});

it('should be possible to iterate using #.forEach', function() {
var index = InvertedIndex.from(DOCS, tokenizer);
var index = InvertedIndex.from(DOCS, tokenizer),
visited = [];

index.forEach(function(doc, i, instance) {
assert.strictEqual(this, index);
assert.strictEqual(instance, index);
assert.strictEqual(i, visited.length);
assert.strictEqual(doc, DOCS[i]);
visited.push(doc);
});

assert.deepStrictEqual(visited, DOCS);
});

it('should be possible to provide a scope to #.forEach.', function() {
var index = InvertedIndex.from(OBJECT_DOCS, documentTokenizer),
scope = {documents: []};

index.forEach(function(doc, i, instance) {
assert.strictEqual(this, scope);
assert.strictEqual(instance, index);
assert.strictEqual(i, this.documents.length);
assert.strictEqual(doc, OBJECT_DOCS[i]);
this.documents.push(doc);
}, scope);

assert.deepStrictEqual(scope.documents, OBJECT_DOCS);
});

it('should not call the #.forEach callback on an empty index.', function() {
var index = new InvertedIndex(tokenizer);

function fail() {
assert.fail('An empty index should not call the callback.');
}

index.forEach(fail);

index.add(DOCS[0]);
index.clear();
index.forEach(fail);
});

it('should be possible to create an iterator over documents.', function() {
Expand Down