From 51768606cae73339c872b8536a53e96735a04ca6 Mon Sep 17 00:00:00 2001 From: Kyle Kirby Date: Wed, 12 Nov 2014 15:59:57 -0600 Subject: [PATCH] Fixed #115 Updated lunr.Store to call toJSON on lunr.SortedSet. --- lib/document_store.js | 13 +++++++++++-- test/serialisation_test.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/document_store.js b/lib/document_store.js index 46fbc9c8..7a31ad64 100644 --- a/lib/document_store.js +++ b/lib/document_store.js @@ -88,9 +88,18 @@ lunr.Store.prototype.remove = function (id) { * @memberOf Store */ lunr.Store.prototype.toJSON = function () { + var store = {}; + for(var key in this.store){ + if(!this.store.hasOwnProperty(key))continue; + if(typeof this.store[key].toJSON == 'function'){ + store[key] = this.store[key].toJSON(); + } + else { + store[key] = this.store[key]; + } + } return { - store: this.store, + store: store, length: this.length } } - diff --git a/test/serialisation_test.js b/test/serialisation_test.js index 333fb248..00b4b726 100644 --- a/test/serialisation_test.js +++ b/test/serialisation_test.js @@ -30,6 +30,20 @@ test('dumping and loading an index', function () { deepEqual(idx.search('green plant'), clonedIdx.search('green plant')) }) +test('dumping and loading an index without stringify', function () { + var idx = new lunr.Index + + idx.field('title', { boost: 10 }) + idx.field('body') + + this.corpus.forEach(function (doc) { idx.add(doc) }) + + var dumpedIdx = idx.toJSON(), + clonedIdx = lunr.Index.load(dumpedIdx) + + deepEqual(idx.search('green plant'), clonedIdx.search('green plant')) +}) + test('dumping and loading an index with a populated pipeline', function () { var idx = lunr(function () { this.field('title', { boost: 10 })