corvid
corvid

Reputation: 11197

Ramda: filter by "fuzzy search"

I have a list of IMDb's top 100 movies. Given the title, I was trying to find a way to "fuzzy search" through it. i.e., if you typed "shaw", the results would display "The Shawshank Redemption". If nothing is typed (i.e., search is an empty string), it would not filter any of the movies.

My selector is as follows:

export const selectMovies = createSelector(
  getMovies,
  getQueryParams,
  (movies, { search = '' }) => R.filter(
    R.propSatisfies(R.contains(search), 'title'),
    movies
  ),
)

Where getMovies is a function that returns a Movie[], and getQueryParams returns an object that has the key search with a string value.

Right now, while I can get it to roughly filter by title, it seems to have a number of problems. For example, if I type in "sha", I get the Shawshank Redemption. However, if I type in "shaw", it doesn't get shawshank redemption.

Is there a good way to implement a string-based "fuzzy filter" with ramda?

Upvotes: 0

Views: 454

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50807

This is untested, but I think you could easily write:

const containsInsensitive = child => parent => contains(toLower(child), toLower(parent))

Then replace

R.contains(search)

with

containsInsensitive(search)

That should be all it takes.

Upvotes: 2

Related Questions