-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbulksearch.min.js
31 lines (31 loc) · 12.6 KB
/
bulksearch.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
BulkSearch v0.1.3
Copyright 2018 Thomas Wilkerling
Released under the Apache 2.0 Licence
https://github.com/nextapps-de/bulksearch
*/
'use strict';(function(p,H,h){var l;(l=h.define)&&l.amd?l([],function(){return H}):(l=h.modules)?l[p.toLowerCase()]=H:"undefined"!==typeof module?module.exports=H:h[p]=H})("BulkSearch",function S(p){function h(b){b||(b=k);this.name="BulkSearch";this.id=b.id||T++;this.init(b);l(this,"index",function(){return this.a});l(this,"length",function(){return Object.keys(this.a).length})}function l(b,a,c){Object.defineProperty(b,a,{get:c})}function d(b){return new RegExp(b,"g")}function C(b,a){return new (("int"===
b||"integer"===b?Uint32Array:"short"===b?Uint16Array:"float"===b||"double"===b?Float64Array:"byte"===b?Uint8Array:Array)||Array)(a)}function m(b,a,c){if("undefined"===typeof c){for(c=0;c<a.length;c+=2)b=b.replace(a[c],a[c+1]);return b}return b.replace(a,c)}function w(b,a){b=b.length-a.length;return 0>b?1:0<b?-1:0}function U(b,a){b=b.score-a.score;return 0>b?-1:0<b?1:0}function z(b){for(var a="",c="",e="",g=0;g<b.length;g++){var f=b[g];if(f!==c)if(g&&"h"===f){if(e="a"===e||"e"===e||"i"===e||"o"===
e||"u"===e||"y"===e,("a"===c||"e"===c||"i"===c||"o"===c||"u"===c||"y"===c)&&e||" "===c)a+=f}else a+=f;e=g===b.length-1?"":b[g+1];c=f}return a}function L(b){var a={};b=b.split(M);for(var c=[],e=0,g=0;g<b.length;g++){var f=b[g];f&&!a[f]&&(a[f]="1",c[e++]=this.encode(f))}return c.join("~")}function V(b,a){var c={};if(b)for(var e=0;e<b.length;e++){var g=a?a.call(t,b[e]):b[e];c[g]=String.fromCharCode(65E3-b.length+e)}return c}function W(b,a){var c=[];if(b){var e=0,g;for(g in b)if(b.hasOwnProperty(g)){var f=
a?a.call(t,g):g;c[e++]=d("(?=.{"+(f.length+3)+",})"+f+"$");c[e++]=a?a.call(t,b[g]):b[g]}}return c}function D(b){b.R||(b.R=N(function(){b.R=null;var a=b.async;a&&(b.async=!1);if(b.h.length){for(var c=O(),e;(e=b.h.shift())||0===e;){var g=b.l[e];switch(g[0]){case A.add:b.add(g[1],g[2]);break;case A.update:b.update(g[1],g[2])}b.l[e]=null;delete b.l[e];if(100<O()-c)break}b.h.length&&D(b)}a&&(b.async=a)},1,"search-async-"+b.id))}function O(){return"undefined"!==typeof performance?performance.now():(new Date).getTime()}
function X(b,a,c,e){b=p("bulksearch","id"+b,function(){var b,a;self.a=function(c){if(c=c.data)if(c.search){var e=a.search(c.content,c.threshold?{limit:c.limit,threshold:c.threshold}:c.limit);self.postMessage({id:b,content:c.content,limit:c.limit,result:e})}else c.add?a.add(c.id,c.content):c.update?a.update(c.id,c.content):c.remove?a.remove(c.id):c.reset?a.reset():c.info?(c=a.info(),c.worker=b,a.N&&console.log(c)):c.register&&(b=c.id,c.options.cache=!1,c.options.async=!0,c.options.worker=!1,a=(new Function(c.register.substring(c.register.indexOf("{")+
1,c.register.lastIndexOf("}"))))(),a=new a(c.options))}},function(a){(a=a.data)&&a.result?e(a.id,a.content,a.result,a.limit):c.debug&&console.log(a)},a);var g=S.toString();c.id=a;b.postMessage(a,{register:g,options:c,id:a});return b}var k={type:"integer",H:!1,async:!1,f:!1,separator:"~",v:!1,F:!0,L:!1,cache:!1,encode:"icase",G:!1,depth:0,size:4E3},r=[],T=0,A={add:0,update:1,remove:2},M=d("[ -/]"),P={},Q={};h.new=function(b){return new this(b)};h.create=function(b){return h.new(b)};h.addMatcher=function(b){for(var a in b)b.hasOwnProperty(a)&&
(r[r.length]=d(a),r[r.length]=b[a]);return this};h.registerEncoder=function(b,a){t[b]=a;return this};h.registerLanguage=function(b,a){P[b]=a.filter;Q[b]=a.stemmer;return this};h.encode=function(b,a){return t[b].call(t,a)};h.prototype.init=function(b){this.I=[];b||(b=k);var a;if(a=b.worker)if("undefined"===typeof Worker)b.worker=!1,b.async=!0,this.u=null;else{var c=this;a=parseInt(a,10)||4;c.A=-1;c.w=0;c.i=[];c.J=null;c.u=Array(a);for(var e=0;e<a;e++)c.u[e]=X(c.id,e,b||k,function(a,b,e,d){c.w!==c.f&&
(c.i=c.i.concat(e),c.w++,d&&c.i.length>=d&&(c.w=c.f),c.J&&c.w===c.f&&("undefined"!==typeof c.b&&(c.i.length?c.b="":c.b||(c.b=b)),c.cache&&c.g.set(b,c.i),c.J(c.i),c.i=[]))})}this.type=b.type||this.type||k.type;this.async=b.async||this.async||k.async;this.f=b.worker||this.f||k.f;this.separator=b.separator||this.separator||k.separator;this.v=b.strict||this.v||k.v;this.L=b.ordered||this.L||k.L;this.F=b.multi||this.F||k.F;this.G=b.paging||this.G||k.G;this.depth=b.depth||this.depth||k.depth;this.H=b.suggest||
this.H||k.H;this.D=(a=b.encode)&&t[a]||("function"===typeof a?a:this.D||!1);this.N=b.debug||this.N;(a=b.matcher)&&this.addMatcher(a);if(a=b.filter)this.filter=V(P[a]||a,this.D);if(a=b.stemmer)this.stemmer=W(Q[a]||a,this.D);this.j={};this.o=b.size||k.size;this.g=(this.cache=a=b.cache||this.cache||k.cache)?new R(a):!1;this.b="";this.M={};this.m=[C(this.type,this.o)];this.a={};this.s={};this.P={};this.c=[""];this.O=0;this.C=!0;this.B=0;this.l={};this.h=[];this.g=this.cache?new R(3E4,50,!0):!1;return this};
h.prototype.encode=function(b){b&&r.length&&(b=m(b,r));b&&this.I.length&&(b=m(b,this.I));b&&this.D&&(b=this.D.call(t,b));if(b&&this.filter){b=b.split(" ");for(var a=0;a<b.length;a++)this.filter[b[a]]&&b.splice(a,1);b=b.join(" ")}b&&this.stemmer&&(b=m(b,this.stemmer));return b};h.prototype.addMatcher=function(b){var a=this.I,c;for(c in b)b.hasOwnProperty(c)&&(a[a.length]=d(c),a[a.length]=b[c]);return this};h.prototype.add=function(b,a,c){if("string"===typeof a&&a&&(b||0===b))if(this.a[b])this.update(b,
a);else{if(this.f)return++this.A>=this.u.length&&(this.A=0),this.u[this.A].postMessage(this.A,{add:!0,id:b,content:a}),this.a[b]=""+this.A,this;if(this.async)return this.l[b]||(this.h[this.h.length]=b),this.l[b]=[A.add,b,a],D(this),this;if(c)a=c;else if(a){a=L.call(this,a.trim());if(!a)return this;a="~"+(this.v?a.replace(/ /g,"~"):a)}var e=this.c;c=this.O;var g=this.s[a.length],f;if(g&&g.length){this.a[b]=f=g.pop();c=f[0];g=f[1];var x=e[c];e[c]=x.substring(0,g)+a+x.substring(f[2]);this.B-=a.length}else g=
e[c].length,g+a.length>this.o&&(a.length>this.o/2-2&&(this.o=2*a.length+2),g&&(this.O=++c),g=0,e[c]="",this.m[c]=C(this.type,this.o)),this.a[b]=f=[c,g,0],e[c]+=a;if(this.m[c].constructor===Array)for(e=0;e<a.length;e++)this.m[c][g++]=b;else this.m[c].fill(b,g,g+a.length),g+=a.length;f[2]=g;this.C=!1}return this};h.prototype.update=function(b,a){if("string"===typeof a&&(b||0===b)){var c=this.a[b];if(c){if(this.f)return c=parseInt(this.a[b],10),this.u[c].postMessage(c,{update:!0,id:b,content:a}),a||
delete this.a[b],this;if(this.async)return this.l[b]||(this.h[this.h.length]=b),this.l[b]=[A.update,b,a],D(this),this;var e=a;if(a){a=L.call(this,a.trim());if(!a)return this;a="~"+(this.v?a.replace(/ /g,"~"):a)}var g=c[1],f=c[2],x=f-g,d=a.length-x,h=a;for(0<d&&(a="~");a.length<x;)a=(a+" ").substring(0,x);var k=this.c,q=c[0],n=k[q];k[q]=n.substring(0,g)+a+n.substring(f);if(0<d||!e)(a=this.s[x])?a=a.length:(this.s[x]=[],a=0),this.s[x][a]=c,this.B+=x,
e?(this.a[b]=null,this.j[b]=0,this.add(b,e,h)):(delete this.a[b],delete this.j[b]);this.C=!1}}return this};h.prototype.remove=function(b){this.a[b]&&this.update(b,"");return this};h.prototype.search=function(b,a,c){var e=0,g=0;b&&"object"===typeof b&&(c=a,a=b,b=b.query);if(a)if("function"===typeof a)c=a,a=1E3;else if("object"===typeof a){if(a.page){var f=a.page.split(":");e=parseInt(f[0],10);g=parseInt(f[1],10)}a.callback&&(c=a.callback);a=a.limit}a||(a=1E3);if(this.f){this.J=c;this.w=0;this.i=[];
for(var d=0;d<this.f;d++)this.u[d].postMessage(d,{search:!0,limit:a,page:e+":"+g,content:b});return null}if(c){if(e||g)a={page:e+":"+g,limit:a};var h=this;N(function(){c(h.search(b,a));h=null},1,"search-"+this.id);return null}if(this.G){var k=e+":"+g,m={current:k,prev:this.M[k]||null,next:null,results:[]};f=m.results}else f=[];if(!b||"string"!==typeof b)return m||f;var q=b;if(!this.C)this.cache&&("undefined"!==typeof this.b&&(this.b=""),this.g.reset()),this.C=!0;else if(this.cache){var n=this.g.get(b);
if(n)return n}else if(this.b&&-1!==b.indexOf(this.b))return m||f;q=this.encode(q.trim());if(!q.length)return m||f;this.v&&(q="~"+q.replace(/ /g," ~"));var p=this.F?q.split(M):[q];n=p.length;if(1<n){p.sort(w);var l=this.H}for(q=[];e<this.c.length;e++){var y=g;d=0;var t=this.c[e],u,r=p[0],z=0;g=0;if(r&&"~"!==r)for(;-1!==(d=t.indexOf(r,y))||l;){if(l&&-1===d)if(z++,z===n+1)break;else{l=n>z+1;r=p[z];continue}y=this.m[e][d];if(u=this.a[y]){var B=u[1],I=u[2],E=I-B,C=!0;B=t.substring(B,I);var A=0;var v=d-
u[1];var K=v-B.lastIndexOf("~",v);u=E/(E-v)+K/3;if(1<n){var D={};D[r]="1";for(d=1;d<n;d++){var J=p[d];if(J&&"~"!==J&&!D[J]){var F=B.lastIndexOf(J,v),G=B.indexOf(J,v);if(-1===G&&-1===F)if(C=!1,l)u+=E;else break;else A++,-1===G||-1!==F&&v-F<G-v?(K=F-B.lastIndexOf("~",F),u+=E/(E-(v-F))+2*K,v=F):(K=G-B.lastIndexOf("~",G),u+=E/(E-(G-v))+2*K,v=G);D[J]="1"}}u/=n}d=I-1;if(C){if(this.j[y]?this.j[y]++:this.j[y]=1,f[f.length]={score:u,id:y},a&&f.length===a)break}else l&&(I=q[A]||(q[A]=[]),I[I.length]={score:u,
id:y})}y=d+1}if(a&&f.length===a){m&&e<this.c.length&&d+1<this.c[e].length&&(m.next=e+":"+d,this.M[m.next]=k);break}}if(!f.length&&l&&(k=a||1E3,l=f.length,n=q.length,l<k&&n))for(g=n-1;0<=g;g--)if(n=q[g])for(d=0;d<n.length;d++)if(f[l++]=n[d],k&&l===k){g=-1;break}if(f.length){f.sort(U);for(d=0;d<f.length;d++)f[d]=f[d].id;this.b=""}else this.b||(this.b=b);this.cache&&this.g.set(b,m||f);return m||f};h.prototype.optimize=function(){var b=this.c,a=this.a,c=this.g,e=this.j;this.reset();var d=Object.keys(a);
d.sort(function(a,b){a=(e[b]||0)-(e[a]||0);return 0>a?-1:0<a?1:0});for(var f=0;f<d.length;f++){var h=d[f],k=a[h];k?(k=b[k[0]].substring(k[1],k[2]),this.add(h,k,k)):delete a[h]}c&&(this.g=c);this.j=e};h.prototype.info=function(){for(var b=this.B,a=Object.keys(this.a).length,c=Object.keys(this.s).length,e=Object.keys(this.P).length,d=0,f=0,h=0;h<this.c.length;h++)f+=this.c[h].length;f&&(b=(100/f*b*100|0)/100,d=this.m[0].byteLength||32*f,d+=2*f+24*(a+c+e));return{id:this.id,length:a,chunks:this.c.length,
register:e,size:this.o,bytes:d,fragments:c,fragmented:b,status:this.C,cache:this.h.length,matcher:r.length,worker:this.f}};h.prototype.reset=function(){this.destroy();return this.init()};h.prototype.destroy=function(){this.cache&&this.g.reset();this.m=this.a=this.s=this.c=this.P=this.g=this.j=this.I=this.M=this.l=this.h=null;return this};var t={icase:function(b){return b.toLowerCase()},simple:function(){var b=[d("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),"a",d("[\u00e8\u00e9\u00ea\u00eb]"),"e",d("[\u00ec\u00ed\u00ee\u00ef]"),
"i",d("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",d("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",d("[\u00fd\u0177\u00ff]"),"y",d("\u00f1"),"n",d("\u00e7"),"c",d("\u00df"),"s",d(" & ")," and ",d("[-/]")," ",d("[^a-z0-9 ]"),"",d("\\s\\s+")," "];return function(a){a=m(a.toLowerCase(),b);return" "!==a?a:""}}(),advanced:function(){var b=[d("ae"),"a",d("ai"),"ei",d("ay"),"ei",d("ey"),"ei",d("oe"),"o",d("ue"),"u",d("ie"),"i",d("sz"),"s",d("zs"),"s",d("sh"),"s",d("ck"),"k",d("cc"),"k",d("dt"),"t",d("ph"),"f",
d("pf"),"f",d("ou"),"o",d("uo"),"u"];return function(a,c){if(!a)return a;a=this.simple(a);2<a.length&&(a=m(a,b));c||1<a.length&&(a=z(a));return a}}(),extra:function(){var b=[d("p"),"b",d("z"),"s",d("[cgq]"),"k",d("n"),"m",d("d"),"t",d("[vw]"),"f",d("[aeiouy]"),""];return function(a){if(!a)return a;a=this.advanced(a,!0);if(1<a.length){a=a.split(" ");for(var c=0;c<a.length;c++){var e=a[c];1<e.length&&(a[c]=e[0]+m(e.substring(1),b))}a=a.join(" ");a=z(a)}return a}}(),balance:function(){var b=[d("[-/]"),
" ",d("[^a-z0-9 ]"),"",d("\\s\\s+")," "];return function(a){return z(m(a.toLowerCase(),b))}}()},N=function(){var b={};return function(a,c,e){var d=b[e];d&&clearTimeout(d);return b[e]=setTimeout(a,c)}}(),R=function(){function b(a){this.reset();this.K=!0!==a&&a}b.prototype.reset=function(){this.cache={};this.count={};this.index={};this.keys=[]};b.prototype.set=function(a,b){if(this.K&&"undefined"===typeof this.cache[a]){var c=this.keys.length;if(c===this.K){c--;var d=this.keys[c];delete this.cache[d];
delete this.count[d];delete this.index[d]}this.index[a]=c;this.keys[c]=a;this.count[a]=-1;this.cache[a]=b;this.get(a)}else this.cache[a]=b};b.prototype.get=function(a){var b=this.cache[a];if(this.K&&b){var d=++this.count[a],g=this.index,f=g[a];if(0<f){for(var h=this.keys,k=f;this.count[h[--f]]<=d&&-1!==f;);f++;if(f!==k){for(d=k;d>f;d--)k=h[d-1],h[d]=k,g[k]=d;h[f]=a;g[a]=f}}}return b};return b}();return h}(function(){var p={},H=!("undefined"===typeof Blob||"undefined"===typeof URL||!URL.createObjectURL);
return function(h,l,d,C,m){var w=h;h=H?URL.createObjectURL(new Blob(["var SUPPORT_WORKER = true;var SUPPORT_BUILTINS = true;var SUPPORT_DEBUG = true;var SUPPORT_CACHE = true;var SUPPORT_ASYNC = true;("+d.toString()+")()"],{type:"text/javascript"})):"../"+w+".js";w+="-"+l;p[w]||(p[w]=[]);p[w][m]=new Worker(h);p[w][m].onmessage=C;console.log("Register Worker: "+w+"@"+m);return{postMessage:function(d,h){p[w][d].postMessage(h)}}}}()),this);