kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
https://npm.im/match-sorter
MIT License
3.73k stars 129 forks source link

threshold NO_MATCH with custom key not returning all results #88

Closed jamesmosier closed 4 years ago

jamesmosier commented 4 years ago

Relevant code or config

const arr = [
  { value: 'Airstream' },
  { value: 'Avatar' },
  { value: 'Foo' },
  { value: 'Cavan' },
];

const works = matchSorter(arr.map(item => item.value), 'Ava', {
  threshold: matchSorter.rankings.NO_MATCH,
}).map(value => ({ value }));
console.log({ works });

const doesNotWork = matchSorter(arr, 'Ava', {
  keys: [{ threshold: matchSorter.rankings.NO_MATCH, key: 'value' }],
});
console.log({ doesNotWork });

Reproduction repository: https://codesandbox.io/s/matchsorter-threshold-bug-bbu2r

Problem description: When trying to use matchSorter.rankings.NO_MATCH with a custom key, the threshold is not respected. Instead it returns a subset of results, as opposed to all the results.

If I instead transform the array into a list of string it works as expected.

Suggested solution: I very well could be using the keys option incorrectly. If this seems like a valid failing case, I'm happy to open a PR.

jamesmosier commented 4 years ago

As with everything, as soon as I opened the issue I figured it out 😄

const worksToo = matchSorter(arr, 'Ava', {
  keys: ['value'],
  threshold: matchSorter.rankings.NO_MATCH
});