mirror of
https://github.com/krisk/Fuse.git
synced 2026-01-25 16:43:11 +00:00
9 lines
14 KiB
JavaScript
9 lines
14 KiB
JavaScript
/**
|
|
* Fuse.js v6.2.0-beta.1 - Lightweight fuzzy-search (http://fusejs.io)
|
|
*
|
|
* Copyright (c) 2020 Kiro Risk (http://kiro.me)
|
|
* All Rights Reserved. Apache Software License 2.0
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*/
|
|
var e,t;e=this,t=function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function n(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e){return function(e){if(Array.isArray(e))return s(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return s(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function c(e){return Array.isArray?Array.isArray(e):"[object Array]"===Object.prototype.toString.call(e)}function h(e){return"string"==typeof e}function u(e){return"number"==typeof e}function d(e){return null!=e}function f(e){return!e.trim().length}var l=function(e){return"Pattern length exceeds max of ".concat(e,".")},v=Object.prototype.hasOwnProperty,g=function(){function t(n){var r=this;e(this,t),this._keys={},this._keyNames=[];var i=0;n.forEach((function(e){var t,n=1;if(h(e))t=e;else{if(!v.call(e,"name"))throw new Error("Missing ".concat("name"," property in key"));if(t=e.name,v.call(e,"weight")&&(n=e.weight)<=0)throw new Error(function(e){return"Property 'weight' in key '".concat(e,"' must be a positive integer")}(t))}r._keyNames.push(t),r._keys[t]={weight:n},i+=n})),this._keyNames.forEach((function(e){r._keys[e].weight/=i}))}return n(t,[{key:"get",value:function(e,t){return this._keys[e]&&this._keys[e][t]}},{key:"keys",value:function(){return this._keyNames}},{key:"toJSON",value:function(){return JSON.stringify(this._keys)}}]),t}(),y=o({},{isCaseSensitive:!1,includeScore:!1,keys:[],shouldSort:!0,sortFn:function(e,t){return e.score===t.score?e.idx<t.idx?-1:1:e.score<t.score?-1:1}},{},{includeMatches:!1,findAllMatches:!1,minMatchCharLength:1},{},{location:0,threshold:.6,distance:100},{},{useExtendedSearch:!1,getFn:function(e,t){var n=[],r=!1;return function e(t,i){if(i){var o=i.indexOf("."),a=i,s=null;-1!==o&&(a=i.slice(0,o),s=i.slice(o+1));var f=t[a];if(!d(f))return;if(s||!h(f)&&!u(f))if(c(f)){r=!0;for(var l=0,v=f.length;l<v;l+=1)e(f[l],s)}else s&&e(f,s);else n.push(function(e){return null==e?"":function(e){if("string"==typeof e)return e;var t=e+"";return"0"==t&&1/e==-1/0?"-0":t}(e)}(f))}else n.push(t)}(e,t),r?n:n[0]},ignoreLocation:!1,ignoreFieldNorm:!1,matchEmptyQuery:!1}),p=/[^ ]+/g;function m(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:3,t=new Map;return{get:function(n){var r=n.match(p).length;if(t.has(r))return t.get(r);var i=parseFloat((1/Math.sqrt(r)).toFixed(e));return t.set(r,i),i},clear:function(){t.clear()}}}var k=function(){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=n.getFn,i=void 0===r?y.getFn:r;e(this,t),this.norm=m(3),this.getFn=i,this.isCreated=!1,this.setIndexRecords()}return n(t,[{key:"setSources",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.docs=e}},{key:"setIndexRecords",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.records=e}},{key:"setKeys",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.keys=e}},{key:"create",value:function(){var e=this;!this.isCreated&&this.docs.length&&(this.isCreated=!0,h(this.docs[0])?this.docs.forEach((function(t,n){e._addString(t,n)})):this.docs.forEach((function(t,n){e._addObject(t,n)})),this.norm.clear())}},{key:"add",value:function(e){var t=this.size();h(e)?this._addString(e,t):this._addObject(e,t)}},{key:"removeAt",value:function(e){this.records.splice(e,1);for(var t=e,n=this.size();t<n;t+=1)this.records[t].i-=1}},{key:"size",value:function(){return this.records.length}},{key:"_addString",value:function(e,t){if(d(e)&&!f(e)){var n={v:e,i:t,n:this.norm.get(e)};this.records.push(n)}}},{key:"_addObject",value:function(e,t){var n=this,r={i:t,$:{}};this.keys.forEach((function(t,i){var o=n.getFn(e,t);if(d(o))if(c(o))!function(){for(var e=[],t=[{nestedArrIndex:-1,value:o}];t.length;){var a=t.pop(),s=a.nestedArrIndex,u=a.value;if(d(u))if(h(u)&&!f(u)){var l={v:u,i:s,n:n.norm.get(u)};e.push(l)}else c(u)&&u.forEach((function(e,n){t.push({nestedArrIndex:n,value:e})}))}r.$[i]=e}();else if(!f(o)){var a={v:o,n:n.norm.get(o)};r.$[i]=a}})),this.records.push(r)}},{key:"toJSON",value:function(){return{keys:this.keys,records:this.records}}}]),t}();function b(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=n.getFn,i=void 0===r?y.getFn:r,o=new k({getFn:i});return o.setKeys(e),o.setSources(t),o.create(),o}function M(e,t){var n=e.matches;t.matches=[],d(n)&&n.forEach((function(e){if(d(e.indices)&&e.indices.length){var n={indices:e.indices,value:e.value};e.key&&(n.key=e.key),e.idx>-1&&(n.refIndex=e.idx),t.matches.push(n)}}))}function x(e,t){t.score=e.score}function L(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.errors,r=void 0===n?0:n,i=t.currentLocation,o=void 0===i?0:i,a=t.expectedLocation,s=void 0===a?0:a,c=t.distance,h=void 0===c?y.distance:c,u=t.ignoreLocation,d=void 0===u?y.ignoreLocation:u,f=r/e.length;if(d)return f;var l=Math.abs(s-o);return h?f+l/h:l?1:f}function _(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:y.minMatchCharLength,n=[],r=-1,i=-1,o=0,a=e.length;o<a;o+=1){var s=e[o];s&&-1===r?r=o:s||-1===r||((i=o-1)-r+1>=t&&n.push([r,i]),r=-1)}return e[o-1]&&o-r>=t&&n.push([r,o-1]),n}function w(e){for(var t={},n=0,r=e.length;n<r;n+=1){var i=e.charAt(n);t[i]=(t[i]||0)|1<<r-n-1}return t}var O=function(){function t(n){var r=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=i.location,a=void 0===o?y.location:o,s=i.threshold,c=void 0===s?y.threshold:s,h=i.distance,u=void 0===h?y.distance:h,d=i.includeMatches,f=void 0===d?y.includeMatches:d,l=i.findAllMatches,v=void 0===l?y.findAllMatches:l,g=i.minMatchCharLength,p=void 0===g?y.minMatchCharLength:g,m=i.isCaseSensitive,k=void 0===m?y.isCaseSensitive:m,b=i.ignoreLocation,M=void 0===b?y.ignoreLocation:b;if(e(this,t),this.options={location:a,threshold:c,distance:u,includeMatches:f,findAllMatches:v,minMatchCharLength:p,isCaseSensitive:k,ignoreLocation:M},this.pattern=k?n:n.toLowerCase(),this.chunks=[],this.pattern.length){var x=function(e,t){r.chunks.push({pattern:e,alphabet:w(e),startIndex:t})},L=this.pattern.length;if(L>32){for(var _=0,O=L%32,S=L-O;_<S;)x(this.pattern.substr(_,32),_),_+=32;if(O){var A=L-32;x(this.pattern.substr(A),A)}}else x(this.pattern,0)}}return n(t,[{key:"searchIn",value:function(e){var t=this.options,n=t.isCaseSensitive,r=t.includeMatches;if(n||(e=e.toLowerCase()),this.pattern===e){var i={isMatch:!0,score:0};return r&&(i.indices=[[0,e.length-1]]),i}var o=this.options,s=o.location,c=o.distance,h=o.threshold,u=o.findAllMatches,d=o.minMatchCharLength,f=o.ignoreLocation,v=[],g=0,p=!1;this.chunks.forEach((function(t){var n=t.pattern,i=t.alphabet,o=t.startIndex,m=function(e,t,n){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},i=r.location,o=void 0===i?y.location:i,a=r.distance,s=void 0===a?y.distance:a,c=r.threshold,h=void 0===c?y.threshold:c,u=r.findAllMatches,d=void 0===u?y.findAllMatches:u,f=r.minMatchCharLength,v=void 0===f?y.minMatchCharLength:f,g=r.includeMatches,p=void 0===g?y.includeMatches:g,m=r.ignoreLocation,k=void 0===m?y.ignoreLocation:m;if(t.length>32)throw new Error(l(32));for(var b,M=t.length,x=e.length,w=Math.max(0,Math.min(o,x)),O=h,S=w,A=v>1||p,E=A?Array(x):[];(b=e.indexOf(t,S))>-1;){var I=L(t,{currentLocation:b,expectedLocation:w,distance:s,ignoreLocation:k});if(O=Math.min(I,O),S=b+M,A)for(var j=0;j<M;)E[b+j]=1,j+=1}S=-1;for(var C=[],F=1,N=M+x,P=1<<M-1,$=0;$<M;$+=1){for(var D=0,z=N;D<z;){var J=L(t,{errors:$,currentLocation:w+z,expectedLocation:w,distance:s,ignoreLocation:k});J<=O?D=z:N=z,z=Math.floor((N-D)/2+D)}N=z;var K=Math.max(1,w-z+1),R=d?x:Math.min(w+z,x)+M,Q=Array(R+2);Q[R+1]=(1<<$)-1;for(var T=R;T>=K;T-=1){var q=T-1,U=n[e.charAt(q)];if(A&&(E[q]=+!!U),Q[T]=(Q[T+1]<<1|1)&U,$&&(Q[T]|=(C[T+1]|C[T])<<1|1|C[T+1]),Q[T]&P&&(F=L(t,{errors:$,currentLocation:q,expectedLocation:w,distance:s,ignoreLocation:k}))<=O){if(O=F,(S=q)<=w)break;K=Math.max(1,2*w-S)}}var B=L(t,{errors:$+1,currentLocation:w,expectedLocation:w,distance:s,ignoreLocation:k});if(B>O)break;C=Q}var G={isMatch:S>=0,score:Math.max(.001,F)};if(A){var H=_(E,v);H.length?p&&(G.indices=H):G.isMatch=!1}return G}(e,n,i,{location:s+o,distance:c,threshold:h,findAllMatches:u,minMatchCharLength:d,includeMatches:r,ignoreLocation:f}),k=m.isMatch,b=m.score,M=m.indices;k&&(p=!0),g+=b,k&&M&&(v=[].concat(a(v),a(M)))}));var m={isMatch:p,score:p?g/this.chunks.length:1};return p&&r&&(m.indices=v),m}}]),t}(),S=[];function A(e,t){for(var n=0,r=S.length;n<r;n+=1){var i=S[n];if(i.condition(e,t))return new i(e,t)}return new O(e,t)}var E=function(){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=arguments.length>2?arguments[2]:void 0;if(e(this,t),this.options=o({},y,{},r),this.options.useExtendedSearch)throw new Error("Extended search is not available");this._keyStore=new g(this.options.keys),this.setCollection(n,i)}return n(t,[{key:"setCollection",value:function(e,t){if(this._docs=e,t&&!(t instanceof k))throw new Error("Incorrect 'index' type");this._myIndex=t||b(this._keyStore.keys(),this._docs,{getFn:this.options.getFn})}},{key:"add",value:function(e){d(e)&&(this._docs.push(e),this._myIndex.add(e))}},{key:"remove",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:function(){return!1},t=[],n=0,r=this._docs.length;n<r;n+=1){var i=this._docs[n];e(i,n)&&(this.removeAt(n),n-=1,t.push(i))}return t}},{key:"removeAt",value:function(e){this._docs.splice(e,1),this._myIndex.removeAt(e)}},{key:"getIndex",value:function(){return this._myIndex}},{key:"search",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.limit,r=void 0===n?-1:n,i=this.options,o=i.includeMatches,a=i.includeScore,s=i.shouldSort,c=i.sortFn,l=i.ignoreFieldNorm,v=i.matchEmptyQuery;if(v&&(!d(e)||f(e)))return this._docs.map((function(e,t){return{item:e,score:1,refIndex:t}}));var g=h(e)?h(this._docs[0])?this._searchStringList(e):this._searchObjectList(e):this._searchLogical(e);return I(g,this._keyStore,{ignoreFieldNorm:l}),s&&g.sort(c),u(r)&&r>-1&&(g=g.slice(0,r)),j(g,this._docs,{includeMatches:o,includeScore:a})}},{key:"_searchStringList",value:function(e){var t=A(e,this.options),n=this._myIndex.records,r=[];return n.forEach((function(e){var n=e.v,i=e.i,o=e.n;if(d(n)){var a=t.searchIn(n),s=a.isMatch,c=a.score,h=a.indices;s&&r.push({item:n,idx:i,matches:[{score:c,value:n,norm:o,indices:h}]})}})),r}},{key:"_searchLogical",value:function(e){throw new Error("Logical search is not available")}},{key:"_searchObjectList",value:function(e){var t=this,n=A(e,this.options),r=this._myIndex,i=r.keys,o=r.records,s=[];return o.forEach((function(e){var r=e.$,o=e.i;if(d(r)){var c=[];i.forEach((function(e,i){c.push.apply(c,a(t._findMatches({key:e,value:r[i],searcher:n})))})),c.length&&s.push({idx:o,item:r,matches:c})}})),s}},{key:"_findMatches",value:function(e){var t=e.key,n=e.value,r=e.searcher;if(!d(n))return[];var i=[];if(c(n))n.forEach((function(e){var n=e.v,o=e.i,a=e.n;if(d(n)){var s=r.searchIn(n),c=s.isMatch,h=s.score,u=s.indices;c&&i.push({score:h,key:t,value:n,idx:o,norm:a,indices:u})}}));else{var o=n.v,a=n.n,s=r.searchIn(o),h=s.isMatch,u=s.score,f=s.indices;h&&i.push({score:u,key:t,value:o,norm:a,indices:f})}return i}}]),t}();function I(e,t,n){var r=n.ignoreFieldNorm,i=void 0===r?y.ignoreFieldNorm:r;e.forEach((function(e){var n=1;e.matches.forEach((function(e){var r=e.key,o=e.norm,a=e.score,s=t.get(r,"weight");n*=Math.pow(0===a&&s?Number.EPSILON:a,(s||1)*(i?1:o))})),e.score=n}))}function j(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=n.includeMatches,i=void 0===r?y.includeMatches:r,o=n.includeScore,a=void 0===o?y.includeScore:o,s=[];return i&&s.push(M),a&&s.push(x),e.map((function(e){var n=e.idx,r={item:t[n],refIndex:n};return s.length&&s.forEach((function(t){t(e,r)})),r}))}return E.version="6.2.0-beta.1",E.createIndex=b,E.parseIndex=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.getFn,r=void 0===n?y.getFn:n,i=e.keys,o=e.records,a=new k({getFn:r});return a.setKeys(i),a.setIndexRecords(o),a},E.config=y,E},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Fuse=t(); |