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

More threshold examples #55

Closed chapmanjacobd closed 5 years ago

chapmanjacobd commented 5 years ago

I'm not sure if this is possible with the threshold option but I'm looking to prioritize when l = false as the most important key to sort on. so results with l = false will be at the top of the results

Relevant code or config:

function getSuggestions(value, results) {
  const inputValue = deburr(value.trim()).toLowerCase();
  const inputLength = inputValue.length;
  let count = 0;
  console.log(results);
  const tmp = [];
  for (let j = 0; j < 2; j++) {
    for (let k = 0; k < results.length; k++) {
      if (results[k].l == (j == 0 ? false : true)) {
        tmp.push(results[k]);
      }
    }
  }
  results = tmp;
  const getItems = value =>
    value
      ? matchSorter(results, value, {
          keys: ["i", "n", "nl_n", "s", "nl_s", "c", "nl_c"],
        })
      : results;

  return inputLength === 0
    ? []
    : getItems(inputValue).filter(() => {
        // put the city data here.
        const keep = count < 8;

        if (keep) {
          count += 1;
        }
        // console.log(keep)
        return keep;
      });
}

I can't figure out a way to put l as a key. I tried using different thresholds but I couldn't wrap my head around the threshold options

kentcdodds commented 5 years ago

I don't think that this is possible with match-sorter by itself. You may need to group the array into two unique arrays which have l = false and those which do not, then run match sorter for each and then combine them with the l = false group first.