fuse.js
Version:
Lightweight fuzzy-search
9 lines • 24.3 kB
JavaScript
/**
* Fuse.js v7.3.0 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2026 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]"===u(t)}function e(t){return null==t?"":function(t){if("string"==typeof t)return t;if("bigint"==typeof t)return t.toString();const e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(t)}function s(t){return"string"==typeof t}function i(t){return"number"==typeof t}function n(t){return!0===t||!1===t||function(t){return r(t)&&null!==t}(t)&&"[object Boolean]"==u(t)}function r(t){return"object"==typeof t}function c(t){return null!=t}function o(t){return!t.trim().length}function u(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=>{const s=l(t);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(e){let i=null,n=null,r=null,c=1,o=null;if(s(e)||t(e))r=e,i=d(e),n=g(e);else{if(!h.call(e,"name"))throw new Error((t=>`Missing ${t} property in key`)("name"));const t=e.name;if(r=t,h.call(e,"weight")&&(c=e.weight,c<=0))throw new Error((t=>`Property 'weight' in key '${t}' must be a positive integer`)(t));i=d(t),n=g(t),o=e.getFn}return{path:i,id:n,weight:c,src:r,getFn:o}}function d(e){return t(e)?e:e.split(".")}function g(e){return t(e)?e.join("."):e}const f={useExtendedSearch:!1,useTokenSearch:!1,getFn:function(r,o){const u=[];let h=!1;const a=(r,o,l,d)=>{if(c(r))if(o[l]){const g=r[o[l]];if(!c(g))return;if(l===o.length-1&&(s(g)||i(g)||n(g)||"bigint"==typeof g))u.push(void 0!==d?{v:e(g),i:d}:e(g));else if(t(g)){h=!0;for(let t=0,e=g.length;t<e;t+=1)a(g[t],o,l+1,t)}else o.length&&a(g,o,l+1,d)}else u.push(void 0!==d?{v:r,i:d}:r)};return a(r,s(o)?o.split("."):o,0),h?u:u[0]},ignoreLocation:!1,ignoreFieldNorm:!1,fieldNormWeight:1},p=Object.freeze({isCaseSensitive:!1,ignoreDiacritics:!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,...f}),A=/[^ ]+/g;class m{constructor({getFn:t=p.getFn,fieldNormWeight:e=p.fieldNormWeight}={}){this.norm=function(t=1,e=3){const s=new Map,i=Math.pow(10,e);return{get(e){const n=e.match(A).length;if(s.has(n))return s.get(n);const r=1/Math.pow(n,.5*t),c=parseFloat(Math.round(r*i)/i);return s.set(n,c),c},clear(){s.clear()}}}(e,3),this.getFn=t,this.isCreated=!1,this.docs=[],this.keys=[],this._keysMap={},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,s(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 e=this.size();s(t)?this._addString(t,e):this._addObject(t,e)}removeAt(t){this.records.splice(t,1);for(let e=t,s=this.size();e<s;e+=1)this.records[e].i-=1}removeAll(t){for(let e=t.length-1;e>=0;e-=1)this.records.splice(t[e],1);for(let t=0,e=this.records.length;t<e;t+=1)this.records[t].i=t}getValueForItemAtKeyId(t,e){return t[this._keysMap[e]]}size(){return this.records.length}_addString(t,e){if(!c(t)||o(t))return;const s={v:t,i:e,n:this.norm.get(t)};this.records.push(s)}_addObject(i,n){const r={i:n,$:{}};this.keys.forEach((n,u)=>{const h=n.getFn?n.getFn(i):this.getFn(i,n.path);if(c(h))if(t(h)){const t=[];for(let i=0,n=h.length;i<n;i+=1){const n=h[i];if(c(n))if(s(n)){if(!o(n)){const e={v:n,i:i,n:this.norm.get(n)};t.push(e)}}else if(c(n.v)){const i=s(n.v)?n.v:e(n.v);if(!o(i)){const e={v:i,i:n.i,n:this.norm.get(i)};t.push(e)}}}r.$[u]=t}else if(s(h)&&!o(h)){const t={v:h,n:this.norm.get(h)};r.$[u]=t}}),this.records.push(r)}toJSON(){return{keys:this.keys.map(({getFn:t,...e})=>e),records:this.records}}}function C(t,e,{getFn:s=p.getFn,fieldNormWeight:i=p.fieldNormWeight}={}){const n=new m({getFn:s,fieldNormWeight:i});return n.setKeys(t.map(l)),n.setSources(e),n.create(),n}const M=32;function y(t,e,s,{location:i=p.location,distance:n=p.distance,threshold:r=p.threshold,findAllMatches:c=p.findAllMatches,minMatchCharLength:o=p.minMatchCharLength,includeMatches:u=p.includeMatches,ignoreLocation:h=p.ignoreLocation}={}){if(e.length>M)throw new Error(`Pattern length exceeds max of ${M}.`);const a=e.length,l=t.length,d=Math.max(0,Math.min(i,l));let g=r,f=d;const A=(t,e)=>{const s=t/a;if(h)return s;const i=Math.abs(d-e);return n?s+i/n:i?1:s},m=o>1||u,C=m?Array(l):[];let y;for(;(y=t.indexOf(e,f))>-1;){const t=A(0,y);if(g=Math.min(t,g),f=y+a,m){let t=0;for(;t<a;)C[y+t]=1,t+=1}}f=-1;let x=[],F=1,D=a+l;const E=1<<a-1;for(let e=0;e<a;e+=1){let i=0,n=D;for(;i<n;){A(e,d+n)<=g?i=n:D=n,n=Math.floor((D-i)/2+i)}D=n;let r=Math.max(1,d-n+1);const o=c?l:Math.min(d+n,l)+a,u=Array(o+2);u[o+1]=(1<<e)-1;for(let i=o;i>=r;i-=1){const n=i-1,c=s[t[n]];if(m&&(C[n]=+!!c),u[i]=(u[i+1]<<1|1)&c,e&&(u[i]|=(x[i+1]|x[i])<<1|1|x[i+1]),u[i]&E&&(F=A(e,n),F<=g)){if(g=F,f=n,f<=d)break;r=Math.max(1,2*d-f)}}if(A(e+1,d)>g)break;x=u}const _={isMatch:f>=0,score:Math.max(.001,F)};if(m){const t=function(t=[],e=p.minMatchCharLength){const s=[];let i=-1,n=-1,r=0;for(let c=t.length;r<c;r+=1){const c=t[r];c&&-1===i?i=r:c||-1===i||(n=r-1,n-i+1>=e&&s.push([i,n]),i=-1)}return t[r-1]&&r-i>=e&&s.push([i,r-1]),s}(C,o);t.length?u&&(_.indices=t):_.isMatch=!1}return _}function x(t){const e={};for(let s=0,i=t.length;s<i;s+=1){const n=t.charAt(s);e[n]=(e[n]||0)|1<<i-s-1}return e}function F(t){if(t.length<=1)return t;t.sort((t,e)=>t[0]-e[0]||t[1]-e[1]);const e=[t[0]];for(let s=1,i=t.length;s<i;s+=1){const i=e[e.length-1],n=t[s];n[0]<=i[1]+1?i[1]=Math.max(i[1],n[1]):e.push(n)}return e}const D={"\u0142":"l","\u0141":"L","\u0111":"d","\u0110":"D","\xf8":"o","\xd8":"O","\u0127":"h","\u0126":"H","\u0167":"t","\u0166":"T","\u0131":"i","\xdf":"ss"},E=new RegExp("["+Object.keys(D).join("")+"]","g"),_=String.prototype.normalize?t=>t.normalize("NFD").replace(/[\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u07FD\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08D3-\u08E1\u08E3-\u0903\u093A-\u093C\u093E-\u094F\u0951-\u0957\u0962\u0963\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u09FE\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0AFA-\u0AFF\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B62\u0B63\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C00-\u0C04\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C81-\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D00-\u0D03\u0D3B\u0D3C\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F\u109A-\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u180B-\u180D\u1885\u1886\u18A9\u1920-\u192B\u1930-\u193B\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F\u1AB0-\u1ABE\u1B00-\u1B04\u1B34-\u1B44\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAD\u1BE6-\u1BF3\u1C24-\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE8\u1CED\u1CF2-\u1CF4\u1CF7-\u1CF9\u1DC0-\u1DF9\u1DFB-\u1DFF\u20D0-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C5\uA8E0-\uA8F1\uA8FF\uA926-\uA92D\uA947-\uA953\uA980-\uA983\uA9B3-\uA9C0\uA9E5\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uAA7B-\uAA7D\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEB-\uAAEF\uAAF5\uAAF6\uABE3-\uABEA\uABEC\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE2F]/g,"").replace(E,t=>D[t]):t=>t;class v{constructor(t,{location:e=p.location,threshold:s=p.threshold,distance:i=p.distance,includeMatches:n=p.includeMatches,findAllMatches:r=p.findAllMatches,minMatchCharLength:c=p.minMatchCharLength,isCaseSensitive:o=p.isCaseSensitive,ignoreDiacritics:u=p.ignoreDiacritics,ignoreLocation:h=p.ignoreLocation}={}){if(this.options={location:e,threshold:s,distance:i,includeMatches:n,findAllMatches:r,minMatchCharLength:c,isCaseSensitive:o,ignoreDiacritics:u,ignoreLocation:h},t=o?t:t.toLowerCase(),t=u?_(t):t,this.pattern=t,this.chunks=[],!this.pattern.length)return;const a=(t,e)=>{this.chunks.push({pattern:t,alphabet:x(t),startIndex:e})},l=this.pattern.length;if(l>M){let t=0;const e=l%M,s=l-e;for(;t<s;)a(this.pattern.substr(t,M),t),t+=M;if(e){const t=l-M;a(this.pattern.substr(t),t)}}else a(this.pattern,0)}searchIn(t){const{isCaseSensitive:e,ignoreDiacritics:s,includeMatches:i}=this.options;if(t=e?t:t.toLowerCase(),t=s?_(t):t,this.pattern===t){const e={isMatch:!0,score:0};return i&&(e.indices=[[0,t.length-1]]),e}const{location:n,distance:r,threshold:c,findAllMatches:o,minMatchCharLength:u,ignoreLocation:h}=this.options,a=[];let l=0,d=!1;this.chunks.forEach(({pattern:e,alphabet:s,startIndex:g})=>{const{isMatch:f,score:p,indices:A}=y(t,e,s,{location:n+g,distance:r,threshold:c,findAllMatches:o,minMatchCharLength:u,includeMatches:i,ignoreLocation:h});f&&(d=!0),l+=p,f&&A&&a.push(...A)});const g={isMatch:d,score:d?l/this.chunks.length:1};return d&&i&&(g.indices=F(a)),g}}class B{constructor(t){this.pattern=t}static isMultiMatch(t){return I(t,this.multiRegex)}static isSingleMatch(t){return I(t,this.singleRegex)}search(t){return{isMatch:!1,score:1}}}function I(t,e){const s=t.match(e);return s?s[1]:null}class k extends B{constructor(t,{location:e=p.location,threshold:s=p.threshold,distance:i=p.distance,includeMatches:n=p.includeMatches,findAllMatches:r=p.findAllMatches,minMatchCharLength:c=p.minMatchCharLength,isCaseSensitive:o=p.isCaseSensitive,ignoreDiacritics:u=p.ignoreDiacritics,ignoreLocation:h=p.ignoreLocation}={}){super(t),this._bitapSearch=new v(t,{location:e,threshold:s,distance:i,includeMatches:n,findAllMatches:r,minMatchCharLength:c,isCaseSensitive:o,ignoreDiacritics:u,ignoreLocation:h})}static get type(){return"fuzzy"}static get multiRegex(){return/^"(.*)"$/}static get singleRegex(){return/^(.*)$/}search(t){return this._bitapSearch.searchIn(t)}}class S extends B{constructor(t){super(t)}static get type(){return"include"}static get multiRegex(){return/^'"(.*)"$/}static get singleRegex(){return/^'(.*)$/}search(t){let e,s=0;const i=[],n=this.pattern.length;for(;(e=t.indexOf(this.pattern,s))>-1;)s=e+n,i.push([e,s-1]);const r=!!i.length;return{isMatch:r,score:r?0:1,indices:i}}}const b=[class extends B{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]}}},S,class extends B{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 B{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 B{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 B{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 B{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]}}},k],w=b.length;const L=new Set([k.type,S.type]);class ${constructor(t,{isCaseSensitive:e=p.isCaseSensitive,ignoreDiacritics:s=p.ignoreDiacritics,includeMatches:i=p.includeMatches,minMatchCharLength:n=p.minMatchCharLength,ignoreLocation:r=p.ignoreLocation,findAllMatches:c=p.findAllMatches,location:o=p.location,threshold:u=p.threshold,distance:h=p.distance}={}){this.query=null,this.options={isCaseSensitive:e,ignoreDiacritics:s,includeMatches:i,minMatchCharLength:n,findAllMatches:c,ignoreLocation:r,location:o,threshold:u,distance:h},t=e?t:t.toLowerCase(),t=s?_(t):t,this.pattern=t,this.query=function(t,e={}){return t.replace(/\\\|/g,"\0").split("|").map(t=>{const s=function(t){const e=[],s=t.length;let i=0;for(;i<s;){for(;i<s&&" "===t[i];)i++;if(i>=s)break;let n=i;for(;n<s&&" "!==t[n]&&'"'!==t[n];)n++;if(n<s&&'"'===t[n]){for(n++;n<s;){if('"'===t[n]){const e=n+1;if(e>=s||" "===t[e]){n++;break}if("$"===t[e]&&(e+1>=s||" "===t[e+1])){n+=2;break}}n++}e.push(t.substring(i,n)),i=n}else{for(;n<s&&" "!==t[n];)n++;e.push(t.substring(i,n)),i=n}}return e}(t.replace(/\u0000/g,"|").trim()).filter(t=>t&&!!t.trim()),i=[];for(let t=0,n=s.length;t<n;t+=1){const n=s[t];let r=!1,c=-1;for(;!r&&++c<w;){const t=b[c],s=t.isMultiMatch(n);s&&(i.push(new t(s,e)),r=!0)}if(!r)for(c=-1;++c<w;){const t=b[c],s=t.isSingleMatch(n);if(s){i.push(new t(s,e));break}}}return i})}(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:i,ignoreDiacritics:n}=this.options;t=i?t:t.toLowerCase(),t=n?_(t):t;let r=0;const c=[];let o=0,u=!1;for(let i=0,n=e.length;i<n;i+=1){const n=e[i];c.length=0,r=0,u=!1;for(let e=0,i=n.length;e<i;e+=1){const i=n[e],{isMatch:h,indices:a,score:l}=i.search(t);if(!h){o=0,r=0,c.length=0,u=!1;break}{r+=1,o+=l;const t=i.constructor.type;t.startsWith("inverse")&&(u=!0),s&&(L.has(t)?c.push(...a):c.push(a))}}if(r){const t={isMatch:!0,score:o/r};return u&&(t.hasInverse=!0),s&&(t.indices=F(c)),t}}return{isMatch:!1,score:1}}}const N=[];function R(...t){N.push(...t)}function O(t,e){for(let s=0,i=N.length;s<i;s+=1){const i=N[s];if(i.condition(t,e))return new i(t,e)}return new v(t,e)}const j="$and",W="$or",z="$path",K="$val",T=t=>!(!t[j]&&!t[W]),P=t=>({[j]:Object.keys(t).map(e=>({[e]:t[e]}))});function q(e,i,{auto:n=!0}={}){const c=e=>{if(s(e)){const t={keyId:null,pattern:e};return n&&(t.searcher=O(e,i)),t}const o=Object.keys(e),u=(t=>!!t[z])(e);if(!u&&o.length>1&&!T(e))return c(P(e));if((e=>!t(e)&&r(e)&&!T(e))(e)){const t=u?e[z]:o[0],r=u?e[K]:e[t];if(!s(r))throw new Error((t=>`Invalid value for key ${t}`)(t));const c={keyId:g(t),pattern:r};return n&&(c.searcher=O(r,i)),c}const h={children:[],operator:o[0]};return o.forEach(s=>{const i=e[s];t(i)&&i.forEach(t=>{h.children.push(c(t))})}),h};return T(e)||(e=P(e)),c(e)}function J(t,{ignoreFieldNorm:e=p.ignoreFieldNorm}){let s=1;return t.forEach(({key:t,norm:i,score:n})=>{const r=t?t.weight:null;s*=Math.pow(0===n&&r?Number.EPSILON:n,(r||1)*(e?1:i))}),s}class Q{constructor(t){this.limit=t,this.heap=[]}get size(){return this.heap.length}shouldInsert(t){return this.size<this.limit||t<this.heap[0].score}insert(t){this.size<this.limit?(this.heap.push(t),this._bubbleUp(this.size-1)):t.score<this.heap[0].score&&(this.heap[0]=t,this._sinkDown(0))}extractSorted(t){return this.heap.sort(t)}_bubbleUp(t){const e=this.heap;for(;t>0;){const s=t-1>>1;if(e[t].score<=e[s].score)break;const i=e[t];e[t]=e[s],e[s]=i,t=s}}_sinkDown(t){const e=this.heap,s=e.length;let i=t;do{const n=2*(t=i)+1,r=2*t+2;if(n<s&&e[n].score>e[i].score&&(i=n),r<s&&e[r].score>e[i].score&&(i=r),i!==t){const s=e[t];e[t]=e[i],e[i]=s}}while(i!==t)}}function U(t,e){const s=t.matches;e.matches=[],c(s)&&s.forEach(t=>{if(!c(t.indices)||!t.indices.length)return;const{indices:s,value:i}=t,n={indices:s,value:i};t.key&&(n.key=t.key.src),t.idx>-1&&(n.refIndex=t.idx),e.matches.push(n)})}function V(t,e){e.score=t.score}const H=/\b\w+\b/g;function G({isCaseSensitive:t=!1,ignoreDiacritics:e=!1}={}){return{tokenize:s=>(t||(s=s.toLowerCase()),e&&(s=_(s)),s.match(H)||[])}}function X(t,e){for(const[s,i]of t.terms){const n=i.filter(t=>t.docIdx!==e),r=i.length-n.length;r>0&&(t.fieldCount-=r,t.df.set(s,(t.df.get(s)||0)-r),0===n.length?(t.terms.delete(s),t.df.delete(s)):t.terms.set(s,n))}}class Y{constructor(t,e,s){this.options={...p,...e},this.options.useExtendedSearch,this.options.useTokenSearch,this._keyStore=new a(this.options.keys),this._docs=t,this._myIndex=null,this._invertedIndex=null,this.setCollection(t,s),this._lastQuery=null,this._lastSearcher=null}_getSearcher(t){if(this._lastQuery===t)return this._lastSearcher;const e=O(t,this._invertedIndex?{...this.options,_invertedIndex:this._invertedIndex}:this.options);return this._lastQuery=t,this._lastSearcher=e,e}setCollection(t,e){if(this._docs=t,e&&!(e instanceof m))throw new Error("Incorrect 'index' type");if(this._myIndex=e||C(this.options.keys,this._docs,{getFn:this.options.getFn,fieldNormWeight:this.options.fieldNormWeight}),this.options.useTokenSearch){const t=G({isCaseSensitive:this.options.isCaseSensitive,ignoreDiacritics:this.options.ignoreDiacritics});this._invertedIndex=function(t,e,s){const i=new Map,n=new Map;let r=0;function c(t,e,c,o){const u=s.tokenize(t);if(!u.length)return;r++;const h=new Map;for(const t of u)h.set(t,(h.get(t)||0)+1);for(const[t,s]of h){const r={docIdx:e,keyIdx:c,subIdx:o,tf:s};let u=i.get(t);u||(u=[],i.set(t,u)),u.push(r),n.set(t,(n.get(t)||0)+1)}}for(const s of t){const{i:t,v:i,$:n}=s;if(void 0===i){if(n)for(let s=0;s<e;s++){const e=n[s];if(e)if(Array.isArray(e))for(const i of e)c(i.v,t,s,i.i??-1);else c(e.v,t,s,-1)}}else c(i,t,-1,-1)}return{terms:i,fieldCount:r,df:n}}(this._myIndex.records,this._myIndex.keys.length,t)}}add(t){if(c(t)&&(this._docs.push(t),this._myIndex.add(t),this._invertedIndex)){const t=this._myIndex.records[this._myIndex.records.length-1],e=G({isCaseSensitive:this.options.isCaseSensitive,ignoreDiacritics:this.options.ignoreDiacritics});!function(t,e,s,i){const{i:n,v:r,$:c}=e;function o(e,s,r){const c=i.tokenize(e);if(!c.length)return;t.fieldCount++;const o=new Map;for(const t of c)o.set(t,(o.get(t)||0)+1);for(const[e,i]of o){const c={docIdx:n,keyIdx:s,subIdx:r,tf:i};let o=t.terms.get(e);o||(o=[],t.terms.set(e,o)),o.push(c),t.df.set(e,(t.df.get(e)||0)+1)}}if(void 0===r){if(c)for(let t=0;t<s;t++){const e=c[t];if(e)if(Array.isArray(e))for(const s of e)o(s.v,t,s.i??-1);else o(e.v,t,-1)}}else o(r,-1,-1)}(this._invertedIndex,t,this._myIndex.keys.length,e)}}remove(t=()=>!1){const e=[],s=[];for(let i=0,n=this._docs.length;i<n;i+=1)t(this._docs[i],i)&&(e.push(this._docs[i]),s.push(i));if(s.length){if(this._invertedIndex)for(const t of s)X(this._invertedIndex,t);for(let t=s.length-1;t>=0;t-=1)this._docs.splice(s[t],1);this._myIndex.removeAll(s)}return e}removeAt(t){this._invertedIndex&&X(this._invertedIndex,t);const e=this._docs.splice(t,1)[0];return this._myIndex.removeAt(t),e}getIndex(){return this._myIndex}search(t,e){const{limit:n=-1}=e||{},{includeMatches:r,includeScore:c,shouldSort:o,sortFn:u,ignoreFieldNorm:h}=this.options;if(s(t)&&!t.trim()){let t=this._docs.map((t,e)=>({item:t,refIndex:e}));return i(n)&&n>-1&&(t=t.slice(0,n)),t}let a;if(i(n)&&n>0&&s(t)){const e=new Q(n);s(this._docs[0])?this._searchStringList(t,{heap:e,ignoreFieldNorm:h}):this._searchObjectList(t,{heap:e,ignoreFieldNorm:h}),a=e.extractSorted(u)}else a=s(t)?s(this._docs[0])?this._searchStringList(t):this._searchObjectList(t):this._searchLogical(t),function(t,{ignoreFieldNorm:e=p.ignoreFieldNorm}){t.forEach(t=>{t.score=J(t.matches,{ignoreFieldNorm:e})})}(a,{ignoreFieldNorm:h}),o&&a.sort(u),i(n)&&n>-1&&(a=a.slice(0,n));return function(t,e,{includeMatches:s=p.includeMatches,includeScore:i=p.includeScore}={}){const n=[];return s&&n.push(U),i&&n.push(V),t.map(t=>{const{idx:s}=t,i={item:e[s],refIndex:s};return n.length&&n.forEach(e=>{e(t,i)}),i})}(a,this._docs,{includeMatches:r,includeScore:c})}_searchStringList(t,{heap:e,ignoreFieldNorm:s}={}){const i=this._getSearcher(t),{records:n}=this._myIndex,r=e?null:[];return n.forEach(({v:t,i:n,n:o})=>{if(!c(t))return;const{isMatch:u,score:h,indices:a}=i.searchIn(t);if(u){const i={item:t,idx:n,matches:[{score:h,value:t,norm:o,indices:a}]};e?(i.score=J(i.matches,{ignoreFieldNorm:s}),e.shouldInsert(i.score)&&e.insert(i)):r.push(i)}}),r}_searchLogical(t){const e=q(t,this.options),s=(t,e,i)=>{if(!("children"in t)){const{keyId:s,searcher:n}=t;let r;return null===s?(r=[],this._myIndex.keys.forEach((t,s)=>{r.push(...this._findMatches({key:t,value:e[s],searcher:n}))})):r=this._findMatches({key:this._keyStore.get(s),value:this._myIndex.getValueForItemAtKeyId(e,s),searcher:n}),r&&r.length?[{idx:i,item:e,matches:r}]:[]}const{children:n,operator:r}=t,c=[];for(let t=0,o=n.length;t<o;t+=1){const o=n[t],u=s(o,e,i);if(u.length)c.push(...u);else if(r===j)return[]}return c},i=this._myIndex.records,n=new Map,r=[];return i.forEach(({$:t,i:i})=>{if(c(t)){const c=s(e,t,i);c.length&&(n.has(i)||(n.set(i,{idx:i,item:t,matches:[]}),r.push(n.get(i))),c.forEach(({matches:t})=>{n.get(i).matches.push(...t)}))}}),r}_searchObjectList(t,{heap:e,ignoreFieldNorm:s}={}){const i=this._getSearcher(t),{keys:n,records:r}=this._myIndex,o=e?null:[];return r.forEach(({$:t,i:r})=>{if(!c(t))return;const u=[];let h=!1,a=!1;if(n.forEach((e,s)=>{const n=this._findMatches({key:e,value:t[s],searcher:i});n.length?(u.push(...n),n[0].hasInverse&&(a=!0)):h=!0}),(!a||!h)&&u.length){const i={idx:r,item:t,matches:u};e?(i.score=J(i.matches,{ignoreFieldNorm:s}),e.shouldInsert(i.score)&&e.insert(i)):o.push(i)}}),o}_findMatches({key:e,value:s,searcher:i}){if(!c(s))return[];const n=[];if(t(s))s.forEach(({v:t,i:s,n:r})=>{if(!c(t))return;const{isMatch:o,score:u,indices:h,hasInverse:a}=i.searchIn(t);o&&n.push({score:u,key:e,value:t,idx:s,norm:r,indices:h,hasInverse:a})});else{const{v:t,n:r}=s,{isMatch:c,score:o,indices:u,hasInverse:h}=i.searchIn(t);c&&n.push({score:o,key:e,value:t,norm:r,indices:u,hasInverse:h})}return n}}class Z{static condition(t,e){return e.useTokenSearch}constructor(t,e){this.options=e,this.analyzer=G({isCaseSensitive:e.isCaseSensitive,ignoreDiacritics:e.ignoreDiacritics});const s=this.analyzer.tokenize(t),i=e._invertedIndex,{df:n,fieldCount:r}=i;this.termSearchers=[],this.idfWeights=[];for(const t of s){this.termSearchers.push(new v(t,{location:e.location,threshold:e.threshold,distance:e.distance,includeMatches:e.includeMatches,findAllMatches:e.findAllMatches,minMatchCharLength:e.minMatchCharLength,isCaseSensitive:e.isCaseSensitive,ignoreDiacritics:e.ignoreDiacritics,ignoreLocation:!0}));const s=n.get(t)||0,i=Math.log(1+(r-s+.5)/(s+.5));this.idfWeights.push(i)}}searchIn(t){if(!this.termSearchers.length)return{isMatch:!1,score:1};const e=[];let s=0,i=0,n=0;for(let r=0;r<this.termSearchers.length;r++){const c=this.termSearchers[r].searchIn(t),o=this.idfWeights[r];i+=o,c.isMatch&&(n++,s+=o*(1-c.score),c.indices&&e.push(...c.indices))}if(0===n)return{isMatch:!1,score:1};const r=i>0?1-s/i:0,c={isMatch:!0,score:Math.max(.001,r)};return this.options.includeMatches&&e.length&&(c.indices=F(e)),c}}Y.version="7.3.0",Y.createIndex=C,Y.parseIndex=function(t,{getFn:e=p.getFn,fieldNormWeight:s=p.fieldNormWeight}={}){const{keys:i,records:n}=t,r=new m({getFn:e,fieldNormWeight:s});return r.setKeys(i),r.setIndexRecords(n),r},Y.config=p,Y.match=function(t,e,s){return O(t,{...p,...s}).searchIn(e)},R($),R(Z),Y.use=function(...t){t.forEach(t=>R(t))};export{Y as default};