Skip to content

Commit 95e9ffb

Browse files
committed
refactor: move servant searcher into own module
1 parent 5a4d9bb commit 95e9ffb

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/search-servant/script.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { MatchData } from "fast-fuzzy";
2-
import { Searcher } from "fast-fuzzy";
31
import { parseArgs } from "util";
4-
import { servantsCache } from "~/cache";
52
import { describeServant } from "~/utils/describeServant";
6-
import { col, log, logger, createTimer } from "~/utils/logger";
3+
import { log, logger, createTimer } from "~/utils/logger";
4+
import {
5+
createServantSearcher,
6+
prettyPrintMatch
7+
} from "~/utils/ServantSearcher";
78

89
const timer = createTimer();
910
const args = parseArgs({
@@ -31,17 +32,6 @@ const textHelp = `Search Servant\n\nUsage:
3132
--alt Search alternative Names (enabled by default)
3233
--no-alt Do not search alternative Names`;
3334

34-
function prettyPrintMatch(match: MatchData<unknown>) {
35-
const text = match.original;
36-
const { index, length } = match.match;
37-
const end = index + length;
38-
const before = text.slice(0, index);
39-
const matched = text.slice(index, end);
40-
const after = text.substring(end);
41-
42-
return `${before}${col.bold(col.redBright(matched))}${after}`;
43-
}
44-
4535
async function main() {
4636
// DEBUG
4737
if (args.values.verbose) logger.setLogLevel("Debug");
@@ -66,15 +56,7 @@ async function main() {
6656
}
6757

6858
// create searcher
69-
const servantsList = await servantsCache.read();
70-
const searcher = new Searcher(servantsList, {
71-
keySelector: candidate =>
72-
args.values.alt
73-
? [candidate.name].concat(candidate.names)
74-
: [candidate.name],
75-
returnMatchData: true,
76-
threshold
77-
});
59+
const searcher = await createServantSearcher(args.values.alt, threshold);
7860

7961
// handle queries
8062
for (const query of args.positionals) {

src/utils/ServantSearcher.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { MatchData } from "fast-fuzzy";
2+
import { Searcher } from "fast-fuzzy";
3+
import { col } from "~/utils/logger";
4+
import { servantsCache } from "~/cache";
5+
6+
export async function createServantSearcher(
7+
includeAltNames = true,
8+
threshold = 0.7
9+
) {
10+
const servantsList = await servantsCache.read();
11+
const searcher = new Searcher(servantsList, {
12+
keySelector: includeAltNames
13+
? candidate => [candidate.name].concat(candidate.names)
14+
: candidate => [candidate.name],
15+
returnMatchData: true,
16+
threshold
17+
});
18+
19+
return searcher;
20+
}
21+
22+
export function prettyPrintMatch(match: MatchData<unknown>) {
23+
const text = match.original;
24+
const { index, length } = match.match;
25+
const end = index + length;
26+
const before = text.slice(0, index);
27+
const matched = text.slice(index, end);
28+
const after = text.substring(end);
29+
30+
return `${before}${col.bold(col.redBright(matched))}${after}`;
31+
}

0 commit comments

Comments
 (0)