Skip to content

Commit 63aea77

Browse files
committed
Add TypeScript declaration file
1 parent a7f844b commit 63aea77

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24+
25+
// Define a union type representing both iterable and non-iterable iterators:
26+
type Iterator = Iter | IterableIterator;
27+
28+
/**
29+
* Callback function invoked for each iterated value.
30+
*
31+
* @param value - iterated value
32+
* @param i - iteration index
33+
* @returns callback result
34+
*/
35+
type Callback = ( value?: any, i?: number ) => any;
36+
37+
/**
38+
* Returns an iterator which invokes a function for each iterated value before returning the iterated value.
39+
*
40+
* ## Notes
41+
*
42+
* - When invoked, the callback function is provided two arguments:
43+
*
44+
* - `value`: iterated value
45+
* - `index`: iteration index (zero-based)
46+
*
47+
* - If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
48+
*
49+
* @param iterator - input iterator
50+
* @param fcn - callback function to invoke for each iterated value
51+
* @param thisArg - execution context
52+
* @returns iterator
53+
*
54+
* @example
55+
* var randu = require( '@stdlib/random/iter/randu' );
56+
* var isnan = require( '@stdlib/math/base/assert/is-nan' );
57+
*
58+
* function assert( v ) {
59+
* if ( isnan( v ) ) {
60+
* throw new Error( 'should not be NaN' );
61+
* }
62+
* }
63+
*
64+
* var iter = iterForEach( randu(), assert );
65+
*
66+
* var r = iter.next().value;
67+
* // returns <number>
68+
*
69+
* r = iter.next().value;
70+
* // returns <number>
71+
*
72+
* r = iter.next().value;
73+
* // returns <number>
74+
*
75+
* // ...
76+
*/
77+
declare function iterForEach( iterator: Iterator, fcn: Callback, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
78+
79+
80+
// EXPORTS //
81+
82+
export = iterForEach;
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) 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 iterForEach = require( './index' );
20+
21+
/**
22+
* Returns an iterator protocol-compliant object.
23+
*
24+
* @returns iterator protocol-compliant object
25+
*/
26+
function iterator() {
27+
return {
28+
'next': next
29+
};
30+
31+
/**
32+
* Implements the iterator protocol `next` method.
33+
*
34+
* @returns iterator protocol-compliant object
35+
*/
36+
function next() {
37+
return {
38+
'value': true,
39+
'done': false
40+
};
41+
}
42+
}
43+
44+
/**
45+
* Callback function.
46+
*
47+
* @returns a boolean
48+
*/
49+
function fcn() {
50+
return 'beep';
51+
}
52+
53+
54+
// TESTS //
55+
56+
// The function returns an iterator...
57+
{
58+
iterForEach( iterator(), fcn ); // $ExpectType Iterator
59+
iterForEach( iterator(), fcn, {} ); // $ExpectType Iterator
60+
iterForEach( iterator(), fcn, null ); // $ExpectType Iterator
61+
}
62+
63+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
64+
{
65+
iterForEach( '5', fcn ); // $ExpectError
66+
iterForEach( 5, fcn ); // $ExpectError
67+
iterForEach( true, fcn ); // $ExpectError
68+
iterForEach( false, fcn ); // $ExpectError
69+
iterForEach( null, fcn ); // $ExpectError
70+
iterForEach( undefined, fcn ); // $ExpectError
71+
iterForEach( [], fcn ); // $ExpectError
72+
iterForEach( {}, fcn ); // $ExpectError
73+
iterForEach( ( x: number ): number => x, fcn ); // $ExpectError
74+
}
75+
76+
// The compiler throws an error if the function is provided a second argument which is not a valid callback function...
77+
{
78+
iterForEach( iterator(), '5' ); // $ExpectError
79+
iterForEach( iterator(), 5 ); // $ExpectError
80+
iterForEach( iterator(), true ); // $ExpectError
81+
iterForEach( iterator(), false ); // $ExpectError
82+
iterForEach( iterator(), null ); // $ExpectError
83+
iterForEach( iterator(), undefined ); // $ExpectError
84+
iterForEach( iterator(), [] ); // $ExpectError
85+
iterForEach( iterator(), {} ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided insufficient arguments...
89+
{
90+
iterForEach(); // $ExpectError
91+
iterForEach( iterator() ); // $ExpectError
92+
}

lib/node_modules/@stdlib/iter/for-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)