diff --git a/fuzzaldrin.js b/fuzzaldrin.js new file mode 100644 index 00000000..c6c4cb63 --- /dev/null +++ b/fuzzaldrin.js @@ -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 {} + }, +} diff --git a/package-lock.json b/package-lock.json index 8ff5429c..1396fb3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "fuzzaldrin-plus-fast", - "version": "0.12.0", + "version": "0.12.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -8223,9 +8223,9 @@ } }, "npm-check-updates": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/npm-check-updates/-/npm-check-updates-8.0.3.tgz", - "integrity": "sha512-KeXs8eflAjV/sh6mHqMg9eOQLYfnvGocJbvbpVMSC0Em/uSqlAqfhBnQ12yAIcL/7RFgGKrzm9g7X02V3gg4XQ==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/npm-check-updates/-/npm-check-updates-8.1.0.tgz", + "integrity": "sha512-cIB3oUkCEzz5kuOXgly8OygBLKacRBWwQJwt+fmSiSj7+jDHBVbdi1EH9O43tqP8XRUhrKIYt5dQroeSwJ/K8g==", "dev": true, "requires": { "chalk": "^4.1.0", diff --git a/package.json b/package.json index dcaed4e6..5a8ae892 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/spec/wrap-spec.js b/spec/wrap-spec.js index 1216fc58..a9053c67 100644 --- a/spec/wrap-spec.js +++ b/spec/wrap-spec.js @@ -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)) - } + }) } - }) + } })