Skip to content

Commit

Permalink
Allow subclassing MiniSearch and change loadJS
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed Nov 23, 2022
1 parent fcb5a62 commit c09ed78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/MiniSearch.test.js
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/MiniSearch.ts
Expand Up @@ -1314,7 +1314,7 @@ export default class MiniSearch<T = any> {
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)
}

/**
Expand Down

0 comments on commit c09ed78

Please sign in to comment.