js模糊搜索插件fuzzysort.js

2017-9-25    分类: 主题下载

自动补全 模糊下拉搜索

根据输入模糊的关键词 显示搜索结果:

node安装:

npm i fuzzysort
node
> require('fuzzysort').single('t', 'test')
{ score: -3, indexes: [0], target: 'test' }

 

浏览器安装:

<script src="https://rawgit.com/farzher/fuzzysort/master/fuzzysort.js"></script>
<script> console.log(fuzzysort.single('t', 'test')) </script>

 

使用方法:

fuzzysort.single(search, target)

var result = fuzzysort.single('query', 'some string that contains my query.')
result.score // -59
result.indexes // [29, 30, 31, 32, 33]
result.target // some string that contains my query.
fuzzysort.highlight(result, '<b>', '</b>') // some string that contains my <b>query</b>.

fuzzysort.single('query', 'irrelevant string') // null

// exact match returns a score of 0. lower is worse
fuzzysort.single('query', 'query').score // 0

fuzzysort.go(search, targets, options=null)

fuzzysort.go('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
// [{score: -18, target: "MeshRenderer.cpp"}, {score: -6009, target: "Monitor.cpp"}]

fuzzysort.goAsync(search, targets, options=null)

let promise = fuzzysort.goAsync('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
promise.then(results => console.log(results))
if(invalidated) promise.cancel()
Options
fuzzysort.go(search, targets, {
  threshold: -Infinity, // Don't return matches worse than this (faster)
  limit: Infinity, // Don't return more results than this (faster)

  key: null, // For when targets are objects (see its example usage)
  keys: null, // For when targets are objects (see its example usage)
  scoreFn: null, // For use with `keys` (see its example usage)
})

https://github.com/farzher/fuzzysort

下载:fuzzysort-master