Skip to content

Commit f1ee87c

Browse files
authored
Convert to JS (#23)
The main coffeescript file is converted to JS
1 parent 204b0f4 commit f1ee87c

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

fuzzaldrin.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const binding = require('node-gyp-build')(__dirname)
2+
3+
const defaultPathSeparator = process.platform === "win32" ? '\\' : '/'
4+
5+
function parseOptions(options, query) {
6+
// options.allowErrors ? = false
7+
if (options.usePathScoring == undefined)
8+
options.usePathScoring = true
9+
// options.useExtensionBonus ? = false
10+
if (!options.pathSeparator)
11+
options.pathSeparator = defaultPathSeparator
12+
// options.optCharRegEx ? = null
13+
// options.wrap ? = null
14+
if (!options.maxResults)
15+
options.maxResults = 0
16+
return options
17+
}
18+
19+
class FuzzaldrinPlusFast {
20+
constructor() {
21+
this.obj = new binding.Fuzzaldrin()
22+
}
23+
24+
setCandidates(candidates, options = {}) {
25+
this.candidates = candidates
26+
if (options.key)
27+
candidates = candidates.map((item) => item[options.key])
28+
return this.obj.setCandidates(candidates)
29+
}
30+
31+
filter(query, options = {}) {
32+
options = parseOptions(options)
33+
const res = this.obj.filter(query, options.maxResults,
34+
Boolean(options.usePathScoring), Boolean(options.useExtensionBonus))
35+
return res.map((ind) => this.candidates[ind])
36+
}
37+
}
38+
39+
module.exports = {
40+
New: () => new FuzzaldrinPlusFast(),
41+
42+
filter: (candidates, query, options = {}) => {
43+
if (!candidates || !query)
44+
return []
45+
const obj = new FuzzaldrinPlusFast()
46+
obj.setCandidates(candidates, options)
47+
return obj.filter(query, options)
48+
},
49+
50+
score: (candidate, query, options = {}) => {
51+
if (!candidate || !query)
52+
return 0
53+
options = parseOptions(options)
54+
return binding.score(candidate, query,
55+
Boolean(options.usePathScoring), Boolean(options.useExtensionBonus))
56+
},
57+
58+
match: (string, query, options = {}) => {
59+
if (!string || !query)
60+
return []
61+
if (string == query)
62+
return Array.from(Array(string.length).keys())
63+
options = parseOptions(options, query)
64+
return binding.match(string, query, options.pathSeparator)
65+
},
66+
67+
wrap: (string, query, options = {}) => {
68+
if (!string || !query)
69+
return []
70+
options = parseOptions(options, query)
71+
return binding.wrap(string, query, options.pathSeparator)
72+
},
73+
74+
prepareQuery: (query, options = {}) => {
75+
// This is no - op since there is no major benefit by precomputing something
76+
// just for the query.
77+
return {}
78+
},
79+
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fuzzaldrin-plus-fast",
3-
"version": "0.12.0",
3+
"version": "0.12.5",
44
"description": "Fuzzaldrin plus - fast using native c bindings",
55
"main": "fuzzaldrin-dist.js",
66
"scripts": {
@@ -9,8 +9,8 @@
99
"native:prebuild": "prebuildify --napi --electron-compat --strip",
1010
"native:prebuild-ia32": "prebuildify --arch=ia32 --napi --electron-compat --strip",
1111
"js:clean": "shx rm -rf dist .parcel-cache",
12-
"js:dev": "cross-env NODE_ENV=development parcel watch --target main fuzzaldrin.coffee",
13-
"js:build": "cross-env NODE_ENV=production parcel build --target main fuzzaldrin.coffee",
12+
"js:dev": "cross-env NODE_ENV=development parcel watch --target main fuzzaldrin.js",
13+
"js:build": "cross-env NODE_ENV=production parcel build --target main fuzzaldrin.js",
1414
"clean": "npm run native:clean && npm run js:clean",
1515
"install": "node-gyp-build",
1616
"build": "npm run native:build && npm run js:build",

spec/wrap-spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ describe("wrap(string, query)", () => {
1313
const queries = [
1414
"he", "hl", "hw", "el", "eo", "ll", "wo", "ld", "", "helloworld",
1515
]
16-
it("returns same for hello world", () => {
17-
for (const c of candidates) {
18-
for (const q of queries) {
16+
for (const c of candidates) {
17+
for (const q of queries) {
18+
it("returns same for " + c, () => {
1919
expect(wrap(c, q)).toEqual(legacy.wrap(c, q))
20-
}
20+
})
2121
}
22-
})
22+
}
2323
})

0 commit comments

Comments
 (0)