UNPKG

12.4 kBTypeScriptView Raw
1/**
2 * An object which can produce an iterator over its values.
3 */
4export interface IIterable<T> {
5 /**
6 * Get an iterator over the object's values.
7 *
8 * @returns An iterator which yields the object's values.
9 *
10 * #### Notes
11 * Depending on the iterable, the returned iterator may or may not be
12 * a new object. A collection or other container-like object should
13 * typically return a new iterator, while an iterator itself should
14 * normally return `this`.
15 */
16 iter(): IIterator<T>;
17}
18/**
19 * An object which traverses a collection of values.
20 *
21 * #### Notes
22 * An `IIterator` is itself an `IIterable`. Most implementations of
23 * `IIterator` should simply return `this` from the `iter()` method.
24 */
25export interface IIterator<T> extends IIterable<T> {
26 /**
27 * Create an independent clone of the iterator.
28 *
29 * @returns A new independent clone of the iterator.
30 *
31 * #### Notes
32 * The cloned iterator can be consumed independently of the current
33 * iterator. In essence, it is a copy of the iterator value stream
34 * which starts at the current location.
35 *
36 * This can be useful for lookahead and stream duplication.
37 */
38 clone(): IIterator<T>;
39 /**
40 * Get the next value from the iterator.
41 *
42 * @returns The next value from the iterator, or `undefined`.
43 *
44 * #### Notes
45 * The `undefined` value is used to signal the end of iteration and
46 * should therefore not be used as a value in a collection.
47 *
48 * The use of the `undefined` sentinel is an explicit design choice
49 * which favors performance over purity. The ES6 iterator design of
50 * returning a `{ value, done }` pair is suboptimal, as it requires
51 * an object allocation on each iteration; and an `isDone()` method
52 * would increase implementation and runtime complexity.
53 */
54 next(): T | undefined;
55}
56/**
57 * A type alias for an iterable or builtin array-like object.
58 */
59export declare type IterableOrArrayLike<T> = IIterable<T> | ArrayLike<T>;
60/**
61 * Create an iterator for an iterable object.
62 *
63 * @param object - The iterable or array-like object of interest.
64 *
65 * @returns A new iterator for the given object.
66 *
67 * #### Notes
68 * This function allows iteration algorithms to operate on user-defined
69 * iterable types and builtin array-like objects in a uniform fashion.
70 */
71export declare function iter<T>(object: IterableOrArrayLike<T>): IIterator<T>;
72/**
73 * Create an iterator for the keys in an object.
74 *
75 * @param object - The object of interest.
76 *
77 * @returns A new iterator for the keys in the given object.
78 *
79 * #### Complexity
80 * Linear.
81 *
82 * #### Example
83 * ```typescript
84 * import { each, keys } from '@lumino/algorithm';
85 *
86 * let data = { one: 1, two: 2, three: 3 };
87 *
88 * each(keys(data), key => { console.log(key); }); // 'one', 'two', 'three'
89 * ```
90 */
91export declare function iterKeys<T>(object: {
92 readonly [key: string]: T;
93}): IIterator<string>;
94/**
95 * Create an iterator for the values in an object.
96 *
97 * @param object - The object of interest.
98 *
99 * @returns A new iterator for the values in the given object.
100 *
101 * #### Complexity
102 * Linear.
103 *
104 * #### Example
105 * ```typescript
106 * import { each, values } from '@lumino/algorithm';
107 *
108 * let data = { one: 1, two: 2, three: 3 };
109 *
110 * each(values(data), value => { console.log(value); }); // 1, 2, 3
111 * ```
112 */
113export declare function iterValues<T>(object: {
114 readonly [key: string]: T;
115}): IIterator<T>;
116/**
117 * Create an iterator for the items in an object.
118 *
119 * @param object - The object of interest.
120 *
121 * @returns A new iterator for the items in the given object.
122 *
123 * #### Complexity
124 * Linear.
125 *
126 * #### Example
127 * ```typescript
128 * import { each, items } from '@lumino/algorithm';
129 *
130 * let data = { one: 1, two: 2, three: 3 };
131 *
132 * each(items(data), value => { console.log(value); }); // ['one', 1], ['two', 2], ['three', 3]
133 * ```
134 */
135export declare function iterItems<T>(object: {
136 readonly [key: string]: T;
137}): IIterator<[string, T]>;
138/**
139 * Create an iterator for an iterator-like function.
140 *
141 * @param fn - A function which behaves like an iterator `next` method.
142 *
143 * @returns A new iterator for the given function.
144 *
145 * #### Notes
146 * The returned iterator **cannot** be cloned.
147 *
148 * #### Example
149 * ```typescript
150 * import { each, iterFn } from '@lumino/algorithm';
151 *
152 * let it = iterFn((() => {
153 * let i = 0;
154 * return () => i > 3 ? undefined : i++;
155 * })());
156 *
157 * each(it, v => { console.log(v); }); // 0, 1, 2, 3
158 * ```
159 */
160export declare function iterFn<T>(fn: () => T | undefined): IIterator<T>;
161/**
162 * Invoke a function for each value in an iterable.
163 *
164 * @param object - The iterable or array-like object of interest.
165 *
166 * @param fn - The callback function to invoke for each value.
167 *
168 * #### Notes
169 * Iteration can be terminated early by returning `false` from the
170 * callback function.
171 *
172 * #### Complexity
173 * Linear.
174 *
175 * #### Example
176 * ```typescript
177 * import { each } from '@lumino/algorithm';
178 *
179 * let data = [5, 7, 0, -2, 9];
180 *
181 * each(data, value => { console.log(value); });
182 * ```
183 */
184export declare function each<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean | void): void;
185/**
186 * Test whether all values in an iterable satisfy a predicate.
187 *
188 * @param object - The iterable or array-like object of interest.
189 *
190 * @param fn - The predicate function to invoke for each value.
191 *
192 * @returns `true` if all values pass the test, `false` otherwise.
193 *
194 * #### Notes
195 * Iteration terminates on the first `false` predicate result.
196 *
197 * #### Complexity
198 * Linear.
199 *
200 * #### Example
201 * ```typescript
202 * import { every } from '@lumino/algorithm';
203 *
204 * let data = [5, 7, 1];
205 *
206 * every(data, value => value % 2 === 0); // false
207 * every(data, value => value % 2 === 1); // true
208 * ```
209 */
210export declare function every<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): boolean;
211/**
212 * Test whether any value in an iterable satisfies a predicate.
213 *
214 * @param object - The iterable or array-like object of interest.
215 *
216 * @param fn - The predicate function to invoke for each value.
217 *
218 * @returns `true` if any value passes the test, `false` otherwise.
219 *
220 * #### Notes
221 * Iteration terminates on the first `true` predicate result.
222 *
223 * #### Complexity
224 * Linear.
225 *
226 * #### Example
227 * ```typescript
228 * import { some } from '@lumino/algorithm';
229 *
230 * let data = [5, 7, 1];
231 *
232 * some(data, value => value === 7); // true
233 * some(data, value => value === 3); // false
234 * ```
235 */
236export declare function some<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): boolean;
237/**
238 * Create an array from an iterable of values.
239 *
240 * @param object - The iterable or array-like object of interest.
241 *
242 * @returns A new array of values from the given object.
243 *
244 * #### Example
245 * ```typescript
246 * import { iter, toArray } from '@lumino/algorithm';
247 *
248 * let data = [1, 2, 3, 4, 5, 6];
249 *
250 * let stream = iter(data);
251 *
252 * toArray(stream); // [1, 2, 3, 4, 5, 6];
253 * ```
254 */
255export declare function toArray<T>(object: IterableOrArrayLike<T>): T[];
256/**
257 * Create an object from an iterable of key/value pairs.
258 *
259 * @param object - The iterable or array-like object of interest.
260 *
261 * @returns A new object mapping keys to values.
262 *
263 * #### Example
264 * ```typescript
265 * import { toObject } from '@lumino/algorithm';
266 *
267 * let data = [['one', 1], ['two', 2], ['three', 3]];
268 *
269 * toObject(data); // { one: 1, two: 2, three: 3 }
270 * ```
271 */
272export declare function toObject<T>(object: IterableOrArrayLike<[string, T]>): {
273 [key: string]: T;
274};
275/**
276 * An iterator for an array-like object.
277 *
278 * #### Notes
279 * This iterator can be used for any builtin JS array-like object.
280 */
281export declare class ArrayIterator<T> implements IIterator<T> {
282 /**
283 * Construct a new array iterator.
284 *
285 * @param source - The array-like object of interest.
286 */
287 constructor(source: ArrayLike<T>);
288 /**
289 * Get an iterator over the object's values.
290 *
291 * @returns An iterator which yields the object's values.
292 */
293 iter(): IIterator<T>;
294 /**
295 * Create an independent clone of the iterator.
296 *
297 * @returns A new independent clone of the iterator.
298 */
299 clone(): IIterator<T>;
300 /**
301 * Get the next value from the iterator.
302 *
303 * @returns The next value from the iterator, or `undefined`.
304 */
305 next(): T | undefined;
306 private _index;
307 private _source;
308}
309/**
310 * An iterator for the keys in an object.
311 *
312 * #### Notes
313 * This iterator can be used for any JS object.
314 */
315export declare class KeyIterator implements IIterator<string> {
316 /**
317 * Construct a new key iterator.
318 *
319 * @param source - The object of interest.
320 *
321 * @param keys - The keys to iterate, if known.
322 */
323 constructor(source: {
324 readonly [key: string]: any;
325 }, keys?: string[]);
326 /**
327 * Get an iterator over the object's values.
328 *
329 * @returns An iterator which yields the object's values.
330 */
331 iter(): IIterator<string>;
332 /**
333 * Create an independent clone of the iterator.
334 *
335 * @returns A new independent clone of the iterator.
336 */
337 clone(): IIterator<string>;
338 /**
339 * Get the next value from the iterator.
340 *
341 * @returns The next value from the iterator, or `undefined`.
342 */
343 next(): string | undefined;
344 private _index;
345 private _keys;
346 private _source;
347}
348/**
349 * An iterator for the values in an object.
350 *
351 * #### Notes
352 * This iterator can be used for any JS object.
353 */
354export declare class ValueIterator<T> implements IIterator<T> {
355 /**
356 * Construct a new value iterator.
357 *
358 * @param source - The object of interest.
359 *
360 * @param keys - The keys to iterate, if known.
361 */
362 constructor(source: {
363 readonly [key: string]: T;
364 }, keys?: string[]);
365 /**
366 * Get an iterator over the object's values.
367 *
368 * @returns An iterator which yields the object's values.
369 */
370 iter(): IIterator<T>;
371 /**
372 * Create an independent clone of the iterator.
373 *
374 * @returns A new independent clone of the iterator.
375 */
376 clone(): IIterator<T>;
377 /**
378 * Get the next value from the iterator.
379 *
380 * @returns The next value from the iterator, or `undefined`.
381 */
382 next(): T | undefined;
383 private _index;
384 private _keys;
385 private _source;
386}
387/**
388 * An iterator for the items in an object.
389 *
390 * #### Notes
391 * This iterator can be used for any JS object.
392 */
393export declare class ItemIterator<T> implements IIterator<[string, T]> {
394 /**
395 * Construct a new item iterator.
396 *
397 * @param source - The object of interest.
398 *
399 * @param keys - The keys to iterate, if known.
400 */
401 constructor(source: {
402 readonly [key: string]: T;
403 }, keys?: string[]);
404 /**
405 * Get an iterator over the object's values.
406 *
407 * @returns An iterator which yields the object's values.
408 */
409 iter(): IIterator<[string, T]>;
410 /**
411 * Create an independent clone of the iterator.
412 *
413 * @returns A new independent clone of the iterator.
414 */
415 clone(): IIterator<[string, T]>;
416 /**
417 * Get the next value from the iterator.
418 *
419 * @returns The next value from the iterator, or `undefined`.
420 */
421 next(): [string, T] | undefined;
422 private _index;
423 private _keys;
424 private _source;
425}
426/**
427 * An iterator for an iterator-like function.
428 */
429export declare class FnIterator<T> implements IIterator<T> {
430 /**
431 * Construct a new function iterator.
432 *
433 * @param fn - The iterator-like function of interest.
434 */
435 constructor(fn: () => T | undefined);
436 /**
437 * Get an iterator over the object's values.
438 *
439 * @returns An iterator which yields the object's values.
440 */
441 iter(): IIterator<T>;
442 /**
443 * Create an independent clone of the iterator.
444 *
445 * @returns A new independent clone of the iterator.
446 */
447 clone(): IIterator<T>;
448 /**
449 * Get the next value from the iterator.
450 *
451 * @returns The next value from the iterator, or `undefined`.
452 */
453 next(): T | undefined;
454 private _fn;
455}
456//# sourceMappingURL=iter.d.ts.map
\No newline at end of file