Skip to content

Commit ee52a74

Browse files
authored
Merge pull request #67 from atom-community/binding-types
2 parents 0678196 + e64897d commit ee52a74

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "eslint-config-atomic",
3-
"ignorePatterns": ["./index.*", "build/", "node_modules/", "dist-test/"]
3+
"ignorePatterns": ["index.js", "index.d.ts", "build/", "node_modules/", "dist-test/"]
44
}

src/binding/binding.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Tree, TreeFilterResult } from "./node"
2+
3+
export declare class Zadeh {
4+
constructor()
5+
6+
filter(query: string, maxResult: number, usePathScoring: boolean, useExtensionBonus: boolean): Array<number>
7+
8+
setArrayFiltererCandidates(candidateStrings: Array<string>): boolean
9+
10+
filterTree(
11+
query: string,
12+
maxResult: number,
13+
usePathScoring: boolean,
14+
useExtensionBonus: boolean
15+
): Array<TreeFilterResult>
16+
17+
setTreeFiltererCandidates(candidateTrees: Array<Tree>, dataKey: string, childrenKey: string): boolean
18+
}
19+
20+
export declare function score(
21+
candidate: string,
22+
query: string,
23+
usePathScoring: boolean,
24+
useExtensionBonus: boolean
25+
): number
26+
27+
export declare function match(str: string, query: string, pathSeparator: string): Array<number>
28+
29+
export declare function wrap(str: string, query: string, pathSeparator: string): string

src/binding/node.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-ignore
22
import nodeGypBuld from "node-gyp-build"
3-
const binding = nodeGypBuld(__dirname) // this relies on Parcel to bundle this file in the root of the package, so __dirname becomes correct
3+
4+
import * as Binding from "./binding"
5+
const binding = nodeGypBuld(__dirname) as typeof Binding // __dirname relies on Parcel to bundle this file in the root of the package, so __dirname becomes correct
46

57
/*
68
██████ ██████ ████████ ██ ██████ ███ ██ ███████
@@ -114,7 +116,7 @@ export class ArrayFilterer<T extends StringOrObjectArray> {
114116
* @param candidates An array of tree objects.
115117
* @param dataKey (optional) if `candidates` is an array of objects, pass the key in the object which holds the data.
116118
*/
117-
setCandidates(candidates: Array<T>, dataKey?: string): void {
119+
setCandidates(candidates: Array<T>, dataKey?: string) {
118120
this.candidates = candidates
119121
let candidateStrings: string[]
120122
if (dataKey) {
@@ -135,7 +137,7 @@ export class ArrayFilterer<T extends StringOrObjectArray> {
135137
parseFilterOptions(options)
136138
const res = this.obj.filter(
137139
query,
138-
options.maxResults,
140+
options.maxResults as number /* numberified by parseFilterOptions */,
139141
Boolean(options.usePathScoring),
140142
Boolean(options.useExtensionBonus)
141143
)
@@ -186,7 +188,7 @@ export interface TreeFilterResult {
186188
/** TreeFilterer is a class that allows to set the `candidates` only once and perform filtering on them multiple times.
187189
* This is much more efficient than calling the `filterTree` function directly.
188190
*/
189-
export class TreeFilterer<T> {
191+
export class TreeFilterer<T extends Tree = Tree> {
190192
obj = new binding.Zadeh()
191193
// @ts-ignore
192194
candidates: Array<T>
@@ -204,7 +206,7 @@ export class TreeFilterer<T> {
204206
* @param dataKey the key of the object (and its children) which holds the data (defaults to `"data"`)
205207
* @param childrenKey the key of the object (and its children) which hold the children (defaults to `"children"`)
206208
*/
207-
setCandidates(candidates: Array<T>, dataKey: string = "data", childrenKey: string = "children"): void {
209+
setCandidates(candidates: Array<T>, dataKey: string = "data", childrenKey: string = "children") {
208210
this.candidates = candidates
209211
return this.obj.setTreeFiltererCandidates(candidates, dataKey, childrenKey)
210212
}
@@ -218,7 +220,7 @@ export class TreeFilterer<T> {
218220
parseFilterOptions(options)
219221
return this.obj.filterTree(
220222
query,
221-
options.maxResults,
223+
options.maxResults as number /* numberified by parseFilterOptions */,
222224
Boolean(options.usePathScoring),
223225
Boolean(options.useExtensionBonus)
224226
)
@@ -289,11 +291,11 @@ export function match(str: string, query: string, options: IOptions = {}): numbe
289291
console.warn(`Zadeh: bad input to match str: ${str}, query: ${query}`)
290292
return []
291293
}
292-
if (str == query) {
294+
if (str === query) {
293295
return Array.from(Array(str.length).keys())
294296
}
295297
parseOptions(options)
296-
return binding.match(str, query, options.pathSeparator)
298+
return binding.match(str, query, options.pathSeparator as string /* stringified by parseOption */)
297299
}
298300

299301
/*
@@ -312,7 +314,7 @@ export function wrap(str: string, query: string, options: IOptions = {}): string
312314
return []
313315
}
314316
parseOptions(options)
315-
return binding.wrap(str, query, options.pathSeparator)
317+
return binding.wrap(str, query, options.pathSeparator as string /* stringified by parseOption */)
316318
}
317319

318320
/*

test/filter-tree-spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("filterTree", () => {
8989
// answers are os dependant because of slight differences
9090
// console.log(filterTree(outlineData, "text", "plainText", "children"))
9191
it("can search in outline data", () => {
92-
process.platform === "win32" &&
92+
if (process.platform === "win32") {
9393
expect(
9494
DeepEqual(filterTree(outlineData, "text", "plainText", "children"), [
9595
{ data: "text", index: 0, level: 4 },
@@ -189,9 +189,10 @@ describe("filterTree", () => {
189189
{ data: "updateMaxScreenLineLength", index: 29, level: 2 },
190190
])
191191
).toBe(true)
192+
}
192193

193194
// console.log(filterTree(outlineData, "disp", "plainText", "children"))
194-
process.platform !== "linux" &&
195+
if (process.platform !== "linux") {
195196
expect(
196197
DeepEqual(filterTree(outlineData, "disp", "plainText", "children"), [
197198
{ data: "disposable", index: 3, level: 4 },
@@ -218,9 +219,10 @@ describe("filterTree", () => {
218219
{ data: "onDidChangeCursorPosition", index: 58, level: 2 },
219220
])
220221
).toBe(true)
222+
}
221223

222224
// console.log(filterTree(outlineData, "dips", "plainText", "children"))
223-
process.platform !== "linux" &&
225+
if (process.platform !== "linux") {
224226
expect(
225227
DeepEqual(filterTree(outlineData, "dips", "plainText", "children"), [
226228
{ data: "didUpdateStyles", index: 2, level: 3 },
@@ -257,5 +259,6 @@ describe("filterTree", () => {
257259
{ data: "destroyFoldsContainingBufferPositions", index: 373, level: 2 },
258260
])
259261
).toBe(true)
262+
}
260263
})
261264
})

test/tree-filterer-spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("TreeFilterer", function () {
3535
treeFilterer.setCandidates(outlineData, "plainText", "children")
3636

3737
// console.log(treeFilterer.filter("text"))
38-
process.platform === "win32" &&
38+
if (process.platform === "win32") {
3939
expect(
4040
DeepEqual(treeFilterer.filter("text"), [
4141
{ data: "text", index: 0, level: 4 },
@@ -135,9 +135,10 @@ describe("TreeFilterer", function () {
135135
{ data: "updateMaxScreenLineLength", index: 29, level: 2 },
136136
])
137137
).toBe(true)
138+
}
138139

139140
// console.log(treeFilterer.filter("disp"))
140-
process.platform !== "linux" &&
141+
if (process.platform !== "linux") {
141142
expect(
142143
DeepEqual(treeFilterer.filter("disp"), [
143144
{ data: "disposable", index: 3, level: 4 },
@@ -164,9 +165,10 @@ describe("TreeFilterer", function () {
164165
{ data: "onDidChangeCursorPosition", index: 58, level: 2 },
165166
])
166167
).toBe(true)
168+
}
167169

168170
// console.log(treeFilterer.filter("dips"))
169-
process.platform !== "linux" &&
171+
if (process.platform !== "linux") {
170172
expect(
171173
DeepEqual(treeFilterer.filter("dips"), [
172174
{ data: "didUpdateStyles", index: 2, level: 3 },
@@ -203,5 +205,6 @@ describe("TreeFilterer", function () {
203205
{ data: "destroyFoldsContainingBufferPositions", index: 373, level: 2 },
204206
])
205207
).toBe(true)
208+
}
206209
})
207210
})

0 commit comments

Comments
 (0)