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 | * Find the first value in an iterable which matches a predicate.
|
13 | *
|
14 | * @param object - The iterable or array-like object to search.
|
15 | *
|
16 | * @param fn - The predicate function to apply to the values.
|
17 | *
|
18 | * @returns The first matching value, or `undefined` if no matching
|
19 | * value is found.
|
20 | *
|
21 | * #### Complexity
|
22 | * Linear.
|
23 | *
|
24 | * #### Example
|
25 | * ```typescript
|
26 | * import { find } from '@phosphor/algorithm';
|
27 | *
|
28 | * interface IAnimal { species: string, name: string };
|
29 | *
|
30 | * function isCat(value: IAnimal): boolean {
|
31 | * return value.species === 'cat';
|
32 | * }
|
33 | *
|
34 | * let data: IAnimal[] = [
|
35 | * { species: 'dog', name: 'spot' },
|
36 | * { species: 'cat', name: 'fluffy' },
|
37 | * { species: 'alligator', name: 'pocho' }
|
38 | * ];
|
39 | *
|
40 | * find(data, isCat).name; // 'fluffy'
|
41 | * ```
|
42 | */
|
43 | function find(object, fn) {
|
44 | var index = 0;
|
45 | var it = iter_1.iter(object);
|
46 | var value;
|
47 | while ((value = it.next()) !== undefined) {
|
48 | if (fn(value, index++)) {
|
49 | return value;
|
50 | }
|
51 | }
|
52 | return undefined;
|
53 | }
|
54 | exports.find = find;
|
55 | /**
|
56 | * Find the index of the first value which matches a predicate.
|
57 | *
|
58 | * @param object - The iterable or array-like object to search.
|
59 | *
|
60 | * @param fn - The predicate function to apply to the values.
|
61 | *
|
62 | * @returns The index of the first matching value, or `-1` if no
|
63 | * matching value is found.
|
64 | *
|
65 | * #### Complexity
|
66 | * Linear.
|
67 | *
|
68 | * #### Example
|
69 | * ```typescript
|
70 | * import { findIndex } from '@phosphor/algorithm';
|
71 | *
|
72 | * interface IAnimal { species: string, name: string };
|
73 | *
|
74 | * function isCat(value: IAnimal): boolean {
|
75 | * return value.species === 'cat';
|
76 | * }
|
77 | *
|
78 | * let data: IAnimal[] = [
|
79 | * { species: 'dog', name: 'spot' },
|
80 | * { species: 'cat', name: 'fluffy' },
|
81 | * { species: 'alligator', name: 'pocho' }
|
82 | * ];
|
83 | *
|
84 | * findIndex(data, isCat); // 1
|
85 | * ```
|
86 | */
|
87 | function findIndex(object, fn) {
|
88 | var index = 0;
|
89 | var it = iter_1.iter(object);
|
90 | var value;
|
91 | while ((value = it.next()) !== undefined) {
|
92 | if (fn(value, index++)) {
|
93 | return index - 1;
|
94 | }
|
95 | }
|
96 | return -1;
|
97 | }
|
98 | exports.findIndex = findIndex;
|
99 | /**
|
100 | * Find the minimum value in an iterable.
|
101 | *
|
102 | * @param object - The iterable or array-like object to search.
|
103 | *
|
104 | * @param fn - The 3-way comparison function to apply to the values.
|
105 | * It should return `< 0` if the first value is less than the second.
|
106 | * `0` if the values are equivalent, or `> 0` if the first value is
|
107 | * greater than the second.
|
108 | *
|
109 | * @returns The minimum value in the iterable. If multiple values are
|
110 | * equivalent to the minimum, the left-most value is returned. If
|
111 | * the iterable is empty, this returns `undefined`.
|
112 | *
|
113 | * #### Complexity
|
114 | * Linear.
|
115 | *
|
116 | * #### Example
|
117 | * ```typescript
|
118 | * import { min } from '@phosphor/algorithm';
|
119 | *
|
120 | * function numberCmp(a: number, b: number): number {
|
121 | * return a - b;
|
122 | * }
|
123 | *
|
124 | * min([7, 4, 0, 3, 9, 4], numberCmp); // 0
|
125 | * ```
|
126 | */
|
127 | function min(object, fn) {
|
128 | var it = iter_1.iter(object);
|
129 | var value = it.next();
|
130 | if (value === undefined) {
|
131 | return undefined;
|
132 | }
|
133 | var result = value;
|
134 | while ((value = it.next()) !== undefined) {
|
135 | if (fn(value, result) < 0) {
|
136 | result = value;
|
137 | }
|
138 | }
|
139 | return result;
|
140 | }
|
141 | exports.min = min;
|
142 | /**
|
143 | * Find the maximum value in an iterable.
|
144 | *
|
145 | * @param object - The iterable or array-like object to search.
|
146 | *
|
147 | * @param fn - The 3-way comparison function to apply to the values.
|
148 | * It should return `< 0` if the first value is less than the second.
|
149 | * `0` if the values are equivalent, or `> 0` if the first value is
|
150 | * greater than the second.
|
151 | *
|
152 | * @returns The maximum value in the iterable. If multiple values are
|
153 | * equivalent to the maximum, the left-most value is returned. If
|
154 | * the iterable is empty, this returns `undefined`.
|
155 | *
|
156 | * #### Complexity
|
157 | * Linear.
|
158 | *
|
159 | * #### Example
|
160 | * ```typescript
|
161 | * import { max } from '@phosphor/algorithm';
|
162 | *
|
163 | * function numberCmp(a: number, b: number): number {
|
164 | * return a - b;
|
165 | * }
|
166 | *
|
167 | * max([7, 4, 0, 3, 9, 4], numberCmp); // 9
|
168 | * ```
|
169 | */
|
170 | function max(object, fn) {
|
171 | var it = iter_1.iter(object);
|
172 | var value = it.next();
|
173 | if (value === undefined) {
|
174 | return undefined;
|
175 | }
|
176 | var result = value;
|
177 | while ((value = it.next()) !== undefined) {
|
178 | if (fn(value, result) > 0) {
|
179 | result = value;
|
180 | }
|
181 | }
|
182 | return result;
|
183 | }
|
184 | exports.max = max;
|
185 | /**
|
186 | * Find the minimum and maximum values in an iterable.
|
187 | *
|
188 | * @param object - The iterable or array-like object to search.
|
189 | *
|
190 | * @param fn - The 3-way comparison function to apply to the values.
|
191 | * It should return `< 0` if the first value is less than the second.
|
192 | * `0` if the values are equivalent, or `> 0` if the first value is
|
193 | * greater than the second.
|
194 | *
|
195 | * @returns A 2-tuple of the `[min, max]` values in the iterable. If
|
196 | * multiple values are equivalent, the left-most values are returned.
|
197 | * If the iterable is empty, this returns `undefined`.
|
198 | *
|
199 | * #### Complexity
|
200 | * Linear.
|
201 | *
|
202 | * #### Example
|
203 | * ```typescript
|
204 | * import { minmax } from '@phosphor/algorithm';
|
205 | *
|
206 | * function numberCmp(a: number, b: number): number {
|
207 | * return a - b;
|
208 | * }
|
209 | *
|
210 | * minmax([7, 4, 0, 3, 9, 4], numberCmp); // [0, 9]
|
211 | * ```
|
212 | */
|
213 | function minmax(object, fn) {
|
214 | var it = iter_1.iter(object);
|
215 | var value = it.next();
|
216 | if (value === undefined) {
|
217 | return undefined;
|
218 | }
|
219 | var vmin = value;
|
220 | var vmax = value;
|
221 | while ((value = it.next()) !== undefined) {
|
222 | if (fn(value, vmin) < 0) {
|
223 | vmin = value;
|
224 | }
|
225 | else if (fn(value, vmax) > 0) {
|
226 | vmax = value;
|
227 | }
|
228 | }
|
229 | return [vmin, vmax];
|
230 | }
|
231 | exports.minmax = minmax;
|