Skip to content

Commit 7dd6609

Browse files
committed
Add TypeScript declaration file
1 parent e485604 commit 7dd6609

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
* Returns an iterator which appends additional values to the end of a provided iterator.
30+
*
31+
* ## Notes
32+
*
33+
* - If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
34+
*
35+
* @param iterator - input iterator
36+
* @param items - items to append
37+
* @returns iterator
38+
*
39+
* @example
40+
* var array2iterator = require( `@stdlib/array/to-iterator` );
41+
*
42+
* var iter = iterPush( array2iterator( [ 1, 2 ] ), 3, 4 );
43+
*
44+
* var v = iter.next().value;
45+
* // returns 1
46+
*
47+
* v = iter.next().value;
48+
* // returns 2
49+
*
50+
* v = iter.next().value;
51+
* // returns 3
52+
*
53+
* v = iter.next().value;
54+
* // returns 4
55+
*
56+
* var bool = iter.next().done;
57+
* // returns true
58+
*/
59+
declare function iterPush( iterator: Iterator, ...items: Array<any> ): Iterator;
60+
61+
62+
// EXPORTS //
63+
64+
export = iterPush;
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 iterPush = 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+
// TESTS //
46+
47+
// The function returns an iterator...
48+
{
49+
iterPush( iterator() ); // $ExpectType Iterator
50+
iterPush( iterator(), 1 ); // $ExpectType Iterator
51+
iterPush( iterator(), 1, 2 ); // $ExpectType Iterator
52+
iterPush( iterator(), 1, 2, 3 ); // $ExpectType Iterator
53+
}
54+
55+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
56+
{
57+
iterPush( '5', 'beep' ); // $ExpectError
58+
iterPush( 5, 'beep' ); // $ExpectError
59+
iterPush( true, 'beep' ); // $ExpectError
60+
iterPush( false, 'beep' ); // $ExpectError
61+
iterPush( null, 'beep' ); // $ExpectError
62+
iterPush( undefined, 'beep' ); // $ExpectError
63+
iterPush( [], 'beep' ); // $ExpectError
64+
iterPush( {}, 'beep' ); // $ExpectError
65+
iterPush( ( x: number ): number => x, 'beep' ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided insufficient arguments...
69+
{
70+
iterPush(); // $ExpectError
71+
}

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