UNPKG

rolling-average

Version:
1 lines 4.42 kB
var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{zv:()=>s,X3:()=>p,iy:()=>l});const n=function(){},r=function(){function t(t){if(void 0===t&&(t=10),this._head=0,this._tail=0,this._inverted=!1,t<1)throw new Error("initialLength must be at least 1");this._data=new Array(t)}return Object.defineProperty(t.prototype,"length",{get:function(){return this._inverted?this._data.length-this._head+this._tail:this._tail-this._head},enumerable:!1,configurable:!0}),t.prototype.peekFront=function(){if(this._head===this._tail&&!this._inverted)throw new Error("Queue is empty");return this._data[this._head]},t.prototype.peekBack=function(){if(this._head===this._tail&&!this._inverted)throw new Error("Queue is empty");return this._data[(this._tail+this._data.length-1)%this._data.length]},t.prototype.enqueue=function(t){this.length===this._data.length&&this._resize();var e=this._tail++;this._tail%=this._data.length,0===this._tail&&(this._inverted=!0),this._data[e]=t},t.prototype.dequeue=function(){if(this._head===this._tail&&!this._inverted)throw new Error("Queue is empty");var t=this._head++;return this._head%=this._data.length,0===this._head&&(this._inverted=!1),this._data[t]},t.prototype._resize=function(){for(var t=Math.floor(1.7*this._data.length),e=new Array(t),n=0;n<this._data.length;n++)e[n]=this._data[(this._head+n)%this._data.length];this._head=0,this._tail=this._data.length,this._data=e,this._inverted=!1},t}();var i,o=(i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},i(t,e)},function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)});const s=function(t){function e(e){var n=this;if(e<1)throw new Error("windowSize must be at least 1");return(n=t.call(this)||this).sum=0,n.windowSize=e,n.samples=new r(e),n}return o(e,t),e.prototype.addSample=function(t){this.samples.enqueue(t),this.sum+=t,this.samples.length>this.windowSize&&(this.sum-=this.samples.dequeue())},e.prototype.getAverage=function(){return this.sum/this.samples.length},e}(n);var a=function(){var t=function(e,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},t(e,n)};return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();const u=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.sum=0,e.count=0,e}return a(e,t),e.prototype.addSample=function(t){this.sum+=t,this.count++},e.prototype.getAverage=function(){return this.sum/this.count},e}(n);var h=function(){var t=function(e,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},t(e,n)};return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();const l=function(t){function e(e){var n=this;if(e<1)throw new Error("windowDuration must be at least 1");return(n=t.call(this)||this).samples=new r,n.sum=0,n.windowDuration=e,n}return h(e,t),e.prototype.addSample=function(t){this.samples.enqueue({time:Date.now(),value:t}),this.sum+=t},e.prototype.getAverage=function(){for(var t=Date.now();this.samples.length>0&&t-this.samples.peekFront().time>=this.windowDuration;)this.sum-=this.samples.dequeue().value;return 0===this.samples.length?0:this.sum/this.samples.length},e}(n),p=function(){function t(){}return t.fixedWindow=function(t){return new s(t)},t.timeWindow=function(t){return new l(t)},t.regular=function(){return new u},t}();var c=e.zv,f=e.X3,_=e.iy;export{c as FixedWindowRollingAverage,f as RollingAverageFactory,_ as TimeWindowRollingAverage};