Socket
Socket
Sign inDemoInstall

fuse.js

Package Overview
Dependencies
Maintainers
1
Versions
113
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fuse.js - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

2

bower.json
{
"name": "fuse.js",
"version": "1.0.3",
"version": "1.1.0",
"main": "./src/fuse.js",

@@ -5,0 +5,0 @@ "ignore": [

{
"name": "fuse.js",
"author": "Kirollos Risk",
"version": "1.0.3",
"version": "1.1.0",
"description": "Lightweight fuzzy-search",

@@ -6,0 +6,0 @@ "license": "Apache",

@@ -21,2 +21,3 @@ /**

(function(global) {
/**

@@ -30,3 +31,3 @@ * Adapted from "Diff, Match and Patch", by Google

* Details: the algorithm and structure was modified to allow the creation of
* <Searcher> instances with a <search> method inside which does the actual
* <Searcher> instances with a <search> method which does the actual
* bitap search. The <pattern> (the string that is searched for) is only defined

@@ -39,4 +40,21 @@ * once per instance and thus it eliminates redundant re-creation when searching

*/
var BitapSearcher = function(pattern, options) {
this.options = options || {};
this.options.location = options.location || BitapSearcher.defaultOptions.location;
this.options.distance = 'distance' in options ? options.distance : BitapSearcher.defaultOptions.distance;
this.options.threshold = 'threshold' in options ? options.threshold : BitapSearcher.defaultOptions.threshold;
this.options.maxPatternLength = options.maxPatternLength || BitapSearcher.defaultOptions.maxPatternLength;
var defaultOptions = {
this.pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
this.patternLen = pattern.length;
if (this.patternLen > this.options.maxPatternLength) {
throw new Error('Pattern length is too long');
}
this.matchmask = 1 << (this.patternLen - 1);
this.patternAlphabet = this._calculatePatternAlphabet();
}
BitapSearcher.defaultOptions = {
// Approximately where in the text is the pattern expected to be found?

@@ -57,179 +75,160 @@ location: 0,

// Machine word size
maxPatternLength: 32
maxPatternLength: 32,
};
function Searcher(pattern, options) {
options = options || {};
/**
* Initialize the alphabet for the Bitap algorithm.
* @return {Object} Hash of character locations.
* @private
*/
BitapSearcher.prototype._calculatePatternAlphabet = function() {
var mask = {},
i = 0;
var MATCH_LOCATION = options.location || defaultOptions.location,
MATCH_DISTANCE = 'distance' in options ? options.distance : defaultOptions.distance,
MATCH_THRESHOLD = 'threshold' in options ? options.threshold : defaultOptions.threshold,
MAX_PATTERN_LEN = options.maxPatternLength || defaultOptions.maxPatternLength,
for (i = 0; i < this.patternLen; i++) {
mask[this.pattern.charAt(i)] = 0;
}
pattern = options.caseSensitive ? pattern : pattern.toLowerCase(),
patternLen = pattern.length;
if (patternLen > MAX_PATTERN_LEN) {
throw new Error('Pattern length is too long');
for (i = 0; i < this.patternLen; i++) {
mask[this.pattern.charAt(i)] |= 1 << (this.pattern.length - i - 1);
}
var matchmask = 1 << (patternLen - 1);
return mask;
};
/**
* Initialize the alphabet for the Bitap algorithm.
* @return {Object} Hash of character locations.
* @private
*/
var pattern_alphabet = (function() {
var mask = {},
i = 0;
/**
* Compute and return the score for a match with `e` errors and `x` location.
* @param {number} e Number of errors in match.
* @param {number} x Location of match.
* @return {number} Overall score for match (0.0 = good, 1.0 = bad).
* @private
*/
BitapSearcher.prototype._bitapScore = function(errors, location) {
var accuracy = errors / this.patternLen,
proximity = Math.abs(this.options.location - location);
for (i = 0; i < patternLen; i++) {
mask[pattern.charAt(i)] = 0;
}
if (!this.options.distance) {
// Dodge divide by zero error.
return proximity ? 1.0 : accuracy;
}
return accuracy + (proximity / this.options.distance);
};
for (i = 0; i < patternLen; i++) {
mask[pattern.charAt(i)] |= 1 << (pattern.length - i - 1);
}
/**
* Compute and return the result of the search
* @param {String} text The text to search in
* @return {Object} Literal containing:
* {Boolean} isMatch Whether the text is a match or not
* {Decimal} score Overall score for the match
* @public
*/
BitapSearcher.prototype.search = function(text) {
text = this.options.caseSensitive ? text : text.toLowerCase();
return mask;
})();
/**
* Compute and return the score for a match with `e` errors and `x` location.
* @param {number} e Number of errors in match.
* @param {number} x Location of match.
* @return {number} Overall score for match (0.0 = good, 1.0 = bad).
* @private
*/
function match_bitapScore(e, x) {
var accuracy = e / patternLen,
proximity = Math.abs(MATCH_LOCATION - x);
if (!MATCH_DISTANCE) {
// Dodge divide by zero error.
return proximity ? 1.0 : accuracy;
}
return accuracy + (proximity / MATCH_DISTANCE);
if (this.pattern === text) {
// Exact match
return {
isMatch: true,
score: 0
};
}
/**
* Compute and return the result of the search
* @param {String} text The text to search in
* @return
* {Object} Literal containing:
* {Boolean} isMatch Whether the text is a match or not
* {Decimal} score Overall score for the match
* @public
*/
this.search = function(text) {
text = options.caseSensitive ? text : text.toLowerCase();
var i, j,
// Set starting location at beginning text and initialize the alphabet.
textLen = text.length,
LOCATION = this.options.location,
// Highest score beyond which we give up.
THRESHOLD = this.options.threshold,
// Is there a nearby exact match? (speedup)
bestLoc = text.indexOf(this.pattern, LOCATION),
binMin, binMid,
binMax = this.patternLen + textLen,
start, finish,
bitArr, lastBitArr,
charMatch,
score = 1,
locations = []
if (pattern === text) {
// Exact match
return {
isMatch: true,
score: 0
};
}
if (bestLoc != -1) {
THRESHOLD = Math.min(this._bitapScore(0, bestLoc), THRESHOLD);
// What about in the other direction? (speedup)
bestLoc = text.lastIndexOf(this.pattern, LOCATION + this.patternLen);
var i, j,
// Set starting location at beginning text and initialize the alphabet.
textLen = text.length,
// Highest score beyond which we give up.
scoreThreshold = MATCH_THRESHOLD,
// Is there a nearby exact match? (speedup)
bestLoc = text.indexOf(pattern, MATCH_LOCATION),
binMin, binMid,
binMax = patternLen + textLen,
start, finish,
bitArr, lastBitArr,
charMatch,
score = 1,
locations = [];
if (bestLoc != -1) {
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
// What about in the other direction? (speedup)
bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
if (bestLoc != -1) {
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
}
THRESHOLD = Math.min(this._bitapScore(0, bestLoc), THRESHOLD);
}
}
bestLoc = -1;
bestLoc = -1;
for (i = 0; i < patternLen; i++) {
// Scan for the best match; each iteration allows for one more error.
// Run a binary search to determine how far from 'MATCH_LOCATION' we can stray at this
// error level.
binMin = 0;
binMid = binMax;
while (binMin < binMid) {
if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
binMin = binMid;
} else {
binMax = binMid;
}
binMid = Math.floor((binMax - binMin) / 2 + binMin);
for (i = 0; i < this.patternLen; i++) {
// Scan for the best match; each iteration allows for one more error.
// Run a binary search to determine how far from 'MATCH_LOCATION' we can stray at this
// error level.
binMin = 0;
binMid = binMax;
while (binMin < binMid) {
if (this._bitapScore(i, LOCATION + binMid) <= THRESHOLD) {
binMin = binMid;
} else {
binMax = binMid;
}
binMid = Math.floor((binMax - binMin) / 2 + binMin);
}
// Use the result from this iteration as the maximum for the next.
binMax = binMid;
start = Math.max(1, MATCH_LOCATION - binMid + 1);
finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
// Use the result from this iteration as the maximum for the next.
binMax = binMid;
start = Math.max(1, LOCATION - binMid + 1);
finish = Math.min(LOCATION + binMid, textLen) + this.patternLen;
// Initialize the bit array
bitArr = Array(finish + 2);
// Initialize the bit array
bitArr = Array(finish + 2);
bitArr[finish + 1] = (1 << i) - 1;
bitArr[finish + 1] = (1 << i) - 1;
for (j = finish; j >= start; j--) {
// The alphabet <pattern_alphabet> is a sparse hash, so the following line generates warnings.
charMatch = pattern_alphabet[text.charAt(j - 1)];
for (j = finish; j >= start; j--) {
// The alphabet <patternAlphabet> is a sparse hash, so the following line generates warnings.
charMatch = this.patternAlphabet[text.charAt(j - 1)];
if (i === 0) {
// First pass: exact match.
bitArr[j] = ((bitArr[j + 1] << 1) | 1) & charMatch;
} else {
// Subsequent passes: fuzzy match.
bitArr[j] = ((bitArr[j + 1] << 1) | 1) & charMatch | (((lastBitArr[j + 1] | lastBitArr[j]) << 1) | 1) | lastBitArr[j + 1];
}
if (bitArr[j] & matchmask) {
score = match_bitapScore(i, j - 1);
// This match will almost certainly be better than any existing match.
// But check anyway.
if (score <= scoreThreshold) {
// Told you so.
scoreThreshold = score;
bestLoc = j - 1;
locations.push(bestLoc);
if (i === 0) {
// First pass: exact match.
bitArr[j] = ((bitArr[j + 1] << 1) | 1) & charMatch;
} else {
// Subsequent passes: fuzzy match.
bitArr[j] = ((bitArr[j + 1] << 1) | 1) & charMatch | (((lastBitArr[j + 1] | lastBitArr[j]) << 1) | 1) | lastBitArr[j + 1];
}
if (bitArr[j] & this.matchmask) {
score = this._bitapScore(i, j - 1);
// This match will almost certainly be better than any existing match.
// But check anyway.
if (score <= THRESHOLD) {
// Told you so.
THRESHOLD = score;
bestLoc = j - 1;
locations.push(bestLoc);
if (bestLoc > MATCH_LOCATION) {
// When passing loc, don't exceed our current distance from loc.
start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
} else {
// Already passed loc, downhill from here on in.
break;
}
if (bestLoc > LOCATION) {
// When passing loc, don't exceed our current distance from loc.
start = Math.max(1, 2 * LOCATION - bestLoc);
} else {
// Already passed loc, downhill from here on in.
break;
}
}
}
}
// No hope for a (better) match at greater error levels.
if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
break;
}
lastBitArr = bitArr;
// No hope for a (better) match at greater error levels.
if (this._bitapScore(i + 1, LOCATION) > THRESHOLD) {
break;
}
lastBitArr = bitArr;
}
return {
isMatch: bestLoc >= 0,
score: score
};
}
return {
isMatch: bestLoc >= 0,
score: score
};
}
var Utils = {

@@ -259,95 +258,150 @@ /**

function Fuse(list, options) {
options = options || {};
var searchKeys = options.keys || [];
this.list = list;
this.options = options = options || {};
this.options.sort = 'sort' in options ? options.sort : Fuse.defaultOptions.sort,
this.options.includeScore = 'includeScore' in options ? options.includeScore : Fuse.defaultOptions.includeScore,
this.options.searchFn = options.searchFn || Fuse.defaultOptions.searchFn,
this.options.sortFn = options.sortFn || Fuse.defaultOptions.sortFn,
this.options.keys = options.keys || Fuse.defaultOptions.keys;
};
Fuse.defaultOptions = {
id: null,
// Whether the score should be included in the result set.
// When <true>, each result in the list will be of the form: `{ item: ..., score: ... }`
includeScore: false,
// Whether to sort the result list, by score
shouldSort: true,
// The search function to use
// Note that the default search function ([[Function]]) must conform the following API:
//
// @param pattern The pattern string to search
// @param options The search option
// [[Function]].constructor = function(pattern, options)
//
// @param text: the string to search in for the pattern
// @return Object in the form of:
// - isMatch: boolean
// - score: Int
// [[Function]].prototype.search = function(text)
searchFn: BitapSearcher,
// Default sort function
sortFn: function(a, b) {
return a.score - b.score;
},
keys: []
};
/**
* Searches for all the items whose keys (fuzzy) match the pattern.
* @param {String} pattern The pattern string to fuzzy search on.
* @return {Array} A list of all serch matches.
* @public
*/
Fuse.prototype.search = function(pattern) {
var searcher = new(this.options.searchFn)(pattern, this.options),
i, j, item, text,
list = this.list,
dataLen = list.length,
searchKeys = this.options.keys,
searchKeysLen = searchKeys.length,
bitapResult,
rawResults = [],
resultMap = {},
existingResult,
results = [];
/**
* Searches for all the items whose keys (fuzzy) match the pattern.
* @param {String} pattern The pattern string to fuzzy search on.
* @return {Array} A list of all serch matches.
* @public
* Calls <Searcher::search> for bitap analysis. Builds the raw result list.
* @param {String} text The pattern string to fuzzy search on.
* @param {String|Int} entity If the <data> is an Array, then entity will be an index,
* otherwise it's the item object.
* @param {Int} index
* @return {Object|Int}
* @private
*/
this.search = function(pattern) {
var searcher = new Searcher(pattern, options),
i, j, item, text,
dataLen = list.length,
searchKeysLen = searchKeys.length,
bitapResult, rawResults = [],
index = 0,
resultMap = {},
rawResultsLen, existingResult, results = [];
var analyzeText = function(text, entity, index) {
// Check if the text can be searched
if (text !== undefined && text !== null && typeof text === 'string') {
/**
* Calls <Searcher::search> for bitap analysis. Builds the raw result list.
* @param {String} text The pattern string to fuzzy search on.
* @param {String|Int} entity If the <data> is an Array, then entity will be an index,
* otherwise it's the item object.
* @param {Int} index
* @return {Object|Int}
* @private
*/
function analyzeText(text, entity, index) {
// Check if the text can be searched
if (text !== undefined && text !== null && typeof text === 'string') {
// Get the result
bitapResult = searcher.search(text);
// Get the result
bitapResult = searcher.search(text);
// If a match is found, add the item to <rawResults>, including its score
if (bitapResult.isMatch) {
// If a match is found, add the item to <rawResults>, including its score
if (bitapResult.isMatch) {
// Check if the item already exists in our results
existingResult = resultMap[index];
if (existingResult) {
// Use the lowest score
existingResult.score = Math.min(existingResult.score, bitapResult.score);
} else {
// Add it to the raw result list
resultMap[index] = {
item: entity,
score: bitapResult.score
};
rawResults.push(resultMap[index]);
}
// Check if the item already exists in our results
existingResult = resultMap[index];
if (existingResult) {
// Use the lowest score
existingResult.score = Math.min(existingResult.score, bitapResult.score);
} else {
// Add it to the raw result list
resultMap[index] = {
item: entity,
score: bitapResult.score
};
rawResults.push(resultMap[index]);
}
}
}
};
// Check the first item in the list, if it's a string, then we assume
// that every item in the list is also a string, and thus it's a flattened array.
if (typeof list[0] === 'string') {
// Iterate over every item
for (; index < dataLen; index++) {
analyzeText(list[index], index, index);
}
} else {
// Otherwise, the first item is an Object (hopefully), and thus the searching
// is done on the values of the keys of each item.
// Check the first item in the list, if it's a string, then we assume
// that every item in the list is also a string, and thus it's a flattened array.
if (typeof list[0] === 'string') {
// Iterate over every item
for (var i = 0; i < dataLen; i++) {
analyzeText(list[i], i, i);
}
} else {
// Otherwise, the first item is an Object (hopefully), and thus the searching
// is done on the values of the keys of each item.
// Iterate over every item
for (; index < dataLen; index++) {
item = list[index];
// Iterate over every key
for (j = 0; j < searchKeysLen; j++) {
analyzeText(Utils.deepValue(item, searchKeys[j]), item, index);
}
// Iterate over every item
for (var i = 0; i < dataLen; i++) {
item = list[i];
// Iterate over every key
for (j = 0; j < searchKeysLen; j++) {
analyzeText(Utils.deepValue(item, searchKeys[j]), item, i);
}
}
}
// Sort the results, form lowest to highest score
rawResults.sort(function(a, b) {
return a.score - b.score;
});
if (this.options.shouldSort) {
rawResults.sort(this.options.sortFn);
}
// From the results, push into a new array only the item identifier (if specified)
// of the entire item. This is because we don't want to return the <rawResults>,
// since it contains other metadata;
rawResultsLen = rawResults.length;
for (i = 0; i < rawResultsLen; i++) {
results.push(options.id ? Utils.deepValue(rawResults[i].item, options.id) : rawResults[i].item);
}
// Helper function, here for speed-up, which returns the
// the raw item, including the score, or simply the item itself, depending
// on the specified option
var getItem = this.options.includeScore ? function(i) {
return rawResults[i];
} : function(i) {
return rawResults[i].item;
};
return results;
// Helper function, here for speed-up, which returns the idenfifer (via deepValue),
// if the options specifies it,
var getValue = this.options.id ? function(i) {
return Utils.deepValue(getItem(i), options.id);
} : function(i) {
return getItem(i);
};
// From the results, push into a new array only the item identifier (if specified)
// of the entire item. This is because we don't want to return the <rawResults>,
// since it contains other metadata;
for (var i = 0, len = rawResults.length; i < len; i++) {
results.push(getValue(i));
}
}
return results;
};
// Export to Common JS Loader

@@ -354,0 +408,0 @@ if (typeof exports === 'object') {

@@ -20,2 +20,2 @@ /**

*/
!function(e){function t(e,t){function r(e,t){var r=e/h,n=Math.abs(o-t);return i?r+n/i:n?1:r}t=t||{};var o=t.location||n.location,i="distance"in t?t.distance:n.distance,a="threshold"in t?t.threshold:n.threshold,s=t.maxPatternLength||n.maxPatternLength,e=t.caseSensitive?e:e.toLowerCase(),h=e.length;if(h>s)throw new Error("Pattern length is too long");var c=1<<h-1,f=function(){var t={},r=0;for(r=0;h>r;r++)t[e.charAt(r)]=0;for(r=0;h>r;r++)t[e.charAt(r)]|=1<<e.length-r-1;return t}();this.search=function(n){if(n=t.caseSensitive?n:n.toLowerCase(),e===n)return{isMatch:!0,score:0};var i,s,u,l,d,g,m,p,v,M=n.length,x=a,y=n.indexOf(e,o),w=h+M,L=1,b=[];for(-1!=y&&(x=Math.min(r(0,y),x),y=n.lastIndexOf(e,o+h),-1!=y&&(x=Math.min(r(0,y),x))),y=-1,i=0;h>i;i++){for(u=0,l=w;l>u;)r(i,o+l)<=x?u=l:w=l,l=Math.floor((w-u)/2+u);for(w=l,d=Math.max(1,o-l+1),g=Math.min(o+l,M)+h,m=Array(g+2),m[g+1]=(1<<i)-1,s=g;s>=d;s--)if(v=f[n.charAt(s-1)],m[s]=0===i?(m[s+1]<<1|1)&v:(m[s+1]<<1|1)&v|((p[s+1]|p[s])<<1|1)|p[s+1],m[s]&c&&(L=r(i,s-1),x>=L)){if(x=L,y=s-1,b.push(y),!(y>o))break;d=Math.max(1,2*o-y)}if(r(i+1,o)>x)break;p=m}return{isMatch:y>=0,score:L}}}function r(e,r){r=r||{};var n=r.keys||[];this.search=function(i){function a(e,t,r){void 0!==e&&null!==e&&"string"==typeof e&&(f=d.search(e),f.isMatch&&(l=M[r],l?l.score=Math.min(l.score,f.score):(M[r]={item:t,score:f.score},p.push(M[r]))))}var s,h,c,f,u,l,d=new t(i,r),g=e.length,m=n.length,p=[],v=0,M={},x=[];if("string"==typeof e[0])for(;g>v;v++)a(e[v],v,v);else for(;g>v;v++)for(c=e[v],h=0;m>h;h++)a(o.deepValue(c,n[h]),c,v);for(p.sort(function(e,t){return e.score-t.score}),u=p.length,s=0;u>s;s++)x.push(r.id?o.deepValue(p[s].item,r.id):p[s].item);return x}}var n={location:0,distance:100,threshold:.6,maxPatternLength:32},o={deepValue:function(e,t){for(var r=0,t=t.split("."),n=t.length;n>r;r++){if(!e)return null;e=e[t[r]]}return e}};"object"==typeof exports?module.exports=r:"function"==typeof define&&define.amd?define(function(){return r}):e.Fuse=r}(this);
!function(t){function n(t,e){this.list=t,this.options=e=e||{},this.options.sort="sort"in e?e.sort:n.defaultOptions.sort,this.options.includeScore="includeScore"in e?e.includeScore:n.defaultOptions.includeScore,this.options.searchFn=e.searchFn||n.defaultOptions.searchFn,this.options.sortFn=e.sortFn||n.defaultOptions.sortFn,this.options.keys=e.keys||n.defaultOptions.keys}var e=function(t,n){if(this.options=n||{},this.options.location=n.location||e.defaultOptions.location,this.options.distance="distance"in n?n.distance:e.defaultOptions.distance,this.options.threshold="threshold"in n?n.threshold:e.defaultOptions.threshold,this.options.maxPatternLength=n.maxPatternLength||e.defaultOptions.maxPatternLength,this.pattern=n.caseSensitive?t:t.toLowerCase(),this.patternLen=t.length,this.patternLen>this.options.maxPatternLength)throw new Error("Pattern length is too long");this.matchmask=1<<this.patternLen-1,this.patternAlphabet=this._calculatePatternAlphabet()};e.defaultOptions={location:0,distance:100,threshold:.6,maxPatternLength:32},e.prototype._calculatePatternAlphabet=function(){var t={},n=0;for(n=0;n<this.patternLen;n++)t[this.pattern.charAt(n)]=0;for(n=0;n<this.patternLen;n++)t[this.pattern.charAt(n)]|=1<<this.pattern.length-n-1;return t},e.prototype._bitapScore=function(t,n){var e=t/this.patternLen,i=Math.abs(this.options.location-n);return this.options.distance?e+i/this.options.distance:i?1:e},e.prototype.search=function(t){if(t=this.options.caseSensitive?t:t.toLowerCase(),this.pattern===t)return{isMatch:!0,score:0};var n,e,i,o,s,r,a,h,p,c=t.length,l=this.options.location,u=this.options.threshold,f=t.indexOf(this.pattern,l),d=this.patternLen+c,m=1,L=[];for(-1!=f&&(u=Math.min(this._bitapScore(0,f),u),f=t.lastIndexOf(this.pattern,l+this.patternLen),-1!=f&&(u=Math.min(this._bitapScore(0,f),u))),f=-1,n=0;n<this.patternLen;n++){for(i=0,o=d;o>i;)this._bitapScore(n,l+o)<=u?i=o:d=o,o=Math.floor((d-i)/2+i);for(d=o,s=Math.max(1,l-o+1),r=Math.min(l+o,c)+this.patternLen,a=Array(r+2),a[r+1]=(1<<n)-1,e=r;e>=s;e--)if(p=this.patternAlphabet[t.charAt(e-1)],a[e]=0===n?(a[e+1]<<1|1)&p:(a[e+1]<<1|1)&p|((h[e+1]|h[e])<<1|1)|h[e+1],a[e]&this.matchmask&&(m=this._bitapScore(n,e-1),u>=m)){if(u=m,f=e-1,L.push(f),!(f>l))break;s=Math.max(1,2*l-f)}if(this._bitapScore(n+1,l)>u)break;h=a}return{isMatch:f>=0,score:m}};var i={deepValue:function(t,n){for(var e=0,n=n.split("."),i=n.length;i>e;e++){if(!t)return null;t=t[n[e]]}return t}};n.defaultOptions={id:null,includeScore:!1,shouldSort:!0,searchFn:e,sortFn:function(t,n){return t.score-n.score},keys:[]},n.prototype.search=function(t){var n,e,o,s,r,a=new this.options.searchFn(t,this.options),h=this.list,p=h.length,c=this.options.keys,l=c.length,u=[],f={},d=[],m=function(t,n,e){void 0!==t&&null!==t&&"string"==typeof t&&(s=a.search(t),s.isMatch&&(r=f[e],r?r.score=Math.min(r.score,s.score):(f[e]={item:n,score:s.score},u.push(f[e]))))};if("string"==typeof h[0])for(var n=0;p>n;n++)m(h[n],n,n);else for(var n=0;p>n;n++)for(o=h[n],e=0;l>e;e++)m(i.deepValue(o,c[e]),o,n);this.options.shouldSort&&u.sort(this.options.sortFn);for(var L=this.options.includeScore?function(t){return u[t]}:function(t){return u[t].item},g=this.options.id?function(t){return i.deepValue(L(t),options.id)}:function(t){return L(t)},n=0,S=u.length;S>n;n++)d.push(g(n));return d},"object"==typeof exports?module.exports=n:"function"==typeof define&&define.amd?define(function(){return n}):t.Fuse=n}(this);

@@ -157,2 +157,42 @@ var assert = require('assert'),

}
}).export(module);
vows.describe('Include score in result list: ["Apple", "Orange", "Banana"]').addBatch({
'Options:': {
topic: function() {
var fruits = ["Apple", "Orange", "Banana"];
var fuse = new Fuse(fruits, {
includeScore: true
});
return fuse;
},
'When searching for the term "Apple"': {
topic: function(fuse) {
var result = fuse.search("Apple");
return result;
},
'we get a list of containing 1 item, which is an exact match': function(result) {
assert.equal(result.length, 1);
},
'whose value and score exist': function(result) {
assert.equal(result[0].item, 0);
assert.equal(result[0].score, 0);
},
},
'When performing a fuzzy search for the term "ran"': {
topic: function(fuse) {
var result = fuse.search("ran");
return result;
},
'we get a list of containing 2 items': function(result) {
assert.equal(result.length, 2);
},
'whose items represent the indices, and have non-zero scores': function(result) {
assert.equal(result[0].item, 1);
assert.equal(result[1].item, 2);
assert.isNotZero(result[0].score);
assert.isNotZero(result[1].score);
},
}
}
}).export(module);
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