UNPKG

@stdlib/esm

Version:

ES module distribution for Stdlib, a standard library for JavaScript and Node.js.

3 lines 2.21 kB
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 import t from"./../assert/is-collection.js";import e from"./../assert/is-positive-integer.js";import r from"./../assert/is-nonnegative-integer.js";import i from"./define-nonenumerable-read-only-property.js";import n from"./define-nonenumerable-read-only-accessor.js";import o from"./../symbol/iterator.js";import s from"./../constants/math/float64-max.js";var u=t,h=e.isPrimitive,f=r.isPrimitive,a=i,l=n,p=o,_=s;function c(t){var e;if(!(this instanceof c))return new c(t);if(h(t))for(this._buffer=[],e=0;e<t;e++)this._buffer.push(0);else{if(!u(t))throw new TypeError("invalid argument. Must provide either a valid buffer size (positive integer) or an array-like object which can serve as the underlying buffer. Value: `"+t+"`.");this._buffer=t}return this._length=this._buffer.length,this._count=0,this._i=-1,this}a(c.prototype,"clear",(function(){return this._count=0,this._i=-1,this})),l(c.prototype,"count",(function(){return this._count})),l(c.prototype,"full",(function(){return this._count===this._length})),a(c.prototype,"iterator",(function(t){var e,r,i,n,o,s;if(arguments.length){if(!f(t))throw new TypeError("invalid argument. Must provide a nonnegative integer. Value: `"+t+"`.");n=t}else n=_;return r=this,s=this._i,o=0,a(e={},"next",u),a(e,"return",h),p&&a(e,p,l),e;function u(){return o+=1,i||o>n?{done:!0}:r._count!==r._length?(i=!0,{done:!0}):(s=(s+1)%r._length,{value:r._buffer[s],done:!1})}function h(t){return i=!0,arguments.length?{value:t,done:!0}:{done:!0}}function l(){return r.iterator(n)}})),l(c.prototype,"length",(function(){return this._length})),a(c.prototype,"push",(function(t){var e;return this._i=(this._i+1)%this._length,this._count<this._length?(this._buffer[this._i]=t,void(this._count+=1)):(e=this._buffer[this._i],this._buffer[this._i]=t,e)})),a(c.prototype,"toArray",(function(){var t,e,r;for(t=[],r=1;r<=this._count;r++)e=(this._i+r)%this._count,t.push(this._buffer[e]);return t})),a(c.prototype,"toJSON",(function(){var t={type:"circular-buffer"};return t.length=this._length,t.data=this.toArray(),t}));var m=c;export default m; //# sourceMappingURL=circular-buffer.js.map