You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 lines
7.6 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//文档见https://github.com/bvaughn/js-search
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).JsSearch={})}(this,(function(e){"use strict";var t=function(){function e(){}return e.prototype.expandToken=function(e){for(var t,n=[],i=0,r=e.length;i<r;++i){t="";for(var o=i;o<r;++o)t+=e.charAt(o),n.push(t)}return n},e}(),n=function(){function e(){}return e.prototype.expandToken=function(e){return e?[e]:[]},e}(),i=function(){function e(){}return e.prototype.expandToken=function(e){for(var t=[],n="",i=0,r=e.length;i<r;++i)n+=e.charAt(i),t.push(n);return t},e}(),r=function(){function e(){}return e.prototype.sanitize=function(e){return e?e.trim():""},e}(),o=function(){function e(){}return e.prototype.sanitize=function(e){return e?e.toLocaleLowerCase().trim():""},e}();function a(e,t){t=t||[];for(var n=e=e||{},i=0;i<t.length;i++)if(null==(n=n[t[i]]))return null;return n}var s=function(){function e(e){this._uidFieldName=e,this._tokenToIdfCache={},this._tokenMap={}}var t=e.prototype;return t.indexDocument=function(e,t,n){this._tokenToIdfCache={};var i,r=this._tokenMap;"object"!=typeof r[e]?r[e]=i={$numDocumentOccurrences:0,$totalNumOccurrences:1,$uidMap:{}}:(i=r[e]).$totalNumOccurrences++;var o=i.$uidMap;"object"!=typeof o[t]?(i.$numDocumentOccurrences++,o[t]={$document:n,$numTokenOccurrences:1}):o[t].$numTokenOccurrences++},t.search=function(e,t){for(var n={},i=0,r=e.length;i<r;i++){var o=e[i],a=this._tokenMap[o];if(!a)return[];if(0===i)for(var s=0,u=(c=Object.keys(a.$uidMap)).length;s<u;s++){n[h=c[s]]=a.$uidMap[h].$document}else{var c;for(s=0,u=(c=Object.keys(n)).length;s<u;s++){var h=c[s];"object"!=typeof a.$uidMap[h]&&delete n[h]}}}var f=[];for(var h in n)f.push(n[h]);var d=this._createCalculateTfIdf();return f.sort((function(n,i){return d(e,i,t)-d(e,n,t)}))},t._createCalculateIdf=function(){var e=this._tokenMap,t=this._tokenToIdfCache;return function(n,i){if(!t[n]){var r=void 0!==e[n]?e[n].$numDocumentOccurrences:0;t[n]=1+Math.log(i.length/(1+r))}return t[n]}},t._createCalculateTfIdf=function(){var e=this._tokenMap,t=this._uidFieldName,n=this._createCalculateIdf();return function(i,r,o){for(var s=0,u=0,c=i.length;u<c;++u){var h,f=i[u],d=n(f,o);d===1/0&&(d=0),h=t instanceof Array?r&&a(r,t):r&&r[t],s+=(void 0!==e[f]&&void 0!==e[f].$uidMap[h]?e[f].$uidMap[h].$numTokenOccurrences:0)*d}return s}},e}(),u=function(){function e(){this._tokenToUidToDocumentMap={}}var t=e.prototype;return t.indexDocument=function(e,t,n){"object"!=typeof this._tokenToUidToDocumentMap[e]&&(this._tokenToUidToDocumentMap[e]={}),this._tokenToUidToDocumentMap[e][t]=n},t.search=function(e,t){for(var n={},i=this._tokenToUidToDocumentMap,r=0,o=e.length;r<o;r++){var a=i[e[r]];if(!a)return[];if(0===r)for(var s=0,u=(c=Object.keys(a)).length;s<u;s++){n[f=c[s]]=a[f]}else for(s=0,u=(c=Object.keys(n)).length;s<u;s++){"object"!=typeof a[f=c[s]]&&delete n[f]}}var c,h=[];for(r=0,u=(c=Object.keys(n)).length;r<u;r++){var f=c[r];h.push(n[f])}return h},e}(),c=/[^a-zа-яё0-9\-']+/i,h=function(){function e(){}return e.prototype.tokenize=function(e){return e.split(c).filter((function(e){return e}))},e}(),f=function(){function e(e,t){this._stemmingFunction=e,this._tokenizer=t}return e.prototype.tokenize=function(e){return this._tokenizer.tokenize(e).map(this._stemmingFunction)},e}(),d={a:!0,able:!0,about:!0,across:!0,after:!0,all:!0,almost:!0,also:!0,am:!0,among:!0,an:!0,and:!0,any:!0,are:!0,as:!0,at:!0,be:!0,because:!0,been:!0,but:!0,by:!0,can:!0,cannot:!0,could:!0,dear:!0,did:!0,do:!0,does:!0,either:!0,else:!0,ever:!0,every:!0,for:!0,from:!0,get:!0,got:!0,had:!0,has:!0,have:!0,he:!0,her:!0,hers:!0,him:!0,his:!0,how:!0,however:!0,i:!0,if:!0,in:!0,into:!0,is:!0,it:!0,its:!0,just:!0,least:!0,let:!0,like:!0,likely:!0,may:!0,me:!0,might:!0,most:!0,must:!0,my:!0,neither:!0,no:!0,nor:!0,not:!0,of:!0,off:!0,often:!0,on:!0,only:!0,or:!0,other:!0,our:!0,own:!0,rather:!0,said:!0,say:!0,says:!0,she:!0,should:!0,since:!0,so:!0,some:!0,than:!0,that:!0,the:!0,their:!0,them:!0,then:!0,there:!0,these:!0,they:!0,this:!0,tis:!0,to:!0,too:!0,twas:!0,us:!0,wants:!0,was:!0,we:!0,were:!0,what:!0,when:!0,where:!0,which:!0,while:!0,who:!0,whom:!0,why:!0,will:!0,with:!0,would:!0,yet:!0,you:!0,your:!0,constructor:!1,hasOwnProperty:!1,isPrototypeOf:!1,propertyIsEnumerable:!1,toLocaleString:!1,toString:!1,valueOf:!1},l=function(){function e(e){this._tokenizer=e}return e.prototype.tokenize=function(e){return this._tokenizer.tokenize(e).filter((function(e){return!d[e]}))},e}();function p(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var _=function(){function e(e){if(!e)throw Error("js-search requires a uid field name constructor parameter");this._uidFieldName=e,this._indexStrategy=new i,this._searchIndex=new s(e),this._sanitizer=new o,this._tokenizer=new h,this._documents=[],this._searchableFields=[]}var t,n,r,u=e.prototype;return u.addDocument=function(e){this.addDocuments([e])},u.addDocuments=function(e){this._documents=this._documents.concat(e),this.indexDocuments_(e,this._searchableFields)},u.addIndex=function(e){this._searchableFields.push(e),this.indexDocuments_(this._documents,[e])},u.search=function(e){var t=this._tokenizer.tokenize(this._sanitizer.sanitize(e));return this._searchIndex.search(t,this._documents)},u.indexDocuments_=function(e,t){this._initialized=!0;for(var n=this._indexStrategy,i=this._sanitizer,r=this._searchIndex,o=this._tokenizer,s=this._uidFieldName,u=0,c=e.length;u<c;u++){var h,f=e[u];h=s instanceof Array?a(f,s):f[s];for(var d=0,l=t.length;d<l;d++){var p,_=t[d];if(null!=(p=_ instanceof Array?a(f,_):f[_])&&"string"!=typeof p&&p.toString&&(p=p.toString()),"string"==typeof p)for(var m=o.tokenize(i.sanitize(p)),y=0,g=m.length;y<g;y++)for(var v=m[y],k=n.expandToken(v),z=0,x=k.length;z<x;z++){var w=k[z];r.indexDocument(w,h,f)}}}},t=e,(n=[{key:"indexStrategy",set:function(e){if(this._initialized)throw Error("IIndexStrategy cannot be set after initialization");this._indexStrategy=e},get:function(){return this._indexStrategy}},{key:"sanitizer",set:function(e){if(this._initialized)throw Error("ISanitizer cannot be set after initialization");this._sanitizer=e},get:function(){return this._sanitizer}},{key:"searchIndex",set:function(e){if(this._initialized)throw Error("ISearchIndex cannot be set after initialization");this._searchIndex=e},get:function(){return this._searchIndex}},{key:"tokenizer",set:function(e){if(this._initialized)throw Error("ITokenizer cannot be set after initialization");this._tokenizer=e},get:function(){return this._tokenizer}}])&&p(t.prototype,n),r&&p(t,r),e}(),m=function(){function e(e,t,n){this._indexStrategy=e||new i,this._sanitizer=t||new o,this._wrapperTagName=n||"mark"}var t=e.prototype;return t.highlight=function(e,t){for(var n=this._wrapText("").length,i=Object.create(null),r=0,o=t.length;r<o;r++)for(var a=this._sanitizer.sanitize(t[r]),s=this._indexStrategy.expandToken(a),u=0,c=s.length;u<c;u++){var h=s[u];i[h]?i[h].push(a):i[h]=[a]}for(var f="",d="",l=0,p=(r=0,e.length);r<p;r++){var _=e.charAt(r);" "===_?(f="",d="",l=r+1):(f+=_,d+=this._sanitizer.sanitize(_)),i[d]&&i[d].indexOf(d)>=0&&(f=this._wrapText(f),e=e.substring(0,l)+f+e.substring(r+1),r+=n,p+=n)}return e},t._wrapText=function(e){var t=this._wrapperTagName;return"<"+t+">"+e+"</"+t+">"},e}();e.AllSubstringsIndexStrategy=t,e.CaseSensitiveSanitizer=r,e.ExactWordIndexStrategy=n,e.LowerCaseSanitizer=o,e.PrefixIndexStrategy=i,e.Search=_,e.SimpleTokenizer=h,e.StemmingTokenizer=f,e.StopWordsMap=d,e.StopWordsTokenizer=l,e.TfIdfSearchIndex=s,e.TokenHighlighter=m,e.UnorderedSearchIndex=u,Object.defineProperty(e,"__esModule",{value:!0})}));