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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
import Quickshell
import QtQuick
import "../utils/fzf.js" as Fzf
import "../utils/fuzzysort.js" as Fuzzy
import "../utils/levendist.js" as Levendist
Singleton {
required property var list
property string key: "name"
enum SearchAlgorithm {
Levendist,
Fzf,
Fuzzysort,
Include
}
property int algorithm: Searchable.SearchAlgorithm.Levendist
property int scoreThreshold: 50
property var extraOpts: ({})
property list<string> keys: [key]
property list<real> weights: [1]
property var queryCache: ({})
readonly property var fzf: algorithm === Searchable.SearchAlgorithm.Fzf ? new Fzf.Finder(list, Object.assign({
selector
}, extraOpts)) : null
readonly property list<var> fzfFinders: algorithm === Searchable.SearchAlgorithm.Fzf ? keys.map(k => {
return new Fzf.Finder(list, Object.assign({
selector: item => fieldString(item, k)
}, extraOpts));
}) : []
readonly property list<var> fuzzyPrepped: algorithm === Searchable.SearchAlgorithm.Fuzzysort ? list.map(e => {
const obj = {
_item: e
};
for (const k of keys)
obj[k] = Fuzzy.prepare(String(e[k] ?? ""));
return obj;
}) : []
function transformSearch(search: string): string {
return search;
}
function fieldString(item: var, k: string): string {
const v = item[k];
return v == null ? "" : String(v);
}
function selector(item: var): string {
return fieldString(item, keys[0]);
}
function getCachedSource(search) {
if (queryCache[search])
return {
hit: true,
data: queryCache[search]
};
let source = list;
let previousLists = Object.keys(queryCache).filter(key => key && search.startsWith(key));
if (previousLists.length > 0) {
var longest = previousLists.reduce((a, b) => a.length >= b.length ? a : b);
source = queryCache[longest];
}
return {
hit: false,
data: source
};
}
function levendistQuery(search: string): list<var> {
const cacheCheck = getCachedSource(search);
if (cacheCheck.hit)
return cacheCheck.data;
let source = cacheCheck.data;
const prefixGroups = keys.map(() => []);
const fuzzyGroups = keys.map(() => []);
for (const s of source) {
let best = null;
for (let i = 0; i < keys.length; i++) {
const t = fieldString(s, keys[i]).toLowerCase();
if (!t)
continue;
if (t.startsWith(search)) {
if (!best || i < best.index || best.type !== "prefix") {
best = {
type: "prefix",
index: i,
score: 0
};
if (best.index === 0)
break;
}
continue;
}
let pos = 0;
for (const c of search) {
pos = t.indexOf(c, pos);
if (pos < 0) {
pos = 0;
break;
}
pos++;
}
if (pos) {
const d = Levendist.distance(search, t);
if (d <= scoreThreshold) {
if (!best || i < best.index || (best.type === "fuzzy" && i === best.index && d < best.score) || best.type === "prefix") {
if (!best || i < best.index || d < best.score) {
best = {
type: "fuzzy",
index: i,
score: d
};
}
}
}
}
}
if (best) {
if (best.type === "prefix")
prefixGroups[best.index].push(s);
else
fuzzyGroups[best.index].push({
s,
score: best.score
});
}
}
const results = [];
const seen = new Set();
for (let i = 0; i < keys.length; i++) {
for (const item of prefixGroups[i]) {
if (!seen.has(item)) {
results.push(item);
seen.add(item);
}
}
fuzzyGroups[i].sort((a, b) => a.score - b.score).forEach(({
s
}) => {
if (!seen.has(s)) {
results.push(s);
seen.add(s);
}
});
}
queryCache[search] = results;
return results;
}
function fuzzysortQuery(search: string): list<var> {
const cacheCheck = getCachedSource(search);
if (cacheCheck.hit)
return cacheCheck.data;
const res = Fuzzy.go(search, fuzzyPrepped, Object.assign({
all: true,
keys,
scoreFn: r => weights.reduce((a, w, i) => a + r[i].score * w, 0)
}, extraOpts));
const groups = keys.map(() => []);
const noMatch = [];
for (const r of res) {
let idx = -1;
for (let i = 0; i < keys.length; i++) {
const kr = r[i];
if (kr && kr.score !== -Infinity) {
idx = i;
break;
}
}
if (idx === -1)
noMatch.push(r);
else
groups[idx].push(r);
}
const ordered = [];
for (let i = 0; i < groups.length; i++) {
for (const r of groups[i])
ordered.push(r.obj._item);
}
for (const r of noMatch)
ordered.push(r.obj._item);
queryCache[search] = ordered;
return ordered;
}
function fzfQuery(search: string): list<var> {
const cacheCheck = getCachedSource(search);
if (cacheCheck.hit)
return cacheCheck.data;
let results;
if (keys.length <= 1) {
results = fzf.find(search).sort((a, b) => {
if (a.score === b.score)
return (selector(a.item).trim().length - selector(b.item).trim().length);
return b.score - a.score;
}).map(r => r.item);
} else {
const groups = keys.map((_, i) => {
const arr = fzfFinders[i].find(search);
arr.sort((a, b) => {
if (a.score === b.score) {
const la = fieldString(a.item, keys[i]).trim().length;
const lb = fieldString(b.item, keys[i]).trim().length;
return la - lb;
}
return b.score - a.score;
});
return arr;
});
const seen = new Set();
const out = [];
for (let i = 0; i < groups.length; i++) {
for (const r of groups[i]) {
const it = r.item;
if (!seen.has(it)) {
out.push(it);
seen.add(it);
}
}
}
results = out;
}
queryCache[search] = results;
return results;
}
function includeQuery(search: string): list<var> {
const cacheCheck = getCachedSource(search);
if (cacheCheck.hit)
return cacheCheck.data;
const results = list.filter(item => keys.some(k => transformSearch(fieldString(item, k)).includes(search)));
queryCache[search] = results;
return results;
}
function query(search: string): list<var> {
search = transformSearch(search);
if (!search)
return [...list];
if (algorithm === Searchable.SearchAlgorithm.Levendist)
return levendistQuery(search.toLowerCase());
if (algorithm === Searchable.SearchAlgorithm.Fuzzysort)
return fuzzysortQuery(search);
if (algorithm === Searchable.SearchAlgorithm.Fzf)
return fzfQuery(search);
if (algorithm === Searchable.SearchAlgorithm.Include)
return includeQuery(search);
return [];
}
}
|