Skip to content

Commit d0e83ff

Browse files
committed
Add Typescript definition
1 parent e558985 commit d0e83ff

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
import find = require( './index' );
20+
21+
const condition = ( val: number ): boolean => val > 20;
22+
23+
24+
// TESTS //
25+
26+
// The function returns an array of indices, element values, or arrays of index-value pairs...
27+
{
28+
const arr = [ 30, 20, 50, 60, 10 ];
29+
find( arr, condition ); // $ExpectType any[] | [number, any][]
30+
const opts = {
31+
'returns': 'indices' as 'indices'
32+
};
33+
find( arr, opts, condition ); // $ExpectType any[] | [number, any][]
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not a collection...
37+
{
38+
find( true, condition ); // $ExpectError
39+
find( false, condition ); // $ExpectError
40+
find( 5, condition ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a last argument which is not a function...
44+
{
45+
const arr = [ 30, 20, 50, 60, 10 ];
46+
find( arr, false ); // $ExpectError
47+
find( arr, true ); // $ExpectError
48+
find( arr, 32 ); // $ExpectError
49+
find( arr, 'abc' ); // $ExpectError
50+
find( arr, [] ); // $ExpectError
51+
find( arr, {} ); // $ExpectError
52+
53+
find( arr, {}, false ); // $ExpectError
54+
find( arr, {}, true ); // $ExpectError
55+
find( arr, {}, 32 ); // $ExpectError
56+
find( arr, {}, 'abc' ); // $ExpectError
57+
find( arr, {}, [] ); // $ExpectError
58+
find( arr, {}, {} ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided an options argument which is not an object...
62+
{
63+
const arr = [ 30, 20, 50, 60, 10 ];
64+
find( arr, null, condition ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
68+
{
69+
const arr = [ 30, 20, 50, 60, 10 ];
70+
find( arr, { 'returns': '5' }, condition ); // $ExpectError
71+
find( arr, { 'returns': 123 }, condition ); // $ExpectError
72+
find( arr, { 'returns': [] }, condition ); // $ExpectError
73+
find( arr, { 'returns': {} }, condition ); // $ExpectError
74+
find( arr, { 'returns': ( x: number ): number => x }, condition ); // $ExpectError
75+
}
76+
77+
// The compiler throws an error if the function is provided a `k` option which is not a number...
78+
{
79+
const arr = [ 30, 20, 50, 60, 10 ];
80+
find( arr, { 'k': '5' }, condition ); // $ExpectError
81+
find( arr, { 'k': false }, condition ); // $ExpectError
82+
find( arr, { 'k': true }, condition ); // $ExpectError
83+
find( arr, { 'k': [] }, condition ); // $ExpectError
84+
find( arr, { 'k': {} }, condition ); // $ExpectError
85+
find( arr, { 'k': ( x: number ): number => x }, condition ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided an invalid number of arguments...
89+
{
90+
const arr = [ 30, 20, 50, 60, 10 ];
91+
find(); // $ExpectError
92+
find( arr ); // $ExpectError
93+
find( arr, {}, condition, 16 ); // $ExpectError
94+
}

lib/node_modules/@stdlib/utils/find/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {

0 commit comments

Comments
 (0)