-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for strength based fuzzy searching
- Loading branch information
1 parent
4e1eaf8
commit 134d6e8
Showing
1 changed file
with
29 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
import { getApps } from '@/service/protontweaks'; | ||
import { delay } from '@ribbon-studios/js-utils'; | ||
|
||
// listen for main to transfer the buffer to myWorker | ||
self.onmessage = async (event) => { | ||
const [filteredApps] = await Promise.all([ | ||
getApps().then((apps) => apps.filter((app) => app.name.toLowerCase().includes(event.data.toLowerCase()))), | ||
delay(500), | ||
]); | ||
const [filteredApps] = await Promise.all([filterApps(event.data.toLowerCase()), delay(500)]); | ||
|
||
self.postMessage(filteredApps); | ||
}; | ||
|
||
async function filterApps(value: string) { | ||
// TODO: Add a method of getting an api version (git sha) and using it to enable caching and cache busting | ||
const apps = await getApps(); | ||
|
||
const partials = value.split(' ').filter(Boolean); | ||
|
||
return apps | ||
.filter((app) => { | ||
return partials.some((partial) => app.name.toLowerCase().includes(partial)); | ||
}) | ||
.map((app) => ({ | ||
...app, | ||
strength: determineMatchStrength(value, partials, app.name.toLowerCase()), | ||
})) | ||
.sort((a, b) => b.strength - a.strength); | ||
} | ||
|
||
function determineMatchStrength(value: string, partials: string[], comparisonValue: string): number { | ||
if (comparisonValue === value) return 100; | ||
const matches = partials | ||
// Filter out the non matches | ||
.filter((partial) => comparisonValue.includes(partial)) | ||
// Calculate the number of characters that matched | ||
.reduce((output, partial) => output + partial.length, 0); | ||
|
||
return Math.floor((matches / value.length) * 100); | ||
} |