Skip to content

Commit

Permalink
Fix bug when "constructor" is a term
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed Jun 25, 2020
1 parent a25e8bc commit 4d0cb2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/MiniSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,18 @@ const termResults = function (self, term, boosts, boostDocument, indexData, weig
const normalizedLength = self._fieldLength[documentId][fieldId] / self._averageFieldLength[fieldId]
results[documentId] = results[documentId] || { score: 0, match: {}, terms: [] }
results[documentId].terms.push(term)
results[documentId].match[term] = results[documentId].match[term] || []
results[documentId].match[term] = getOwnProperty(results[documentId].match, term) || []
results[documentId].score += docBoost * score(tf, df, self._documentCount, normalizedLength, boost, editDistance)
results[documentId].match[term].push(field)
})
return results
}, {})
}

const getOwnProperty = function (object, property) {
return Object.prototype.hasOwnProperty.call(object, property) ? object[property] : undefined
}

const addFieldLength = function (self, documentId, fieldId, count, length) {
self._averageFieldLength[fieldId] = self._averageFieldLength[fieldId] || 0
const totalLength = (self._averageFieldLength[fieldId] * count) + length
Expand Down
16 changes: 16 additions & 0 deletions src/MiniSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,22 @@ describe('MiniSearch', () => {
expect(processTerm).toHaveBeenCalledWith(term)
})
})

it('does not break when special properties of object are used as a term', () => {
const specialWords = ['constructor', 'hasOwnProperty', 'isPrototypeOf']
const ms = new MiniSearch({ fields: ['text'] })
const processTerm = MiniSearch.getDefault('processTerm')

ms.add({ id: 1, text: specialWords.join(' ') })

specialWords.forEach((word) => {
expect(() => { ms.search(word) }).not.toThrowError()

const results = ms.search(word)
expect(results[0].id).toEqual(1)
expect(results[0].match[processTerm(word)]).toEqual(['text'])
})
})
})
})

Expand Down

0 comments on commit 4d0cb2c

Please sign in to comment.