Skip to content

Commit 685d4d4

Browse files
committed
Add type declarations
1 parent 44085d5 commit 685d4d4

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 Options {
22+
/**
23+
* Number of iterations.
24+
*/
25+
iter?: number;
26+
}
27+
28+
interface Iterator {
29+
/**
30+
* Returns an iterator protocol-compliant object containing the next iterated value (if one exists) and a boolean flag indicating whether the iterator is finished.
31+
*
32+
* @returns iterator protocol-compliant object
33+
*/
34+
next(): IteratorResult;
35+
36+
/**
37+
* Finishes an iterator.
38+
*
39+
* @param value - value to return
40+
* @returns iterator protocol-compliant object
41+
*/
42+
return( value?: any ): IteratorResult;
43+
}
44+
45+
interface IterableIterator extends Iterator {
46+
/**
47+
* Returns a new iterable iterator.
48+
*
49+
* @returns iterable iterator
50+
*/
51+
[Symbol.iterator](): IterableIterator;
52+
}
53+
54+
interface IteratorResult {
55+
/**
56+
* Iterated value (if one exists).
57+
*/
58+
value?: any;
59+
60+
/**
61+
* Boolean flag indicating whether the iterator is finished.
62+
*/
63+
done: boolean;
64+
}
65+
66+
/**
67+
* Returns an iterator which always returns the same value.
68+
*
69+
* ## Notes
70+
*
71+
* - If an environment supports `Symbol.iterator`, the returned iterator is iterable.
72+
*
73+
* @param value - value to return
74+
* @param options - function options
75+
* @param options.iter - number of iterations (default: 1e308)
76+
* @throws `iter` option must be a nonnegative integer
77+
* @returns iterator
78+
*
79+
* @example
80+
*
81+
* var iter = iterConstant( 3.14 );
82+
*
83+
* var v = iter.next().value;
84+
* // returns 3.14
85+
*
86+
* v = iter.next().value;
87+
* // returns 3.14
88+
*
89+
* v = iter.next().value;
90+
* // returns 3.14
91+
*
92+
* // ...
93+
*/
94+
declare function iterConstant( value: any, options?: Options ): Iterator | IterableIterator; // tslint:disable-line:max-line-length
95+
96+
97+
// EXPORTS //
98+
99+
export = iterConstant;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 iterConstant = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an iterator...
25+
{
26+
iterConstant( 3.14 ); // $ExpectType Iterator | IterableIterator
27+
iterConstant( 3.14, {} ); // $ExpectType Iterator | IterableIterator
28+
iterConstant( 3.14, { 'iter': 10 } ); // $ExpectType Iterator | IterableIterator
29+
}
30+
31+
// The compiler throws an error if the function is provided a second argument which is not an object...
32+
{
33+
iterConstant( 3.14, null ); // $ExpectError
34+
}
35+
36+
// The compiler throws an error if the function is provided an `iter` option which is not a number...
37+
{
38+
iterConstant( 3.14, { 'iter': '5' } ); // $ExpectError
39+
iterConstant( 3.14, { 'iter': true } ); // $ExpectError
40+
iterConstant( 3.14, { 'iter': false } ); // $ExpectError
41+
iterConstant( 3.14, { 'iter': null } ); // $ExpectError
42+
iterConstant( 3.14, { 'iter': [] } ); // $ExpectError
43+
iterConstant( 3.14, { 'iter': {} } ); // $ExpectError
44+
iterConstant( 3.14, { 'iter': ( x: number ): number => x } ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided insufficient arguments...
48+
{
49+
iterConstant(); // $ExpectError
50+
}

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