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

match fails under certain conditions when keys use different thresholds #126

Closed le0nik closed 3 years ago

le0nik commented 3 years ago

Relevant code or config

import { matchSorter } from 'match-sorter';

// BUG, match-sorter returns [] even though `code` contains `1`
// If you change `id` to something else like e.g. `a1` it works correctly
// If you remove the 2nd key or remove `threshold` property from it it also works correctly
// If you change `code` to `1t` making `1` the first char it also works correctly

const arr = [{ id: '11', code: 't1t' }];

console.log(
  matchSorter(arr, '1', {
    keys: [
      (obj) => obj.code,
      { key: (obj) => obj.id, threshold: matchSorter.rankings.EQUAL }
    ]
  })
);

What you did:

Ran match-sorter with a single character(doesn't matter which one) in query-string and 2 keys: 1 with default threshold and another with rankings.EQUAL threshold.

What happened:

match-sorter returned an empty array, even though it should've matched an item from provided array

Reproduction repository: https://codesandbox.io/s/sweet-bush-e496s?file=/src/index.ts

Problem description: This is a werid combination of conditions that causes this bug. I've described what changes make it work correctly

Suggested solution: Don't have one