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

Filter based on a boolean #74

Closed valgeirb closed 5 years ago

valgeirb commented 5 years ago

I'm trying to filter based on an active state.

const objList = [
  {name: 'Janice', color: 'Green', active: false},
  {name: 'Fred', color: 'Orange', active: true},
  {name: 'George', color: 'Blue', active: true},
  {name: 'Jen', color: 'Red', active: false},
]

matchSorter(objList, false, {keys: ['active']})

If I try to filter by false it returns the whole list.

It works correctly if I filter by true.

Should I not be able to filter by using a false value? Thanks for the great package.

kentcdodds commented 5 years ago

Hi @valgeirb,

Would this work?

const objList = [
  {name: 'Janice', color: 'Green', active: false},
  {name: 'Fred', color: 'Orange', active: true},
  {name: 'George', color: 'Blue', active: true},
  {name: 'Jen', color: 'Red', active: false},
]

objList.filter(o => o.active)

:)

Then you can pass the result of that to match-sorter to match on the other properties:

const objList = [
  {name: 'Janice', color: 'Green', active: false},
  {name: 'Fred', color: 'Orange', active: true},
  {name: 'George', color: 'Blue', active: true},
  {name: 'Jen', color: 'Red', active: false},
]

matchSorter(objList.filter(o => o.active), 'jn', {keys: ['name', 'color']})
// [{name: 'Janice', color: 'Green', active: false}, {name: 'Jen', color: 'Red', active: false}]

I hope that helps!