Skip to content

Commit 3f5e275

Browse files
committed
Add Typescript definition
1 parent fd41737 commit 3f5e275

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
* Function composition.
23+
*
24+
* ## Notes
25+
*
26+
* - Returns a composite function. Starting from the right, the composite function evaluates each function and passes the result as an argument to the next function. The result of the leftmost function is the result of the whole.
27+
* - Only the rightmost function is explicitly permitted to accept multiple arguments. All other functions are evaluated as unary functions.
28+
* - The function will throw if provided fewer than two input arguments.
29+
*
30+
* @param fcn - functions to compose
31+
* @throws must provide more than one argument
32+
* @returns composite function
33+
*
34+
* @example
35+
* function a( x ) {
36+
* return 2 * x;
37+
* }
38+
*
39+
* function b( x ) {
40+
* return x + 3;
41+
* }
42+
*
43+
* function c( x ) {
44+
* return x / 5;
45+
* }
46+
*
47+
* var f = compose( c, b, a );
48+
*
49+
* var z = f( 6 );
50+
* // returns 3
51+
*/
52+
declare function compose( ...fcn: Array<Function> ): Function;
53+
54+
55+
// EXPORTS //
56+
57+
export = compose;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 compose = require( './index' );
20+
21+
const a = ( x: number ): number => {
22+
return x * 2;
23+
};
24+
25+
const b = ( x: number ): number => {
26+
return x + 3;
27+
};
28+
29+
const c = ( x: number ): number => {
30+
return x / 5;
31+
};
32+
33+
// TESTS //
34+
35+
// The function returns a function...
36+
{
37+
compose( a, b, c ); // $ExpectType Function
38+
compose( a, b ); // $ExpectType Function
39+
}
40+
41+
// The compiler throws an error if the function is provided arguments other than functions...
42+
{
43+
compose( a, b, 'abc' ); // $ExpectError
44+
compose( a, b, 5 ); // $ExpectError
45+
compose( a, b, [] ); // $ExpectError
46+
compose( a, b, {} ); // $ExpectError
47+
compose( a, b, true ); // $ExpectError
48+
compose( a, b, false ); // $ExpectError
49+
50+
compose( a, 'abc', c ); // $ExpectError
51+
compose( a, 5, c ); // $ExpectError
52+
compose( a, [], c ); // $ExpectError
53+
compose( a, {}, c ); // $ExpectError
54+
compose( a, true, c ); // $ExpectError
55+
compose( a, false, c ); // $ExpectError
56+
57+
compose( 'abc', b, c ); // $ExpectError
58+
compose( 5, b, c ); // $ExpectError
59+
compose( [], b, c ); // $ExpectError
60+
compose( {}, b, c ); // $ExpectError
61+
compose( true, b, c ); // $ExpectError
62+
compose( false, b, c ); // $ExpectError
63+
}

lib/node_modules/@stdlib/utils/compose/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)