-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfuzzaldrin.js
119 lines (99 loc) · 3.44 KB
/
fuzzaldrin.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const binding = require("node-gyp-build")(__dirname)
const defaultPathSeparator = process.platform === "win32" ? "\\" : "/"
function parseOptions(options, query) {
// options.allowErrors ? = false
if (options.usePathScoring == undefined) options.usePathScoring = true
// options.useExtensionBonus ? = false
if (!options.pathSeparator) options.pathSeparator = defaultPathSeparator
// options.optCharRegEx ? = null
// options.wrap ? = null
if (!options.maxResults) options.maxResults = 0
return options
}
/** Array Filter */
export class ArrayFilterer {
constructor() {
this.obj = new binding.Fuzzaldrin()
}
setCandidates(candidates, dataKey = undefined) {
this.candidates = candidates
if (dataKey) {
if (typeof dataKey == "string") {
candidates = candidates.map((item) => item[dataKey])
}
// @deprecated pass the key as the second argument as a string
else if (dataKey.key) {
// an object (options) containing the key
candidates = candidates.map((item) => item[dataKey.key])
}
}
return this.obj.setArrayFiltererCandidates(candidates)
}
filter(query, options = {}) {
options = parseOptions(options)
const res = this.obj.filter(
query,
options.maxResults,
Boolean(options.usePathScoring),
Boolean(options.useExtensionBonus)
)
return res.map((ind) => this.candidates[ind])
}
}
/**
* @deprecated use ArrayFilterer or TreeFilterer classes instead
*/
export const New = () => new ArrayFilterer()
export function filter(candidates, query, options = {}) {
if (!candidates || !query) return []
const arrayFilterer = new ArrayFilterer()
arrayFilterer.setCandidates(candidates, options)
return arrayFilterer.filter(query, options)
}
/** Tree Filter */
export class TreeFilterer {
constructor() {
this.obj = new binding.Fuzzaldrin()
}
setCandidates(candidates, dataKey = "data", childrenKey = "children") {
this.candidates = candidates
return this.obj.setTreeFiltererCandidates(candidates, dataKey, childrenKey)
}
filter(query, options = {}) {
options = parseOptions(options)
return this.obj.filterTree(
query,
options.maxResults,
Boolean(options.usePathScoring),
Boolean(options.useExtensionBonus)
)
}
}
export function filterTree(candidatesTrees, query, dataKey = "data", childrenKey = "children", options = {}) {
if (!candidatesTrees || !query) return []
const treeFilterer = new TreeFilterer()
treeFilterer.setCandidates(candidatesTrees, dataKey, childrenKey)
return treeFilterer.filter(query, options)
}
export function score(candidate, query, options = {}) {
if (!candidate || !query) return 0
options = parseOptions(options)
return binding.score(candidate, query, Boolean(options.usePathScoring), Boolean(options.useExtensionBonus))
}
/** Other functions */
export function match(string, query, options = {}) {
if (!string || !query) return []
if (string == query) return Array.from(Array(string.length).keys())
options = parseOptions(options, query)
return binding.match(string, query, options.pathSeparator)
}
export function wrap(string, query, options = {}) {
if (!string || !query) return []
options = parseOptions(options, query)
return binding.wrap(string, query, options.pathSeparator)
}
export function prepareQuery(query, options = {}) {
// This is no - op since there is no major benefit by precomputing something
// just for the query.
return {}
}