Skip to content

Implements fuzzy searching #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 12, 2025
Merged

Implements fuzzy searching #166

merged 16 commits into from
May 12, 2025

Conversation

mcbalaam
Copy link
Contributor

@mcbalaam mcbalaam commented May 9, 2025

About the PR

Implements fuzzy searching using https://github.com/Nozbe/microfuzz

Fuzzy searching, or approximate string matching, is the technique of finding strings that mach the pattern approximately rather than exactly, which makes it easier to search for strings in a large array of data, even by fragments. "wo chai" will find "wooden chair" while in smart mode and "wobble chamber lid" while in aggressive mode.

Demonstration:

2025-05-11.02-26-33.mp4

Why's this needed?

A lot of interfaces that provide searching for entries will benefit from this.

Implementation

useFuzzySearch is a hook that accepts an array of items and returns a matched array any time the setQuery hook is updated. Below is a simple showcase:

const items = [
	"first item",
	"second item",
	"third item",
]

const { query, setQuery, results } = useFuzzySearch({
  searchArray: items, // an array to match
  matchStrategy: 'smart', // which matching strategy to use
  getSearchString: (item) => item, // a hook to fetch the value of the input field
});
  
<Input
  value={query}
  onChange={setQuery}
/>
<Box mt={2}>
  {results.map((item, index) => (
    <Box key={index}>{item}</Box> // the mapped array 
  ))}
</Box>

@mcbalaam mcbalaam marked this pull request as draft May 9, 2025 18:56
@mcbalaam mcbalaam marked this pull request as ready for review May 10, 2025 21:25
Copy link
Member

@jlsnow301 jlsnow301 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits. Curious if there was any process to you selecting this library. I have no preferences though

@mcbalaam
Copy link
Contributor Author

mcbalaam commented May 12, 2025

Mostly nits. Curious if there was any process to you selecting this library. I have no preferences though

I was looking for something simple and lightweight. Fuse was too large and complicated, ts-fuzzysearch wasn't updated in years, others didn't have an npm package or it was outdated aswell

@mcbalaam mcbalaam requested a review from jlsnow301 May 12, 2025 08:53
@jlsnow301
Copy link
Member

I was looking for something simple and lightweight. Fuse was too large and complicated, ts-fuzzysearch wasn't updated in years, others didn't have an npm package or it was outdated aswell

tbf, this hasn't been either
image

@jlsnow301 jlsnow301 merged commit 34c8532 into tgstation:main May 12, 2025
1 check passed
@mcbalaam mcbalaam deleted the fuzzy-search branch May 13, 2025 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants