Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. // 模糊搜索 needle:检索词, haystack:目标值
  2. function fuzzySearch (needle, haystack, ignoreCase) {
  3. var hlen = haystack.length
  4. var nlen = needle.length
  5. if (nlen > hlen) {
  6. return false
  7. }
  8. if (nlen === 0) {
  9. return true
  10. }
  11. if (ignoreCase) {
  12. haystack = haystack.toLocaleLowerCase()
  13. needle = needle.toLocaleLowerCase()
  14. }
  15. if (nlen === hlen) {
  16. return needle === haystack
  17. }
  18.  
  19. var i = 0
  20. var j = 0
  21. while (j < hlen && i < nlen) {
  22. if (haystack[j] === needle[i]) {
  23. i++
  24. }
  25. j++
  26. }
  27. return i === nlen
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement