Skip to content

Commit 98c9381

Browse files
committed
Add TypeScript declaration file
1 parent d5380f7 commit 98c9381

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 replaces all values from a provided iterator from a start index to an end index with a static value.
30+
*
31+
* ## Notes
32+
*
33+
* - If `end` exceeds the length of the provided iterator, the returned iterator replaces the subsequence of iterated values starting from `begin` until the last iterated value of the provided iterator.
34+
* - If an environment supports Symbol.iterator, the returned iterator is iterable.
35+
*
36+
* @param iterator - input iterator
37+
* @param value - static (fill) value
38+
* @param begin - start iteration index (inclusive)
39+
* @param end - end iteration index (non-inclusive)
40+
* @returns iterator
41+
*
42+
* @example
43+
* var randu = require( '@stdlib/random/iter/randu' );
44+
*
45+
* var iter = iterFill( randu(), 3.14, 0, 2 );
46+
*
47+
* var r = iter.next().value;
48+
* // returns 3.14
49+
*
50+
* r = iter.next().value;
51+
* // returns 3.14
52+
*
53+
* r = iter.next().value;
54+
* // returns <number>
55+
*
56+
* // ...
57+
*/
58+
declare function iterFill( iterator: Iterator, value: any, begin?: number, end?: number ): Iterator; // tslint:disable-line:max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = iterFill;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 iterFill = 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+
iterFill( iterator(), 'beep' ); // $ExpectType Iterator
50+
iterFill( iterator(), 'beep', 2 ); // $ExpectType Iterator
51+
iterFill( iterator(), 'beep', 2, 5 ); // $ExpectType Iterator
52+
}
53+
54+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
55+
{
56+
iterFill( '5', 'beep' ); // $ExpectError
57+
iterFill( 5, 'beep' ); // $ExpectError
58+
iterFill( true, 'beep' ); // $ExpectError
59+
iterFill( false, 'beep' ); // $ExpectError
60+
iterFill( null, 'beep' ); // $ExpectError
61+
iterFill( undefined, 'beep' ); // $ExpectError
62+
iterFill( [], 'beep' ); // $ExpectError
63+
iterFill( {}, 'beep' ); // $ExpectError
64+
iterFill( ( x: number ): number => x, 'beep' ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided a third argument which is not is a number...
68+
{
69+
iterFill( iterator(), 'beep', '5' ); // $ExpectError
70+
iterFill( iterator(), 'beep', true ); // $ExpectError
71+
iterFill( iterator(), 'beep', false ); // $ExpectError
72+
iterFill( iterator(), 'beep', null ); // $ExpectError
73+
iterFill( iterator(), 'beep', [] ); // $ExpectError
74+
iterFill( iterator(), 'beep', {} ); // $ExpectError
75+
iterFill( iterator(), 'beep', ( x: number ): number => x ); // $ExpectError
76+
}
77+
78+
// The compiler throws an error if the function is provided a fourth argument which is not is a number...
79+
{
80+
iterFill( iterator(), 'beep', 2, '5' ); // $ExpectError
81+
iterFill( iterator(), 'beep', 2, true ); // $ExpectError
82+
iterFill( iterator(), 'beep', 2, false ); // $ExpectError
83+
iterFill( iterator(), 'beep', 2, null ); // $ExpectError
84+
iterFill( iterator(), 'beep', 2, [] ); // $ExpectError
85+
iterFill( iterator(), 'beep', 2, {} ); // $ExpectError
86+
iterFill( iterator(), 'beep', 2, ( x: number ): number => x ); // $ExpectError
87+
}
88+
89+
// The compiler throws an error if the function is provided insufficient arguments...
90+
{
91+
iterFill(); // $ExpectError
92+
iterFill( iterator() ); // $ExpectError
93+
}

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