1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | /**
|
4 | * Create an iterator of evenly spaced values.
|
5 | *
|
6 | * @param start - The starting value for the range, inclusive.
|
7 | *
|
8 | * @param stop - The stopping value for the range, exclusive.
|
9 | *
|
10 | * @param step - The distance between each value.
|
11 | *
|
12 | * @returns An iterator which produces evenly spaced values.
|
13 | *
|
14 | * #### Notes
|
15 | * In the single argument form of `range(stop)`, `start` defaults to
|
16 | * `0` and `step` defaults to `1`.
|
17 | *
|
18 | * In the two argument form of `range(start, stop)`, `step` defaults
|
19 | * to `1`.
|
20 | */
|
21 | function range(start, stop, step) {
|
22 | if (stop === undefined) {
|
23 | return new RangeIterator(0, start, 1);
|
24 | }
|
25 | if (step === undefined) {
|
26 | return new RangeIterator(start, stop, 1);
|
27 | }
|
28 | return new RangeIterator(start, stop, step);
|
29 | }
|
30 | exports.range = range;
|
31 | /**
|
32 | * An iterator which produces a range of evenly spaced values.
|
33 | */
|
34 | var RangeIterator = /** @class */ (function () {
|
35 | /**
|
36 | * Construct a new range iterator.
|
37 | *
|
38 | * @param start - The starting value for the range, inclusive.
|
39 | *
|
40 | * @param stop - The stopping value for the range, exclusive.
|
41 | *
|
42 | * @param step - The distance between each value.
|
43 | */
|
44 | function RangeIterator(start, stop, step) {
|
45 | this._index = 0;
|
46 | this._start = start;
|
47 | this._stop = stop;
|
48 | this._step = step;
|
49 | this._length = Private.rangeLength(start, stop, step);
|
50 | }
|
51 | /**
|
52 | * Get an iterator over the object's values.
|
53 | *
|
54 | * @returns An iterator which yields the object's values.
|
55 | */
|
56 | RangeIterator.prototype.iter = function () {
|
57 | return this;
|
58 | };
|
59 | /**
|
60 | * Create an independent clone of the iterator.
|
61 | *
|
62 | * @returns A new independent clone of the iterator.
|
63 | */
|
64 | RangeIterator.prototype.clone = function () {
|
65 | var result = new RangeIterator(this._start, this._stop, this._step);
|
66 | result._index = this._index;
|
67 | return result;
|
68 | };
|
69 | /**
|
70 | * Get the next value from the iterator.
|
71 | *
|
72 | * @returns The next value from the iterator, or `undefined`.
|
73 | */
|
74 | RangeIterator.prototype.next = function () {
|
75 | if (this._index >= this._length) {
|
76 | return undefined;
|
77 | }
|
78 | return this._start + this._step * this._index++;
|
79 | };
|
80 | return RangeIterator;
|
81 | }());
|
82 | exports.RangeIterator = RangeIterator;
|
83 | /**
|
84 | * The namespace for the module implementation details.
|
85 | */
|
86 | var Private;
|
87 | (function (Private) {
|
88 | /**
|
89 | * Compute the effective length of a range.
|
90 | *
|
91 | * @param start - The starting value for the range, inclusive.
|
92 | *
|
93 | * @param stop - The stopping value for the range, exclusive.
|
94 | *
|
95 | * @param step - The distance between each value.
|
96 | *
|
97 | * @returns The number of steps need to traverse the range.
|
98 | */
|
99 | function rangeLength(start, stop, step) {
|
100 | if (step === 0) {
|
101 | return Infinity;
|
102 | }
|
103 | if (start > stop && step > 0) {
|
104 | return 0;
|
105 | }
|
106 | if (start < stop && step < 0) {
|
107 | return 0;
|
108 | }
|
109 | return Math.ceil((stop - start) / step);
|
110 | }
|
111 | Private.rangeLength = rangeLength;
|
112 | })(Private || (Private = {}));
|