deno.land / x / fuse@v6.4.1 / dist / fuse.esm.min.js

fuse.esm.min.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
/** * Fuse.js v6.4.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 */function t(t){return Array.isArray?Array.isArray(t):"[object Array]"===o(t)}function e(t){return"string"==typeof t}function s(t){return"number"==typeof t}function n(t){return!0===t||!1===t||function(t){return r(t)&&null!==t}(t)&&"[object Boolean]"==o(t)}function r(t){return"object"==typeof t}function i(t){return null!=t}function c(t){return!t.trim().length}function o(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":Object.prototype.toString.call(t)}const h=Object.prototype.hasOwnProperty;class a{constructor(t){this._keys=[],this._keyMap={};let e=0;t.forEach(t=>{let s=l(t);e+=s.weight,this._keys.push(s),this._keyMap[s.id]=s,e+=s.weight}),this._keys.forEach(t=>{t.weight/=e})}get(t){return this._keyMap[t]}keys(){return this._keys}toJSON(){return JSON.stringify(this._keys)}}function l(s){let n=null,r=null,i=null,c=1;if(e(s)||t(s))i=s,n=u(s),r=d(s);else{if(!h.call(s,"name"))throw new Error((t=>`Missing ${t} property in key`)("name"));const t=s.name;if(i=t,h.call(s,"weight")&&(c=s.weight,c<=0))throw new Error((t=>`Property 'weight' in key '${t}' must be a positive integer`)(t));n=u(t),r=d(t)}return{path:n,id:r,weight:c,src:i}}function u(e){return t(e)?e:e.split(".")}function d(e){return t(e)?e.join("."):e}var g={isCaseSensitive:!1,includeScore:!1,keys:[],shouldSort:!0,sortFn:(t,e)=>t.score===e.score?t.idx<e.idx?-1:1:t.score<e.score?-1:1,includeMatches:!1,findAllMatches:!1,minMatchCharLength:1,location:0,threshold:.6,distance:100,...{useExtendedSearch:!1,getFn:function(r,c){let o=[],h=!1;const a=(r,c,l)=>{if(c[l]){const u=r[c[l]];if(!i(u))return;if(l===c.length-1&&(e(u)||s(u)||n(u)))o.push(function(t){return null==t?"":function(t){if("string"==typeof t)return t;let e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(t)}(u));else if(t(u)){h=!0;for(let t=0,e=u.length;t<e;t+=1)a(u[t],c,l+1)}else c.length&&a(u,c,l+1)}else o.push(r)};return a(r,e(c)?c.split("."):c,0),h?o:o[0]},ignoreLocation:!1,ignoreFieldNorm:!1}};const f=/[^ ]+/g;class p{constructor({getFn:t=g.getFn}={}){this.norm=function(t=3){const e=new Map;return{get(s){const n=s.match(f).length;if(e.has(n))return e.get(n);const r=parseFloat((1/Math.sqrt(n)).toFixed(t));return e.set(n,r),r},clear(){e.clear()}}}(3),this.getFn=t,this.isCreated=!1,this.setIndexRecords()}setSources(t=[]){this.docs=t}setIndexRecords(t=[]){this.records=t}setKeys(t=[]){this.keys=t,this._keysMap={},t.forEach((t,e)=>{this._keysMap[t.id]=e})}create(){!this.isCreated&&this.docs.length&&(this.isCreated=!0,e(this.docs[0])?this.docs.forEach((t,e)=>{this._addString(t,e)}):this.docs.forEach((t,e)=>{this._addObject(t,e)}),this.norm.clear())}add(t){const s=this.size();e(t)?this._addString(t,s):this._addObject(t,s)}removeAt(t){this.records.splice(t,1);for(let e=t,s=this.size();e<s;e+=1)this.records[e].i-=1}getValueForItemAtKeyId(t,e){return t[this._keysMap[e]]}size(){return this.records.length}_addString(t,e){if(!i(t)||c(t))return;let s={v:t,i:e,n:this.norm.get(t)};this.records.push(s)}_addObject(s,n){let r={i:n,$:{}};this.keys.forEach((n,o)=>{let h=this.getFn(s,n.path);if(i(h))if(t(h)){let s=[];const n=[{nestedArrIndex:-1,value:h}];for(;n.length;){const{nestedArrIndex:r,value:o}=n.pop();if(i(o))if(e(o)&&!c(o)){let t={v:o,i:r,n:this.norm.get(o)};s.push(t)}else t(o)&&o.forEach((t,e)=>{n.push({nestedArrIndex:e,value:t})})}r.$[o]=s}else if(!c(h)){let t={v:h,n:this.norm.get(h)};r.$[o]=t}}),this.records.push(r)}toJSON(){return{keys:this.keys,records:this.records}}}function M(t,e,{getFn:s=g.getFn}={}){const n=new p({getFn:s});return n.setKeys(t.map(l)),n.setSources(e),n.create(),n}function m(t,e){const s=t.matches;e.matches=[],i(s)&&s.forEach(t=>{if(!i(t.indices)||!t.indices.length)return;const{indices:s,value:n}=t;let r={indices:s,value:n};t.key&&(r.key=t.key.src),t.idx>-1&&(r.refIndex=t.idx),e.matches.push(r)})}function x(t,e){e.score=t.score}function y(t,{errors:e=0,currentLocation:s=0,expectedLocation:n=0,distance:r=g.distance,ignoreLocation:i=g.ignoreLocation}={}){const c=e/t.length;if(i)return c;const o=Math.abs(n-s);return r?c+o/r:o?1:c}function L(t,e,s,{location:n=g.location,distance:r=g.distance,threshold:i=g.threshold,findAllMatches:c=g.findAllMatches,minMatchCharLength:o=g.minMatchCharLength,includeMatches:h=g.includeMatches,ignoreLocation:a=g.ignoreLocation}={}){if(e.length>32)throw new Error(`Pattern length exceeds max of ${32}.`);const l=e.length,u=t.length,d=Math.max(0,Math.min(n,u));let f=i,p=d;const M=o>1||h,m=M?Array(u):[];let x;for(;(x=t.indexOf(e,p))>-1;){let t=y(e,{currentLocation:x,expectedLocation:d,distance:r,ignoreLocation:a});if(f=Math.min(t,f),p=x+l,M){let t=0;for(;t<l;)m[x+t]=1,t+=1}}p=-1;let L=[],k=1,_=l+u;const v=1<<l-1;for(let n=0;n<l;n+=1){let i=0,o=_;for(;i<o;){y(e,{errors:n,currentLocation:d+o,expectedLocation:d,distance:r,ignoreLocation:a})<=f?i=o:_=o,o=Math.floor((_-i)/2+i)}_=o;let h=Math.max(1,d-o+1),g=c?u:Math.min(d+o,u)+l,x=Array(g+2);x[g+1]=(1<<n)-1;for(let i=g;i>=h;i-=1){let c=i-1,o=s[t.charAt(c)];if(M&&(m[c]=+!!o),x[i]=(x[i+1]<<1|1)&o,n&&(x[i]|=(L[i+1]|L[i])<<1|1|L[i+1]),x[i]&v&&(k=y(e,{errors:n,currentLocation:c,expectedLocation:d,distance:r,ignoreLocation:a}),k<=f)){if(f=k,p=c,p<=d)break;h=Math.max(1,2*d-p)}}if(y(e,{errors:n+1,currentLocation:d,expectedLocation:d,distance:r,ignoreLocation:a})>f)break;L=x}const S={isMatch:p>=0,score:Math.max(.001,k)};if(M){const t=function(t=[],e=g.minMatchCharLength){let s=[],n=-1,r=-1,i=0;for(let c=t.length;i<c;i+=1){let c=t[i];c&&-1===n?n=i:c||-1===n||(r=i-1,r-n+1>=e&&s.push([n,r]),n=-1)}return t[i-1]&&i-n>=e&&s.push([n,i-1]),s}(m,o);t.length?h&&(S.indices=t):S.isMatch=!1}return S}function k(t){let e={};for(let s=0,n=t.length;s<n;s+=1){const r=t.charAt(s);e[r]=(e[r]||0)|1<<n-s-1}return e}class _{constructor(t,{location:e=g.location,threshold:s=g.threshold,distance:n=g.distance,includeMatches:r=g.includeMatches,findAllMatches:i=g.findAllMatches,minMatchCharLength:c=g.minMatchCharLength,isCaseSensitive:o=g.isCaseSensitive,ignoreLocation:h=g.ignoreLocation}={}){if(this.options={location:e,threshold:s,distance:n,includeMatches:r,findAllMatches:i,minMatchCharLength:c,isCaseSensitive:o,ignoreLocation:h},this.pattern=o?t:t.toLowerCase(),this.chunks=[],!this.pattern.length)return;const a=(t,e)=>{this.chunks.push({pattern:t,alphabet:k(t),startIndex:e})},l=this.pattern.length;if(l>32){let t=0;const e=l%32,s=l-e;for(;t<s;)a(this.pattern.substr(t,32),t),t+=32;if(e){const t=l-32;a(this.pattern.substr(t),t)}}else a(this.pattern,0)}searchIn(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.indices=[[0,t.length-1]]),e}const{location:n,distance:r,threshold:i,findAllMatches:c,minMatchCharLength:o,ignoreLocation:h}=this.options;let a=[],l=0,u=!1;this.chunks.forEach(({pattern:e,alphabet:d,startIndex:g})=>{const{isMatch:f,score:p,indices:M}=L(t,e,d,{location:n+g,distance:r,threshold:i,findAllMatches:c,minMatchCharLength:o,includeMatches:s,ignoreLocation:h});f&&(u=!0),l+=p,f&&M&&(a=[...a,...M])});let d={isMatch:u,score:u?l/this.chunks.length:1};return u&&s&&(d.indices=a),d}}class v{constructor(t){this.pattern=t}static isMultiMatch(t){return S(t,this.multiRegex)}static isSingleMatch(t){return S(t,this.singleRegex)}search(){}}function S(t,e){const s=t.match(e);return s?s[1]:null}class C extends v{constructor(t,{location:e=g.location,threshold:s=g.threshold,distance:n=g.distance,includeMatches:r=g.includeMatches,findAllMatches:i=g.findAllMatches,minMatchCharLength:c=g.minMatchCharLength,isCaseSensitive:o=g.isCaseSensitive}={}){super(t),this._bitapSearch=new _(t,{location:e,threshold:s,distance:n,includeMatches:r,findAllMatches:i,minMatchCharLength:c,isCaseSensitive:o})}static get type(){return"fuzzy"}static get multiRegex(){return/^"(.*)"$/}static get singleRegex(){return/^(.*)$/}search(t){return this._bitapSearch.searchIn(t)}}class I extends v{constructor(t){super(t)}static get type(){return"include"}static get multiRegex(){return/^'"(.*)"$/}static get singleRegex(){return/^'(.*)$/}search(t){let e,s=0;const n=[],r=this.pattern.length;for(;(e=t.indexOf(this.pattern,s))>-1;)s=e+r,n.push([e,s-1]);const i=!!n.length;return{isMatch:i,score:i?1:0,indices:n}}}const w=[class extends v{constructor(t){super(t)}static get type(){return"exact"}static get multiRegex(){return/^="(.*)"$/}static get singleRegex(){return/^=(.*)$/}search(t){const e=t===this.pattern;return{isMatch:e,score:e?0:1,indices:[0,this.pattern.length-1]}}},I,class extends v{constructor(t){super(t)}static get type(){return"prefix-exact"}static get multiRegex(){return/^\^"(.*)"$/}static get singleRegex(){return/^\^(.*)$/}search(t){const e=t.startsWith(this.pattern);return{isMatch:e,score:e?0:1,indices:[0,this.pattern.length-1]}}},class extends v{constructor(t){super(t)}static get type(){return"inverse-prefix-exact"}static get multiRegex(){return/^!\^"(.*)"$/}static get singleRegex(){return/^!\^(.*)$/}search(t){const e=!t.startsWith(this.pattern);return{isMatch:e,score:e?0:1,indices:[0,t.length-1]}}},class extends v{constructor(t){super(t)}static get type(){return"inverse-suffix-exact"}static get multiRegex(){return/^!"(.*)"\$$/}static get singleRegex(){return/^!(.*)\$$/}search(t){const e=!t.endsWith(this.pattern);return{isMatch:e,score:e?0:1,indices:[0,t.length-1]}}},class extends v{constructor(t){super(t)}static get type(){return"suffix-exact"}static get multiRegex(){return/^"(.*)"\$$/}static get singleRegex(){return/^(.*)\$$/}search(t){const e=t.endsWith(this.pattern);return{isMatch:e,score:e?0:1,indices:[t.length-this.pattern.length,t.length-1]}}},class extends v{constructor(t){super(t)}static get type(){return"inverse-exact"}static get multiRegex(){return/^!"(.*)"$/}static get singleRegex(){return/^!(.*)$/}search(t){const e=-1===t.indexOf(this.pattern);return{isMatch:e,score:e?0:1,indices:[0,t.length-1]}}},C],$=w.length,A=/ +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;const b=new Set([C.type,I.type]);class E{constructor(t,{isCaseSensitive:e=g.isCaseSensitive,includeMatches:s=g.includeMatches,minMatchCharLength:n=g.minMatchCharLength,findAllMatches:r=g.findAllMatches,location:i=g.location,threshold:c=g.threshold,distance:o=g.distance}={}){this.query=null,this.options={isCaseSensitive:e,includeMatches:s,minMatchCharLength:n,findAllMatches:r,location:i,threshold:c,distance:o},this.pattern=e?t:t.toLowerCase(),this.query=function(t,e={}){return t.split("|").map(t=>{let s=t.trim().split(A).filter(t=>t&&!!t.trim()),n=[];for(let t=0,r=s.length;t<r;t+=1){const r=s[t];let i=!1,c=-1;for(;!i&&++c<$;){const t=w[c];let s=t.isMultiMatch(r);s&&(n.push(new t(s,e)),i=!0)}if(!i)for(c=-1;++c<$;){const t=w[c];let s=t.isSingleMatch(r);if(s){n.push(new t(s,e));break}}}return n})}(this.pattern,this.options)}static condition(t,e){return e.useExtendedSearch}searchIn(t){const e=this.query;if(!e)return{isMatch:!1,score:1};const{includeMatches:s,isCaseSensitive:n}=this.options;t=n?t:t.toLowerCase();let r=0,i=[],c=0;for(let n=0,o=e.length;n<o;n+=1){const o=e[n];i.length=0,r=0;for(let e=0,n=o.length;e<n;e+=1){const n=o[e],{isMatch:h,indices:a,score:l}=n.search(t);if(!h){c=0,r=0,i.length=0;break}if(r+=1,c+=l,s){const t=n.constructor.type;b.has(t)?i=[...i,...a]:i.push(a)}}if(r){let t={isMatch:!0,score:c/r};return s&&(t.indices=i),t}}return{isMatch:!1,score:1}}}const F=[];function R(t,e){for(let s=0,n=F.length;s<n;s+=1){let n=F[s];if(n.condition(t,e))return new n(t,e)}return new _(t,e)}const O="$and",j="$or",N="$path",z="$val",K=t=>!(!t[O]&&!t[j]),q=t=>({[O]:Object.keys(t).map(e=>({[e]:t[e]}))});function P(s,n,{auto:i=!0}={}){const c=s=>{let o=Object.keys(s);const h=(t=>!!t[N])(s);if(!h&&o.length>1&&!K(s))return c(q(s));if((e=>!t(e)&&r(e)&&!K(e))(s)){const t=h?s[N]:o[0],r=h?s[z]:s[t];if(!e(r))throw new Error((t=>"Invalid value for key "+t)(t));const c={keyId:d(t),pattern:r};return i&&(c.searcher=R(r,n)),c}let a={children:[],operator:o[0]};return o.forEach(e=>{const n=s[e];t(n)&&n.forEach(t=>{a.children.push(c(t))})}),a};return K(s)||(s=q(s)),c(s)}class W{constructor(t,e={},s){this.options={...g,...e},this.options.useExtendedSearch,this._keyStore=new a(this.options.keys),this.setCollection(t,s)}setCollection(t,e){if(this._docs=t,e&&!(e instanceof p))throw new Error("Incorrect 'index' type");this._myIndex=e||M(this.options.keys,this._docs,{getFn:this.options.getFn})}add(t){i(t)&&(this._docs.push(t),this._myIndex.add(t))}remove(t=(()=>!1)){const e=[];for(let s=0,n=this._docs.length;s<n;s+=1){const n=this._docs[s];t(n,s)&&(this.removeAt(s),s-=1,e.push(n))}return e}removeAt(t){this._docs.splice(t,1),this._myIndex.removeAt(t)}getIndex(){return this._myIndex}search(t,{limit:n=-1}={}){const{includeMatches:r,includeScore:i,shouldSort:c,sortFn:o,ignoreFieldNorm:h}=this.options;let a=e(t)?e(this._docs[0])?this._searchStringList(t):this._searchObjectList(t):this._searchLogical(t);return function(t,{ignoreFieldNorm:e=g.ignoreFieldNorm}){t.forEach(t=>{let s=1;t.matches.forEach(({key:t,norm:n,score:r})=>{const i=t?t.weight:null;s*=Math.pow(0===r&&i?Number.EPSILON:r,(i||1)*(e?1:n))}),t.score=s})}(a,{ignoreFieldNorm:h}),c&&a.sort(o),s(n)&&n>-1&&(a=a.slice(0,n)),function(t,e,{includeMatches:s=g.includeMatches,includeScore:n=g.includeScore}={}){const r=[];s&&r.push(m);n&&r.push(x);return t.map(t=>{const{idx:s}=t,n={item:e[s],refIndex:s};return r.length&&r.forEach(e=>{e(t,n)}),n})}(a,this._docs,{includeMatches:r,includeScore:i})}_searchStringList(t){const e=R(t,this.options),{records:s}=this._myIndex,n=[];return s.forEach(({v:t,i:s,n:r})=>{if(!i(t))return;const{isMatch:c,score:o,indices:h}=e.searchIn(t);c&&n.push({item:t,idx:s,matches:[{score:o,value:t,norm:r,indices:h}]})}),n}_searchLogical(t){const e=P(t,this.options),s=(t,e,n)=>{if(!t.children){const{keyId:s,searcher:r}=t,i=this._findMatches({key:this._keyStore.get(s),value:this._myIndex.getValueForItemAtKeyId(e,s),searcher:r});return i&&i.length?[{idx:n,item:e,matches:i}]:[]}switch(t.operator){case O:{const r=[];for(let i=0,c=t.children.length;i<c;i+=1){const c=t.children[i],o=s(c,e,n);if(!o.length)return[];r.push(...o)}return r}case j:{const r=[];for(let i=0,c=t.children.length;i<c;i+=1){const c=t.children[i],o=s(c,e,n);if(o.length){r.push(...o);break}}return r}}},n=this._myIndex.records,r={},c=[];return n.forEach(({$:t,i:n})=>{if(i(t)){let i=s(e,t,n);i.length&&(r[n]||(r[n]={idx:n,item:t,matches:[]},c.push(r[n])),i.forEach(({matches:t})=>{r[n].matches.push(...t)}))}}),c}_searchObjectList(t){const e=R(t,this.options),{keys:s,records:n}=this._myIndex,r=[];return n.forEach(({$:t,i:n})=>{if(!i(t))return;let c=[];s.forEach((s,n)=>{c.push(...this._findMatches({key:s,value:t[n],searcher:e}))}),c.length&&r.push({idx:n,item:t,matches:c})}),r}_findMatches({key:e,value:s,searcher:n}){if(!i(s))return[];let r=[];if(t(s))s.forEach(({v:t,i:s,n:c})=>{if(!i(t))return;const{isMatch:o,score:h,indices:a}=n.searchIn(t);o&&r.push({score:h,key:e,value:t,idx:s,norm:c,indices:a})});else{const{v:t,n:i}=s,{isMatch:c,score:o,indices:h}=n.searchIn(t);c&&r.push({score:o,key:e,value:t,norm:i,indices:h})}return r}}W.version="6.4.1",W.createIndex=M,W.parseIndex=function(t,{getFn:e=g.getFn}={}){const{keys:s,records:n}=t,r=new p({getFn:e});return r.setKeys(s),r.setIndexRecords(n),r},W.config=g,function(...t){F.push(...t)}(E);export default W;
fuse

Version Info

Tagged at
3 years ago