diff --git a/lib/vector.js b/lib/vector.js index 0351a531..1a98aabe 100644 --- a/lib/vector.js +++ b/lib/vector.js @@ -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 diff --git a/test/vector_test.js b/test/vector_test.js index f9375b27..95d182e7 100644 --- a/test/vector_test.js +++ b/test/vector_test.js @@ -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) +})