-
Notifications
You must be signed in to change notification settings - Fork 2
/
querytext.min.js
15 lines (15 loc) · 7.13 KB
/
querytext.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var querytext=function(k){var x=function(a){return a.replace(/[\u00c1\u00c2\u00c0\u00c5\u00c3\u00c4]/gm,"A").replace(/\u00c7/gm,"C").replace(/[\u00c9\u00ca\u00c8\u00cb]/gm,"E").replace(/[\u00cd\u00ce\u00cc\u00cf]/gm,"I").replace(/\u00d1/gm,"N").replace(/[\u00d3\u00d4\u00d2\u00d8\u00d5\u00d6]/gm,"O").replace(/[\u00da\u00db\u00d9\u00dc]/gm,"U").replace(/\u00dd/gm,"Y").replace(/[\u00e1\u00e2\u00e0\u00e5\u00e3\u00e4]/gm,"a").replace(/\u00e7/gm,"c").replace(/[\u00e9\u00ea\u00e8\u00eb]/gm,"e").replace(/[\u00ed\u00ee\u00ec\u00ef]/gm,
"i").replace(/\u00f1/gm,"n").replace(/[\u00f3\u00f4\u00f2\u00f8\u00f5\u00f6]/gm,"o").replace(/[\u00fa\u00fb\u00f9\u00fc]/gm,"u").replace(/[\u00fd\u00ff]/gm,"y")},y=function(a,e,h,d){if(a){var f=[];a.forEach(function(a){var h=!1,d=a.ofs;a=a.txt.length;var e=d+a-1,c;for(c in f)d>=f[c].p&&d<=f[c].e?(e>f[c].e&&(f[c].e=e,f[c].l=1+e-f[c].p),h=!0):e>=f[c].p&&e<=f[c].e?(f[c].p=d,f[c].l=1+f[c].e-d,h=!0):d<f[c].p&&e>f[c].e&&(f[c].p=d,f[c].e=e,f[c].l=a,h=!0);h||f.push({p:d,l:a,e:e})});f.sort(function(a,h){return h.p-
a.p}).forEach(function(a){e=e.substr(0,a.p)+h+e.substr(a.p,a.l)+d+e.substr(a.p+a.l)})}return e},q={VERSION:"1.1",opts:{dftbool:"OR",sensitive:!1,wholeword:!0,unaccent:!0,matches:!0,debug:!1,wordpos:!1},error:!1,query:!1,tree:!1,parse:function(a){var e=function(a,d,f){d||(d=0);for(var b=0,p=!1,m=!1,n=a.length,c,k,l,g,u,r,t,z=function(a){a=a.replace(/(^\s+|\s+$)/gm,"");var b="*"!=a[0]&&f.wholeword?"(^|[\\s.,;:\\-+=<>\\\\\\/'\"\\(\\)~&\\[\\]{}\u300b\u300a\uff0c]:?)":"",d="*"!=a.substr(-1)&&f.wholeword?
"($|[\\s.,;:\\-+=<>\\\\\\/'\"\\(\\)~&\\[\\]{}\u300b\u300a\uff0c]:?)":"";a=a.replace(/(^\*|\*$)/g,"");a=a.replace(/([\(\)\+\*\?\:\[\]])/g,"\\$1");a=a.replace(/\s+/g,"\\s+");return new RegExp(b+a+d,"m"+(f.sensitive?"":"i")+(f.matches?"g":""))},v=function(a,b){p&&(a.not=!0,m||(m="AND"),p=!1);a.text&&(a.rex=z(a.text));b&&(a.src=b);t?(m||(m=f.dftbool),m===t.bool&&p===(t.not||!1)?t.subs.push(a):t={bool:m,subs:[t,a]}):t=a;m=!1};b<n;)if('"'==a[b]){c=b++;for(g="";b<n&&'"'!=a[b];)g+=a[b++];if(b>=n)return{error:"unbalanced quotes",
pos:c+d};if(!g.length||"*"==g)return{error:"empty quotes",pos:c+d};b++;if(b<n&&"~"==a[b]){b++;for(l="";b<n&&"0"<=a[b]&&"9">=a[b];)l+=a[b++];if(!l.length)return{error:"proximity distance missing",pos:c+d};l=parseInt(l,10);r={bool:"NEAR",dist:l,subs:[]};g.split(/\s+/).forEach(function(a){r.subs.push({text:a,rex:z(a)})});if(2>r.subs.length)return{error:"2 words expected at least for near operator",pos:c+d};v(r);f.wordpos=f.matches=!0}else v({text:g})}else{if(")"==a[b])return{error:"unbalanced parenthesis",
pos:c+d};if("("==a[b]){c=b++;u=1;for(g="";b<n&&(")"!=a[b]||0!=--u);){"("==a[b]&&u++;g+=a[b];if('"'==a[b]){for(l=b++;b<n&&'"'!=a[b];)g+=a[b++];g+=a[b];if(b>=n)return{error:"unbalanced quotes",pos:l+d}}b++}if(b>=n)return{error:"unbalanced parenthesis",pos:c+d};r=e(g,c+d+1,f);if(r.error)return r;v(r,g);b++}else if(" ">=a[b])for(;b<n&&" ">=a[b];)b++;else if("+"==a[b]){if(p||m)return{error:"unexpected operator",pos:b+d};m="AND";k=b++}else if("|"==a[b]){if(p||m)return{error:"unexpected operator",pos:b+
d};m="OR";k=b++}else if("-"==a[b]||"!"==a[b])p=!p,k=b++;else{c=b;for(g="";b<n&&" "<a[b]&&!/[\(\)\+\-\|\!\"]/.test(a[b]);)g+=a[b++];if(/^(AND|OR|NOT)$/i.test(g))if(k=c,r=RegExp.$1.toUpperCase(),"NOT"==r)p=!p;else{if(p||m)return{error:"unexpected operator",pos:c+d};m=r}else{if("*"==g)return{error:"empty word",pos:c+d};v({text:g})}}}return p||m?{error:"unexpected operator",pos:k+d}:t?t:{error:"empty query",pos:d}};this.error=this.tree=!1;this.query=this.opts.unaccent?x(a):a;a=e(this.query,0,this.opts);
delete this.pos;a.error?(this.error=a.error,this.pos=a.pos,this.opts.debug&&console.log(a.error,"at",a.pos)):(this.tree=a,this.opts.debug&&console.log("[QT:parser]\n"+this.dump()));return this},dump:function(a,e){if(!this.tree)return"";a||(a=this.tree);e||(e="");var h=a.not?"NOT ":"",d=a.src?" : "+h+"("+a.src+")":"",f=void 0!=a.dist?"("+a.dist+")":"",b=void 0!=a.match?" = "+a.match:"",p=void 0!=a.pos?" ["+a.pos.map(function(a){return a.txt+":"+a.ofs+":"+a.pos}).join("|")+"]":"",m=this;return a.bool?
e+h+a.bool+f+b+d+"\n"+a.subs.map(function(a){return m.dump(a,e+" | ")}).join("\n"):e+h+'"'+a.text+'"'+b+p},flatten:function(a,e,h){void 0==a&&(a=this.tree);void 0==e&&(e={not:[],or:[],and:[]});void 0==h&&(h={});a.not&&(h.not=!h.not);if(a.bool){var d=this;a.subs.forEach(function(f){e=d.flatten(f,e,{not:h.not,or:"OR"==a.bool,and:"AND"==a.bool})})}else h.not?e.not.push(a.text):h.and?e.and.push(a.text):e.or.push(a.text);return e},normalize:function(a){a||(a=this.tree);if(!a)return"";var e=a.not?"NOT ":
"",h=[],d=this,f;if(a.bool){if("NEAR"==a.bool)return f=a.subs.map(function(a){return a.text}).join(" "),e+'"'+f+'"~'+a.dist;a.subs.forEach(function(a){h.push(d.normalize(a))});return e||a!=this.tree?e+"("+h.join(" "+a.bool+" ")+")":h.join(" "+a.bool+" ")}return/^(and|or|not)$/i.test(a.text)||/[\s\(\)\+\-\!\?\|]/.test(a.text)?e+'"'+a.text+'"':e+a.text},match:function(a){if(!this.tree)return!1;var e=this,h=this.opts.matches?[]:!1,d=this.opts.wordpos?{}:!1,f=function(a){delete a.match;delete a.pos;a.bool&&
a.subs.forEach(function(a){f(a)})},b=function(a){if(void 0==d[a]){var b=0,e;for(e in d){if(e>a)break;b=d[e]}return b}return d[a]},p=function(a,b){var d,e=Number.MAX_VALUE;b.pos.forEach(function(b){d=Math.abs(b.pos-a);d<e&&(e=d)});return e-1},m=function(a,f){var c,g,k,l,n,q;if(a.bool)if("NEAR"==a.bool){c=!0;q=a.subs.length;for(g=0;g<q&&c;++g)c=m(a.subs[g],f);if(c)for(g=0;g<q&&c;++g)c=a.subs[g],c.pos=c.pos.filter(function(b){for(k=0;k<q;++k)if(k!=g&&p(b.pos,a.subs[k])<=a.dist)return!0;return!1}),c=
c.pos.length}else if("AND"==a.bool)for(c=!0,g=0;g<a.subs.length&&c;++g)c=m(a.subs[g],f);else for(c=!1,g=0;g<a.subs.length&&(!(c=m(a.subs[g],f)||c)||h);++g);else if(h&&!a.not)for(c=!1,a.pos=[];null!=(g=a.rex.exec(f));)c=!0,l=g[0],n=g.index,e.opts.sensitive||(l=l.toLowerCase()),e.opts.wholeword&&(q=l.length,l=l.replace(/^[\s\.,;:\-+=<>\\\/'"\(\)~&\[\]{}\u300b\u300a\uff0c]+/g,""),n+=q-l.length,l=l.replace(/[\s,\.;:\-+=<>\\\/'"\(\)~&\[\]{}\u300b\u300a\uff0c]+$/g,""),1<q&&a.rex.lastIndex--),n={txt:l,ofs:n},
d&&(n.pos=b(n.ofs)),a.pos.push(n);else c=a.rex.test(f);a.not&&(c=!c);return a.match=c},k=function(a){a.bool&&a.match?a.subs.forEach(function(a){k(a)}):a.pos&&a.pos.forEach(function(a){h.push(a)});return h};if(d)for(var c=0,q=0,l=a.length,g=/^[\-0-9A-Za-z\u00C0-\u017F]+$/;c<l;)if(g.test(a[c]))for(d[c]=q++;++c<l&&g.test(a[c]););else for(;++c<l&&!g.test(a[c]););f(this.tree);a=m(this.tree,this.opts.unaccent?x(a):a);this.opts.debug&&console.log("[QT:matcher]\n"+this.dump());if(a&&h){var u={};return k(this.tree).filter(function(a){var b=
!u[a.ofs];u[a.ofs]=!0;return b})}return a},highlightml:function(a,e,h){if(!this.tree)return a;if(!this.opts.matches)return!1;for(var d=a.innerHTML,f="",b=0,k=!1;b<d.length;)k||(k="<"==d[b]),k?(k=">"!=d[b],f+=" "):f+=d[b],b++;a.innerHTML=y(this.match(f),d,e,h);return a},highlight:function(a,e,h,d){return"object"==typeof a?this.highlightml(a,e,h):d?(d=document.createElement("div"),d.innerHTML=a,this.highlightml(d,e,h).innerHTML):this.tree?this.opts.matches?y(this.match(a),a,e,h):!1:a}};if(k)if("string"==
typeof k)q.parse(k);else if("object"==typeof k){for(var w in q.opts)void 0!==k[w]&&(q.opts[w]=k[w]);k.query&&q.parse(k.query)}return q};