deno.land / x / fuse@v6.4.1 / test / indexing.test.js

indexing.test.js
نووسراو ببینە
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const Fuse = require('../dist/fuse')const Books = require('./fixtures/books.json')
const idx = (result) => result.map((obj) => obj.refIndex)const idxMap = (fuse) => fuse.getIndex().records.map((item) => [item.v, item.i])
describe('Searching', () => { const options = { useExtendedSearch: true, includeMatches: true, includeScore: true, threshold: 0.3, keys: ['title', 'author.firstName', 'author.lastName'] }
test('createIndex: ensure properties exist', () => { let myIndex = Fuse.createIndex(options.keys, Books)
expect(myIndex.records).toBeDefined() expect(myIndex.keys).toBeDefined() })
test('createIndex: ensure keys can be created with objects', () => { let myIndex = Fuse.createIndex( [{ name: 'title' }, { name: 'author.firstName' }], Books ) expect(myIndex.records).toBeDefined() expect(myIndex.keys).toBeDefined() })
test('parseIndex: ensure index can be exported and Fuse can be initialized', () => { const myIndex = Fuse.createIndex(options.keys, Books) expect(myIndex.size()).toBe(Books.length)
const data = myIndex.toJSON() expect(data.records).toBeDefined() expect(data.keys).toBeDefined()
const parsedIndex = Fuse.parseIndex(data) expect(parsedIndex.size()).toBe(Books.length)
const fuse = new Fuse(Books, options, parsedIndex) const result = fuse.search({ title: 'old man' }) expect(result.length).toBe(1) expect(idx(result)).toMatchObject([0]) })
test('Fuse can be instantiated with an index', () => { let myIndex = Fuse.createIndex(options.keys, Books) const fuse = new Fuse(Books, options, myIndex)
let result = fuse.search({ title: 'old man' })
expect(result.length).toBe(1) expect(idx(result)).toMatchObject([0]) expect(result[0].matches[0].indices).toMatchObject([ [0, 2], [4, 6] ]) })
test('Throws on invalid index format', () => { expect(() => { new Fuse(Books, options, []) }).toThrow() })
test('Add object to Index', () => { const fuse = new Fuse(Books, options)
fuse.add({ title: 'book', author: { firstName: 'Kiro', lastName: 'Risk' } })
let result = fuse.search('kiro')
expect(result.length).toBe(1) expect(idx(result)).toMatchObject([Books.length - 1]) })
test('Add string to Index', () => { const fruits = ['apple', 'orange'] const fuse = new Fuse(fruits, { includeScore: true })
fuse.add('banana')
let result = fuse.search('banana')
expect(result.length).toBe(1) expect(idx(result)).toMatchObject([2]) })
test('Remove string from the Index', () => { const fruits = ['apple', 'orange', 'banana', 'pear'] const fuse = new Fuse(fruits)
expect(fuse.getIndex().size()).toBe(4) expect(idxMap(fuse)).toMatchObject([ ['apple', 0], ['orange', 1], ['banana', 2], ['pear', 3] ])
fuse.removeAt(1)
expect(fuse.getIndex().size()).toBe(3) expect(idxMap(fuse)).toMatchObject([ ['apple', 0], ['banana', 1], ['pear', 2] ])
const results = fuse.remove((doc) => { return doc === 'banana' || doc === 'pear' })
expect(results.length).toBe(2) expect(fuse.getIndex().size()).toBe(1) expect(fuse._docs.length).toBe(1) })})
fuse

Version Info

Tagged at
3 years ago