Skip to content

Commit a4420d7

Browse files
committed
Add TypeScript declaration file
1 parent c420f7c commit a4420d7

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
* Checks whether an iterated value passes a test.
30+
*
31+
* @param value - iterated value
32+
* @param i - iteration index
33+
* @returns boolean indicating whether an iterated value passes a test
34+
*/
35+
type Predicate = ( value?: any, i?: number ) => boolean;
36+
37+
/**
38+
* Returns an iterator which rejects a provided iterator's values according to a predicate function.
39+
*
40+
* ## Notes
41+
*
42+
* - When invoked, the `predicate` 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 predicate - predicate function
51+
* @param thisArg - execution context
52+
* @returns iterator
53+
*
54+
* @example
55+
* var array2iterator = require( `@stdlib/array/to-iterator` );
56+
*
57+
* function predicate( v ) {
58+
* return ( v > 2 );
59+
* }
60+
*
61+
* var src = array2iterator( [ 1, 3, 2, 4 ] );
62+
* var iter = iterReject( src, predicate );
63+
*
64+
* var v = iter.next().value;
65+
* // returns 1
66+
*
67+
* v = iter.next().value;
68+
* // returns 2
69+
*
70+
* var bool = iter.next().done;
71+
* // returns true
72+
*/
73+
declare function iterReject( iterator: Iterator, predicate: Predicate, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
74+
75+
76+
// EXPORTS //
77+
78+
export = iterReject;
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 iterReject = 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 an iterator...
66+
{
67+
iterReject( iterator(), predicate1 ); // $ExpectType Iterator
68+
iterReject( iterator(), predicate2 ); // $ExpectType Iterator
69+
iterReject( iterator(), predicate1, {} ); // $ExpectType Iterator
70+
iterReject( iterator(), predicate2, {} ); // $ExpectType Iterator
71+
iterReject( iterator(), predicate1, null ); // $ExpectType Iterator
72+
iterReject( iterator(), predicate2, null ); // $ExpectType Iterator
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+
iterReject( '5', predicate1 ); // $ExpectError
78+
iterReject( 5, predicate1 ); // $ExpectError
79+
iterReject( true, predicate1 ); // $ExpectError
80+
iterReject( false, predicate1 ); // $ExpectError
81+
iterReject( null, predicate1 ); // $ExpectError
82+
iterReject( undefined, predicate1 ); // $ExpectError
83+
iterReject( [], predicate1 ); // $ExpectError
84+
iterReject( {}, predicate1 ); // $ExpectError
85+
iterReject( ( 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+
iterReject( iterator(), '5' ); // $ExpectError
91+
iterReject( iterator(), 5 ); // $ExpectError
92+
iterReject( iterator(), true ); // $ExpectError
93+
iterReject( iterator(), false ); // $ExpectError
94+
iterReject( iterator(), null ); // $ExpectError
95+
iterReject( iterator(), undefined ); // $ExpectError
96+
iterReject( iterator(), [] ); // $ExpectError
97+
iterReject( iterator(), {} ); // $ExpectError
98+
iterReject( iterator(), ( x: number ): number => x ); // $ExpectError
99+
}
100+
101+
// The compiler throws an error if the function is provided insufficient arguments...
102+
{
103+
iterReject(); // $ExpectError
104+
iterReject( iterator() ); // $ExpectError
105+
}

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