-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
40 lines (36 loc) · 928 Bytes
/
simple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const fuzzy = require('fuzzy')
const Utils = require('../utils')
const options = {
pre: Utils.color.NAME.Red,
post: Utils.color.NAME.End,
extract: el => el.name
}
/**
* 返回搜索结果
* @parameter searchTerm 搜索字
* @parameter list Array 要搜索的列表
* @return Array [{ string, name, url }]
*/
exports.search = function (searchTerm, list) {
const result = []
for (const el of list) {
const m = match(options.extract(el), searchTerm)
if (m) {
result.push({
string: m,
name: el.name,
url: el.path
})
}
}
return result
}
function match (string, term) {
const matches = string.toLowerCase().match(term.toLowerCase())
if (matches) {
return string.slice(0, matches.index) + options.pre +
string.slice(matches.index, matches.index + term.length) +
options.post + string.slice(matches.index + term.length)
}
return null
}