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

1 year ago
//文档见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: