Skip to content

Commit

Permalink
Fixed Vector.insert when index of new element is less than first element
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpoole committed Apr 2, 2015
1 parent 95b1cf2 commit 2ced8f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ lunr.Vector.prototype.insert = function (idx, val) {
return this.length++
}

if (idx < list.idx) {
this.list = new lunr.Vector.Node (idx, val, list)
return this.length++
}

var prev = list,
next = list.next

Expand Down
12 changes: 12 additions & 0 deletions test/vector_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ test("calculating the similarity between two vectors", function () {
equal(roundedSimilarity, 0.111)
})

test("inserted elements are kept in index order", function () {
var vector = new lunr.Vector,
elements = [6,5,4]

vector.insert(2, 4)
vector.insert(1, 5)
vector.insert(0, 6)

equal(vector.list.idx, 0)
equal(vector.list.next.idx, 1)
equal(vector.list.next.next.idx, 2)
})

0 comments on commit 2ced8f7

Please sign in to comment.