f67799347e7ecbd1e575d7db53c9086446f19adb11ccd98440f8aa3a7c64b4a19374fda842d84b8dbbdf998f5642a0780e73528c3c0fd779ab9414dbf31946 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # fuzzysearch
  2. > Tiny and blazing-fast fuzzy search in JavaScript
  3. Fuzzy searching allows for flexibly matching a string with partial input, useful for filtering data very quickly based on lightweight user input.
  4. # Demo
  5. To see `fuzzysearch` in action, head over to [bevacqua.github.io/horsey][3], which is a demo of an autocomplete component that uses `fuzzysearch` to filter out results based on user input.
  6. # Install
  7. From `npm`
  8. ```shell
  9. npm install --save fuzzysearch
  10. ```
  11. # `fuzzysearch(needle, haystack)`
  12. Returns `true` if `needle` matches `haystack` using a fuzzy-searching algorithm. Note that this program doesn't implement _[levenshtein distance][2]_, but rather a simplified version where **there's no approximation**. The method will return `true` only if each character in the `needle` can be found in the `haystack` and occurs after the preceding character.
  13. ```js
  14. fuzzysearch('twl', 'cartwheel') // <- true
  15. fuzzysearch('cart', 'cartwheel') // <- true
  16. fuzzysearch('cw', 'cartwheel') // <- true
  17. fuzzysearch('ee', 'cartwheel') // <- true
  18. fuzzysearch('art', 'cartwheel') // <- true
  19. fuzzysearch('eeel', 'cartwheel') // <- false
  20. fuzzysearch('dog', 'cartwheel') // <- false
  21. ```
  22. An exciting application for this kind of algorithm is to filter options from an autocomplete menu, check out [horsey][3] for an example on how that might look like.
  23. # But! _`RegExp`s...!_
  24. ![chart showing abysmal performance for regexp-based implementation][1]
  25. # License
  26. MIT
  27. [1]: https://cloud.githubusercontent.com/assets/934293/6495796/106a61a6-c2ac-11e4-945d-3d1bb066a76e.png
  28. [2]: http://en.wikipedia.org/wiki/Levenshtein_distance
  29. [3]: http://bevacqua.github.io/horsey