mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 03:25:41 +00:00
1 line
24 KiB
JavaScript
1 line
24 KiB
JavaScript
|
!function(){var e,t,r,i,n,s,o,a,u,l,d,h,c,f,p,y,m,g,x,v,w,k,Q,L,T,S,b,P,E=function(e){var t=new E.Builder;return t.pipeline.add(E.trimmer,E.stopWordFilter,E.stemmer),t.searchPipeline.add(E.stemmer),e.call(t,t),t.build()};E.version="2.1.2",E.utils={},E.utils.warn=(e=this,function(t){e.console&&console.warn&&console.warn(t)}),E.utils.asString=function(e){return null==e?"":e.toString()},E.FieldRef=function(e,t){this.docRef=e,this.fieldName=t,this._stringValue=t+E.FieldRef.joiner+e},E.FieldRef.joiner="/",E.FieldRef.fromString=function(e){var t=e.indexOf(E.FieldRef.joiner);if(-1===t)throw"malformed field ref string";var r=e.slice(0,t),i=e.slice(t+1);return new E.FieldRef(i,r)},E.FieldRef.prototype.toString=function(){return this._stringValue},E.idf=function(e,t){var r=0;for(var i in e)"_index"!=i&&(r+=Object.keys(e[i]).length);var n=(t-r+.5)/(r+.5);return Math.log(1+Math.abs(n))},E.Token=function(e,t){this.str=e||"",this.metadata=t||{}},E.Token.prototype.toString=function(){return this.str},E.Token.prototype.update=function(e){return this.str=e(this.str,this.metadata),this},E.Token.prototype.clone=function(e){return e=e||function(e){return e},new E.Token(e(this.str,this.metadata),this.metadata)},E.tokenizer=function(e){if(null==e||null==e)return[];if(Array.isArray(e))return e.map(function(e){return new E.Token(E.utils.asString(e).toLowerCase())});for(var t=e.toString().trim().toLowerCase(),r=t.length,i=[],n=0,s=0;n<=r;n++){var o=n-s;(t.charAt(n).match(E.tokenizer.separator)||n==r)&&(o>0&&i.push(new E.Token(t.slice(s,n),{position:[s,o],index:i.length})),s=n+1)}return i},E.tokenizer.separator=/[\s\-]+/,E.Pipeline=function(){this._stack=[]},E.Pipeline.registeredFunctions=Object.create(null),E.Pipeline.registerFunction=function(e,t){t in this.registeredFunctions&&E.utils.warn("Overwriting existing registered function: "+t),e.label=t,E.Pipeline.registeredFunctions[e.label]=e},E.Pipeline.warnIfFunctionNotRegistered=function(e){e.label&&e.label in this.registeredFunctions||E.utils.warn("Function is not registered with pipeline. This may cause problems when serialising the index.\n",e)},E.Pipeline.load=function(e){var t=new E.Pipeline;return e.forEach(function(e){var r=E.Pipeline.registeredFunctions[e];if(!r)throw new Error("Cannot load unregistered function: "+e);t.add(r)}),t},E.Pipeline.prototype.add=function(){Array.prototype.slice.call(arguments).forEach(function(e){E.Pipeline.warnIfFunctionNotRegistered(e),this._stack.push(e)},this)},E.Pipeline.prototype.after=function(e,t){E.Pipeline.warnIfFunctionNotRegistered(t);var r=this._stack.indexOf(e);if(-1==r)throw new Error("Cannot find existingFn");r+=1,this._stack.splice(r,0,t)},E.Pipeline.prototype.before=function(e,t){E.Pipeline.warnIfFunctionNotRegistered(t);var r=this._stack.indexOf(e);if(-1==r)throw new Error("Cannot find existingFn");this._stack.splice(r,0,t)},E.Pipeline.prototype.remove=function(e){var t=this._stack.indexOf(e);-1!=t&&this._stack.splice(t,1)},E.Pipeline.prototype.run=function(e){for(var t=this._stack.length,r=0;r<t;r++){var i=this._stack[r];e=e.reduce(function(t,r,n){var s=i(r,n,e);return void 0===s||""===s?t:t.concat(s)},[])}return e},E.Pipeline.prototype.runString=function(e){var t=new E.Token(e);return this.run([t]).map(function(e){return e.toString()})},E.Pipeline.prototype.reset=function(){this._stack=[]},E.Pipeline.prototype.toJSON=function(){return this._stack.map(function(e){return E.Pipeline.warnIfFunctionNotRegistered(e),e.label})},E.Vector=function(e){this._magnitude=0,this.elements=e||[]},E.Vector.prototype.positionForIndex=function(e){if(0==this.elements.length)return 0;for(var t=0,r=this.elements.length/2,i=r-t,n=Math.floor(i/2),s=this.elements[2*n];i>1&&(s<e&&(t=n),s>e&&(r=n),s!=e);)i=r-t,n=t+Math.floor(i/2),s=this.elements[2*n];return s==e?2*n:s>e?2*n:s<e?2*(n+1):void 0},E.Vector.prototype.insert=function(e,t){this.upsert(e,t,function(){throw"duplicate index"})},E.Vector.prototype.upsert=function(e,t,r){this._magnitude=0;var i=this.positionForIndex(e);this.elements[i]==e?this.elements[i+1]=r(this.elements[i+1],t):this.elements.splice(i,0,e,t)}
|