-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod-fuzzy-worker.js
44 lines (37 loc) · 970 Bytes
/
mod-fuzzy-worker.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
import { fuzzyMatch } from "https://deno.land/x/fuzzy_search@0.3.0/fuzzy-search.js";
import { walk, walkSync } from "https://deno.land/std@0.83.0/fs/mod.ts";
const find = (data) => {
let { msgId, pattern, dir } = data.data;
if (dir[0] == "~") {
const home = Deno.env.get("HOME") || "/home";
dir = dir.replace("~", home);
}
let rank = [];
for (const entry of walkSync(dir)) {
const search = fuzzyMatch(pattern, entry.path);
if (search[0] === true && entry.isFile === true) {
search[0] = entry;
rank.push(search);
}
}
rank.sort((a, b) => {
let x = a[1];
let y = b[1];
if (x < y) {
return 1;
} else if (x > y) {
return -1;
} else {
return 0;
}
});
return { responseMsgId: msgId, rank };
};
self.onmessage = (data) => {
try {
const msg = find(data);
self.postMessage(msg);
} catch (error) {
self.postMessage({ responseMsgId: data.data.msgId, error: JSON.stringify(error) });
}
};