-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The main coffeescript file is converted to JS
- Loading branch information
1 parent
204b0f4
commit f1ee87c
Showing
4 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
}, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters