@lumino/algorithm
Version:
Lumino Algorithms and Iterators
155 lines • 4.67 kB
TypeScript
import { IterableOrArrayLike } from './iter';
/**
* Find the first value in an iterable which matches a predicate.
*
* @param object - The iterable or array-like object to search.
*
* @param fn - The predicate function to apply to the values.
*
* @returns The first matching value, or `undefined` if no matching
* value is found.
*
* #### Complexity
* Linear.
*
* #### Example
* ```typescript
* import { find } from '@lumino/algorithm';
*
* interface IAnimal { species: string, name: string };
*
* function isCat(value: IAnimal): boolean {
* return value.species === 'cat';
* }
*
* let data: IAnimal[] = [
* { species: 'dog', name: 'spot' },
* { species: 'cat', name: 'fluffy' },
* { species: 'alligator', name: 'pocho' }
* ];
*
* find(data, isCat).name; // 'fluffy'
* ```
*/
export declare function find<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): T | undefined;
/**
* Find the index of the first value which matches a predicate.
*
* @param object - The iterable or array-like object to search.
*
* @param fn - The predicate function to apply to the values.
*
* @returns The index of the first matching value, or `-1` if no
* matching value is found.
*
* #### Complexity
* Linear.
*
* #### Example
* ```typescript
* import { findIndex } from '@lumino/algorithm';
*
* interface IAnimal { species: string, name: string };
*
* function isCat(value: IAnimal): boolean {
* return value.species === 'cat';
* }
*
* let data: IAnimal[] = [
* { species: 'dog', name: 'spot' },
* { species: 'cat', name: 'fluffy' },
* { species: 'alligator', name: 'pocho' }
* ];
*
* findIndex(data, isCat); // 1
* ```
*/
export declare function findIndex<T>(object: IterableOrArrayLike<T>, fn: (value: T, index: number) => boolean): number;
/**
* Find the minimum value in an iterable.
*
* @param object - The iterable or array-like object to search.
*
* @param fn - The 3-way comparison function to apply to the values.
* It should return `< 0` if the first value is less than the second.
* `0` if the values are equivalent, or `> 0` if the first value is
* greater than the second.
*
* @returns The minimum value in the iterable. If multiple values are
* equivalent to the minimum, the left-most value is returned. If
* the iterable is empty, this returns `undefined`.
*
* #### Complexity
* Linear.
*
* #### Example
* ```typescript
* import { min } from '@lumino/algorithm';
*
* function numberCmp(a: number, b: number): number {
* return a - b;
* }
*
* min([7, 4, 0, 3, 9, 4], numberCmp); // 0
* ```
*/
export declare function min<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): T | undefined;
/**
* Find the maximum value in an iterable.
*
* @param object - The iterable or array-like object to search.
*
* @param fn - The 3-way comparison function to apply to the values.
* It should return `< 0` if the first value is less than the second.
* `0` if the values are equivalent, or `> 0` if the first value is
* greater than the second.
*
* @returns The maximum value in the iterable. If multiple values are
* equivalent to the maximum, the left-most value is returned. If
* the iterable is empty, this returns `undefined`.
*
* #### Complexity
* Linear.
*
* #### Example
* ```typescript
* import { max } from '@lumino/algorithm';
*
* function numberCmp(a: number, b: number): number {
* return a - b;
* }
*
* max([7, 4, 0, 3, 9, 4], numberCmp); // 9
* ```
*/
export declare function max<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): T | undefined;
/**
* Find the minimum and maximum values in an iterable.
*
* @param object - The iterable or array-like object to search.
*
* @param fn - The 3-way comparison function to apply to the values.
* It should return `< 0` if the first value is less than the second.
* `0` if the values are equivalent, or `> 0` if the first value is
* greater than the second.
*
* @returns A 2-tuple of the `[min, max]` values in the iterable. If
* multiple values are equivalent, the left-most values are returned.
* If the iterable is empty, this returns `undefined`.
*
* #### Complexity
* Linear.
*
* #### Example
* ```typescript
* import { minmax } from '@lumino/algorithm';
*
* function numberCmp(a: number, b: number): number {
* return a - b;
* }
*
* minmax([7, 4, 0, 3, 9, 4], numberCmp); // [0, 9]
* ```
*/
export declare function minmax<T>(object: IterableOrArrayLike<T>, fn: (first: T, second: T) => number): [T, T] | undefined;
//# sourceMappingURL=find.d.ts.map