From 2ced8f74573256cbc58a64741f189aa7eb73d1f3 Mon Sep 17 00:00:00 2001 From: Richard Poole Date: Thu, 2 Apr 2015 17:10:33 +0100 Subject: [PATCH] Fixed Vector.insert when index of new element is less than first element --- lib/vector.js | 5 +++++ test/vector_test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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) +})