UNPKG

circularr

Version:
129 lines (110 loc) 3.08 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _Symbol$iterator; _Symbol$iterator = Symbol.iterator; var Circularr = /*#__PURE__*/function () { _createClass(Circularr, null, [{ key: "from", value: function from(source) { var arr = new Circularr(source.length); for (var i = 0; i < source.length; i++) { arr._data[i] = source[i]; } return arr; } }]); function Circularr(length) { _classCallCheck(this, Circularr); _defineProperty(this, "_data", void 0); _defineProperty(this, "_index", void 0); this._data = new Array(length); this._index = 0; } _createClass(Circularr, [{ key: _Symbol$iterator, value: function* value() { for (var i = 0; i < this._data.length; i++) { yield this._data[(i + this._index) % this._data.length]; } } }, { key: "fill", value: function fill(value) { for (var i = 0; i < this._data.length; i++) { this._data[i] = value; } this._index = 0; return this; } }, { key: "clear", value: function clear() { this._data = new Array(this._data.length); this._index = 0; return this; } }, { key: "shift", value: function shift(value) { var returnValue = this._data[this._index]; this._data[this._index] = value; this._index = (this._index + 1) % this._data.length; return returnValue; } }, { key: "unshift", value: function unshift(value) { this._index = (this._index + this._data.length - 1) % this._data.length; var returnValue = this._data[this._index]; this._data[this._index] = value; return returnValue; } }, { key: "slice", value: function slice(startIndex, endIndex) { return Circularr.from(Array.from(this).slice(startIndex, endIndex)); } }, { key: "trim", value: function trim() { var data = Array.from(this); var startIndex = 0; var endIndex = data.length; for (var i = 0; i < data.length; i++) { if (typeof data[i] !== 'undefined') { break; } startIndex++; } for (var _i = data.length - 1; _i >= 0; --_i) { if (typeof data[_i] !== 'undefined') { break; } endIndex--; } return Circularr.from(data.slice(startIndex, endIndex)); } }, { key: "at", value: function at(index) { if (index < 0 || index >= this._data.length) { return; } return this._data[(index + this._index) % this._data.length]; } }, { key: "wrapAt", value: function wrapAt(index) { return this._data[(index + this._index) % this._data.length]; } }, { key: "length", get: function get() { return this._data.length; } }]); return Circularr; }(); export { Circularr as default }; //# sourceMappingURL=index.js.map