Skip to content

Commit

Permalink
Improve field search algorithm (#117)
Browse files Browse the repository at this point in the history
Switches to Fuse.js, which is faster than the old approach and supports fuzzy matching
  • Loading branch information
jwbonner committed Feb 4, 2024
1 parent 6bbd5c5 commit 47fe5fa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"chart.js": "^4.4.0",
"electron": "^26.2.1",
"electron-builder": "^24.6.4",
"fuse.js": "^7.0.0",
"heatmap.js": "https://github.com/jwbonner/heatmap.js.git",
"highlight.js": "^11.9.0",
"mathjs": "11.3.0",
Expand Down
22 changes: 7 additions & 15 deletions src/shared/log/LogUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Fuse from "fuse.js";
import { Rotation2d, Translation2d } from "../geometry";
import MatchInfo, { MatchType } from "../MatchInfo";
import { convert } from "../units";
Expand Down Expand Up @@ -444,23 +445,14 @@ export function mergeMechanismStates(states: MechanismState[]): MechanismState {
};
}

const SEARCH_FUSE = new Fuse([] as string[], { findAllMatches: true, ignoreLocation: true });

export function searchFields(log: Log, query: string): string[] {
if (query.length == 1) return [];
query = query.toLowerCase();
let fieldStrings = log.getFieldKeys().filter((field) => field.toLowerCase().includes(query));
let fields = fieldStrings.map((field) => {
return {
string: field,
endDistance: field.length - field.toLowerCase().lastIndexOf(query)
};
});
fields.sort((a, b) => a.string.localeCompare(b.string, undefined, { numeric: true }));
fields.sort((a, b) => a.string.length - b.string.length);
fields.sort((a, b) => a.endDistance - b.endDistance);
return fields
if (query.length == 0) return [];
SEARCH_FUSE.setCollection(log.getFieldKeys());
return SEARCH_FUSE.search(query)
.slice(0, MAX_SEARCH_RESULTS)
.map((field) => field.string)
.filter((field) => !log.isGenerated(field));
.map((field) => field.item);
}

export function getMatchInfo(log: Log): MatchInfo | null {
Expand Down

0 comments on commit 47fe5fa

Please sign in to comment.