Skip to content

Commit 0593f34

Browse files
committed
Add type declarations
1 parent 4dac1d5 commit 0593f34

3 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
interface Iterator {
22+
/**
23+
* Returns an iterator protocol-compliant object containing the next iterated value (if one exists) and a boolean flag indicating whether the iterator is finished.
24+
*
25+
* @returns iterator protocol-compliant object
26+
*/
27+
next(): IteratorResult;
28+
}
29+
30+
interface IteratorResult {
31+
/**
32+
* Iterated value (if one exists).
33+
*/
34+
value?: any;
35+
36+
/**
37+
* Boolean flag indicating whether the iterator is finished.
38+
*/
39+
done: boolean;
40+
}
41+
42+
interface Predicate {
43+
/**
44+
* Checks whether an iterated value passes a test.
45+
*
46+
* @param value - iterated value
47+
* @param i - iteration index
48+
* @returns boolean indicating whether an iterated value passes a test
49+
*/
50+
(value?: any, i?: number): boolean;
51+
}
52+
53+
/**
54+
* Tests whether at least one iterated value passes a test implemented by a predicate function.
55+
*
56+
* @param iterator - input iterator
57+
* @param predicate - predicate function
58+
* @param thisArg - execution context
59+
* @returns boolean indicating whether at least one iterated value passes a test implemented by a predicate function
60+
*
61+
* @example
62+
*
63+
* var array2iterator = require( `@stdlib/array/to-iterator` );
64+
*
65+
* function predicate( v ) {
66+
* return ( v === 1 );
67+
* }
68+
*
69+
* var it = array2iterator( [ 0, 0, 0, 0, 1 ] );
70+
*
71+
* var bool = iterAnyBy( it, predicate );
72+
* // returns true
73+
*/
74+
declare function iterAnyBy( iterator: Iterator, predicate: Predicate, thisArg?: any | null | undefined ): boolean;
75+
76+
77+
// EXPORTS //
78+
79+
export = iterAnyBy;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 iterAnyBy = 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+
* Predicate function.
46+
*
47+
* @returns a boolean
48+
*/
49+
function predicate1() {
50+
return false;
51+
}
52+
53+
/**
54+
* Predicate function.
55+
*
56+
* @returns a boolean
57+
*/
58+
function predicate2() {
59+
return true;
60+
}
61+
62+
63+
// TESTS //
64+
65+
// The function returns a boolean...
66+
{
67+
iterAnyBy( iterator(), predicate1 ); // $ExpectType boolean
68+
iterAnyBy( iterator(), predicate2 ); // $ExpectType boolean
69+
iterAnyBy( iterator(), predicate1, {} ); // $ExpectType boolean
70+
iterAnyBy( iterator(), predicate2, {} ); // $ExpectType boolean
71+
iterAnyBy( iterator(), predicate1, null ); // $ExpectType boolean
72+
iterAnyBy( iterator(), predicate2, null ); // $ExpectType boolean
73+
}
74+
75+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
76+
{
77+
iterAnyBy( '5', predicate1 ); // $ExpectError
78+
iterAnyBy( 5, predicate1 ); // $ExpectError
79+
iterAnyBy( true, predicate1 ); // $ExpectError
80+
iterAnyBy( false, predicate1 ); // $ExpectError
81+
iterAnyBy( null, predicate1 ); // $ExpectError
82+
iterAnyBy( undefined, predicate1 ); // $ExpectError
83+
iterAnyBy( [], predicate1 ); // $ExpectError
84+
iterAnyBy( {}, predicate1 ); // $ExpectError
85+
iterAnyBy( ( x ) => x, predicate1 ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided a second argument which is not a predicate function...
89+
{
90+
iterAnyBy( iterator(), '5' ); // $ExpectError
91+
iterAnyBy( iterator(), 5 ); // $ExpectError
92+
iterAnyBy( iterator(), true ); // $ExpectError
93+
iterAnyBy( iterator(), false ); // $ExpectError
94+
iterAnyBy( iterator(), null ); // $ExpectError
95+
iterAnyBy( iterator(), undefined ); // $ExpectError
96+
iterAnyBy( iterator(), [] ); // $ExpectError
97+
iterAnyBy( iterator(), {} ); // $ExpectError
98+
iterAnyBy( iterator(), ( x ) => x ); // $ExpectError
99+
}
100+
101+
// The compiler throws an error if the function is provided insufficient arguments...
102+
{
103+
iterAnyBy(); // $ExpectError
104+
iterAnyBy( iterator() ); // $ExpectError
105+
}

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