|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2019 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// TypeScript Version: 2.0 |
| 20 | + |
| 21 | +/// <reference types="@stdlib/types"/> |
| 22 | + |
| 23 | +import { Collection } from '@stdlib/types/object'; |
| 24 | + |
| 25 | +/** |
| 26 | +* Interface defining function options. |
| 27 | +*/ |
| 28 | +interface Options { |
| 29 | + /** |
| 30 | + * Limits the number of returned elements. |
| 31 | + */ |
| 32 | + k?: number; |
| 33 | + |
| 34 | + /** |
| 35 | + * If `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned. |
| 36 | + */ |
| 37 | + returns?: 'values' | 'indices' | '*'; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | +* Checks whether an element in a collection passes a test. |
| 42 | +* |
| 43 | +* @returns boolean indicating whether an element in a collection passes a test |
| 44 | +*/ |
| 45 | +type Nullary = () => boolean; |
| 46 | + |
| 47 | +/** |
| 48 | +* Checks whether an element in a collection passes a test. |
| 49 | +* |
| 50 | +* @param value - collection value |
| 51 | +* @returns boolean indicating whether an element in a collection passes a test |
| 52 | +*/ |
| 53 | +type Unary = ( value: any ) => boolean; |
| 54 | + |
| 55 | +/** |
| 56 | +* Checks whether an element in a collection passes a test. |
| 57 | +* |
| 58 | +* @param value - collection value |
| 59 | +* @param index - collection index |
| 60 | +* @returns boolean indicating whether an element in a collection passes a test |
| 61 | +*/ |
| 62 | +type Binary = ( value: any, index: number ) => boolean; |
| 63 | + |
| 64 | +/** |
| 65 | +* Checks whether an element in a collection passes a test. |
| 66 | +* |
| 67 | +* @param value - collection value |
| 68 | +* @param index - collection index |
| 69 | +* @param collection - input collection |
| 70 | +* @returns boolean indicating whether an element in a collection passes a test |
| 71 | +*/ |
| 72 | +type Tertiary = ( value: any, index: number, collection: Collection ) => boolean; // tslint-disable-line max-line-length |
| 73 | + |
| 74 | +/** |
| 75 | +* Checks whether an element in a collection passes a test. |
| 76 | +* |
| 77 | +* @param value - collection value |
| 78 | +* @param index - collection index |
| 79 | +* @param collection - input collection |
| 80 | +* @returns boolean indicating whether an element in a collection passes a test |
| 81 | +*/ |
| 82 | +type Callback = Nullary | Unary | Binary | Tertiary; |
| 83 | + |
| 84 | +/** |
| 85 | +* Finds elements in an array-like object that satisfy a test condition. |
| 86 | +* |
| 87 | +* @param arr - object from which elements will be tested |
| 88 | +* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition. |
| 89 | +* @returns array of indices, element values, or arrays of index-value pairs |
| 90 | +* |
| 91 | +* @example |
| 92 | +* var data = [ 30, 20, 50, 60, 10 ]; |
| 93 | +* var vals = find( data, condition ); |
| 94 | +* // returns [ 0, 2, 3 ] |
| 95 | +* |
| 96 | +* function condition( val ) { |
| 97 | +* return val > 20; |
| 98 | +* } |
| 99 | +* |
| 100 | +* @example |
| 101 | +* var data = [ 30, 20, 50, 60, 10 ]; |
| 102 | +* var vals = find( data, condition ); |
| 103 | +* // returns [] |
| 104 | +* |
| 105 | +* function condition( val ) { |
| 106 | +* return val > 1000; |
| 107 | +* } |
| 108 | +*/ |
| 109 | +declare function find( arr: Collection, clbk: Callback ): Array<any> | Array<[ number, any ]>; // tslint-disable-line max-line-length |
| 110 | + |
| 111 | +/** |
| 112 | +* Finds elements in an array-like object that satisfy a test condition. |
| 113 | +* |
| 114 | +* @param arr - object from which elements will be tested |
| 115 | +* @param options - function options |
| 116 | +* @param options.k - limits the number of returned elements (default: arr.length) |
| 117 | +* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'indices') |
| 118 | +* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition. |
| 119 | +* @returns array of indices, element values, or arrays of index-value pairs |
| 120 | +* |
| 121 | +* @example |
| 122 | +* var data = [ 30, 20, 50, 60, 10 ]; |
| 123 | +* var opts = { |
| 124 | +* 'k': 2, |
| 125 | +* 'returns': 'values' |
| 126 | +* }; |
| 127 | +* var vals = find( data, opts, condition ); |
| 128 | +* // returns [ 30, 50 ] |
| 129 | +* |
| 130 | +* function condition( val ) { |
| 131 | +* return val > 20; |
| 132 | +* } |
| 133 | +* |
| 134 | +* @example |
| 135 | +* var data = [ 30, 20, 50, 60, 10 ]; |
| 136 | +* var opts = { |
| 137 | +* 'k': -2, |
| 138 | +* 'returns': 'values' |
| 139 | +* }; |
| 140 | +* var vals = find( data, opts, condition ); |
| 141 | +* // returns [ 60, 50 ] |
| 142 | +* |
| 143 | +* function condition( val ) { |
| 144 | +* return val > 20; |
| 145 | +* } |
| 146 | +* |
| 147 | +* @example |
| 148 | +* var data = [ 30, 20, 50, 60, 10 ]; |
| 149 | +* var opts = { |
| 150 | +* 'k': -2, |
| 151 | +* 'returns': '*' |
| 152 | +* }; |
| 153 | +* var vals = find( data, opts, condition ); |
| 154 | +* // returns [ [3, 60], [2, 50] ] |
| 155 | +* |
| 156 | +* function condition( val ) { |
| 157 | +* return val > 20; |
| 158 | +* } |
| 159 | +*/ |
| 160 | +declare function find( arr: Collection | string, options: Options, clbk: Callback ): Array<any> | Array<[ number, any ]>; // tslint-disable-line max-line-length |
| 161 | + |
| 162 | + |
| 163 | +// EXPORTS // |
| 164 | + |
| 165 | +export = find; |
0 commit comments