js fuzzy search highlight effect

js fuzzy search highlight effect

First look at the renderings
insert image description here
and then the code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>auto complete</title>
  <style>
    bdi {
      
      
      color: rgb(0, 136, 255);
    }
    li {
      
      
      list-style: none;
    }
  </style>
</head>

<body>
  <input class="inp" type="text">
  <section>
    <ul class="container"></ul>
  </section>
</body>
<script>
  function debounce(fn, timeout = 300) {
      
      //防抖
    let t; //设置一个接收标记,接受定时器返回值
    return (...args) => {
      
      
    //argsfn的插入参 ,即最下面的输入事件参数e,以数组的形式返回
      if (t) {
      
      
        clearTimeout(t);
      }
      t = setTimeout(() => {
      
      
        fn.apply(fn, args);//执行(e) => {} 改变指针传入参数e
      }, timeout);
    }
  }

  function memorize(fn) {
      
      //缓存
    const cache = new Map();//创建一个Map对象进行缓存
    return (name) => {
      
      //name为fn插入参  即value 输入值为空,清空container
      if (!name) {
      
      
        container.innerHTML = '';
        return;
      }
      if (cache.get(name)) {
      
      //如果map对象中有输入的valu 直接插入container
        container.innerHTML = cache.get(name);
        return;
      }
      const res = fn.call(fn, name).join('');
      //以上两种情况都不是的话 fn执行后返回handleInput的Search 
      //并进行渲染 join方法把数组转成了字符串并清除了","
      cache.set(name, res);
      console.log(name)
      console.log(cache)
      console.log(res)
      container.innerHTML = res;
    }
  }

  function handleInput(value) {
      
      
    const reg = new RegExp(value);
    //三个标志:全局模式g,不区分大小写模式i,多行模式m 即第二个参数  
    //这里只匹配第一个结果 
    const search = data.reduce((res, cur) => {
      
      
      if (reg.test(cur)) {
      
      
        res.push(`<li>${ 
        cur.replace(reg, '<bdi>$&</bdi>')}</li>`);
        //亮高关键字显示,如果再Vue项目里 结合v-html
      }
      return res;
    }, []);
    //第二个参数[] 默认第一开始遍历时res为数组
    return search;
  }

  const data = ["蜡笔小新", "野生动物园", "北京吃货巷子", "上海市中心", 
  "上海黄埔江", "迪士尼上海", "陆家嘴上海中心", "蜡笔十元一只", "野生哈子",
   "北京方便面", "北京烤鸭", "北京博物馆", "北京纪念馆", 
   "北京儿童吉他培训班", "冬季野生面包屑", "野生骆驼", "野生王八"]
  const container = document.querySelector('.container');
  const memorizeInput = memorize(handleInput);
  document.querySelector('.inp').addEventListener('input', debounce(e => {
      
      
    memorizeInput(e.target.value);
  }))
</script>

</html>

Guess you like

Origin blog.csdn.net/weixin_53532986/article/details/123128638