fast-fuzzy
Advanced tools
Comparing version 1.0.0 to 1.1.0
51
fuzzy.js
const nonWordRegex = /[`~!@#$%^&*()\-=_+{}[\]\|\\;':",./<>?]+/g; | ||
const whiteSpaceRegex = /\s/g; | ||
const whitespaceRegex = /\s+/g; | ||
//normalize a string: comparisons should ignore case, non-word characters, and whitespace differences | ||
function normalize(string) { | ||
return string.toLowerCase().replace(nonWordRegex, "").replace(whiteSpaceRegex, " ").trim(); | ||
const defaultOptions = { | ||
keySelector: (_) => _, | ||
threshold: .6, | ||
ignoreCase: true, | ||
ignoreSymbols: true, | ||
normalizeWhitespace: true, | ||
}; | ||
//normalize a string according to the options passed in | ||
function normalize(string, options) { | ||
if (options.ignoreCase) { | ||
string = string.toLowerCase(); | ||
} | ||
if (options.ignoreSymbols) { | ||
string = string.replace(nonWordRegex, ""); | ||
} | ||
if (options.normalizeWhitespace) { | ||
string = string.replace(whitespaceRegex, " ").trim(); | ||
} | ||
return string; | ||
} | ||
@@ -33,6 +50,6 @@ | ||
//it also expects candidates in the form {item: any, key: string} | ||
function searchCore(term, candidates) { | ||
function searchCore(term, candidates, threshold) { | ||
return candidates.map((candidate) => { | ||
return {item: candidate.item, key: candidate.key, score: sellers(term, candidate.key)}; | ||
}).filter((candidate) => candidate.score >= .6).sort((a, b) => { | ||
}).filter((candidate) => candidate.score >= threshold).sort((a, b) => { | ||
if (a.score === b.score) return a.key.length - b.key.length; | ||
@@ -45,9 +62,10 @@ return b.score - a.score; | ||
//the keySelector is used to pick a string from an object to search by | ||
function createSearchItems(items, keySelector = (_) => _) { | ||
return items.map((item) => ({item, key: normalize(keySelector(item))})); | ||
function createSearchItems(items, options) { | ||
return items.map((item) => ({item, key: normalize(options.keySelector(item), options)})); | ||
} | ||
//simple one-off search. Useful if you don't expect to use the same candidate list again | ||
function search(term, candidates, keySelector) { | ||
return searchCore(normalize(term), createSearchItems(candidates, keySelector)); | ||
function search(term, candidates, options) { | ||
options = Object.assign({}, defaultOptions, options); | ||
return searchCore(normalize(term, options), createSearchItems(candidates, options), options.threshold); | ||
} | ||
@@ -58,4 +76,4 @@ | ||
class Searcher { | ||
constructor({candidates, keySelector}) { | ||
this.keySelector = keySelector; | ||
constructor(candidates, options) { | ||
this.options = Object.assign({}, defaultOptions, options); | ||
this.candidates = []; | ||
@@ -65,6 +83,9 @@ this.add(...candidates); | ||
add(...candidates) { | ||
this.candidates.push(...createSearchItems(candidates, this.keySelector)); | ||
this.candidates.push(...createSearchItems(candidates, this.options)); | ||
} | ||
search(term) { | ||
return searchCore(normalize(term), this.candidates); | ||
search(term, threshold) { | ||
if (threshold == null) { | ||
threshold = this.options.threshold; | ||
} | ||
return searchCore(normalize(term, this.options), this.candidates, threshold); | ||
} | ||
@@ -71,0 +92,0 @@ } |
{ | ||
"name": "fast-fuzzy", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Fast and tiny fuzzy-search utility", | ||
"main": "fuzzy.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test.js" | ||
}, | ||
@@ -22,3 +22,6 @@ "keywords": [ | ||
}, | ||
"homepage": "https://github.com/EthanRutherford/fast-fuzzy#readme" | ||
"homepage": "https://github.com/EthanRutherford/fast-fuzzy#readme", | ||
"devDependencies": { | ||
"mocha": "^3.2.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10389
6
225
0
1
1