3 lines
18 KiB
JavaScript
3 lines
18 KiB
JavaScript
/* esm.sh - esbuild bundle(minisearch@7.1.0) es2022 production */
|
|
function M(i,t,e,s){function n(o){return o instanceof e?o:new e(function(r){r(o)})}return new(e||(e=Promise))(function(o,r){function c(h){try{u(s.next(h))}catch(l){r(l)}}function d(h){try{u(s.throw(h))}catch(l){r(l)}}function u(h){h.done?o(h.value):n(h.value).then(c,d)}u((s=s.apply(i,t||[])).next())})}var X="ENTRIES",U="KEYS",q="VALUES",p="",v=class{constructor(t,e){let s=t._tree,n=Array.from(s.keys());this.set=t,this._type=e,this._path=n.length>0?[{node:s,keys:n}]:[]}next(){let t=this.dive();return this.backtrack(),t}dive(){if(this._path.length===0)return{done:!0,value:void 0};let{node:t,keys:e}=S(this._path);if(S(e)===p)return{done:!1,value:this.result()};let s=t.get(S(e));return this._path.push({node:s,keys:Array.from(s.keys())}),this.dive()}backtrack(){if(this._path.length===0)return;let t=S(this._path).keys;t.pop(),!(t.length>0)&&(this._path.pop(),this.backtrack())}key(){return this.set._prefix+this._path.map(({keys:t})=>S(t)).filter(t=>t!==p).join("")}value(){return S(this._path).node.get(p)}result(){switch(this._type){case q:return this.value();case U:return this.key();default:return[this.key(),this.value()]}}[Symbol.iterator](){return this}},S=i=>i[i.length-1],tt=(i,t,e)=>{let s=new Map;if(t===void 0)return s;let n=t.length+1,o=n+e,r=new Uint8Array(o*n).fill(e+1);for(let c=0;c<n;++c)r[c]=c;for(let c=1;c<o;++c)r[c*n]=c;return K(i,t,e,s,r,1,n,""),s},K=(i,t,e,s,n,o,r,c)=>{let d=o*r;t:for(let u of i.keys())if(u===p){let h=n[d-1];h<=e&&s.set(c,[i.get(u),h])}else{let h=o;for(let l=0;l<u.length;++l,++h){let a=u[l],m=r*h,_=m-r,f=n[m],g=Math.max(0,h-e-1),b=Math.min(r-1,h+e);for(let w=g;w<b;++w){let V=a!==t[w],C=n[_+w]+ +V,I=n[_+w+1]+1,y=n[m+w]+1,z=n[m+w+1]=Math.min(C,I,y);z<f&&(f=z)}if(f>e)continue t}K(i.get(u),t,e,s,n,h,r,c+u)}},x=class i{constructor(t=new Map,e=""){this._size=void 0,this._tree=t,this._prefix=e}atPrefix(t){if(!t.startsWith(this._prefix))throw new Error("Mismatched prefix");let[e,s]=k(this._tree,t.slice(this._prefix.length));if(e===void 0){let[n,o]=R(s);for(let r of n.keys())if(r!==p&&r.startsWith(o)){let c=new Map;return c.set(r.slice(o.length),n.get(r)),new i(c,t)}}return new i(e,t)}clear(){this._size=void 0,this._tree.clear()}delete(t){return this._size=void 0,et(this._tree,t)}entries(){return new v(this,X)}forEach(t){for(let[e,s]of this)t(e,s,this)}fuzzyGet(t,e){return tt(this._tree,t,e)}get(t){let e=A(this._tree,t);return e!==void 0?e.get(p):void 0}has(t){let e=A(this._tree,t);return e!==void 0&&e.has(p)}keys(){return new v(this,U)}set(t,e){if(typeof t!="string")throw new Error("key must be a string");return this._size=void 0,j(this._tree,t).set(p,e),this}get size(){if(this._size)return this._size;this._size=0;let t=this.entries();for(;!t.next().done;)this._size+=1;return this._size}update(t,e){if(typeof t!="string")throw new Error("key must be a string");this._size=void 0;let s=j(this._tree,t);return s.set(p,e(s.get(p))),this}fetch(t,e){if(typeof t!="string")throw new Error("key must be a string");this._size=void 0;let s=j(this._tree,t),n=s.get(p);return n===void 0&&s.set(p,n=e()),n}values(){return new v(this,q)}[Symbol.iterator](){return this.entries()}static from(t){let e=new i;for(let[s,n]of t)e.set(s,n);return e}static fromObject(t){return i.from(Object.entries(t))}},k=(i,t,e=[])=>{if(t.length===0||i==null)return[i,e];for(let s of i.keys())if(s!==p&&t.startsWith(s))return e.push([i,s]),k(i.get(s),t.slice(s.length),e);return e.push([i,t]),k(void 0,"",e)},A=(i,t)=>{if(t.length===0||i==null)return i;for(let e of i.keys())if(e!==p&&t.startsWith(e))return A(i.get(e),t.slice(e.length))},j=(i,t)=>{let e=t.length;t:for(let s=0;i&&s<e;){for(let o of i.keys())if(o!==p&&t[s]===o[0]){let r=Math.min(e-s,o.length),c=1;for(;c<r&&t[s+c]===o[c];)++c;let d=i.get(o);if(c===o.length)i=d;else{let u=new Map;u.set(o.slice(c),d),i.set(t.slice(s,s+c),u),i.delete(o),i=u}s+=c;continue t}let n=new Map;return i.set(t.slice(s),n),n}return i},et=(i,t)=>{let[e,s]=k(i,t);if(e!==void 0){if(e.delete(p),e.size===0)G(s);else if(e.size===1){let[n,o]=e.entries().next().value;Y(s,n,o)}}},G=i=>{if(i.length===0)return;let[t,e]=R(i);if(t.delete(e),t.size===0)G(i.slice(0,-1));else if(t.size===1){let[s,n]=t.entries().next().value;s!==p&&Y(i.slice(0,-1),s,n)}},Y=(i,t,e)=>{if(i.length===0)return;let[s,n]=R(i);s.set(n+t,e),s.delete(n)},R=i=>i[i.length-1],P="or",Z="and",st="and_not",D=class i{constructor(t){if(t?.fields==null)throw new Error('MiniSearch: option "fields" must be provided');let e=t.autoVacuum==null||t.autoVacuum===!0?E:t.autoVacuum;this._options=Object.assign(Object.assign(Object.assign({},T),t),{autoVacuum:e,searchOptions:Object.assign(Object.assign({},J),t.searchOptions||{}),autoSuggestOptions:Object.assign(Object.assign({},ct),t.autoSuggestOptions||{})}),this._index=new x,this._documentCount=0,this._documentIds=new Map,this._idToShortId=new Map,this._fieldIds={},this._fieldLength=new Map,this._avgFieldLength=[],this._nextId=0,this._storedFields=new Map,this._dirtCount=0,this._currentVacuum=null,this._enqueuedVacuum=null,this._enqueuedVacuumConditions=W,this.addFields(this._options.fields)}add(t){let{extractField:e,tokenize:s,processTerm:n,fields:o,idField:r}=this._options,c=e(t,r);if(c==null)throw new Error(`MiniSearch: document does not have ID field "${r}"`);if(this._idToShortId.has(c))throw new Error(`MiniSearch: duplicate ID ${c}`);let d=this.addDocumentId(c);this.saveStoredFields(d,t);for(let u of o){let h=e(t,u);if(h==null)continue;let l=s(h.toString(),u),a=this._fieldIds[u],m=new Set(l).size;this.addFieldLength(d,a,this._documentCount-1,m);for(let _ of l){let f=n(_,u);if(Array.isArray(f))for(let g of f)this.addTerm(a,d,g);else f&&this.addTerm(a,d,f)}}}addAll(t){for(let e of t)this.add(e)}addAllAsync(t,e={}){let{chunkSize:s=10}=e,n={chunk:[],promise:Promise.resolve()},{chunk:o,promise:r}=t.reduce(({chunk:c,promise:d},u,h)=>(c.push(u),(h+1)%s===0?{chunk:[],promise:d.then(()=>new Promise(l=>setTimeout(l,0))).then(()=>this.addAll(c))}:{chunk:c,promise:d}),n);return r.then(()=>this.addAll(o))}remove(t){let{tokenize:e,processTerm:s,extractField:n,fields:o,idField:r}=this._options,c=n(t,r);if(c==null)throw new Error(`MiniSearch: document does not have ID field "${r}"`);let d=this._idToShortId.get(c);if(d==null)throw new Error(`MiniSearch: cannot remove document with ID ${c}: it is not in the index`);for(let u of o){let h=n(t,u);if(h==null)continue;let l=e(h.toString(),u),a=this._fieldIds[u],m=new Set(l).size;this.removeFieldLength(d,a,this._documentCount,m);for(let _ of l){let f=s(_,u);if(Array.isArray(f))for(let g of f)this.removeTerm(a,d,g);else f&&this.removeTerm(a,d,f)}}this._storedFields.delete(d),this._documentIds.delete(d),this._idToShortId.delete(c),this._fieldLength.delete(d),this._documentCount-=1}removeAll(t){if(t)for(let e of t)this.remove(e);else{if(arguments.length>0)throw new Error("Expected documents to be present. Omit the argument to remove all documents.");this._index=new x,this._documentCount=0,this._documentIds=new Map,this._idToShortId=new Map,this._fieldLength=new Map,this._avgFieldLength=[],this._storedFields=new Map,this._nextId=0}}discard(t){let e=this._idToShortId.get(t);if(e==null)throw new Error(`MiniSearch: cannot discard document with ID ${t}: it is not in the index`);this._idToShortId.delete(t),this._documentIds.delete(e),this._storedFields.delete(e),(this._fieldLength.get(e)||[]).forEach((s,n)=>{this.removeFieldLength(e,n,this._documentCount,s)}),this._fieldLength.delete(e),this._documentCount-=1,this._dirtCount+=1,this.maybeAutoVacuum()}maybeAutoVacuum(){if(this._options.autoVacuum===!1)return;let{minDirtFactor:t,minDirtCount:e,batchSize:s,batchWait:n}=this._options.autoVacuum;this.conditionalVacuum({batchSize:s,batchWait:n},{minDirtCount:e,minDirtFactor:t})}discardAll(t){let e=this._options.autoVacuum;try{this._options.autoVacuum=!1;for(let s of t)this.discard(s)}finally{this._options.autoVacuum=e}this.maybeAutoVacuum()}replace(t){let{idField:e,extractField:s}=this._options,n=s(t,e);this.discard(n),this.add(t)}vacuum(t={}){return this.conditionalVacuum(t)}conditionalVacuum(t,e){return this._currentVacuum?(this._enqueuedVacuumConditions=this._enqueuedVacuumConditions&&e,this._enqueuedVacuum!=null?this._enqueuedVacuum:(this._enqueuedVacuum=this._currentVacuum.then(()=>{let s=this._enqueuedVacuumConditions;return this._enqueuedVacuumConditions=W,this.performVacuuming(t,s)}),this._enqueuedVacuum)):this.vacuumConditionsMet(e)===!1?Promise.resolve():(this._currentVacuum=this.performVacuuming(t),this._currentVacuum)}performVacuuming(t,e){return M(this,void 0,void 0,function*(){let s=this._dirtCount;if(this.vacuumConditionsMet(e)){let n=t.batchSize||N.batchSize,o=t.batchWait||N.batchWait,r=1;for(let[c,d]of this._index){for(let[u,h]of d)for(let[l]of h)this._documentIds.has(l)||(h.size<=1?d.delete(u):h.delete(l));this._index.get(c).size===0&&this._index.delete(c),r%n===0&&(yield new Promise(u=>setTimeout(u,o))),r+=1}this._dirtCount-=s}yield null,this._currentVacuum=this._enqueuedVacuum,this._enqueuedVacuum=null})}vacuumConditionsMet(t){if(t==null)return!0;let{minDirtCount:e,minDirtFactor:s}=t;return e=e||E.minDirtCount,s=s||E.minDirtFactor,this.dirtCount>=e&&this.dirtFactor>=s}get isVacuuming(){return this._currentVacuum!=null}get dirtCount(){return this._dirtCount}get dirtFactor(){return this._dirtCount/(1+this._documentCount+this._dirtCount)}has(t){return this._idToShortId.has(t)}getStoredFields(t){let e=this._idToShortId.get(t);if(e!=null)return this._storedFields.get(e)}search(t,e={}){let s=this.executeQuery(t,e),n=[];for(let[o,{score:r,terms:c,match:d}]of s){let u=c.length||1,h={id:this._documentIds.get(o),score:r*u,terms:Object.keys(d),queryTerms:c,match:d};Object.assign(h,this._storedFields.get(o)),(e.filter==null||e.filter(h))&&n.push(h)}return t===i.wildcard&&e.boostDocument==null&&this._options.searchOptions.boostDocument==null||n.sort(Q),n}autoSuggest(t,e={}){e=Object.assign(Object.assign({},this._options.autoSuggestOptions),e);let s=new Map;for(let{score:o,terms:r}of this.search(t,e)){let c=r.join(" "),d=s.get(c);d!=null?(d.score+=o,d.count+=1):s.set(c,{score:o,terms:r,count:1})}let n=[];for(let[o,{score:r,terms:c,count:d}]of s)n.push({suggestion:o,terms:c,score:r/d});return n.sort(Q),n}get documentCount(){return this._documentCount}get termCount(){return this._index.size}static loadJSON(t,e){if(e==null)throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");return this.loadJS(JSON.parse(t),e)}static loadJSONAsync(t,e){return M(this,void 0,void 0,function*(){if(e==null)throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");return this.loadJSAsync(JSON.parse(t),e)})}static getDefault(t){if(T.hasOwnProperty(t))return L(T,t);throw new Error(`MiniSearch: unknown option "${t}"`)}static loadJS(t,e){let{index:s,documentIds:n,fieldLength:o,storedFields:r,serializationVersion:c}=t,d=this.instantiateMiniSearch(t,e);d._documentIds=O(n),d._fieldLength=O(o),d._storedFields=O(r);for(let[u,h]of d._documentIds)d._idToShortId.set(h,u);for(let[u,h]of s){let l=new Map;for(let a of Object.keys(h)){let m=h[a];c===1&&(m=m.ds),l.set(parseInt(a,10),O(m))}d._index.set(u,l)}return d}static loadJSAsync(t,e){return M(this,void 0,void 0,function*(){let{index:s,documentIds:n,fieldLength:o,storedFields:r,serializationVersion:c}=t,d=this.instantiateMiniSearch(t,e);d._documentIds=yield F(n),d._fieldLength=yield F(o),d._storedFields=yield F(r);for(let[h,l]of d._documentIds)d._idToShortId.set(l,h);let u=0;for(let[h,l]of s){let a=new Map;for(let m of Object.keys(l)){let _=l[m];c===1&&(_=_.ds),a.set(parseInt(m,10),yield F(_))}++u%1e3===0&&(yield H(0)),d._index.set(h,a)}return d})}static instantiateMiniSearch(t,e){let{documentCount:s,nextId:n,fieldIds:o,averageFieldLength:r,dirtCount:c,serializationVersion:d}=t;if(d!==1&&d!==2)throw new Error("MiniSearch: cannot deserialize an index created with an incompatible version");let u=new i(e);return u._documentCount=s,u._nextId=n,u._idToShortId=new Map,u._fieldIds=o,u._avgFieldLength=r,u._dirtCount=c||0,u._index=new x,u}executeQuery(t,e={}){if(t===i.wildcard)return this.executeWildcardQuery(e);if(typeof t!="string"){let a=Object.assign(Object.assign(Object.assign({},e),t),{queries:void 0}),m=t.queries.map(_=>this.executeQuery(_,a));return this.combineResults(m,a.combineWith)}let{tokenize:s,processTerm:n,searchOptions:o}=this._options,r=Object.assign(Object.assign({tokenize:s,processTerm:n},o),e),{tokenize:c,processTerm:d}=r,l=c(t).flatMap(a=>d(a)).filter(a=>!!a).map(rt(r)).map(a=>this.executeQuerySpec(a,r));return this.combineResults(l,r.combineWith)}executeQuerySpec(t,e){let s=Object.assign(Object.assign({},this._options.searchOptions),e),n=(s.fields||this._options.fields).reduce((f,g)=>Object.assign(Object.assign({},f),{[g]:L(s.boost,g)||1}),{}),{boostDocument:o,weights:r,maxFuzzy:c,bm25:d}=s,{fuzzy:u,prefix:h}=Object.assign(Object.assign({},J.weights),r),l=this._index.get(t.term),a=this.termResults(t.term,t.term,1,t.termBoost,l,n,o,d),m,_;if(t.prefix&&(m=this._index.atPrefix(t.term)),t.fuzzy){let f=t.fuzzy===!0?.2:t.fuzzy,g=f<1?Math.min(c,Math.round(t.term.length*f)):f;g&&(_=this._index.fuzzyGet(t.term,g))}if(m)for(let[f,g]of m){let b=f.length-t.term.length;if(!b)continue;_?.delete(f);let w=h*f.length/(f.length+.3*b);this.termResults(t.term,f,w,t.termBoost,g,n,o,d,a)}if(_)for(let f of _.keys()){let[g,b]=_.get(f);if(!b)continue;let w=u*f.length/(f.length+b);this.termResults(t.term,f,w,t.termBoost,g,n,o,d,a)}return a}executeWildcardQuery(t){let e=new Map,s=Object.assign(Object.assign({},this._options.searchOptions),t);for(let[n,o]of this._documentIds){let r=s.boostDocument?s.boostDocument(o,"",this._storedFields.get(n)):1;e.set(n,{score:r,terms:[],match:{}})}return e}combineResults(t,e=P){if(t.length===0)return new Map;let s=e.toLowerCase(),n=nt[s];if(!n)throw new Error(`Invalid combination operator: ${e}`);return t.reduce(n)||new Map}toJSON(){let t=[];for(let[e,s]of this._index){let n={};for(let[o,r]of s)n[o]=Object.fromEntries(r);t.push([e,n])}return{documentCount:this._documentCount,nextId:this._nextId,documentIds:Object.fromEntries(this._documentIds),fieldIds:this._fieldIds,fieldLength:Object.fromEntries(this._fieldLength),averageFieldLength:this._avgFieldLength,storedFields:Object.fromEntries(this._storedFields),dirtCount:this._dirtCount,index:t,serializationVersion:2}}termResults(t,e,s,n,o,r,c,d,u=new Map){if(o==null)return u;for(let h of Object.keys(r)){let l=r[h],a=this._fieldIds[h],m=o.get(a);if(m==null)continue;let _=m.size,f=this._avgFieldLength[a];for(let g of m.keys()){if(!this._documentIds.has(g)){this.removeTerm(a,g,e),_-=1;continue}let b=c?c(this._documentIds.get(g),e,this._storedFields.get(g)):1;if(!b)continue;let w=m.get(g),V=this._fieldLength.get(g)[a],C=ot(w,_,this._documentCount,V,f,d),I=s*n*l*b*C,y=u.get(g);if(y){y.score+=I,dt(y.terms,t);let z=L(y.match,e);z?z.push(h):y.match[e]=[h]}else u.set(g,{score:I,terms:[t],match:{[e]:[h]}})}}return u}addTerm(t,e,s){let n=this._index.fetch(s,B),o=n.get(t);if(o==null)o=new Map,o.set(e,1),n.set(t,o);else{let r=o.get(e);o.set(e,(r||0)+1)}}removeTerm(t,e,s){if(!this._index.has(s)){this.warnDocumentChanged(e,t,s);return}let n=this._index.fetch(s,B),o=n.get(t);o==null||o.get(e)==null?this.warnDocumentChanged(e,t,s):o.get(e)<=1?o.size<=1?n.delete(t):o.delete(e):o.set(e,o.get(e)-1),this._index.get(s).size===0&&this._index.delete(s)}warnDocumentChanged(t,e,s){for(let n of Object.keys(this._fieldIds))if(this._fieldIds[n]===e){this._options.logger("warn",`MiniSearch: document with ID ${this._documentIds.get(t)} has changed before removal: term "${s}" was not present in field "${n}". Removing a document after it has changed can corrupt the index!`,"version_conflict");return}}addDocumentId(t){let e=this._nextId;return this._idToShortId.set(t,e),this._documentIds.set(e,t),this._documentCount+=1,this._nextId+=1,e}addFields(t){for(let e=0;e<t.length;e++)this._fieldIds[t[e]]=e}addFieldLength(t,e,s,n){let o=this._fieldLength.get(t);o==null&&this._fieldLength.set(t,o=[]),o[e]=n;let c=(this._avgFieldLength[e]||0)*s+n;this._avgFieldLength[e]=c/(s+1)}removeFieldLength(t,e,s,n){if(s===1){this._avgFieldLength[e]=0;return}let o=this._avgFieldLength[e]*s-n;this._avgFieldLength[e]=o/(s-1)}saveStoredFields(t,e){let{storeFields:s,extractField:n}=this._options;if(s==null||s.length===0)return;let o=this._storedFields.get(t);o==null&&this._storedFields.set(t,o={});for(let r of s){let c=n(e,r);c!==void 0&&(o[r]=c)}}};D.wildcard=Symbol("*");var L=(i,t)=>Object.prototype.hasOwnProperty.call(i,t)?i[t]:void 0,nt={[P]:(i,t)=>{for(let e of t.keys()){let s=i.get(e);if(s==null)i.set(e,t.get(e));else{let{score:n,terms:o,match:r}=t.get(e);s.score=s.score+n,s.match=Object.assign(s.match,r),$(s.terms,o)}}return i},[Z]:(i,t)=>{let e=new Map;for(let s of t.keys()){let n=i.get(s);if(n==null)continue;let{score:o,terms:r,match:c}=t.get(s);$(n.terms,r),e.set(s,{score:n.score+o,terms:n.terms,match:Object.assign(n.match,c)})}return e},[st]:(i,t)=>{for(let e of t.keys())i.delete(e);return i}},it={k:1.2,b:.7,d:.5},ot=(i,t,e,s,n,o)=>{let{k:r,b:c,d}=o;return Math.log(1+(e-t+.5)/(t+.5))*(d+i*(r+1)/(i+r*(1-c+c*s/n)))},rt=i=>(t,e,s)=>{let n=typeof i.fuzzy=="function"?i.fuzzy(t,e,s):i.fuzzy||!1,o=typeof i.prefix=="function"?i.prefix(t,e,s):i.prefix===!0,r=typeof i.boostTerm=="function"?i.boostTerm(t,e,s):1;return{term:t,fuzzy:n,prefix:o,termBoost:r}},T={idField:"id",extractField:(i,t)=>i[t],tokenize:i=>i.split(ut),processTerm:i=>i.toLowerCase(),fields:void 0,searchOptions:void 0,storeFields:[],logger:(i,t)=>{typeof console?.[i]=="function"&&console[i](t)},autoVacuum:!0},J={combineWith:P,prefix:!1,fuzzy:!1,maxFuzzy:6,boost:{},weights:{fuzzy:.45,prefix:.375},bm25:it},ct={combineWith:Z,prefix:(i,t,e)=>t===e.length-1},N={batchSize:1e3,batchWait:10},W={minDirtFactor:.1,minDirtCount:20},E=Object.assign(Object.assign({},N),W),dt=(i,t)=>{i.includes(t)||i.push(t)},$=(i,t)=>{for(let e of t)i.includes(e)||i.push(e)},Q=({score:i},{score:t})=>t-i,B=()=>new Map,O=i=>{let t=new Map;for(let e of Object.keys(i))t.set(parseInt(e,10),i[e]);return t},F=i=>M(void 0,void 0,void 0,function*(){let t=new Map,e=0;for(let s of Object.keys(i))t.set(parseInt(s,10),i[s]),++e%1e3===0&&(yield H(0));return t}),H=i=>new Promise(t=>setTimeout(t,i)),ut=/[\n\r\p{Z}\p{P}]+/u;export{D as default};
|
|
//# sourceMappingURL=minisearch.mjs.map
|