Skip to content

Commit 142a145

Browse files
committed
Add Typescript definitions
1 parent b45b2d7 commit 142a145

File tree

9 files changed

+352
-0
lines changed

9 files changed

+352
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
* If a function does not throw, returns the function return value; otherwise, returns `y`.
23+
*
24+
* @param x - function to try invoking
25+
* @param y - value to return if a function throws
26+
* @returns either the return value of `x` or the provided argument `y`
27+
*
28+
* @example
29+
* var randu = require( `@stdlib/random/base/randu` );
30+
*
31+
* function x() {
32+
* if ( randu() < 0.5 ) {
33+
* throw new Error( 'beep' );
34+
* }
35+
* return 1.0;
36+
* }
37+
* var z = trycatch( x, -1.0 );
38+
* // returns <number>
39+
*/
40+
declare function trycatch( x: Function, y: any ): any;
41+
42+
43+
// EXPORTS //
44+
45+
export = trycatch;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 trycatch = require( './index' );
20+
21+
/**
22+
* First function.
23+
*/
24+
function x(): number {
25+
if ( Math.random() < 0.5 ) {
26+
throw new Error( 'beep' );
27+
}
28+
return 1.0;
29+
}
30+
31+
// TESTS //
32+
33+
// The function returns a value...
34+
{
35+
trycatch( x, -1 ); // $ExpectType any
36+
}
37+
38+
// The compiler throws an error if the function is provided a first argument other than a function...
39+
{
40+
trycatch( 'abc', -1 ); // $ExpectError
41+
trycatch( 5, -1 ); // $ExpectError
42+
trycatch( [], -1 ); // $ExpectError
43+
trycatch( {}, -1 ); // $ExpectError
44+
trycatch( true, -1 ); // $ExpectError
45+
trycatch( false, -1 ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided an invalid number of arguments...
49+
{
50+
trycatch(); // $ExpectError
51+
trycatch( x ); // $ExpectError
52+
trycatch( x, -1, 2 ); // $ExpectError
53+
}

lib/node_modules/@stdlib/utils/try-catch/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": {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
* Wraps a function in a try/catch block.
23+
*
24+
* ## Notes
25+
*
26+
* - If provided an asynchronous function, the returned function only traps errors which occur during the current event loop tick.
27+
* - If a function throws a literal, the literal is serialized as a string and returned as an `Error` object.
28+
*
29+
* @param fcn - function to wrap
30+
* @param thisArg - function context
31+
* @returns wrapped function
32+
*
33+
* @example
34+
* function fcn() {
35+
* throw new Error( 'beep boop' );
36+
* }
37+
*
38+
* var f = wrap( fcn );
39+
*
40+
* var out = f();
41+
* if ( out instanceof Error ) {
42+
* console.error( out.message );
43+
* // => 'beep boop'
44+
* }
45+
*/
46+
declare function wrap( fcn: Function, thisArg?: any ): Function;
47+
48+
49+
// EXPORTS //
50+
51+
export = wrap;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 wrap = require( './index' );
20+
21+
/**
22+
* Function that throws an error.
23+
*/
24+
function fcn(): void {
25+
throw new Error( 'beep boop' );
26+
}
27+
28+
// TESTS //
29+
30+
// The function returns a function...
31+
{
32+
wrap( fcn ); // $ExpectType Function
33+
wrap( fcn, {} ); // $ExpectType Function
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument other than a function...
37+
{
38+
wrap( 'abc' ); // $ExpectError
39+
wrap( 5 ); // $ExpectError
40+
wrap( [] ); // $ExpectError
41+
wrap( {} ); // $ExpectError
42+
wrap( true ); // $ExpectError
43+
wrap( false ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided an invalid number of arguments...
47+
{
48+
wrap(); // $ExpectError
49+
wrap( fcn, {}, false ); // $ExpectError
50+
}

lib/node_modules/@stdlib/utils/try-function/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": {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 invoked if initial function throws an error.
23+
*
24+
* @returns return value of `trythen` function
25+
*/
26+
type Nullary = () => any;
27+
28+
/**
29+
* Function invoked if initial function throws an error.
30+
*
31+
* @param err - the error thrown by `x`
32+
* @returns return value of `trythen` function
33+
*/
34+
type Unary = ( err: Error ) => any;
35+
36+
/**
37+
* Function invoked if initial function throws an error.
38+
*
39+
* @param err - the error thrown by `x`
40+
* @returns return value of `trythen` function
41+
*/
42+
type ErrorHandler = Nullary | Unary;
43+
44+
45+
/**
46+
* If a function does not throw, returns the function return value; otherwise, returns the return value of a second function `y`.
47+
*
48+
* ## Notes
49+
*
50+
* - The function `y` is provided a single argument:
51+
*
52+
* - error: the error thrown by `x`
53+
*
54+
* @param x - function to try invoking
55+
* @param y - function to invoke if a function throws
56+
* @returns the return value of either `x` or `y`
57+
*
58+
* @example
59+
* var randu = require( `@stdlib/random/base/randu` );
60+
*
61+
* function x() {
62+
* if ( randu() < 0.5 ) {
63+
* throw new Error( 'beep' );
64+
* }
65+
* return 1.0;
66+
* }
67+
*
68+
* function y() {
69+
* return randu();
70+
* }
71+
*
72+
* var z = trythen( x, y );
73+
* // returns <number>
74+
*/
75+
declare function trythen( x: Function, y: ErrorHandler ): any;
76+
77+
78+
// EXPORTS //
79+
80+
export = trythen;
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+
import trythen = require( './index' );
20+
21+
/**
22+
* First function.
23+
*/
24+
function x(): number {
25+
if ( Math.random() < 0.5 ) {
26+
throw new Error( 'beep' );
27+
}
28+
return 1.0;
29+
}
30+
31+
/**
32+
* Second function.
33+
*/
34+
function y(): number {
35+
return Math.random();
36+
}
37+
38+
// TESTS //
39+
40+
// The function returns a value...
41+
{
42+
trythen( x, y ); // $ExpectType any
43+
}
44+
45+
// The compiler throws an error if the function is provided a first argument other than a function...
46+
{
47+
trythen( 'abc', y ); // $ExpectError
48+
trythen( 5, y ); // $ExpectError
49+
trythen( [], y ); // $ExpectError
50+
trythen( {}, y ); // $ExpectError
51+
trythen( true, y ); // $ExpectError
52+
trythen( false, y ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a second argument other than a function...
56+
{
57+
trythen( x, 'abc' ); // $ExpectError
58+
trythen( x, 5 ); // $ExpectError
59+
trythen( x, [] ); // $ExpectError
60+
trythen( x, {} ); // $ExpectError
61+
trythen( x, true ); // $ExpectError
62+
trythen( x, false ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an invalid number of arguments...
66+
{
67+
trythen(); // $ExpectError
68+
trythen( x ); // $ExpectError
69+
trythen( x, y, 2 ); // $ExpectError
70+
}

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