1 line
47 KiB
JavaScript
1 line
47 KiB
JavaScript
|
(function(a,b){if(typeof define==="function"&&define.amd){define("sifter",b)}else{if(typeof exports==="object"){module.exports=b()}else{a.Sifter=b()}}}(this,function(){var h=function(j,k){this.items=j;this.settings=k||{diacritics:true}};h.prototype.tokenize=function(m){m=b(String(m||"").toLowerCase());if(!m||!m.length){return[]}var j,q,l,k;var o=[];var p=m.split(/ +/);for(j=0,q=p.length;j<q;j++){l=e(p[j]);if(this.settings.diacritics){for(k in i){if(i.hasOwnProperty(k)){l=l.replace(new RegExp(k,"g"),i[k])}}}o.push({string:p[j],regex:new RegExp(l,"i")})}return o};h.prototype.iterator=function(j,l){var k;if(c(j)){k=Array.prototype.forEach||function(p){for(var m=0,o=this.length;m<o;m++){p(this[m],m,this)}}}else{k=function(n){for(var m in this){if(this.hasOwnProperty(m)){n(this[m],m,this)}}}}k.apply(j,[l])};h.prototype.getScoreFunction=function(q,r){var p,l,n,o,k;p=this;q=p.prepareSearch(q,r);n=q.tokens;l=q.options.fields;o=n.length;k=q.options.nesting;var m=function(t,s){var u,v;if(!t){return 0}t=String(t||"");v=t.search(s.regex);if(v===-1){return 0}u=s.string.length/t.length;if(v===0){u+=0.5}return u};var j=(function(){var s=l.length;if(!s){return function(){return 0}}if(s===1){return function(t,u){return m(a(u,l[0],k),t)}}return function(u,w){for(var t=0,v=0;t<s;t++){v+=m(a(w,l[t],k),u)}return v/s}})();if(!o){return function(){return 0}}if(o===1){return function(s){return j(n[0],s)}}if(q.options.conjunction==="and"){return function(u){var v;for(var s=0,t=0;s<o;s++){v=j(n[s],u);if(v<=0){return 0}t+=v}return t/o}}else{return function(u){for(var s=0,t=0;s<o;s++){t+=j(n[s],u)}return t/o}}};h.prototype.getSortFunction=function(v,w){var q,j,u,s,r,l,t,o,k,m,p;u=this;v=u.prepareSearch(v,w);p=(!v.query&&w.sort_empty)||w.sort;k=function(x,n){if(x==="$score"){return n.score}return a(u.items[n.id],x,w.nesting)};r=[];if(p){for(q=0,j=p.length;q<j;q++){if(v.query||p[q].field!=="$score"){r.push(p[q])}}}if(v.query){m=true;for(q=0,j=r.length;q<j;q++){if(r[q].field==="$score"){m=false;break}}if(m){r.unshift({field:"$score",direction:"desc"})}}else{for(q=0,j=r.length;q<j;q++){if(r[q].field==="$score"){r.splice(q,1);break}}}o=[];for(q=0,j=r.length;q<j;q++){o.push(r[q].direction==="desc"?-1:1)}l=r.length;if(!l){return null}else{if(l===1){s=r[0].field;t=o[0];return function(x,n){return t*f(k(s,x),k(s,n))}}else{return function(z,x){var B,n,y,A,C;for(B=0;B<l;B++){C=r[B].field;n=o[B]*f(k(C,z),k(C,x));if(n){return n}}return 0}}}};h.prototype.prepareSearch=function(m,j){if(typeof m==="object"){return m}j=d({},j);var l=j.fields;var n=j.sort;var k=j.sort_empty;if(l&&!c(l)){j.fields=[l]}if(n&&!c(n)){j.sort=[n]}if(k&&!c(k)){j.sort_empty=[k]}return{options:j,query:String(m||"").toLowerCase(),tokens:this.tokenize(m),total:0,items:[]}};h.prototype.search=function(m,r){var p=this,o,k,q,l;var j;var n;q=this.prepareSearch(m,r);r=q.options;m=q.query;n=r.score||p.getScoreFunction(q);if(m.length){p.iterator(p.items,function(s,t){k=n(s);if(r.filter===false||k>0){q.items.push({score:k,id:t})}})}else{p.iterator(p.items,function(s,t){q.items.push({score:1,id:t})})}j=p.getSortFunction(q,r);if(j){q.items.sort(j)}q.total=q.items.length;if(typeof r.limit==="number"){q.items=q.items.slice(0,r.limit)}return q};var f=function(k,j){if(typeof k==="number"&&typeof j==="number"){return k>j?1:(k<j?-1:0)}k=g(String(k||""));j=g(String(j||""));if(k>j){return 1}if(j>k){return -1}return 0};var d=function(l,j){var p,q,m,o;for(p=1,q=arguments.length;p<q;p++){o=arguments[p];if(!o){continue}for(m in o){if(o.hasOwnProperty(m)){l[m]=o[m]}}}return l};var a=function(m,j,k){if(!m||!j){return}if(!k){return m[j]}var l=j.split(".");while(l.length&&(m=m[l.shift()])){}return m};var b=function(j){return(j+"").replace(/^\s+|\s+$|/g,"")};var e=function(j){return(j+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")};var c=Array.isArray||(typeof $!=="undefined"&&$.isArray)||function(j){return Object.prototype.toString.call(j)==="[object Array]"};var i={a:"[aḀḁĂăÂâǍǎȺⱥȦȧẠạÄäÀàÁáĀāÃãÅåąĄÃąĄ]",b:"[b␢βΒB฿𐌁ᛒ]",c:"[cĆćĈĉČčĊċC̄c̄ÇçḈḉȻȼƇƈɕᴄCc
|