iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
85 lines • 3.43 kB
JavaScript
import toArray from '../toArray';
/**
* A class for representing a range of numbers and also iterating through them. When `next.done` is true, resets the
* internal counter back to `start`.
*/
var RangeIterator = /** @class */ (function () {
function RangeIterator() {
var params = [];
for (var _i = 0; _i < arguments.length; _i++) {
params[_i] = arguments[_i];
}
/** Alias of `includes`. */
this.has = this.includes;
/** Alias of `nth`. */
this.at = this.nth;
var start = 0, stop = 0;
var step;
if (params.length === 1)
stop = params[0];
else if (params.length > 1)
start = params[0], stop = params[1], step = params[2];
if (typeof step !== 'number')
step = Math.sign(stop - start);
var stepSign = Math.sign(step);
if (stepSign === 0)
this.next = function () { return ({ done: true, value: undefined }); };
this.start = this.i = start;
this.stop = stop;
this.step = step;
this.stepSign = stepSign;
// If the start is not in this range, then it's 0, otherwise calc normally.
this._length = this.includes(start) ? Math.abs(Math.ceil((stop - start) / step)) : 0;
}
Object.defineProperty(RangeIterator.prototype, "length", {
/** The length of this range of numbers. */
get: function () {
return this._length;
},
enumerable: false,
configurable: true
});
RangeIterator.prototype[Symbol.iterator] = function () {
return this;
};
RangeIterator.prototype.next = function () {
if (Math.sign(this.stop - this.i) !== this.stepSign) {
this.i = this.start;
return { done: true, value: undefined };
}
var value = this.i;
this.i += this.step;
return { done: false, value: value };
};
/** Returns true if `n` is inside of this range. */
RangeIterator.prototype.includes = function (n) {
if ((n - this.start) % this.step !== 0)
return false;
return this.stepSign > 0 ? n >= this.start && n < this.stop : n <= this.start && n > this.stop;
};
/** Returns the number at `index` in this range. `index` can be negative to access indices starting from the end. */
RangeIterator.prototype.nth = function (index) {
var num = index >= 0 ? this.start + index * this.step : this.stop + index * this.step;
return this.includes(num) ? num : undefined;
};
/** Returns the index that `n` is at in this range. */
RangeIterator.prototype.index = function (n) {
return (n - this.start) / this.step;
};
/** Returns true if this range is equal to another. */
RangeIterator.prototype.equal = function (other) {
return this.start === other.start && this.stop === other.stop && this.step === other.step;
};
// slice(start: number, end?: number) {}
RangeIterator.prototype.toString = function () {
return "range(".concat(this.start, ", ").concat(this.stop, ", ").concat(this.step, ")");
};
/** Iterates and collects all values into an Array. */
RangeIterator.prototype.toArray = function () {
return toArray(this);
};
return RangeIterator;
}());
export { RangeIterator };
export default RangeIterator;
//# sourceMappingURL=RangeIterator.js.map