forked from nikopol/querytext.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerytext.min.js
10 lines (10 loc) · 5.11 KB
/
querytext.min.js
1
2
3
4
5
6
7
8
9
10
var querytext=function(p){var u=function(a){return a.replace(/\u00c6/gm,"AE").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(/\u0152/gm,"OE").replace(/[\u00d3\u00d4\u00d2\u00d8\u00d5\u00d6]/gm,"O").replace(/[\u00da\u00db\u00d9\u00dc]/gm,"U").replace(/\u00dd/gm,"Y").replace(/\u00e6/gm,"ae").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(/\u0153/gm,"oe").replace(/[\u00f3\u00f4\u00f2\u00f8\u00f5\u00f6]/gm,"o").replace(/[\u00fa\u00fb\u00f9\u00fc]/gm,"u").replace(/[\u00fd\u00ff]/gm,"y")},v=function(a,l,m,c){var e,b,d,h,f,k,q,g=[];for(e in a)for(b in a[e]){k=!1;h=a[e][b];q=e.length;f=h+q-1;for(d in g)h>=g[d].p&&h<=g[d].e?(f>g[d].e&&(g[d].e=f,g[d].l=1+f-g[d].p),k=!0):f>=g[d].p&&f<=g[d].e?(g[d].p=h,g[d].l=1+g[d].e-
h,k=!0):h<g[d].p&&f>g[d].e&&(g[d].p=h,g[d].e=f,g[d].l=q,k=!0);k||g.push({p:h,l:q,e:f})}g.sort(function(b,a){return a.p-b.p}).forEach(function(b){l=l.substr(0,b.p)+m+l.substr(b.p,b.l)+c+l.substr(b.p+b.l)});return l},s={VERSION:0.6,opts:{dftbool:"OR",sensitive:!1,wholeword:!0,unaccent:!0,matches:!1,debug:!1},error:!1,query:!1,tree:!1,parse:function(a){var l=function(a,c,e){c||(c=0);for(var b=0,d=!1,h=!1,f=a.length,k,q,g,n,p,r,s=function(a,b){d&&(a.not=!0,h||(h="AND"),d=!1);if(a.text){var c=a.text.replace(/(^\s+|\s+$)/gm,
""),f="*"!=c[0]&&e.wholeword?"(^|\\W:?)":"",g="*"!=c.substr(-1)&&e.wholeword?"($|\\W:?)":"",c=c.replace(/(^\*|\*$)/g,""),c=c.replace(/([\(\)\+\*\?\:\[\]])/g,"\\$1"),c=c.replace(/\s+/g,"\\s+");a.rex=RegExp(f+c+g,"m"+(e.sensitive?"":"i")+(e.matches?"g":""))}b&&(a.src=b);r?(h||(h=e.dftbool),h===r.bool&&d===(r.not||!1)?r.subs.push(a):r={bool:h,subs:[r,a]}):r=a;h=!1};b<f;)if('"'==a[b]){k=b++;for(n="";b<f&&'"'!=a[b];)n+=a[b++];if(b>=f)return{error:"unbalanced quotes",pos:k+c};s({text:n});b++}else{if(")"==
a[b])return{error:"unbalanced parenthesis",pos:k+c};if("("==a[b]){k=b++;p=1;for(n="";b<f&&(")"!=a[b]||0!=--p);){"("==a[b]&&p++;n+=a[b];if('"'==a[b]){for(g=b++;b<f&&'"'!=a[b];)n+=a[b++];n+=a[b];if(b>=f)return{error:"unbalanced quotes",pos:g+c}}b++}if(b>=f)return{error:"unbalanced parenthesis",pos:k+c};g=l(n,k+1,e);if(g.error)return g;s(g,n);b++}else if(" ">=a[b])for(;b<f&&" ">=a[b];)b++;else if("+"==a[b]){if(d||h)return{error:"unexpected operator",pos:b+c};h="AND";q=b++}else if("|"==a[b]){if(d||h)return{error:"unexpected operator",
pos:b+c};h="OR";q=b++}else if("-"==a[b]||"!"==a[b])d=!d,q=b++;else{k=b;for(n="";b<f&&" "<a[b]&&!/[\(\)\+\-\|\!]/.test(a[b]);)n+=a[b++];if(/^(AND|OR|NOT|NEAR\d)$/i.test(n))if(q=k,g=RegExp.$1.toUpperCase(),"NOT"==g)d=!d;else{if(d||h)return{error:"unexpected operator",pos:k+c};h=g}else s({text:n})}}return d||h?{error:"unexpected operator",pos:q+c}:r?r:{error:"empty query",pos:c}};this.error=this.tree=!1;this.query=this.opts.unaccent?u(a):a;a=l(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(this.dump()));return this},dump:function(a,l){if(!this.tree)return"";a||(a=this.tree);l||(l="");var m=a.not?"NOT ":"",c=a.src?" : "+m+"("+a.src+")":"",e=void 0!=a.match?" = "+a.match:"";self=this;return a.bool?l+m+a.bool+e+c+"\n"+a.subs.map(function(a){return self.dump(a,l+" | ")}).join("\n"):l+m+'"'+a.text+'"'+e},normalize:function(a){a||(a=this.tree);if(!a)return"";var l=a.not?"NOT ":
"",m=[],c=this;return a.bool?(a.subs.forEach(function(a){m.push(c.normalize(a))}),l||a!=this.tree?l+"("+m.join(" "+a.bool+" ")+")":m.join(" "+a.bool+" ")):/^(and|or|not)$/i.test(a.text)||/[\s\(\)\+\-\!\?\|]/.test(a.text)?l+'"'+a.text+'"':l+a.text},match:function(a){if(!this.tree)return!1;var l=this,m=function(a){delete a.match;a.bool&&a.subs.forEach(function(a){m(a)})},c=function(a,d,h){var f,k,e,g;if(a.bool)if("AND"==a.bool)for(f=!0,k=0;k<a.subs.length&&f;++k)f=c(a.subs[k],d,h);else for(f=!1,k=0;k<
a.subs.length&&(!(f=c(a.subs[k],d,h)||f)||h);++k);else if(!1===h||a.not)f=a.rex.test(d);else for(f=!1;null!=(k=a.rex.exec(d));)f=!0,e=k[0],k=k.index,l.opts.sensitive||(e=e.toLowerCase()),l.opts.wholeword&&(g=e.length,e=e.replace(/^\W+/g,""),k+=g-e.length,e=e.replace(/\W+$/g,"")),void 0==h[e]&&(h[e]=[]),h[e].push(k);a.not&&(f=!f);return a.match=f},e=this.opts.matches?{}:!1;m(this.tree);a=c(this.tree,this.opts.unaccent?u(a):a,e);this.opts.debug&&(console.log(this.dump()),e&&console.log(e));return this.opts.matches&&
a?e:a},highlightml:function(a,l,m){if(!this.tree)return a;if(!this.opts.matches)return!1;var c=a.innerHTML,e="",b,d;b=0;for(d=!1;b<c.length;++b)d||(d="<"==c[b]),d?(d=">"!=c[b],e+=" "):e+=c[b];a.innerHTML=v(this.match(e),c,l,m);return a},highlight:function(a,l,m,c){return"object"==typeof a?this.highlightml(a,l,m):c?(c=document.createElement("div"),c.innerHTML=a,this.highlightml(c,l,m).innerHTML):this.tree?this.opts.matches?v(this.match(a),a,l,m):!1:a}};if(p)if("string"==typeof p)s.parse(p);else if("object"==
typeof p){for(var t in s.opts)void 0!==p[t]&&(s.opts[t]=p[t]);p.query&&s.parse(p.query)}return s};