UNPKG

minisearch-synonyms

Version:
2 lines (1 loc) 3.19 kB
"use strict";var MiniSearchSynonyms=(()=>{var f=Object.defineProperty,k=Object.defineProperties,x=Object.getOwnPropertyDescriptor,b=Object.getOwnPropertyDescriptors,S=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var y=(r,t,n)=>t in r?f(r,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[t]=n,c=(r,t)=>{for(var n in t||(t={}))w.call(t,n)&&y(r,n,t[n]);if(m)for(var n of m(t))W.call(t,n)&&y(r,n,t[n]);return r},v=(r,t)=>k(r,b(t));var L=(r,t)=>{for(var n in t)f(r,n,{get:t[n],enumerable:!0})},T=(r,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of S(t))!w.call(r,e)&&e!==n&&f(r,e,{get:()=>t[e],enumerable:!(s=x(t,e))||s.enumerable});return r};var E=r=>T(f({},"__esModule",{value:!0}),r);var j={};L(j,{MiniSearchSynonyms:()=>d,default:()=>O});function C(r,t){let n=/[\n\r\p{Z}\p{P}]/u,s=[],e=`${r.toLowerCase()}`,i=0,o={type:"separator",position:0,length:0,value:""};for(;e.length>0;){let a,g=1;if(o.type==="separator"){let l;for(let p of t)if(e.startsWith(p)){l=p;break}l?(a={type:"synonym",position:i,length:l.length,value:e.slice(0,l.length)},g=l.length):e[0].match(n)?(o.length+=1,o.value+=e[0]):a={type:"word",position:i,length:1,value:e[0]}}else if(o.type==="synonym")if(e[0].match(n))a={type:"separator",position:i,length:1,value:e[0]};else{let l=t.filter(u=>u!==o.value.toLowerCase()),p=o.value.toLowerCase()+e,h=l.find(u=>p.startsWith(u));h?(g=h.length-o.length,o.length=h.length,o.value=h):(o.type="word",o.length+=1,o.value+=e[0])}else e[0].match(n)?a={type:"separator",position:i,length:1,value:e[0]}:(o.length+=1,o.value+=e[0]);a&&(o.length>0&&s.push(o),o=a),i+=g,e=e.slice(g)}return o.length>0&&s.push(o),s}var d=class{constructor(t){if(this.groups=[],this.wordmap=new Map,t)for(let n of t)this.addSynonyms(n)}addSynonyms(t){if(t.length<2)throw new Error("Synonym must have at least 2 words");let n=[];for(let e of t){if(this.wordmap.has(e))throw new Error(`Word \`${e}\` cannot be in multiple groups`);if(n.find(i=>i===e))throw new Error(`Duplicate synonym: \`${e}\``);n.push(e)}let s=t.map(e=>e.toLowerCase()).sort();this.groups.push(s);for(let e of s)this.wordmap.set(e,s)}removeSynonyms(t){let n=t.toLowerCase(),s=this.groups.findIndex(e=>e.find(i=>i===n));if(s>=0){let e=this.groups[s];for(let i of e)this.wordmap.delete(i);this.groups.splice(s,1)}}getSynonyms(t){let n=t.toLowerCase();return(this.wordmap.get(n)||[]).filter(e=>e!==n)}expandQuery(t){if(!t||t.length===0)return t;let n=C(t,[...this.wordmap.keys()]),s={};for(let o=0;o<n.length;o++){let a=n[o];if(a.type==="synonym"){let g=a.value.toLowerCase(),l=this.getSynonyms(g);l.length>0&&(s[o.toString()]=[g,...l])}}let i=this.generateKeywordCombinations(s,[]).map(o=>n.map((a,g)=>{let l=g.toString();return s[l]?o[l]:a.value}).join(""));return i.length===0?t:{combineWith:"OR",queries:i}}generateKeywordCombinations(t,n){let s=Object.keys(t);if(s.length===0)return n;let e=s[0],i=[];if(Object.keys(n).length===0)for(let a of t[e])i.push({[e]:a});else for(let a of t[e])for(let g of n)i.push(v(c({},g),{[e]:a}));let o=c({},t);return delete o[e],this.generateKeywordCombinations(o,i)}},O=d;return E(j);})();