Skip to content

Commit 863cc25

Browse files
committed
Add Typescript definitions
1 parent 6b3d7ef commit 863cc25

6 files changed

Lines changed: 262 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* Checks whether an iteration number passes a test.
23+
*
24+
* @param i - iteration number (starting from zero)
25+
* @returns boolean indicating whether an iteration number passes a test
26+
*/
27+
type Predicate = ( i: number ) => boolean;
28+
29+
/**
30+
* Invokes a function until a test condition is true.
31+
*
32+
* ## Notes
33+
*
34+
* - The condition is evaluated *after* executing the provided function; thus, `fcn` *always* executes at least once.
35+
* - When invoked, both the predicate function and the function to invoke are provided a single argument:
36+
*
37+
* - `i`: iteration number (starting from zero)
38+
*
39+
* @param predicate - function which indicates whether to stop invoking a function
40+
* @param fcn - function to invoke
41+
* @param thisArg - execution context for the invoked function
42+
*
43+
* @example
44+
* function predicate( i ) {
45+
* return ( i >= 5 );
46+
* }
47+
*
48+
* function beep( i ) {
49+
* console.log( 'beep: %d', i );
50+
* }
51+
*
52+
* until( predicate, beep );
53+
*/
54+
declare function until( fcn: Function, predicate: Predicate, thisArg?: any ): void; // tslint:disable-line: max-line-length
55+
56+
57+
// EXPORTS //
58+
59+
export = until;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 until = require( './index' );
20+
21+
/**
22+
* Predicate function.
23+
*
24+
* @param i - iteration index
25+
* @returns a boolean
26+
*/
27+
function predicate( i: number ): boolean {
28+
return ( i <= 10 );
29+
}
30+
31+
/**
32+
* Empty function.
33+
*/
34+
function noop() {
35+
// Body is empty...
36+
}
37+
38+
39+
// TESTS //
40+
41+
// The function does not return a value...
42+
{
43+
until( noop, predicate ); // $ExpectType void
44+
until( noop, predicate, {} ); // $ExpectType void
45+
}
46+
47+
// The compiler throws an error if the function is provided a first argument which is not a function...
48+
{
49+
until( 'abc', predicate ); // $ExpectError
50+
until( 5, predicate ); // $ExpectError
51+
until( true, predicate ); // $ExpectError
52+
until( false, predicate ); // $ExpectError
53+
until( [], predicate ); // $ExpectError
54+
until( {}, predicate ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is provided a second argument which is not a function...
58+
{
59+
until( noop, 'abc' ); // $ExpectError
60+
until( noop, 5 ); // $ExpectError
61+
until( noop, true ); // $ExpectError
62+
until( noop, false ); // $ExpectError
63+
until( noop, [] ); // $ExpectError
64+
until( noop, {} ); // $ExpectError
65+
}
66+
67+
// The function does not compile if provided less than two arguments...
68+
{
69+
until(); // $ExpectError
70+
until( noop ); // $ExpectError
71+
}

lib/node_modules/@stdlib/utils/until/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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* Checks whether an iteration number passes a test.
23+
*
24+
* @param i - iteration number (starting from zero)
25+
* @returns boolean indicating whether an iteration number passes a test
26+
*/
27+
type Predicate = ( i: number ) => boolean;
28+
29+
/**
30+
* Invokes a function while a test condition is true.
31+
*
32+
* ## Notes
33+
*
34+
* - The condition is evaluated *after* executing the provided function; thus, `fcn` *always* executes at least once.
35+
* - When invoked, both the predicate function and the function to invoke are provided a single argument:
36+
*
37+
* - `i`: iteration number (starting from zero)
38+
*
39+
* @param predicate - function which indicates whether to continue invoking a function
40+
* @param fcn - function to invoke
41+
* @param thisArg - execution context for the invoked function
42+
*
43+
* @example
44+
* function predicate( i ) {
45+
* return ( i < 5 );
46+
* }
47+
*
48+
* function beep( i ) {
49+
* console.log( 'beep: %d', i );
50+
* }
51+
*
52+
* whilst( predicate, beep );
53+
*/
54+
declare function whilst( fcn: Function, predicate: Predicate, thisArg?: any ): void; // tslint:disable-line: max-line-length
55+
56+
57+
// EXPORTS //
58+
59+
export = whilst;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 whilst = require( './index' );
20+
21+
/**
22+
* Predicate function.
23+
*
24+
* @param i - iteration index
25+
* @returns a boolean
26+
*/
27+
function predicate( i: number ): boolean {
28+
return ( i <= 10 );
29+
}
30+
31+
/**
32+
* Empty function.
33+
*/
34+
function noop() {
35+
// Body is empty...
36+
}
37+
38+
39+
// TESTS //
40+
41+
// The function does not return a value...
42+
{
43+
whilst( noop, predicate ); // $ExpectType void
44+
whilst( noop, predicate, {} ); // $ExpectType void
45+
}
46+
47+
// The compiler throws an error if the function is provided a first argument which is not a function...
48+
{
49+
whilst( 'abc', predicate ); // $ExpectError
50+
whilst( 5, predicate ); // $ExpectError
51+
whilst( true, predicate ); // $ExpectError
52+
whilst( false, predicate ); // $ExpectError
53+
whilst( [], predicate ); // $ExpectError
54+
whilst( {}, predicate ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is provided a second argument which is not a function...
58+
{
59+
whilst( noop, 'abc' ); // $ExpectError
60+
whilst( noop, 5 ); // $ExpectError
61+
whilst( noop, true ); // $ExpectError
62+
whilst( noop, false ); // $ExpectError
63+
whilst( noop, [] ); // $ExpectError
64+
whilst( noop, {} ); // $ExpectError
65+
}
66+
67+
// The function does not compile if provided less than two arguments...
68+
{
69+
whilst(); // $ExpectError
70+
whilst( noop ); // $ExpectError
71+
}

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