Skip to content

Commit

Permalink
lucaong#261 Dedupe and test .loadJSONAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed May 28, 2024
1 parent 699babb commit 6caa15a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
20 changes: 20 additions & 0 deletions src/MiniSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,26 @@ e forse del mio dir poco ti cale`
})
})

describe('loadJSONAsync', () => {
const documents = [
{ id: 1, title: 'Divina Commedia', text: 'Nel mezzo del cammin di nostra vita', category: 'poetry' },
{ id: 2, title: 'I Promessi Sposi', text: 'Quel ramo del lago di Como', category: 'fiction' },
{ id: 3, title: 'Vita Nova', text: 'In quella parte del libro della mia memoria', category: 'poetry' }
]

it('makes a MiniSearch instance that is identical to .loadJSON()', async () => {
const options = { fields: ['title', 'text'], storeFields: ['category'] }
const ms = new MiniSearch(options)
ms.addAll(documents)
const json = JSON.stringify(ms)

const deserializedAsync = await MiniSearch.loadJSONAsync(json, options)
const deserialized = MiniSearch.loadJSON(json, options)

expect(deserialized).toEqual(deserializedAsync)
})
})

describe('getDefault', () => {
it('returns the default value of the given option', () => {
expect(MiniSearch.getDefault('idField')).toEqual('id')
Expand Down
64 changes: 32 additions & 32 deletions src/MiniSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1515,32 +1515,17 @@ export default class MiniSearch<T = any> {
static loadJS<T = any> (js: AsPlainObject, options: Options<T>): MiniSearch<T> {
const {
index,
documentCount,
nextId,
documentIds,
fieldIds,
fieldLength,
averageFieldLength,
storedFields,
dirtCount,
serializationVersion
} = js
if (serializationVersion !== 1 && serializationVersion !== 2) {
throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version')
}

const miniSearch = new MiniSearch(options)
const miniSearch = this.instantiateMiniSearch(js, options)

miniSearch._documentCount = documentCount
miniSearch._nextId = nextId
miniSearch._documentIds = objectToNumericMap(documentIds)
miniSearch._idToShortId = new Map<any, number>()
miniSearch._fieldIds = fieldIds
miniSearch._fieldLength = objectToNumericMap(fieldLength)
miniSearch._avgFieldLength = averageFieldLength
miniSearch._storedFields = objectToNumericMap(storedFields)
miniSearch._dirtCount = dirtCount || 0
miniSearch._index = new SearchableMap()

for (const [shortId, id] of miniSearch._documentIds) {
miniSearch._idToShortId.set(id, shortId)
Expand Down Expand Up @@ -1572,32 +1557,17 @@ export default class MiniSearch<T = any> {
static async loadJSAsync<T = any> (js: AsPlainObject, options: Options<T>): Promise<MiniSearch<T>> {
const {
index,
documentCount,
nextId,
documentIds,
fieldIds,
fieldLength,
averageFieldLength,
storedFields,
dirtCount,
serializationVersion
} = js
if (serializationVersion !== 1 && serializationVersion !== 2) {
throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version')
}

const miniSearch = new MiniSearch(options)
const miniSearch = this.instantiateMiniSearch(js, options)

miniSearch._documentCount = documentCount
miniSearch._nextId = nextId
miniSearch._documentIds = await objectToNumericMapAsync(documentIds)
miniSearch._idToShortId = new Map<any, number>()
miniSearch._fieldIds = fieldIds
miniSearch._fieldLength = await objectToNumericMapAsync(fieldLength)
miniSearch._avgFieldLength = averageFieldLength
miniSearch._storedFields = await objectToNumericMapAsync(storedFields)
miniSearch._dirtCount = dirtCount || 0
miniSearch._index = new SearchableMap()

for (const [shortId, id] of miniSearch._documentIds) {
miniSearch._idToShortId.set(id, shortId)
Expand Down Expand Up @@ -1625,6 +1595,36 @@ export default class MiniSearch<T = any> {
return miniSearch
}

/**
* @ignore
*/
private static instantiateMiniSearch<T = any> (js: AsPlainObject, options: Options<T>): MiniSearch<T> {
const {
documentCount,
nextId,
fieldIds,
averageFieldLength,
dirtCount,
serializationVersion
} = js

if (serializationVersion !== 1 && serializationVersion !== 2) {
throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version')
}

const miniSearch = new MiniSearch(options)

miniSearch._documentCount = documentCount
miniSearch._nextId = nextId
miniSearch._idToShortId = new Map<any, number>()
miniSearch._fieldIds = fieldIds
miniSearch._avgFieldLength = averageFieldLength
miniSearch._dirtCount = dirtCount || 0
miniSearch._index = new SearchableMap()

return miniSearch
}

/**
* @ignore
*/
Expand Down

0 comments on commit 6caa15a

Please sign in to comment.