如何像 vs code 一样设置 fuse.js

问题描述 投票:0回答:1

当我想要像 VSCode (Ctrl+P) 文件搜索一样的行为时,如何设置 fuse.js 的选项?

在官方示例中,使用 {ignoreLocation: true}

https://fusejs.io/demo.html

搜索模式 =

"tprt"

'The Preservationist' 应该是第一个,因为它有所有 4 个搜索字符(索引),而不是只有 2

的 'Incompetence'
fuzzy-search fuse.js
1个回答
0
投票

如果您想按

indicies
搜索,那么您需要公开它们并为此创建自定义排序功能。

这是一个将按匹配字符搜索的示例 (

indicies
):

const options = {
  ignoreLocation: true,
  keys: ['title'],
  includeMatches: true,
  sortFn: (a, b) => b.matches[0].indices?.length - a.matches[0].indices?.length
};

const list = [
  {
    "title": "Old Man's War",
    "author": {
      "firstName": "John",
      "lastName": "Scalzi"
    }
  },
  {
    "title": "The Lock Artist",
    "author": {
      "firstName": "Steve",
      "lastName": "Hamilton"
    }
  },
  {
    "title": "HTML5",
    "author": {
      "firstName": "Remy",
      "lastName": "Sharp"
    }
  },
  {
    "title": "Right Ho Jeeves",
    "author": {
      "firstName": "P.D",
      "lastName": "Woodhouse"
    }
  },
  {
    "title": "The Code of the Wooster",
    "author": {
      "firstName": "P.D",
      "lastName": "Woodhouse"
    }
  },
  {
    "title": "Thank You Jeeves",
    "author": {
      "firstName": "P.D",
      "lastName": "Woodhouse"
    }
  },
  {
    "title": "The DaVinci Code",
    "author": {
      "firstName": "Dan",
      "lastName": "Brown"
    }
  },
  {
    "title": "Angels & Demons",
    "author": {
      "firstName": "Dan",
      "lastName": "Brown"
    }
  },
  {
    "title": "The Silmarillion",
    "author": {
      "firstName": "J.R.R",
      "lastName": "Tolkien"
    }
  },
  {
    "title": "Syrup",
    "author": {
      "firstName": "Max",
      "lastName": "Barry"
    }
  },
  {
    "title": "The Lost Symbol",
    "author": {
      "firstName": "Dan",
      "lastName": "Brown"
    }
  },
  {
    "title": "The Book of Lies",
    "author": {
      "firstName": "Brad",
      "lastName": "Meltzer"
    }
  },
  {
    "title": "Lamb",
    "author": {
      "firstName": "Christopher",
      "lastName": "Moore"
    }
  },
  {
    "title": "Fool",
    "author": {
      "firstName": "Christopher",
      "lastName": "Moore"
    }
  },
  {
    "title": "Incompetence",
    "author": {
      "firstName": "Rob",
      "lastName": "Grant"
    }
  },
  {
    "title": "Fat",
    "author": {
      "firstName": "Rob",
      "lastName": "Grant"
    }
  },
  {
    "title": "Colony",
    "author": {
      "firstName": "Rob",
      "lastName": "Grant"
    }
  },
  {
    "title": "Backwards, Red Dwarf",
    "author": {
      "firstName": "Rob",
      "lastName": "Grant"
    }
  },
  {
    "title": "The Grand Design",
    "author": {
      "firstName": "Stephen",
      "lastName": "Hawking"
    }
  },
  {
    "title": "The Book of Samson",
    "author": {
      "firstName": "David",
      "lastName": "Maine"
    }
  },
  {
    "title": "The Preservationist",
    "author": {
      "firstName": "David",
      "lastName": "Maine"
    }
  },
  {
    "title": "Fallen",
    "author": {
      "firstName": "David",
      "lastName": "Maine"
    }
  },
  {
    "title": "Monster 1959",
    "author": {
      "firstName": "David",
      "lastName": "Maine"
    }
  }
]

const options = {
  ignoreLocation: true,
  keys: ['title'],
  includeMatches: true,
  sortFn: (a, b) => b.matches[0].indices?.length - a.matches[0].indices?.length
};
const fuse = new Fuse(list, options);
const pattern = "tprt";
document.getElementById('output').innerHTML = JSON.stringify(fuse.search(pattern), null, 3);
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<pre id='output'></pre>

© www.soinside.com 2019 - 2024. All rights reserved.