Skip to content

Commit dcaf8f2

Browse files
committed
Add Typescript definition
1 parent 926840a commit dcaf8f2

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
* Callback invoked upon function completion.
23+
*/
24+
type DoneNullary = () => void;
25+
26+
/**
27+
* Callback invoked upon function completion.
28+
*
29+
* @param error - error argument
30+
*/
31+
type DoneUnary = ( error: Error | null ) => void;
32+
33+
/**
34+
* Callback invoked upon function completion.
35+
*
36+
* @param error - error argument
37+
* @param result - function result
38+
*/
39+
type DoneBinary = ( error: Error | null, result: any ) => void;
40+
41+
/**
42+
* Callback invoked upon function completion.
43+
*
44+
* @param error - error argument
45+
* @param result - function result
46+
*/
47+
type DoneCallback = DoneNullary | DoneUnary | DoneBinary;
48+
49+
/**
50+
* Binary function to compose.
51+
*
52+
* @param result - result
53+
* @param next - invoked upon function completion
54+
*/
55+
type BinaryFunction = ( result: any, next: DoneCallback ) => void;
56+
57+
/**
58+
* Function to compose.
59+
*
60+
* ## Notes
61+
*
62+
* - Only the leftmost function is explicitly permitted to accept multiple arguments. All other functions are evaluated as binary functions.
63+
*
64+
* @param args - arguments
65+
* @param next - invoked upon function completion
66+
*/
67+
type VariadicFunction = ( ...args: Array<any> ) => void;
68+
69+
/**
70+
* Pipeline function.
71+
*
72+
* @param args - arguments
73+
* @param done - callback to invoke after invoking all functions
74+
*/
75+
type PipelineFunction = ( ...args: Array<any> ) => void;
76+
77+
/**
78+
* Function sequence.
79+
*
80+
* @param f0 - first function to evaluate
81+
* @param f1 - second function to evaluate
82+
* @param f - remaining functions to evaluate in sequential order
83+
* @returns pipeline function
84+
*
85+
* @example
86+
* function a( x, next ) {
87+
* setTimeout( onTimeout, 0 );
88+
* function onTimeout() {
89+
* next( null, 2*x );
90+
* }
91+
* }
92+
*
93+
* function b( x, next ) {
94+
* setTimeout( onTimeout, 0 );
95+
* function onTimeout() {
96+
* next( null, x+3 );
97+
* }
98+
* }
99+
*
100+
* function c( x, next ) {
101+
* setTimeout( onTimeout, 0 );
102+
* function onTimeout() {
103+
* next( null, x/5 );
104+
* }
105+
* }
106+
*
107+
* var f = funseqAsync( a, b, c );
108+
*
109+
* function done( error, result ) {
110+
* if ( error ) {
111+
* throw error;
112+
* }
113+
* console.log( result );
114+
* // => 3
115+
* }
116+
*
117+
* f( 6, done );
118+
*/
119+
declare function funseqAsync( f0: VariadicFunction, f1: BinaryFunction, ...f: Array<BinaryFunction> ): PipelineFunction; // tslint-disable-line max-line-length
120+
121+
122+
// EXPORTS //
123+
124+
export = funseqAsync;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 funseqAsync = require( './index' );
20+
21+
/**
22+
* First function.
23+
*
24+
* @param x - result
25+
* @param next - invoked upon function completion
26+
*/
27+
const a = ( x: number, next: Function ): void => {
28+
next( null, x * 2 );
29+
};
30+
31+
/**
32+
* Second function.
33+
*
34+
* @param x - result
35+
* @param next - invoked upon function completion
36+
*/
37+
const b = ( x: number, next: Function ): void => {
38+
next( null, x + 3 );
39+
};
40+
41+
/**
42+
* Third function.
43+
*
44+
* @param x - result
45+
* @param next - invoked upon function completion
46+
*/
47+
const c = ( x: number, next: Function ): void => {
48+
next( null, x / 5 );
49+
};
50+
51+
52+
// TESTS //
53+
54+
// The function returns a pipeline function...
55+
{
56+
funseqAsync( a, b, c ); // $ExpectType PipelineFunction
57+
}
58+
59+
// The compiler throws an error if the function is provided a first argument which is not a function...
60+
{
61+
funseqAsync( '5' ); // $ExpectError
62+
funseqAsync( 5 ); // $ExpectError
63+
funseqAsync( true ); // $ExpectError
64+
funseqAsync( false ); // $ExpectError
65+
funseqAsync( null ); // $ExpectError
66+
funseqAsync( undefined ); // $ExpectError
67+
funseqAsync( [ 1, 2, 3 ] ); // $ExpectError
68+
funseqAsync( {} ); // $ExpectError
69+
}
70+
71+
// The compiler throws an error if the function is provided another argument which is not a function...
72+
{
73+
funseqAsync( a, '5' ); // $ExpectError
74+
funseqAsync( a, true ); // $ExpectError
75+
funseqAsync( a, false ); // $ExpectError
76+
funseqAsync( a, null ); // $ExpectError
77+
funseqAsync( a, [] ); // $ExpectError
78+
funseqAsync( a, {} ); // $ExpectError
79+
80+
funseqAsync( a, b, '5' ); // $ExpectError
81+
funseqAsync( a, b, true ); // $ExpectError
82+
funseqAsync( a, b, false ); // $ExpectError
83+
funseqAsync( a, b, null ); // $ExpectError
84+
funseqAsync( a, b, [] ); // $ExpectError
85+
funseqAsync( a, b, {} ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided insufficient arguments...
89+
{
90+
funseqAsync(); // $ExpectError
91+
funseqAsync( a ); // $ExpectError
92+
}

lib/node_modules/@stdlib/utils/async/function-sequence/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)