UNPKG

3.93 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
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
11/**
12 * Create an array from an iterable of values.
13 *
14 * @deprecated
15 *
16 * @param object - The iterable object of interest.
17 *
18 * @returns A new array of values from the given object.
19 *
20 * #### Example
21 * ```typescript
22 * import { toArray } from '@lumino/algorithm';
23 *
24 * let stream = [1, 2, 3, 4, 5, 6][Symbol.iterator]();
25 *
26 * toArray(stream); // [1, 2, 3, 4, 5, 6];
27 * ```
28 */
29export function toArray<T>(object: Iterable<T>): T[] {
30 return Array.from(object);
31}
32
33/**
34 * Create an object from an iterable of key/value pairs.
35 *
36 * @param object - The iterable object of interest.
37 *
38 * @returns A new object mapping keys to values.
39 *
40 * #### Example
41 * ```typescript
42 * import { toObject } from '@lumino/algorithm';
43 *
44 * let data: [string, number][] = [['one', 1], ['two', 2], ['three', 3]];
45 *
46 * toObject(data); // { one: 1, two: 2, three: 3 }
47 * ```
48 */
49export function toObject<T>(object: Iterable<[string, T]>): {
50 [key: string]: T;
51} {
52 const result: { [key: string]: T } = {};
53 for (const [key, value] of object) {
54 result[key] = value;
55 }
56 return result;
57}
58
59/**
60 * Invoke a function for each value in an iterable.
61 *
62 * @deprecated
63 *
64 * @param object - The iterable object of interest.
65 *
66 * @param fn - The callback function to invoke for each value.
67 *
68 * #### Notes
69 * Iteration can be terminated early by returning `false` from the
70 * callback function.
71 *
72 * #### Complexity
73 * Linear.
74 *
75 * #### Example
76 * ```typescript
77 * import { each } from '@lumino/algorithm';
78 *
79 * let data = [5, 7, 0, -2, 9];
80 *
81 * each(data, value => { console.log(value); });
82 * ```
83 */
84export function each<T>(
85 object: Iterable<T>,
86 fn: (value: T, index: number) => boolean | void
87): void {
88 let index = 0;
89 for (const value of object) {
90 if (false === fn(value, index++)) {
91 return;
92 }
93 }
94}
95
96/**
97 * Test whether all values in an iterable satisfy a predicate.
98 *
99 * @param object - The iterable object of interest.
100 *
101 * @param fn - The predicate function to invoke for each value.
102 *
103 * @returns `true` if all values pass the test, `false` otherwise.
104 *
105 * #### Notes
106 * Iteration terminates on the first `false` predicate result.
107 *
108 * #### Complexity
109 * Linear.
110 *
111 * #### Example
112 * ```typescript
113 * import { every } from '@lumino/algorithm';
114 *
115 * let data = [5, 7, 1];
116 *
117 * every(data, value => value % 2 === 0); // false
118 * every(data, value => value % 2 === 1); // true
119 * ```
120 */
121export function every<T>(
122 object: Iterable<T>,
123 fn: (value: T, index: number) => boolean
124): boolean {
125 let index = 0;
126 for (const value of object) {
127 if (false === fn(value, index++)) {
128 return false;
129 }
130 }
131 return true;
132}
133
134/**
135 * Test whether any value in an iterable satisfies a predicate.
136 *
137 * @param object - The iterable object of interest.
138 *
139 * @param fn - The predicate function to invoke for each value.
140 *
141 * @returns `true` if any value passes the test, `false` otherwise.
142 *
143 * #### Notes
144 * Iteration terminates on the first `true` predicate result.
145 *
146 * #### Complexity
147 * Linear.
148 *
149 * #### Example
150 * ```typescript
151 * import { some } from '@lumino/algorithm';
152 *
153 * let data = [5, 7, 1];
154 *
155 * some(data, value => value === 7); // true
156 * some(data, value => value === 3); // false
157 * ```
158 */
159export function some<T>(
160 object: Iterable<T>,
161 fn: (value: T, index: number) => boolean
162): boolean {
163 let index = 0;
164 for (const value of object) {
165 if (fn(value, index++)) {
166 return true;
167 }
168 }
169 return false;
170}