fuse.js-ie11
Version:
Lightweight fuzzy-search
9 lines • 8.82 kB
JavaScript
/**
* Fuse.js v6.0.0 - 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
*/
function e(e){return Array.isArray?Array.isArray(e):"[object Array]"===Object.prototype.toString.call(e)}function t(e){return"string"==typeof e}function s(e){return"number"==typeof e}function n(e){return null!=e}function i(e){return!e.trim().length}const r=Object.prototype.hasOwnProperty;class c{constructor(e){this._keys={},this._keyNames=[];let s=0;e.forEach(e=>{let n,i=1;if(t(e))n=e;else{if(!r.call(e,"name"))throw new Error(`Missing ${"name"} property in key`);if(n=e.name,r.call(e,"weight")&&(i=e.weight,i<=0))throw new Error((e=>`Property 'weight' in key '${e}' must be a positive integer`)(n))}this._keyNames.push(n),this._keys[n]={weight:i},s+=i}),this._keyNames.forEach(e=>{this._keys[e].weight/=s})}get(e,t){return this._keys[e]&&this._keys[e][t]}keys(){return this._keyNames}toJSON(){return JSON.stringify(this._keys)}}var o={isCaseSensitive:!1,includeScore:!1,keys:[],shouldSort:!0,sortFn:(e,t)=>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(i,r){let c=[],o=!1;const h=(i,r)=>{if(r){const a=r.indexOf(".");let l=r,d=null;-1!==a&&(l=r.slice(0,a),d=r.slice(a+1));const u=i[l];if(!n(u))return;if(d||!t(u)&&!s(u))if(e(u)){o=!0;for(let e=0,t=u.length;e<t;e+=1)h(u[e],d)}else d&&h(u,d);else c.push(function(e){return null==e?"":function(e){if("string"==typeof e)return e;let t=e+"";return"0"==t&&1/e==-1/0?"-0":t}(e)}(u))}else c.push(i)};return h(i,r),o?c:c[0]}}};const h=/[^ ]+/g;class a{constructor({getFn:e=o.getFn}={}){this.norm=function(e=3){const t=new Map;return{get(s){const n=s.match(h).length;if(t.has(n))return t.get(n);const i=parseFloat((1/Math.sqrt(n)).toFixed(e));return t.set(n,i),i},clear(){t.clear()}}}(3),this.getFn=e,this.isCreated=!1,this.setRecords()}setCollection(e=[]){this.docs=e}setRecords(e=[]){this.records=e}setKeys(e=[]){this.keys=e}create(){!this.isCreated&&this.docs.length&&(this.isCreated=!0,t(this.docs[0])?this.docs.forEach((e,t)=>{this._addString(e,t)}):this.docs.forEach((e,t)=>{this._addObject(e,t)}),this.norm.clear())}add(e){const s=this.size();t(e)?this._addString(e,s):this._addObject(e,s)}removeAt(e){this.records.splice(e,1);for(let t=e,s=this.size();t<s;t+=1)this.records[t].i-=1}size(){return this.records.length}_addString(e,t){if(!n(e)||i(e))return;let s={v:e,i:t,n:this.norm.get(e)};this.records.push(s)}_addObject(s,r){let c={i:r,$:{}};this.keys.forEach((r,o)=>{let h=this.getFn(s,r);if(n(h))if(e(h)){let s=[];const r=[{nestedArrIndex:-1,value:h}];for(;r.length;){const{nestedArrIndex:c,value:o}=r.pop();if(n(o))if(t(o)&&!i(o)){let e={v:o,i:c,n:this.norm.get(o)};s.push(e)}else e(o)&&o.forEach((e,t)=>{r.push({nestedArrIndex:t,value:e})})}c.$[o]=s}else if(!i(h)){let e={v:h,n:this.norm.get(h)};c.$[o]=e}}),this.records.push(c)}toJSON(){return{keys:this.keys,records:this.records}}}function l(e,t,{getFn:s=o.getFn}={}){let n=new a({getFn:s});return n.setKeys(e),n.setCollection(t),n.create(),n}function d(e,t){const s=e.matches;t.matches=[],n(s)&&s.forEach(e=>{if(!n(e.indices)||!e.indices.length)return;const{indices:s,value:i}=e;let r={indices:s,value:i};e.key&&(r.key=e.key),e.idx>-1&&(r.refIndex=e.idx),t.matches.push(r)})}function u(e,t){t.score=e.score}function f(e,{errors:t=0,currentLocation:s=0,expectedLocation:n=0,distance:i=o.distance}={}){const r=t/e.length,c=Math.abs(n-s);return i?r+c/i:c?1:r}function g(e,t,s,{location:n=o.location,distance:i=o.distance,threshold:r=o.threshold,findAllMatches:c=o.findAllMatches,minMatchCharLength:h=o.minMatchCharLength,includeMatches:a=o.includeMatches}={}){if(t.length>32)throw new Error(`Pattern length exceeds max of ${32}.`);const l=t.length,d=e.length,u=Math.max(0,Math.min(n,d));let g=r,p=u;const m=[];if(a)for(let e=0;e<d;e+=1)m[e]=0;let y;for(;(y=e.indexOf(t,p))>-1;){let e=f(t,{currentLocation:y,expectedLocation:u,distance:i});if(g=Math.min(e,g),p=y+l,a){let e=0;for(;e<l;)m[y+e]=1,e+=1}}p=-1;let M=[],x=1,k=l+d;const _=1<<(l<=31?l-1:30);for(let n=0;n<l;n+=1){let r=0,o=k;for(;r<o;){f(t,{errors:n,currentLocation:u+o,expectedLocation:u,distance:i})<=g?r=o:k=o,o=Math.floor((k-r)/2+r)}k=o;let h=Math.max(1,u-o+1),y=c?d:Math.min(u+o,d)+l,v=Array(y+2);v[y+1]=(1<<n)-1;for(let r=y;r>=h;r-=1){let c=r-1,o=s[e.charAt(c)];if(o&&a&&(m[c]=1),v[r]=(v[r+1]<<1|1)&o,0!==n&&(v[r]|=(M[r+1]|M[r])<<1|1|M[r+1]),v[r]&_&&(x=f(t,{errors:n,currentLocation:c,expectedLocation:u,distance:i}),x<=g)){if(g=x,p=c,p<=u)break;h=Math.max(1,2*u-p)}}if(f(t,{errors:n+1,currentLocation:u,expectedLocation:u,distance:i})>g)break;M=v}let v={isMatch:p>=0,score:Math.max(.001,x)};return a&&(v.indices=function(e=[],t=o.minMatchCharLength){let s=[],n=-1,i=-1,r=0;for(let c=e.length;r<c;r+=1){let c=e[r];c&&-1===n?n=r:c||-1===n||(i=r-1,i-n+1>=t&&s.push([n,i]),n=-1)}return e[r-1]&&r-n>=t&&s.push([n,r-1]),s}(m,h)),v}function p(e){let t={};for(let s=0,n=e.length;s<n;s+=1){const i=e.charAt(s);t[i]=(t[i]||0)|1<<n-s-1}return t}class m{constructor(e,{location:t=o.location,threshold:s=o.threshold,distance:n=o.distance,includeMatches:i=o.includeMatches,findAllMatches:r=o.findAllMatches,minMatchCharLength:c=o.minMatchCharLength,isCaseSensitive:h=o.isCaseSensitive}={}){this.options={location:t,threshold:s,distance:n,includeMatches:i,findAllMatches:r,minMatchCharLength:c,isCaseSensitive:h},this.pattern=h?e:e.toLowerCase(),this.chunks=[];let a=0;for(;a<this.pattern.length;){let e=this.pattern.substring(a,a+32);this.chunks.push({pattern:e,alphabet:p(e)}),a+=32}}searchIn(e){const{isCaseSensitive:t,includeMatches:s}=this.options;if(t||(e=e.toLowerCase()),this.pattern===e){let t={isMatch:!0,score:0};return s&&(t.indices=[[0,e.length-1]]),t}const{location:n,distance:i,threshold:r,findAllMatches:c,minMatchCharLength:o}=this.options;let h=[],a=0,l=!1;this.chunks.forEach(({pattern:t,alphabet:d},u)=>{const{isMatch:f,score:p,indices:m}=g(e,t,d,{location:n+32*u,distance:i,threshold:r,findAllMatches:c,minMatchCharLength:o,includeMatches:s});f&&(l=!0),a+=p,f&&m&&(h=[...h,...m])});let d={isMatch:l,score:l?a/this.chunks.length:1};return l&&s&&(d.indices=h),d}}const y=[];function M(e,t){for(let s=0,n=y.length;s<n;s+=1){let n=y[s];if(n.condition(e,t))return new n(e,t)}return new m(e,t)}class x{constructor(e,t={},s){if(this.options={...o,...t},this.options.useExtendedSearch)throw new Error("Extended search is not available");this._keyStore=new c(this.options.keys),this.setCollection(e,s)}setCollection(e,t){if(this._docs=e,t&&!(t instanceof a))throw new Error("Incorrect 'index' type");this._myIndex=t||l(this._keyStore.keys(),this._docs,{getFn:this.options.getFn})}add(e){n(e)&&(this._docs.push(e),this._myIndex.add(e))}removeAt(e){this._docs.splice(e,1),this._myIndex.removeAt(e)}getIndex(){return this._myIndex}search(e,{limit:n=-1}={}){const{includeMatches:i,includeScore:r,shouldSort:c,sortFn:h}=this.options;let a=t(e)?t(this._docs[0])?this._searchStringList(e):this._searchObjectList(e):this._searchLogical(e);return function(e,t){e.forEach(e=>{let s=1;e.matches.forEach(({key:e,norm:n,score:i})=>{const r=t.get(e,"weight");s*=Math.pow(0===i&&r?Number.EPSILON:i,(r||1)*n)}),e.score=s})}(a,this._keyStore),c&&a.sort(h),s(n)&&n>-1&&(a=a.slice(0,n)),function(e,t,{includeMatches:s=o.includeMatches,includeScore:n=o.includeScore}={}){const i=[];s&&i.push(d);n&&i.push(u);return e.map(e=>{const{idx:s}=e,n={item:t[s],refIndex:s};return i.length&&i.forEach(t=>{t(e,n)}),n})}(a,this._docs,{includeMatches:i,includeScore:r})}_searchStringList(e){const t=M(e,this.options),{records:s}=this._myIndex,i=[];return s.forEach(({v:e,i:s,n:r})=>{if(!n(e))return;const{isMatch:c,score:o,indices:h}=t.searchIn(e);c&&i.push({item:e,idx:s,matches:[{score:o,value:e,norm:r,indices:h}]})}),i}_searchLogical(e){throw new Error("Logical search is not available")}_searchObjectList(e){const t=M(e,this.options),{keys:s,records:i}=this._myIndex,r=[];return i.forEach(({$:e,i:i})=>{if(!n(e))return;let c=[];s.forEach((s,n)=>{c.push(...this._findMatches({key:s,value:e[n],searcher:t}))}),c.length&&r.push({idx:i,item:e,matches:c})}),r}_findMatches({key:t,value:s,searcher:i}){if(!n(s))return[];let r=[];if(e(s))s.forEach(({v:e,i:s,n:c})=>{if(!n(e))return;const{isMatch:o,score:h,indices:a}=i.searchIn(e);o&&r.push({score:h,key:t,value:e,idx:s,norm:c,indices:a})});else{const{v:e,n:n}=s,{isMatch:c,score:o,indices:h}=i.searchIn(e);c&&r.push({score:o,key:t,value:e,norm:n,indices:h})}return r}}x.version="6.0.0",x.createIndex=l,x.parseIndex=function(e,{getFn:t=o.getFn}={}){const{keys:s,records:n}=e;let i=new a({getFn:t});return i.setKeys(s),i.setRecords(n),i},x.config=o;export default x;