UNPKG

2.44 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 * Take a fixed number of items from an iterable.
13 *
14 * @param object - The iterable or array-like object of interest.
15 *
16 * @param count - The number of items to take from the iterable.
17 *
18 * @returns An iterator which yields the specified number of items
19 * from the source iterable.
20 *
21 * #### Notes
22 * The returned iterator will exhaust early if the source iterable
23 * contains an insufficient number of items.
24 */
25function take(object, count) {
26 return new TakeIterator(iter_1.iter(object), count);
27}
28exports.take = take;
29/**
30 * An iterator which takes a fixed number of items from a source.
31 */
32var TakeIterator = /** @class */ (function () {
33 /**
34 * Construct a new take iterator.
35 *
36 * @param source - The iterator of interest.
37 *
38 * @param count - The number of items to take from the source.
39 */
40 function TakeIterator(source, count) {
41 this._source = source;
42 this._count = count;
43 }
44 /**
45 * Get an iterator over the object's values.
46 *
47 * @returns An iterator which yields the object's values.
48 */
49 TakeIterator.prototype.iter = function () {
50 return this;
51 };
52 /**
53 * Create an independent clone of the iterator.
54 *
55 * @returns A new independent clone of the iterator.
56 */
57 TakeIterator.prototype.clone = function () {
58 return new TakeIterator(this._source.clone(), this._count);
59 };
60 /**
61 * Get the next value from the iterator.
62 *
63 * @returns The next value from the iterator, or `undefined`.
64 */
65 TakeIterator.prototype.next = function () {
66 if (this._count <= 0) {
67 return undefined;
68 }
69 var value = this._source.next();
70 if (value === undefined) {
71 return undefined;
72 }
73 this._count--;
74 return value;
75 };
76 return TakeIterator;
77}());
78exports.TakeIterator = TakeIterator;