Skip to content

Commit c205a7f

Browse files
committed
Add Typescript definition
1 parent 142a145 commit c205a7f

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
/**
22+
* Invoked function.
23+
*
24+
* @returns result
25+
*/
26+
type Nullary = () => any;
27+
28+
/**
29+
* Invoked function.
30+
*
31+
* @param i - invocation index
32+
* @returns result
33+
*/
34+
type Unary = ( i: number ) => any;
35+
36+
/**
37+
* Invoked function.
38+
*
39+
* @param i - invocation index
40+
* @returns result
41+
*/
42+
type Fcn = Nullary | Unary;
43+
44+
/**
45+
* Invokes a function `n` times and returns an array of accumulated function return values.
46+
*
47+
* ## Notes
48+
*
49+
* - The invoked function is provided a single argument: the invocation index (zero-based).
50+
*
51+
* @param fcn - function to invoke
52+
* @param n - number of function invocations
53+
* @param thisArg - execution context
54+
* @throws second argument must be a nonnegative integer
55+
* @returns accumulated results
56+
*
57+
* @example
58+
* function fcn( i ) {
59+
* return i;
60+
* }
61+
*
62+
* var arr = mapFun( fcn, 5 );
63+
* // returns [ 0, 1, 2, 3, 4 ]
64+
*/
65+
declare function mapFun( fcn: Fcn, n: number, thisArg?: any ): Array<any>;
66+
67+
68+
// EXPORTS //
69+
70+
export = mapFun;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 mapFun = require( './index' );
20+
21+
/**
22+
* Identity function.
23+
*/
24+
function identity( x: any ): any {
25+
return x;
26+
}
27+
28+
29+
// TESTS //
30+
31+
// The function return an array of values...
32+
{
33+
mapFun( identity, 5 ); // $ExpectType any[]
34+
mapFun( identity, 5, {} ); // $ExpectType any[]
35+
}
36+
37+
// The compiler throws an error if the function is provided a first argument which is not a function...
38+
{
39+
mapFun( 'abc', 5 ); // $ExpectError
40+
mapFun( 5, 5 ); // $ExpectError
41+
mapFun( true, 5 ); // $ExpectError
42+
mapFun( false, 5 ); // $ExpectError
43+
mapFun( [], 5 ); // $ExpectError
44+
mapFun( {}, 5 ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided a second argument which is not a number...
48+
{
49+
mapFun( identity, 'abc' ); // $ExpectError
50+
mapFun( identity, identity ); // $ExpectError
51+
mapFun( identity, true ); // $ExpectError
52+
mapFun( identity, false ); // $ExpectError
53+
mapFun( identity, [] ); // $ExpectError
54+
mapFun( identity, {} ); // $ExpectError
55+
}
56+
57+
// The function does not compile if provided less than two arguments...
58+
{
59+
mapFun(); // $ExpectError
60+
mapFun( identity ); // $ExpectError
61+
}

lib/node_modules/@stdlib/utils/map-function/package.json

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

0 commit comments

Comments
 (0)