UNPKG

2.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10var iter_1 = require("./iter");
11/**
12 * Iterate over an iterable using a stepped increment.
13 *
14 * @param object - The iterable or array-like object of interest.
15 *
16 * @param step - The distance to step on each iteration. A value
17 * of less than `1` will behave the same as a value of `1`.
18 *
19 * @returns An iterator which traverses the iterable step-wise.
20 *
21 * #### Example
22 * ```typescript
23 * import { stride, toArray } from '@phosphor/algorithm';
24 *
25 * let data = [1, 2, 3, 4, 5, 6];
26 *
27 * let stream = stride(data, 2);
28 *
29 * toArray(stream); // [1, 3, 5];
30 * ```
31 */
32function stride(object, step) {
33 return new StrideIterator(iter_1.iter(object), step);
34}
35exports.stride = stride;
36/**
37 * An iterator which traverses a source iterator step-wise.
38 */
39var StrideIterator = /** @class */ (function () {
40 /**
41 * Construct a new stride iterator.
42 *
43 * @param source - The iterator of values of interest.
44 *
45 * @param step - The distance to step on each iteration. A value
46 * of less than `1` will behave the same as a value of `1`.
47 */
48 function StrideIterator(source, step) {
49 this._source = source;
50 this._step = step;
51 }
52 /**
53 * Get an iterator over the object's values.
54 *
55 * @returns An iterator which yields the object's values.
56 */
57 StrideIterator.prototype.iter = function () {
58 return this;
59 };
60 /**
61 * Create an independent clone of the iterator.
62 *
63 * @returns A new independent clone of the iterator.
64 */
65 StrideIterator.prototype.clone = function () {
66 return new StrideIterator(this._source.clone(), this._step);
67 };
68 /**
69 * Get the next value from the iterator.
70 *
71 * @returns The next value from the iterator, or `undefined`.
72 */
73 StrideIterator.prototype.next = function () {
74 var value = this._source.next();
75 for (var n = this._step - 1; n > 0; --n) {
76 this._source.next();
77 }
78 return value;
79 };
80 return StrideIterator;
81}());
82exports.StrideIterator = StrideIterator;