1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | /**
|
4 | * Create an iterator for a retroable object.
|
5 | *
|
6 | * @param object - The retroable or array-like object of interest.
|
7 | *
|
8 | * @returns An iterator which traverses the object's values in reverse.
|
9 | *
|
10 | * #### Example
|
11 | * ```typescript
|
12 | * import { retro, toArray } from '@phosphor/algorithm';
|
13 | *
|
14 | * let data = [1, 2, 3, 4, 5, 6];
|
15 | *
|
16 | * let stream = retro(data);
|
17 | *
|
18 | * toArray(stream); // [6, 5, 4, 3, 2, 1]
|
19 | * ```
|
20 | */
|
21 | function retro(object) {
|
22 | var it;
|
23 | if (typeof object.retro === 'function') {
|
24 | it = object.retro();
|
25 | }
|
26 | else {
|
27 | it = new RetroArrayIterator(object);
|
28 | }
|
29 | return it;
|
30 | }
|
31 | exports.retro = retro;
|
32 | /**
|
33 | * An iterator which traverses an array-like object in reverse.
|
34 | *
|
35 | * #### Notes
|
36 | * This iterator can be used for any builtin JS array-like object.
|
37 | */
|
38 | var RetroArrayIterator = /** @class */ (function () {
|
39 | /**
|
40 | * Construct a new retro iterator.
|
41 | *
|
42 | * @param source - The array-like object of interest.
|
43 | */
|
44 | function RetroArrayIterator(source) {
|
45 | this._source = source;
|
46 | this._index = source.length - 1;
|
47 | }
|
48 | /**
|
49 | * Get an iterator over the object's values.
|
50 | *
|
51 | * @returns An iterator which yields the object's values.
|
52 | */
|
53 | RetroArrayIterator.prototype.iter = function () {
|
54 | return this;
|
55 | };
|
56 | /**
|
57 | * Create an independent clone of the iterator.
|
58 | *
|
59 | * @returns A new independent clone of the iterator.
|
60 | */
|
61 | RetroArrayIterator.prototype.clone = function () {
|
62 | var result = new RetroArrayIterator(this._source);
|
63 | result._index = this._index;
|
64 | return result;
|
65 | };
|
66 | /**
|
67 | * Get the next value from the iterator.
|
68 | *
|
69 | * @returns The next value from the iterator, or `undefined`.
|
70 | */
|
71 | RetroArrayIterator.prototype.next = function () {
|
72 | if (this._index < 0 || this._index >= this._source.length) {
|
73 | return undefined;
|
74 | }
|
75 | return this._source[this._index--];
|
76 | };
|
77 | return RetroArrayIterator;
|
78 | }());
|
79 | exports.RetroArrayIterator = RetroArrayIterator;
|