Fuse/dist/fuse.basic.esm.min.js
2020-04-11 13:41:55 -07:00

9 lines
8.0 KiB
JavaScript

/**
* Fuse.js v5.2.0-alpha.4 - 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
*/
const t=t=>Array.isArray?Array.isArray(t):"[object Array]"===Object.prototype.toString.call(t),e=t=>"string"==typeof t,s=t=>"number"==typeof t,n=t=>null!=t;function i(i,r){let o=[],h=!1;const c=(i,r)=>{if(r){const l=r.indexOf(".");let a=r,u=null;-1!==l&&(a=r.slice(0,l),u=r.slice(l+1));const d=i[a];if(n(d))if(u||!e(d)&&!s(d))if(t(d)){h=!0;for(let t=0,e=d.length;t<e;t+=1)c(d[t],u)}else u&&c(d,u);else o.push((t=>null==t?"":(t=>{if("string"==typeof t)return t;let e=t+"";return"0"==e&&1/t==-1/0?"-0":e})(t))(d))}else o.push(i)};return c(i,r),h?o:o[0]}var r={isCaseSensitive:!1,includeScore:!1,keys:[],shouldSort:!0,sortFn:(t,e)=>t.score-e.score,includeMatches:!1,findAllMatches:!1,minMatchCharLength:1,location:0,threshold:.6,distance:100,...{useExtendedSearch:!1,getFn:i}};function o(t,{errors:e=0,currentLocation:s=0,expectedLocation:n=0,distance:i=r.distance}={}){const o=e/t.length,h=Math.abs(n-s);return i?o+h/i:h?1:o}function h(t,e,s,{location:n=r.location,distance:i=r.distance,threshold:h=r.threshold,findAllMatches:c=r.findAllMatches,minMatchCharLength:l=r.minMatchCharLength,includeMatches:a=r.includeMatches}={}){const u=e.length,d=t.length,f=Math.max(0,Math.min(n,d));let g=h,p=f;const y=[];if(a)for(let t=0;t<d;t+=1)y[t]=0;let m;for(;(m=t.indexOf(e,p))>-1;){let t=o(e,{currentLocation:m,expectedLocation:f,distance:i});if(g=Math.min(t,g),p=m+u,a){let t=0;for(;t<u;)y[m+t]=1,t+=1}}p=-1;let M=[],x=1,_=u+d;const k=1<<(u<=31?u-1:30);for(let n=0;n<u;n+=1){let r=0,h=_;for(;r<h;){o(e,{errors:n,currentLocation:f+h,expectedLocation:f,distance:i})<=g?r=h:_=h,h=Math.floor((_-r)/2+r)}_=h;let l=Math.max(1,f-h+1),m=c?d:Math.min(f+h,d)+u,w=Array(m+2);w[m+1]=(1<<n)-1;for(let r=m;r>=l;r-=1){let h=r-1,c=s[t.charAt(h)];if(c&&a&&(y[h]=1),w[r]=(w[r+1]<<1|1)&c,0!==n&&(w[r]|=(M[r+1]|M[r])<<1|1|M[r+1]),w[r]&k&&(x=o(e,{errors:n,currentLocation:h,expectedLocation:f,distance:i}),x<=g)){if(g=x,p=h,p<=f)break;l=Math.max(1,2*f-p)}}if(o(e,{errors:n+1,currentLocation:f,expectedLocation:f,distance:i})>g)break;M=w}let w={isMatch:p>=0,score:x||.001};return a&&(w.matchedIndices=function(t=[],e=r.minMatchCharLength){let s=[],n=-1,i=-1,o=0;for(let r=t.length;o<r;o+=1){let r=t[o];r&&-1===n?n=o:r||-1===n||(i=o-1,i-n+1>=e&&s.push([n,i]),n=-1)}return t[o-1]&&o-n>=e&&s.push([n,o-1]),s}(y,l)),w}class c{constructor(t,{location:e=r.location,threshold:s=r.threshold,distance:n=r.distance,includeMatches:i=r.includeMatches,findAllMatches:o=r.findAllMatches,minMatchCharLength:h=r.minMatchCharLength,isCaseSensitive:c=r.isCaseSensitive}={}){if(this.options={location:e,threshold:s,distance:n,includeMatches:i,findAllMatches:o,minMatchCharLength:h,isCaseSensitive:c},t.length>32)throw new Error("Pattern length exceeds max of 32.");this.pattern=c?t:t.toLowerCase(),this.patternAlphabet=function(t){let e={},s=t.length;for(let n=0;n<s;n+=1)e[t.charAt(n)]=0;for(let n=0;n<s;n+=1)e[t.charAt(n)]|=1<<s-n-1;return e}(this.pattern)}searchIn(t){let e=t.$;return this.searchInString(e)}searchInString(t){const{isCaseSensitive:e,includeMatches:s}=this.options;if(e||(t=t.toLowerCase()),this.pattern===t){let e={isMatch:!0,score:0};return s&&(e.matchedIndices=[[0,t.length-1]]),e}const{location:n,distance:i,threshold:r,findAllMatches:o,minMatchCharLength:c}=this.options;return h(t,this.pattern,this.patternAlphabet,{location:n,distance:i,threshold:r,findAllMatches:o,minMatchCharLength:c,includeMatches:s})}}function l(t,{n:e=3,pad:s=!0,sort:n=!1}){let i=[];if(null==t)return i;t=t.toLowerCase(),s&&(t=` ${t} `);let r=t.length-e+1;if(r<1)return i;for(;r--;)i[r]=t.substr(r,e);return n&&i.sort((t,e)=>t==e?0:t<e?-1:1),i}const a=/[^ ]+/g;function u(s,r,{getFn:o=i,ngrams:h=!1}={}){let c=[];if(e(r[0]))for(let t=0,e=r.length;t<e;t+=1){const e=r[t];if(n(e)){let s={$:e,idx:t,t:e.match(a).length};h&&(s.ng=l(e,{sort:!0})),c.push(s)}}else{const i=s.length;for(let u=0,d=r.length;u<d;u+=1){let d=r[u],f={idx:u,$:{}};for(let r=0;r<i;r+=1){let i=s[r],c=o(d,i);if(n(c))if(t(c)){let s=[];const r=[{arrayIndex:-1,value:c}];for(;r.length;){const{arrayIndex:i,value:o}=r.pop();if(n(o))if(e(o)){let t={$:o,idx:i,t:o.match(a).length};h&&(t.ng=l(o,{sort:!0})),s.push(t)}else if(t(o))for(let t=0,e=o.length;t<e;t+=1)r.push({arrayIndex:t,value:o[t]})}f.$[i]=s}else{let t={$:c,t:c.match(a).length};h&&(t.ng=l(c,{sort:!0})),f.$[i]=t}}c.push(f)}}return c}class d{constructor(t){if(this._keys={},this._keyNames=[],this._length=t.length,t.length&&e(t[0]))for(let e=0;e<this._length;e+=1){const s=t[e];this._keys[s]={weight:1},this._keyNames.push(s)}else{let e=0;for(let s=0;s<this._length;s+=1){const n=t[s];if(!Object.prototype.hasOwnProperty.call(n,"name"))throw new Error('Missing "name" property in key object');const i=n.name;if(this._keyNames.push(i),!Object.prototype.hasOwnProperty.call(n,"weight"))throw new Error('Missing "weight" property in key object');const r=n.weight;if(r<=0||r>=1)throw new Error('"weight" property in key must be in the range of (0, 1)');this._keys[i]={weight:r},e+=r}for(let t=0;t<this._length;t+=1){const s=this._keyNames[t],n=this._keys[s].weight;this._keys[s].weight=n/e}}}get(t,e){return this._keys[t]?this._keys[t][e]:-1}keys(){return this._keyNames}count(){return this._length}toJSON(){return JSON.stringify(this._keys)}}function f(t,e){const s=t.matches;if(e.matches=[],n(s))for(let t=0,i=s.length;t<i;t+=1){let i=s[t];if(!n(i.indices)||0===i.indices.length)continue;let r={indices:i.indices,value:i.value};i.key&&(r.key=i.key),i.idx>-1&&(r.refIndex=i.idx),e.matches.push(r)}}function g(t,e){e.score=t.score}const p=[];class y{constructor(t,e={},s=null){this.options={...r,...e},this._processKeys(this.options.keys),this.setCollection(t,s)}setCollection(t,s=null){this.list=t,this.listIsStringArray=e(t[0]),s?this.setIndex(s):this.setIndex(this._createIndex())}setIndex(t){this._indexedList=t}_processKeys(t){this._keyStore=new d(t)}_createIndex(){return u(this._keyStore.keys(),this.list,{getFn:this.options.getFn})}search(t,e={limit:!1}){if(!(t=t.trim()).length)return[];const{shouldSort:n}=this.options;let i=null;for(let e=0,s=p.length;e<s;e+=1){let s=p[e];if(s.condition(t,this.options)){i=new s(t,this.options);break}}i||(i=new c(t,this.options));let r=this._searchUsing(i);return this._computeScore(r),n&&this._sort(r),e.limit&&s(e.limit)&&(r=r.slice(0,e.limit)),this._format(r)}_searchUsing(e){const s=this._indexedList,i=[],{includeMatches:r}=this.options;if(this.listIsStringArray)for(let t=0,o=s.length;t<o;t+=1){let o=s[t],{$:h,idx:c,t:l}=o;if(!n(h))continue;let a=e.searchIn(o);const{isMatch:u,score:d}=a;if(!u)continue;let f={score:d,value:h,t:l};r&&(f.indices=a.matchedIndices),i.push({item:h,idx:c,matches:[f]})}else{const o=this._keyStore.keys(),h=this._keyStore.count();for(let c=0,l=s.length;c<l;c+=1){let{$:l,idx:a}=s[c];if(!n(l))continue;let u=[];for(let s=0;s<h;s+=1){let i=o[s],h=l[i];if(n(h))if(t(h))for(let t=0,s=h.length;t<s;t+=1){let s=h[t];const{$:o,idx:c,t:l}=s;if(!n(o))continue;let a=e.searchIn(s);const{isMatch:d,score:f}=a;if(!d)continue;let g={score:f,key:i,value:o,idx:c,t:l};r&&(g.indices=a.matchedIndices),u.push(g)}else{const{$:t,t:s}=h;let n=e.searchIn(h);const{isMatch:o,score:c}=n;if(!o)continue;let l={score:c,key:i,value:t,t:s};r&&(l.indices=n.matchedIndices),u.push(l)}}u.length&&i.push({idx:a,item:l,matches:u})}}return i}_computeScore(t){const e=t.length;for(let s=0;s<e;s+=1){const e=t[s],n=e.matches,i=n.length;let r=1;for(let t=0;t<i;t+=1){const e=n[t],{key:s,t:i}=e,o=this._keyStore.get(s,"weight"),h=o>-1?o:1,c=0===e.score&&o>-1?Number.EPSILON:e.score,l=1/Math.sqrt(i);r*=Math.pow(c,h*l)}e.score=r}}_sort(t){t.sort(this.options.sortFn)}_format(t){const e=[],{includeMatches:s,includeScore:n}=this.options;let i=[];s&&i.push(f),n&&i.push(g);for(let s=0,n=t.length;s<n;s+=1){const n=t[s],{idx:r}=n,o={item:this.list[r],refIndex:r};if(i.length)for(let t=0,e=i.length;t<e;t+=1)i[t](n,o);e.push(o)}return e}}y.version="5.2.0-alpha.4",y.createIndex=u,y.config=r;export default y;