Skip to content

Commit de0ea96

Browse files
committed
Add type declarations
1 parent 6aaee9f commit de0ea96

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
/**
43+
* Checks whether an iterated value passes a test.
44+
*
45+
* @param value - iterated value
46+
* @param i - iteration index
47+
* @returns boolean indicating whether an iterated value passes a test
48+
*/
49+
type Predicate = ( value?: any, i?: number ) => boolean;
50+
51+
/**
52+
* Tests whether every iterated value fails a test implemented by a predicate function.
53+
*
54+
* @param iterator - input iterator
55+
* @param predicate - predicate function
56+
* @param thisArg - execution context
57+
* @returns boolean indicating whether every iterated value fails a test implemented by a predicate function
58+
*
59+
* @example
60+
*
61+
* var array2iterator = require( '@stdlib/array/to-iterator' );
62+
*
63+
* function predicate( v ) {
64+
* return ( v <= 0 );
65+
* }
66+
*
67+
* var it = array2iterator( [ 1, 1, 1, 1, 1 ] );
68+
*
69+
* var bool = iterNoneBy( it, predicate );
70+
* // returns true
71+
*/
72+
declare function iterNoneBy( iterator: Iterator, predicate: Predicate, thisArg?: any ): boolean; // tslint:disable-line:max-line-length
73+
74+
75+
// EXPORTS //
76+
77+
export = iterNoneBy;
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 iterNoneBy = 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+
iterNoneBy( iterator(), predicate1 ); // $ExpectType boolean
68+
iterNoneBy( iterator(), predicate2 ); // $ExpectType boolean
69+
iterNoneBy( iterator(), predicate1, {} ); // $ExpectType boolean
70+
iterNoneBy( iterator(), predicate2, {} ); // $ExpectType boolean
71+
iterNoneBy( iterator(), predicate1, null ); // $ExpectType boolean
72+
iterNoneBy( 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+
iterNoneBy( '5', predicate1 ); // $ExpectError
78+
iterNoneBy( 5, predicate1 ); // $ExpectError
79+
iterNoneBy( true, predicate1 ); // $ExpectError
80+
iterNoneBy( false, predicate1 ); // $ExpectError
81+
iterNoneBy( null, predicate1 ); // $ExpectError
82+
iterNoneBy( undefined, predicate1 ); // $ExpectError
83+
iterNoneBy( [], predicate1 ); // $ExpectError
84+
iterNoneBy( {}, predicate1 ); // $ExpectError
85+
iterNoneBy( ( x: number ): number => 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+
iterNoneBy( iterator(), '5' ); // $ExpectError
91+
iterNoneBy( iterator(), 5 ); // $ExpectError
92+
iterNoneBy( iterator(), true ); // $ExpectError
93+
iterNoneBy( iterator(), false ); // $ExpectError
94+
iterNoneBy( iterator(), null ); // $ExpectError
95+
iterNoneBy( iterator(), undefined ); // $ExpectError
96+
iterNoneBy( iterator(), [] ); // $ExpectError
97+
iterNoneBy( iterator(), {} ); // $ExpectError
98+
iterNoneBy( iterator(), ( x: number ): number => x ); // $ExpectError
99+
}
100+
101+
// The compiler throws an error if the function is provided insufficient arguments...
102+
{
103+
iterNoneBy(); // $ExpectError
104+
iterNoneBy( iterator() ); // $ExpectError
105+
}

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