1.0.0 • Published 7 years ago

magic-search v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

magic-search

A simple fuzzy search tool similar to Sublime-Text's cmd-P search system.

Filters a collection down to only items that contain all of the characters in the query:

  • in the correct order;
  • with any amount of other characters in between.

It should work with any collection that implements filter(lambda) like Array does.

const search = require('magic-search');

const fruit = [
  'Apple',
  'Apricot',
  'Avocado',
  'Banana',
  'Bilberry',
  'Blackberry',
  'Blackcurrant',
  ...
];
search(fruit, 'aple')
→ ['Apple', 'Custard apple', 'Pineapple']

search(fruit, 'sberry')
→ ['Boysenberry', 'Gooseberry', 'Raspberry', ...]

Use with a getter

If you need to filter a collection of items that are more than just strings, you can use a getter function as the third argument.

const filteredByGenus = search(fruit, 'rosales', item => item.getGenus())