diff --git a/src/MiniSearch.test.js b/src/MiniSearch.test.js index 5043cc08..e7febaf7 100644 --- a/src/MiniSearch.test.js +++ b/src/MiniSearch.test.js @@ -1712,6 +1712,22 @@ e forse del mio dir poco ti cale` expect(original).toEqual(expected) }) + + it('allows subclassing and changing .loadJS', () => { + class Modified extends MiniSearch { + static loadJS(js, options) { + return super.loadJS({ ...js, documentCount: 99 }, options) + } + } + + const options = { fields: ['title', 'text'], storeFields: ['category'] } + const ms = new MiniSearch(options) + ms.addAll(documents) + + const json = JSON.stringify(ms) + const deserialized = Modified.loadJSON(json, options) + expect(deserialized.documentCount).toEqual(99) + }) }) describe('getDefault', () => { diff --git a/src/MiniSearch.ts b/src/MiniSearch.ts index 21bc15ff..8619e6d4 100644 --- a/src/MiniSearch.ts +++ b/src/MiniSearch.ts @@ -1314,7 +1314,7 @@ export default class MiniSearch { if (options == null) { throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index') } - return MiniSearch.loadJS(JSON.parse(json), options) + return this.loadJS(JSON.parse(json), options) } /**