Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/search/bitap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export default class BitapSearch {

// Exact match
if (this.pattern === text) {
// Respect minMatchCharLength: the whole string is the matched region, so
// its length must meet the minimum. The non-exact bitap path enforces
// this via convertMaskToIndices; the shortcut must do the same.
if (text.length < this.options.minMatchCharLength) {
return { isMatch: false, score: 1 }
}

const result: SearchResult = {
isMatch: true,
score: 0
Expand Down
22 changes: 22 additions & 0 deletions test/match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@
const result = Fuse.match('apple', 'apple pie', { useTokenSearch: false })
expect(result.isMatch).toBe(true)
})

test('exact match returns isMatch: false when pattern is shorter than minMatchCharLength', () => {
// The exact-match shortcut (pattern === text) used to bypass the
// minMatchCharLength constraint, returning isMatch: true even though the
// matched region was shorter than the required minimum.
const result = Fuse.match('abc', 'abc', { minMatchCharLength: 5 })
expect(result.isMatch).toBe(false)

Check failure on line 91 in test/match.test.js

View workflow job for this annotation

GitHub Actions / build (20)

test/match.test.js > Fuse.match > exact match returns isMatch: false when pattern is shorter than minMatchCharLength

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ test/match.test.js:91:28
})

test('exact match still succeeds when pattern length meets minMatchCharLength', () => {
const result = Fuse.match('hello', 'hello', { minMatchCharLength: 5 })
expect(result.isMatch).toBe(true)
expect(result.score).toBe(0)
})

test('corpus search: exact-match item excluded when shorter than minMatchCharLength', () => {
const fuse = new Fuse(['abc', 'abcde', 'abcdefgh'], { minMatchCharLength: 5 })
const results = fuse.search('abc')
// 'abc' (3 chars) must not appear; bitap non-exact matches inside longer
// strings are correctly filtered by convertMaskToIndices already.
expect(results.map((r) => r.item)).not.toContain('abc')

Check failure on line 105 in test/match.test.js

View workflow job for this annotation

GitHub Actions / build (20)

test/match.test.js > Fuse.match > corpus search: exact-match item excluded when shorter than minMatchCharLength

AssertionError: expected [ 'abc' ] to not include 'abc' ❯ test/match.test.js:105:44
})
})
Loading