1 | ;
|
2 | Object.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 | |----------------------------------------------------------------------------*/
|
10 | var iter_1 = require("./iter");
|
11 | /**
|
12 | * Transform the values of an iterable with a mapping function.
|
13 | *
|
14 | * @param object - The iterable or array-like object of interest.
|
15 | *
|
16 | * @param fn - The mapping function to invoke for each value.
|
17 | *
|
18 | * @returns An iterator which yields the transformed values.
|
19 | *
|
20 | * #### Example
|
21 | * ```typescript
|
22 | * import { map, toArray } from '@phosphor/algorithm';
|
23 | *
|
24 | * let data = [1, 2, 3];
|
25 | *
|
26 | * let stream = map(data, value => value * 2);
|
27 | *
|
28 | * toArray(stream); // [2, 4, 6]
|
29 | * ```
|
30 | */
|
31 | function map(object, fn) {
|
32 | return new MapIterator(iter_1.iter(object), fn);
|
33 | }
|
34 | exports.map = map;
|
35 | /**
|
36 | * An iterator which transforms values using a mapping function.
|
37 | */
|
38 | var MapIterator = /** @class */ (function () {
|
39 | /**
|
40 | * Construct a new map iterator.
|
41 | *
|
42 | * @param source - The iterator of values of interest.
|
43 | *
|
44 | * @param fn - The mapping function to invoke for each value.
|
45 | */
|
46 | function MapIterator(source, fn) {
|
47 | this._index = 0;
|
48 | this._source = source;
|
49 | this._fn = fn;
|
50 | }
|
51 | /**
|
52 | * Get an iterator over the object's values.
|
53 | *
|
54 | * @returns An iterator which yields the object's values.
|
55 | */
|
56 | MapIterator.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 | MapIterator.prototype.clone = function () {
|
65 | var result = new MapIterator(this._source.clone(), this._fn);
|
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 | MapIterator.prototype.next = function () {
|
75 | var value = this._source.next();
|
76 | if (value === undefined) {
|
77 | return undefined;
|
78 | }
|
79 | return this._fn.call(undefined, value, this._index++);
|
80 | };
|
81 | return MapIterator;
|
82 | }());
|
83 | exports.MapIterator = MapIterator;
|