UNPKG

2.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Create an iterator which repeats a value a number of times.
5 *
6 * @param value - The value to repeat.
7 *
8 * @param count - The number of times to repeat the value.
9 *
10 * @returns A new iterator which repeats the specified value.
11 *
12 * #### Example
13 * ```typescript
14 * import { repeat, toArray } from '@phosphor/algorithm';
15 *
16 * let stream = repeat(7, 3);
17 *
18 * toArray(stream); // [7, 7, 7]
19 * ```
20 */
21function repeat(value, count) {
22 return new RepeatIterator(value, count);
23}
24exports.repeat = repeat;
25/**
26 * Create an iterator which yields a value a single time.
27 *
28 * @param value - The value to wrap in an iterator.
29 *
30 * @returns A new iterator which yields the value a single time.
31 *
32 * #### Example
33 * ```typescript
34 * import { once, toArray } from '@phosphor/algorithm';
35 *
36 * let stream = once(7);
37 *
38 * toArray(stream); // [7]
39 * ```
40 */
41function once(value) {
42 return new RepeatIterator(value, 1);
43}
44exports.once = once;
45/**
46 * An iterator which repeats a value a specified number of times.
47 */
48var RepeatIterator = /** @class */ (function () {
49 /**
50 * Construct a new repeat iterator.
51 *
52 * @param value - The value to repeat.
53 *
54 * @param count - The number of times to repeat the value.
55 */
56 function RepeatIterator(value, count) {
57 this._value = value;
58 this._count = count;
59 }
60 /**
61 * Get an iterator over the object's values.
62 *
63 * @returns An iterator which yields the object's values.
64 */
65 RepeatIterator.prototype.iter = function () {
66 return this;
67 };
68 /**
69 * Create an independent clone of the iterator.
70 *
71 * @returns A new independent clone of the iterator.
72 */
73 RepeatIterator.prototype.clone = function () {
74 return new RepeatIterator(this._value, this._count);
75 };
76 /**
77 * Get the next value from the iterator.
78 *
79 * @returns The next value from the iterator, or `undefined`.
80 */
81 RepeatIterator.prototype.next = function () {
82 if (this._count <= 0) {
83 return undefined;
84 }
85 this._count--;
86 return this._value;
87 };
88 return RepeatIterator;
89}());
90exports.RepeatIterator = RepeatIterator;