Skip to content

Commit 88d3a95

Browse files
committed
Add Typescript definitions
1 parent dab9254 commit 88d3a95

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 condition is truthy, returns `x`; otherwise, returns `y`.
23+
*
24+
* @param bool - condition
25+
* @param x - value to return if a condition is truthy
26+
* @param y - value to return if a condition is falsy
27+
* @returns either `x` or `y`
28+
*
29+
* @example
30+
* var randu = require( `@stdlib/random/base/randu` );
31+
*
32+
* var z = ifelse( randu() > 0.5, 1.0, -1.0 );
33+
* // returns <number>
34+
*/
35+
declare function ifelse( bool: boolean, x: any, y: any ): any;
36+
37+
38+
// EXPORTS //
39+
40+
export = ifelse;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 ifelse = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns either the `x` or `y` value...
25+
{
26+
ifelse( true, 0, 1 ); // $ExpectType any
27+
ifelse( true, 'a', 'b' ); // $ExpectType any
28+
ifelse( false, 0, 1 ); // $ExpectType any
29+
ifelse( false, 'a', 'b' ); // $ExpectType any
30+
}
31+
32+
// The compiler throws an error if the function is not provided a boolean as its first argument...
33+
{
34+
ifelse( ( x: number ): number => x, 0, 1 ); // $ExpectError
35+
ifelse( 'abc', 0, 1 ); // $ExpectError
36+
ifelse( 3.12, 0, 1 ); // $ExpectError
37+
ifelse( [], 0, 1 ); // $ExpectError
38+
ifelse( {}, 0, 1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an incorrect number of arguments...
42+
{
43+
ifelse(); // $ExpectError
44+
ifelse( true, 2 ); // $ExpectError
45+
ifelse( true, 0, 1, 'a' ); // $ExpectError
46+
}

lib/node_modules/@stdlib/utils/if-else/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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 condition is truthy, invokes `x`; otherwise, invokes `y`.
23+
*
24+
* @param bool - condition
25+
* @param x - function to invoke if a condition is truthy
26+
* @param y - function to invoke if a condition is falsy
27+
* @returns return value of either `x` or `y`
28+
*
29+
* @example
30+
* var randu = require( `@stdlib/random/base/randu` );
31+
*
32+
* function x() {
33+
* return randu() * 100.0;
34+
* }
35+
*
36+
* function y() {
37+
* return -1.0 * randu() * 100.0;
38+
* }
39+
*
40+
* var z = ifthen( randu() > 0.5, x, y );
41+
* // returns <number>
42+
*/
43+
declare function ifthen( bool: boolean, x: Function, y: Function ): any;
44+
45+
46+
// EXPORTS //
47+
48+
export = ifthen;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 ifthen = require( './index' );
20+
21+
/**
22+
* Returns the number one.
23+
*
24+
* @returns one
25+
*/
26+
function one(): number {
27+
return 1;
28+
}
29+
30+
/**
31+
* Returns the number zero.
32+
*
33+
* @returns zero
34+
*/
35+
function zero(): number {
36+
return 0;
37+
}
38+
39+
40+
// TESTS //
41+
42+
// The function returns the return value of either the `x` or `y` function...
43+
{
44+
ifthen( true, zero, one ); // $ExpectType any
45+
ifthen( false, zero, one ); // $ExpectType any
46+
}
47+
48+
// The compiler throws an error if the function is not provided a function as its first argument...
49+
{
50+
ifthen( zero, zero, one ); // $ExpectError
51+
ifthen( 'abc', zero, one ); // $ExpectError
52+
ifthen( 3.12, zero, one ); // $ExpectError
53+
ifthen( [], zero, one ); // $ExpectError
54+
ifthen( {}, zero, one ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is not provided a function as its second argument...
58+
{
59+
ifthen( true, 'abc', one ); // $ExpectError
60+
ifthen( true, 3.12, one ); // $ExpectError
61+
ifthen( true, false, one ); // $ExpectError
62+
ifthen( true, true, one ); // $ExpectError
63+
ifthen( true, [], one ); // $ExpectError
64+
ifthen( true, {}, one ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is not provided a function as its third argument...
68+
{
69+
ifthen( true, zero, 'abc' ); // $ExpectError
70+
ifthen( true, zero, 3.12 ); // $ExpectError
71+
ifthen( true, zero, false ); // $ExpectError
72+
ifthen( true, zero, true ); // $ExpectError
73+
ifthen( true, zero, [] ); // $ExpectError
74+
ifthen( true, zero, {} ); // $ExpectError
75+
}
76+
77+
// The compiler throws an error if the function is provided an incorrect number of arguments...
78+
{
79+
ifthen(); // $ExpectError
80+
ifthen( true, zero ); // $ExpectError
81+
ifthen( true, zero, one, 'a' ); // $ExpectError
82+
}

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