Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Vector.insert when index of new element is less than first element #143

Merged
merged 1 commit into from
Apr 9, 2015
Merged
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
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)
})