From 45021637d64a36a389df5adb66c29faffd4a07c5 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sat, 4 May 2024 17:13:51 +0800 Subject: [PATCH] fix: do not return partial matches for threshold 0 --- src/search/bitap/search.js | 2 +- test/fuzzy-search.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/search/bitap/search.js b/src/search/bitap/search.js index 03041af75..bf0a9548a 100644 --- a/src/search/bitap/search.js +++ b/src/search/bitap/search.js @@ -112,7 +112,7 @@ export default function search( let currentLocation = j - 1 let charMatch = patternAlphabet[text.charAt(currentLocation)] - if (computeMatches) { + if (computeMatches && threshold !== 0) { // Speed up: quick bool to int conversion (i.e, `charMatch ? 1 : 0`) matchMask[currentLocation] = +!!charMatch } diff --git a/test/fuzzy-search.test.js b/test/fuzzy-search.test.js index a35526ee7..92c4d2f7c 100644 --- a/test/fuzzy-search.test.js +++ b/test/fuzzy-search.test.js @@ -840,6 +840,34 @@ describe('Searching with minCharLength', () => { }) }) +describe('Searching with threshold 0', () => { + const customList = ['t te tes test tes te t'] + let fuse + + beforeEach( + () => + (fuse = new Fuse(customList, { + includeMatches: true, + threshold: 0, + ignoreLocation: true, + })) + ) + + describe('When searching for the term "test"', () => { + let result + beforeEach(() => (result = fuse.search('test'))) + + test('We get a match containing 1 indices', () => { + expect(result[0].matches[0].indices).toHaveLength(1) + }) + + test('and the matching index is a exact match', () => { + expect(result[0].matches[0].indices[0][0]).toBe(9) + expect(result[0].matches[0].indices[0][1]).toBe(12) + }) + }) +}) + describe('Searching with minCharLength and pattern larger than machine word size', () => { const customList = [ 'Apple pie is a tasty treat that is always best made by mom! But we love store bought too.',