UNPKG

4.8 kBTypeScriptView Raw
1import { IterableOrArrayLike } from './iter';
2/**
3 * Find the first value in an iterable which matches a predicate.
4 *
5 * @param object - The iterable or array-like object to search.
6 *
7 * @param fn - The predicate function to apply to the values.
8 *
9 * @returns The first matching value, or `undefined` if no matching
10 * value is found.
11 *
12 * #### Complexity
13 * Linear.
14 *
15 * #### Example
16 * ```typescript
17 * import { find } from '@phosphor/algorithm';
18 *
19 * interface IAnimal { species: string, name: string };
20 *
21 * function isCat(value: IAnimal): boolean {
22 * return value.species === 'cat';
23 * }
24 *
25 * let data: IAnimal[] = [
26 * { species: 'dog', name: 'spot' },
27 * { species: 'cat', name: 'fluffy' },
28 * { species: 'alligator', name: 'pocho' }
29 * ];
30 *
31 * find(data, isCat).name; // 'fluffy'
32 * ```
33 */
34export declare function find<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): T | undefined;
35/**
36 * Find the index of the first value which matches a predicate.
37 *
38 * @param object - The iterable or array-like object to search.
39 *
40 * @param fn - The predicate function to apply to the values.
41 *
42 * @returns The index of the first matching value, or `-1` if no
43 * matching value is found.
44 *
45 * #### Complexity
46 * Linear.
47 *
48 * #### Example
49 * ```typescript
50 * import { findIndex } from '@phosphor/algorithm';
51 *
52 * interface IAnimal { species: string, name: string };
53 *
54 * function isCat(value: IAnimal): boolean {
55 * return value.species === 'cat';
56 * }
57 *
58 * let data: IAnimal[] = [
59 * { species: 'dog', name: 'spot' },
60 * { species: 'cat', name: 'fluffy' },
61 * { species: 'alligator', name: 'pocho' }
62 * ];
63 *
64 * findIndex(data, isCat); // 1
65 * ```
66 */
67export declare function findIndex<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): number;
68/**
69 * Find the minimum value in an iterable.
70 *
71 * @param object - The iterable or array-like object to search.
72 *
73 * @param fn - The 3-way comparison function to apply to the values.
74 * It should return `< 0` if the first value is less than the second.
75 * `0` if the values are equivalent, or `> 0` if the first value is
76 * greater than the second.
77 *
78 * @returns The minimum value in the iterable. If multiple values are
79 * equivalent to the minimum, the left-most value is returned. If
80 * the iterable is empty, this returns `undefined`.
81 *
82 * #### Complexity
83 * Linear.
84 *
85 * #### Example
86 * ```typescript
87 * import { min } from '@phosphor/algorithm';
88 *
89 * function numberCmp(a: number, b: number): number {
90 * return a - b;
91 * }
92 *
93 * min([7, 4, 0, 3, 9, 4], numberCmp); // 0
94 * ```
95 */
96export declare function min<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): T | undefined;
97/**
98 * Find the maximum value in an iterable.
99 *
100 * @param object - The iterable or array-like object to search.
101 *
102 * @param fn - The 3-way comparison function to apply to the values.
103 * It should return `< 0` if the first value is less than the second.
104 * `0` if the values are equivalent, or `> 0` if the first value is
105 * greater than the second.
106 *
107 * @returns The maximum value in the iterable. If multiple values are
108 * equivalent to the maximum, the left-most value is returned. If
109 * the iterable is empty, this returns `undefined`.
110 *
111 * #### Complexity
112 * Linear.
113 *
114 * #### Example
115 * ```typescript
116 * import { max } from '@phosphor/algorithm';
117 *
118 * function numberCmp(a: number, b: number): number {
119 * return a - b;
120 * }
121 *
122 * max([7, 4, 0, 3, 9, 4], numberCmp); // 9
123 * ```
124 */
125export declare function max<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): T | undefined;
126/**
127 * Find the minimum and maximum values in an iterable.
128 *
129 * @param object - The iterable or array-like object to search.
130 *
131 * @param fn - The 3-way comparison function to apply to the values.
132 * It should return `< 0` if the first value is less than the second.
133 * `0` if the values are equivalent, or `> 0` if the first value is
134 * greater than the second.
135 *
136 * @returns A 2-tuple of the `[min, max]` values in the iterable. If
137 * multiple values are equivalent, the left-most values are returned.
138 * If the iterable is empty, this returns `undefined`.
139 *
140 * #### Complexity
141 * Linear.
142 *
143 * #### Example
144 * ```typescript
145 * import { minmax } from '@phosphor/algorithm';
146 *
147 * function numberCmp(a: number, b: number): number {
148 * return a - b;
149 * }
150 *
151 * minmax([7, 4, 0, 3, 9, 4], numberCmp); // [0, 9]
152 * ```
153 */
154export declare function minmax<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): [T, T] | undefined;