From 1fb86f0ebd8bba374d81ef4b8ffa8a296cd7898e Mon Sep 17 00:00:00 2001 From: Kirollos Risk Date: Sun, 8 Mar 2020 20:37:56 -0700 Subject: [PATCH] Build version 3.6.0 --- dist/fuse.d.ts | 2 +- dist/fuse.js | 4 +- package.json | 2 +- src/index.js | 100 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 79 insertions(+), 29 deletions(-) diff --git a/dist/fuse.d.ts b/dist/fuse.d.ts index 316a533fc..426491311 100644 --- a/dist/fuse.d.ts +++ b/dist/fuse.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Fuse.js v3.5.0 +// Type definitions for Fuse.js v3.6.0 // TypeScript Version: 3.1 export = Fuse; diff --git a/dist/fuse.js b/dist/fuse.js index 1c0690a68..8380327b9 100644 --- a/dist/fuse.js +++ b/dist/fuse.js @@ -1,9 +1,9 @@ /*! - * Fuse.js v3.5.0 - Lightweight fuzzy-search (http://fusejs.io) + * Fuse.js v3.6.0 - Lightweight fuzzy-search (http://fusejs.io) * * Copyright (c) 2012-2017 Kirollos Risk (http://kiro.me) * All Rights Reserved. Apache Software License 2.0 * * http://www.apache.org/licenses/LICENSE-2.0 */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("Fuse",[],t):"object"==typeof exports?exports.Fuse=t():e.Fuse=t()}(this,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){for(var n=0;n1&&void 0!==arguments[1]?arguments[1]:{limit:!1};this._log('---------\nSearch pattern: "'.concat(e,'"'));var n=this._prepareSearchers(e),r=n.tokenSearchers,o=n.fullSearcher,i=this._search(r,o),a=i.weights,s=i.results;return this._computeScore(a,s),this.options.shouldSort&&this._sort(s),t.limit&&"number"==typeof t.limit&&(s=s.slice(0,t.limit)),this._format(s)}},{key:"_prepareSearchers",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=[];if(this.options.tokenize)for(var n=e.split(this.options.tokenSeparator),r=0,o=n.length;r0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1?arguments[1]:void 0,n=this.list,r={},o=[];if("string"==typeof n[0]){for(var i=0,a=n.length;i1)throw new Error("Key weight has to be > 0 and <= 1");v=v.name}else s[v]=1;this._analyze({key:v,value:this.options.getFn(h,v),record:h,index:c},{resultMap:r,results:o,tokenSearchers:e,fullSearcher:t})}return{weights:s,results:o}}},{key:"_analyze",value:function(e,t){var n=this,r=e.key,o=e.arrayIndex,i=void 0===o?-1:o,a=e.value,s=e.record,l=e.index,h=t.tokenSearchers,u=void 0===h?[]:h,f=t.fullSearcher,v=t.resultMap,d=void 0===v?{}:v,p=t.results,g=void 0===p?[]:p;!function e(t,o,i,a){if(null!=o)if("string"==typeof o){var s=!1,l=-1,h=0;n._log("\nKey: ".concat(""===r?"--":r));var v=f.search(o);if(n._log('Full text: "'.concat(o,'", score: ').concat(v.score)),n.options.tokenize){for(var p=o.split(n.options.tokenSeparator),y=p.length,m=[],k=0,S=u.length;k-1&&(O=(O+l)/2),n._log("Score average:",O);var j=!n.options.tokenize||!n.options.matchAllTokens||h>=u.length;if(n._log("\nCheck Matches: ".concat(j)),(s||v.isMatch)&&j){var I={key:r,arrayIndex:t,value:o,score:O};n.options.includeMatches&&(I.matchedIndices=v.matchedIndices);var P=d[a];P?P.output.push(I):(d[a]={item:i,output:[I]},g.push(d[a]))}}else if(c(o))for(var F=0,T=o.length;F0?Number.EPSILON:l.score;s*=Math.pow(f,u)}o.score=s,this._log(o)}}},{key:"_sort",value:function(e){this._log("\n\nSorting...."),e.sort(this.options.sortFn)}},{key:"_format",value:function(e){var t=[];if(this.options.verbose){var n=[];this._log("\n\nOutput:\n\n",JSON.stringify(e,function(e,t){if("object"===r(t)&&null!==t){if(-1!==n.indexOf(t))return;n.push(t)}return t})),n=null}var o=[];this.options.includeMatches&&o.push(function(e,t){var n=e.output;t.matches=[];for(var r=0,o=n.length;r-1&&(a.arrayIndex=i.arrayIndex),t.matches.push(a)}}}),this.options.includeScore&&o.push(function(e,t){t.score=e.score});for(var i=0,a=e.length;ic)return o(e,this.pattern,l);var h=this.options,u=h.location,f=h.distance,v=h.threshold,d=h.findAllMatches,p=h.minMatchCharLength;return i(e,this.pattern,this.patternAlphabet,{location:u,distance:f,threshold:v,findAllMatches:d,minMatchCharLength:p,includeMatches:r})}}])&&r(t.prototype,n),s&&r(t,s),e}();e.exports=s},function(e,t){var n=/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;e.exports=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:/ +/g,o=new RegExp(t.replace(n,"\\$&").replace(r,"|")),i=e.match(o),a=!!i,s=[];if(a)for(var c=0,l=i.length;c=T;N-=1){var K=N-1,$=n[e.charAt(K)];if($&&(M[K]=1),E[N]=(E[N+1]<<1|1)&$,0!==I&&(E[N]|=(w[N+1]|w[N])<<1|1|w[N+1]),E[N]&j&&(C=r(t,{errors:I,currentLocation:K,expectedLocation:m,distance:l}))<=S){if(S=C,(b=K)<=m)break;T=Math.max(1,2*m-b)}}if(r(t,{errors:I+1,currentLocation:m,expectedLocation:m,distance:l})>S)break;w=E}var J={isMatch:b>=0,score:0===C?.001:C};return y&&(J.matchedIndices=o(M,p)),J}},function(e,t){e.exports=function(e,t){var n=t.errors,r=void 0===n?0:n,o=t.currentLocation,i=void 0===o?0:o,a=t.expectedLocation,s=void 0===a?0:a,c=t.distance,l=void 0===c?100:c,h=r/e.length,u=Math.abs(s-i);return l?h+u/l:u?1:h}},function(e,t){e.exports=function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,n=[],r=-1,o=-1,i=0,a=e.length;i=t&&n.push([r,o]),r=-1)}return e[i-1]&&i-r>=t&&n.push([r,i-1]),n}},function(e,t){e.exports=function(e){for(var t={},n=e.length,r=0;r1)throw new Error('"weight" property in key must bein the range of [0, 1)');i=null==i?u:Math.max(i,u),o=null==o?u:Math.min(o,u),this._keyWeights[l]=u,a+=u}if(a>1)throw new Error("Total of weights cannot exceed 1")}}},{key:"search",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{limit:!1};this._log('---------\nSearch pattern: "'.concat(e,'"'));var r=this._prepareSearchers(e),n=r.tokenSearchers,o=r.fullSearcher,i=this._search(n,o);return this._computeScore(i),this.options.shouldSort&&this._sort(i),t.limit&&"number"==typeof t.limit&&(i=i.slice(0,t.limit)),this._format(i)}},{key:"_prepareSearchers",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=[];if(this.options.tokenize)for(var r=e.split(this.options.tokenSeparator),n=0,o=r.length;n0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1?arguments[1]:void 0,r=this.list,n={},o=[];if("string"==typeof r[0]){for(var i=0,a=r.length;i-1&&(C=(C+h)/2),r._log("Score average:",C);var j=!r.options.tokenize||!r.options.matchAllTokens||l>=u.length;if(r._log("\nCheck Matches: ".concat(j)),(s||v.isMatch)&&j){var P={key:n,arrayIndex:t,value:o,score:C};r.options.includeMatches&&(P.matchedIndices=v.matchedIndices);var I=p[a];I?I.output.push(P):(p[a]={item:i,output:[P]},g.push(p[a]))}}else if(c(o))for(var F=0,T=o.length;F0?Number.EPSILON:l.score;c*=Math.pow(v,f)}i.score=c,this._log(i)}}},{key:"_sort",value:function(e){this._log("\n\nSorting...."),e.sort(this.options.sortFn)}},{key:"_format",value:function(e){var t=[];if(this.options.verbose){var r=[];this._log("\n\nOutput:\n\n",JSON.stringify(e,function(e,t){if("object"===n(t)&&null!==t){if(-1!==r.indexOf(t))return;r.push(t)}return t},2)),r=null}var o=[];this.options.includeMatches&&o.push(function(e,t){var r=e.output;t.matches=[];for(var n=0,o=r.length;n-1&&(a.arrayIndex=i.arrayIndex),t.matches.push(a)}}}),this.options.includeScore&&o.push(function(e,t){t.score=e.score});for(var i=0,a=e.length;ic)return o(e,this.pattern,h);var l=this.options,u=l.location,f=l.distance,v=l.threshold,p=l.findAllMatches,d=l.minMatchCharLength;return i(e,this.pattern,this.patternAlphabet,{location:u,distance:f,threshold:v,findAllMatches:p,minMatchCharLength:d,includeMatches:n})}}])&&n(t.prototype,r),s&&n(t,s),e}();e.exports=s},function(e,t){var r=/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;e.exports=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:/ +/g,o=new RegExp(t.replace(r,"\\$&").replace(n,"|")),i=e.match(o),a=!!i,s=[];if(a)for(var c=0,h=i.length;c=T;z-=1){var W=z-1,K=r[e.charAt(W)];if(K&&(M[W]=1),N[z]=(N[z+1]<<1|1)&K,0!==P&&(N[z]|=(A[z+1]|A[z])<<1|1|A[z+1]),N[z]&j&&(O=n(t,{errors:P,currentLocation:W,expectedLocation:m,distance:h}))<=b){if(b=O,(S=W)<=m)break;T=Math.max(1,2*m-S)}}if(n(t,{errors:P+1,currentLocation:m,expectedLocation:m,distance:h})>b)break;A=N}var $={isMatch:S>=0,score:0===O?.001:O};return y&&($.matchedIndices=o(M,d)),$}},function(e,t){e.exports=function(e,t){var r=t.errors,n=void 0===r?0:r,o=t.currentLocation,i=void 0===o?0:o,a=t.expectedLocation,s=void 0===a?0:a,c=t.distance,h=void 0===c?100:c,l=n/e.length,u=Math.abs(s-i);return h?l+u/h:u?1:l}},function(e,t){e.exports=function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,r=[],n=-1,o=-1,i=0,a=e.length;i=t&&r.push([n,o]),n=-1)}return e[i-1]&&i-n>=t&&r.push([n,i-1]),r}},function(e,t){e.exports=function(e){for(var t={},r=e.length,n=0;n 1) { + throw new Error('"weight" property in key must bein the range of [0, 1)') + } + + if (highest == null) { + highest = keyWeight + } else { + highest = Math.max(highest, keyWeight) + } + + if (lowest == null) { + lowest = keyWeight + } else { + lowest = Math.min(lowest, keyWeight) + } + + this._keyWeights[keyName] = keyWeight + + weightsTotal += keyWeight + } + + if (weightsTotal > 1) { + throw new Error('Total of weights cannot exceed 1') + } + } + } + search(pattern, opts = { limit: false }) { this._log(`---------\nSearch pattern: "${pattern}"`) - const { - tokenSearchers, - fullSearcher - } = this._prepareSearchers(pattern) + const { tokenSearchers, fullSearcher } = this._prepareSearchers(pattern) //console.time('Search'); - let { weights, results } = this._search(tokenSearchers, fullSearcher) + let results = this._search(tokenSearchers, fullSearcher) //console.timeEnd('Search'); //console.time('_computeScore'); - this._computeScore(weights, results) + this._computeScore(results) //console.timeEnd('_computeScore'); if (this.options.shouldSort) { @@ -147,27 +204,17 @@ class Fuse { fullSearcher }) } - return { weights: null, results } + return results } // Otherwise, the first item is an Object (hopefully), and thus the searching // is done on the values of the keys of each item. - //console.time('_search'); - const weights = {} + console.time('_search'); for (let i = 0, len = list.length; i < len; i += 1) { let item = list[i] // Iterate over every key - for (let j = 0, keysLen = this.options.keys.length; j < keysLen; j += 1) { - let key = this.options.keys[j] - if (typeof key !== 'string') { - weights[key.name] = key.weight//(1 - key.weight) || 1 - if (key.weight <= 0 || key.weight > 1) { - throw new Error('Key weight has to be > 0 and <= 1') - } - key = key.name - } else { - weights[key] = 1 - } + for (let j = 0, keysLen = this._keyNames.length; j < keysLen; j += 1) { + let key = this._keyNames[j] this._analyze({ key, @@ -183,9 +230,9 @@ class Fuse { } } - //console.timeEnd('_search'); + console.timeEnd('_search'); - return { weights, results } + return results } _analyze({ key, arrayIndex = -1, value, record, index }, { tokenSearchers = [], fullSearcher, resultMap = {}, results = [] }) { @@ -302,9 +349,12 @@ class Fuse { search(arrayIndex, value, record, index) } - _computeScore(weights, results) { + _computeScore(results) { this._log('\n\nComputing score:\n') + const weights = this._keyWeights + const hasWeights = !!Object.keys(weights).length + for (let i = 0, len = results.length; i < len; i += 1) { const result = results[i] const output = result.output @@ -315,7 +365,7 @@ class Fuse { for (let j = 0; j < scoreLen; j += 1) { const item = output[j] const key = item.key - const weight = weights ? weights[key] : 1 + const weight = hasWeights ? weights[key] : 1 const score = item.score === 0 && weights && weights[key] > 0 ? Number.EPSILON : item.score @@ -349,7 +399,7 @@ class Fuse { cache.push(value) } return value - })) + }, 2)) cache = null }