Skip to content

Commit

Permalink
Convert to JS (#23)
Browse files Browse the repository at this point in the history
The main coffeescript file is converted to JS
  • Loading branch information
rajendrant authored Sep 5, 2020
1 parent 204b0f4 commit f1ee87c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
79 changes: 79 additions & 0 deletions fuzzaldrin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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
}

class FuzzaldrinPlusFast {
constructor() {
this.obj = new binding.Fuzzaldrin()
}

setCandidates(candidates, options = {}) {
this.candidates = candidates
if (options.key)
candidates = candidates.map((item) => item[options.key])
return this.obj.setCandidates(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])
}
}

module.exports = {
New: () => new FuzzaldrinPlusFast(),

filter: (candidates, query, options = {}) => {
if (!candidates || !query)
return []
const obj = new FuzzaldrinPlusFast()
obj.setCandidates(candidates, options)
return obj.filter(query, options)
},

score: (candidate, query, options = {}) => {
if (!candidate || !query)
return 0
options = parseOptions(options)
return binding.score(candidate, query,
Boolean(options.usePathScoring), Boolean(options.useExtensionBonus))
},

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)
},

wrap: (string, query, options = {}) => {
if (!string || !query)
return []
options = parseOptions(options, query)
return binding.wrap(string, query, options.pathSeparator)
},

prepareQuery: (query, options = {}) => {
// This is no - op since there is no major benefit by precomputing something
// just for the query.
return {}
},
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuzzaldrin-plus-fast",
"version": "0.12.0",
"version": "0.12.5",
"description": "Fuzzaldrin plus - fast using native c bindings",
"main": "fuzzaldrin-dist.js",
"scripts": {
Expand All @@ -9,8 +9,8 @@
"native:prebuild": "prebuildify --napi --electron-compat --strip",
"native:prebuild-ia32": "prebuildify --arch=ia32 --napi --electron-compat --strip",
"js:clean": "shx rm -rf dist .parcel-cache",
"js:dev": "cross-env NODE_ENV=development parcel watch --target main fuzzaldrin.coffee",
"js:build": "cross-env NODE_ENV=production parcel build --target main fuzzaldrin.coffee",
"js:dev": "cross-env NODE_ENV=development parcel watch --target main fuzzaldrin.js",
"js:build": "cross-env NODE_ENV=production parcel build --target main fuzzaldrin.js",
"clean": "npm run native:clean && npm run js:clean",
"install": "node-gyp-build",
"build": "npm run native:build && npm run js:build",
Expand Down
10 changes: 5 additions & 5 deletions spec/wrap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe("wrap(string, query)", () => {
const queries = [
"he", "hl", "hw", "el", "eo", "ll", "wo", "ld", "", "helloworld",
]
it("returns same for hello world", () => {
for (const c of candidates) {
for (const q of queries) {
for (const c of candidates) {
for (const q of queries) {
it("returns same for " + c, () => {
expect(wrap(c, q)).toEqual(legacy.wrap(c, q))
}
})
}
})
}
})

0 comments on commit f1ee87c

Please sign in to comment.