Skip to content

Commit a18545a

Browse files
committed
Add Typescript definitions
1 parent 467a8d1 commit a18545a

File tree

6 files changed

+267
-0
lines changed

6 files changed

+267
-0
lines changed
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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* While a test condition is true, invokes a function once for each element in a collection, iterating from right to left.
27+
*
28+
* ## Notes
29+
*
30+
* - When invoked, both the predicate function and the function to apply are provided three arguments:
31+
*
32+
* - `value`: collection value
33+
* - `index`: collection index
34+
* - `collection`: the input collection
35+
*
36+
* - For dynamic array resizing, the only behavior made intentionally consistent with `whileEach` (iterating from left to right) is when elements are pushed onto the beginning (end) of an array. In other words, for `whileEach()`, `[].push()` behavior is consistent with `whileEachRight()` `[].unshift()` behavior.
37+
*
38+
*
39+
* @param collection - input collection
40+
* @param predicate - function which indicates whether to continue iterating over a collection
41+
* @param fcn - function to invoke
42+
* @param thisArg - execution context for the applied function
43+
* @returns input collection
44+
*
45+
* @example
46+
* function predicate( v, index, collection ) {
47+
* return ( v === v );
48+
* }
49+
*
50+
* function log( v, index, collection ) {
51+
* console.log( '%s: %d', index, v );
52+
* }
53+
*
54+
* var arr = [ 1, NaN, 2, 3, 4, 5 ];
55+
*
56+
* whileEachRight( arr, predicate, log );
57+
*/
58+
declare function whileEachRight( collection: Collection, predicate: Function, fcn: Function, thisArg?: any ): Collection; // tslint-disable-line max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = whileEachRight;
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 whileEachRight = require( './index' );
20+
21+
const log = ( v: any, index: number ): string => {
22+
return `${index}: ${v}`;
23+
};
24+
25+
const isNotNaN = ( v: number ): boolean => {
26+
return ( v === v );
27+
};
28+
29+
// TESTS //
30+
31+
// The function returns the input collection...
32+
{
33+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, log ); // $ExpectType Collection
34+
whileEachRight( [ -1, 1, 2 ], isNotNaN, log, {} ); // $ExpectType Collection
35+
}
36+
37+
// The compiler throws an error if the function is provided a first argument which is not a collection...
38+
{
39+
whileEachRight( 2, isNotNaN, log ); // $ExpectError
40+
whileEachRight( false, isNotNaN, log ); // $ExpectError
41+
whileEachRight( true, isNotNaN, log ); // $ExpectError
42+
whileEachRight( {}, isNotNaN, log ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided a second argument which is not a function...
46+
{
47+
whileEachRight( [ 0, 1, 1, NaN, 2 ], 2, log ); // $ExpectError
48+
whileEachRight( [ 0, 1, 1, NaN, 2 ], false, log ); // $ExpectError
49+
whileEachRight( [ 0, 1, 1, NaN, 2 ], true, log ); // $ExpectError
50+
whileEachRight( [ 0, 1, 1, NaN, 2 ], 'abc', log ); // $ExpectError
51+
whileEachRight( [ 0, 1, 1, NaN, 2 ], {}, log ); // $ExpectError
52+
whileEachRight( [ 0, 1, 1, NaN, 2 ], [], log ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a third argument which is not a function...
56+
{
57+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, 2 ); // $ExpectError
58+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, false ); // $ExpectError
59+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, true ); // $ExpectError
60+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, 'abc' ); // $ExpectError
61+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, {} ); // $ExpectError
62+
whileEachRight( [ 0, 1, 1, NaN, 2 ], isNotNaN, [] ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an invalid number of arguments...
66+
{
67+
whileEachRight(); // $ExpectError
68+
whileEachRight( [ 1, 2, 3 ] ); // $ExpectError
69+
whileEachRight( [ 1, 2, 3 ], isNotNaN ); // $ExpectError
70+
whileEachRight( [ 1, 2, 3 ], isNotNaN, log, {}, 3 ); // $ExpectError
71+
}

lib/node_modules/@stdlib/utils/while-each-right/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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* While a test condition is true, invokes a function once for each element in a collection.
27+
*
28+
* ## Notes
29+
*
30+
* - When invoked, both the predicate function and the function to apply are provided three arguments:
31+
*
32+
* - `value`: collection value
33+
* - `index`: collection index
34+
* - `collection`: the input collection
35+
*
36+
* @param collection - input collection
37+
* @param predicate - function which indicates whether to continue iterating over a collection
38+
* @param fcn - function to invoke
39+
* @param thisArg - execution context for the applied function
40+
* @returns input collection
41+
*
42+
* @example
43+
* function predicate( v, index, collection ) {
44+
* return ( v === v );
45+
* }
46+
*
47+
* function log( v, index, collection ) {
48+
* console.log( '%s: %d', index, v );
49+
* }
50+
*
51+
* var arr = [ 1, 2, 3, 4, NaN, 5 ];
52+
*
53+
* whileEach( arr, predicate, log );
54+
*/
55+
declare function whileEach( collection: Collection, predicate: Function, fcn: Function, thisArg?: any ): Collection; // tslint-disable-line max-line-length
56+
57+
58+
// EXPORTS //
59+
60+
export = whileEach;
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 whileEach = require( './index' );
20+
21+
const log = ( v: any, index: number ): string => {
22+
return `${index}: ${v}`;
23+
};
24+
25+
const isNotNaN = ( v: number ): boolean => {
26+
return ( v === v );
27+
};
28+
29+
// TESTS //
30+
31+
// The function returns the input collection...
32+
{
33+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, log ); // $ExpectType Collection
34+
whileEach( [ -1, 1, 2 ], isNotNaN, log, {} ); // $ExpectType Collection
35+
}
36+
37+
// The compiler throws an error if the function is provided a first argument which is not a collection...
38+
{
39+
whileEach( 2, isNotNaN, log ); // $ExpectError
40+
whileEach( false, isNotNaN, log ); // $ExpectError
41+
whileEach( true, isNotNaN, log ); // $ExpectError
42+
whileEach( {}, isNotNaN, log ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided a second argument which is not a function...
46+
{
47+
whileEach( [ 0, 1, 1, NaN, 2 ], 2, log ); // $ExpectError
48+
whileEach( [ 0, 1, 1, NaN, 2 ], false, log ); // $ExpectError
49+
whileEach( [ 0, 1, 1, NaN, 2 ], true, log ); // $ExpectError
50+
whileEach( [ 0, 1, 1, NaN, 2 ], 'abc', log ); // $ExpectError
51+
whileEach( [ 0, 1, 1, NaN, 2 ], {}, log ); // $ExpectError
52+
whileEach( [ 0, 1, 1, NaN, 2 ], [], log ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a third argument which is not a function...
56+
{
57+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, 2 ); // $ExpectError
58+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, false ); // $ExpectError
59+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, true ); // $ExpectError
60+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, 'abc' ); // $ExpectError
61+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, {} ); // $ExpectError
62+
whileEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, [] ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an invalid number of arguments...
66+
{
67+
whileEach(); // $ExpectError
68+
whileEach( [ 1, 2, 3 ] ); // $ExpectError
69+
whileEach( [ 1, 2, 3 ], isNotNaN ); // $ExpectError
70+
whileEach( [ 1, 2, 3 ], isNotNaN, log, {}, 3 ); // $ExpectError
71+
}

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