forked from krisk/Fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.test.js
More file actions
107 lines (91 loc) · 4.04 KB
/
Copy pathmatch.test.js
File metadata and controls
107 lines (91 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Fuse from '../dist/fuse.mjs'
import FuseBasic from '../dist/fuse.basic.mjs'
import * as ErrorMsg from '../src/core/errorMessages'
describe('Fuse.match', () => {
test('returns a match for a fuzzy match', () => {
const result = Fuse.match('apple', 'Paul likes apples')
expect(result.isMatch).toBe(true)
expect(result.score).toBeGreaterThan(0)
expect(result.score).toBeLessThan(1)
})
test('returns a perfect score for an exact match', () => {
const result = Fuse.match('apple', 'apple')
expect(result.isMatch).toBe(true)
expect(result.score).toBe(0)
})
test('returns no match when strings are unrelated', () => {
const result = Fuse.match('xyz', 'apple')
expect(result.isMatch).toBe(false)
expect(result.score).toBe(1)
})
test('includes indices when includeMatches is true', () => {
const result = Fuse.match('apple', 'apple pie', { includeMatches: true })
expect(result.isMatch).toBe(true)
expect(result.indices).toBeDefined()
expect(result.indices.length).toBeGreaterThan(0)
})
test('does not include indices by default', () => {
const result = Fuse.match('apple', 'apple pie')
expect(result.indices).toBeUndefined()
})
test('respects isCaseSensitive option', () => {
const insensitive = Fuse.match('APPLE', 'apple')
expect(insensitive.isMatch).toBe(true)
const sensitive = Fuse.match('APPLE', 'apple', { isCaseSensitive: true })
expect(sensitive.isMatch).toBe(false)
})
test('respects threshold option', () => {
const loose = Fuse.match('aple', 'apple', { threshold: 0.6 })
expect(loose.isMatch).toBe(true)
const strict = Fuse.match('aple', 'apple', { threshold: 0 })
expect(strict.isMatch).toBe(false)
})
test('respects minMatchCharLength option', () => {
const result = Fuse.match('app', 'apple', {
includeMatches: true,
minMatchCharLength: 3
})
expect(result.isMatch).toBe(true)
expect(result.indices).toBeDefined()
result.indices.forEach(([start, end]) => {
expect(end - start + 1).toBeGreaterThanOrEqual(3)
})
})
// Token search needs corpus-level statistics (df, fieldCount) that a one-off
// string comparison can't provide. Both builds must reject it explicitly —
// the full build used to crash with an opaque TypeError, the basic build
// used to silently fall back to plain fuzzy matching.
test('throws when useTokenSearch is true (full build)', () => {
expect(() =>
Fuse.match('apple', 'apple pie', { useTokenSearch: true })
).toThrowError(ErrorMsg.FUSE_MATCH_TOKEN_SEARCH_UNSUPPORTED)
})
test('throws when useTokenSearch is true (basic build)', () => {
expect(() =>
FuseBasic.match('apple', 'apple pie', { useTokenSearch: true })
).toThrowError(ErrorMsg.FUSE_MATCH_TOKEN_SEARCH_UNSUPPORTED)
})
test('still works when useTokenSearch is explicitly false', () => {
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)
})
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')
})
})