Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-fuzzy

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-fuzzy - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

test.js

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc