question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`matcher` attr fuzzy matching

See original GitHub issue

I’ve got a list of countries in JSON format like so:

  {
    'name': 'United States',
    'number': '+1',
    'abbr': 'US'
  },
  {
    'name': 'United Kingdom',
    'number': '+44',
    'abbr': 'GB'
  },
  {
    'name': 'Japan',
    'number': '+81',
    'abbr': 'JP'
  },
  ...

At the moment I’m using power-select to allow the user to search by either the number or name properties by using the matcher option. My matcher code is:

    let concatenatedFields = get(country, 'name') + get(country, 'number');
    let lowerCasedFields = (concatenatedFields).toLowerCase();

    return (concatenatedFields + lowerCasedFields).indexOf(term);

I need to concatenate a lower case version of the fields onto the matcher because if I don’t then the matcher is taken literally and I only get exact matches. For example united states would not trigger a match for United States, which is annoying.

Basically, I want to be able to make use of the power-select’s fuzzy matching with the matcher attribute that you get when you use the searchField option, and I feel like this is how it should work out of the box.

This would mean that my matcher code just be:

    let concatenatedFields = get(country, 'name') + get(country, 'number');
    return (concatenatedFields).indexOf(term);

and I’d get all of the fuzzy matching options like fuzzy case matching as well as all the other great fuzzy matching features power-select has!

I’m not sure if this is already possible somehow. If it is I apologise.

I think another good way of achieving this behaviour would be to enable you to pass an array of the fields that you want to search by into the searchField attribute or create a searchFields option that would take multiple fields as described.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
cibernoxcommented, May 17, 2017

@BennyAlex I don’t think it would be wise as there is a lot of possible strategies. Imagine you have first_name and last_name. If the user types jo it will find “John Williams” but also “Scarlet Johanson”. So far so good. But what about n a. Should it find “Anton Allaire” or “Akira Ronin” (because properties can be concatenated as “Ronin Akira”)? Or both? The space is meaninful or should “Navidad” also match?

I gave it some thought and I think that it will be better to let people do their own matchers. To avoid repetition you can extract your matchers to util helpers that you just import.

import { matchNameAndSurname } from 'my-app/utils/custom-matchers';
export default Component.extend({
  matchNameAndSurname
});
{{#power-select ... matcher=matchNameAndSurname}}

If you want to skip the javascript file, a helper to generate matchers is also extremely simple:

{{#power-select ... matcher=(match-by "firstName" "lastName")}}
export default Helper.helper(function(fields, { separator = ' ' } = {}) {
  return function(item, term) {
    let concatenation = fields.map((f) => get(item, f)).join(separator);
    return concatenation.indexOf(term);
  }
});
1reaction
cibernoxcommented, Apr 3, 2017

You can. I made the default matches function an exported util: https://github.com/cibernox/ember-power-select/blob/master/addon/utils/group-utils.js#L970

It doesn’t use fuzzy matching, but it normalizes diacritics like áàøçĉ and such. If you invoke it with defaultMatcher(concatenatedFields, searchTerm) you will get the default behaviour searching on both fields. But it’s not fuzzy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

intuit/fuzzy-matcher: A Java library to determine ... - GitHub
A java-based library to match and group "similar" elements in a collection of documents. Imagine working in a system with a collection of...
Read more >
Fuzzy Matching 101: Cleaning and Linking Messy Data
Fuzzy matching identifies the likelihood that two records are a true match based on whether they agree or disagree on the various identifiers....
Read more >
Incorporating fuzzy search in a matcher object - Stack Overflow
I think the easiest way would be to add an additional predicate type called something like FUZZY . Look at how the regex,...
Read more >
Fuzzy Matching or Fuzzy Logic Algorithms Explained - Nanonets
Fuzzy Matching (also called Approximate String Matching) is a technique that helps identify two elements of text, strings, or entries that are ...
Read more >
Using the match attribute - Crossref
fuzzy: Fuzzy matching is allowed but the field is not optional, it must be matched. · optional: In most cases, optional is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found