Skip to content

Commit 86eeb18

Browse files
committed
Add Typescript definition
1 parent 0cd41a8 commit 86eeb18

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import { ArrayLike } from '@stdlib/types/array';
25+
import { Collection } from '@stdlib/types/object';
26+
27+
// Define a union type representing both iterable and non-iterable iterators:
28+
type Iterator = Iter | IterableIterator;
29+
30+
/**
31+
* Map function invoked for each iterated value.
32+
*
33+
* @returns iterator value
34+
*/
35+
type Nullary = () => any;
36+
37+
/**
38+
* Map function invoked for each iterated value.
39+
*
40+
* @param value - iterated value
41+
* @returns iterator value
42+
*/
43+
type Unary = ( value: any ) => any;
44+
45+
/**
46+
* Map function invoked for each iterated value.
47+
*
48+
* @param value - iterated value
49+
* @param index - iterated value index
50+
* @returns iterator value
51+
*/
52+
type Binary = ( value: any, index: number ) => any;
53+
54+
/**
55+
* Map function invoked for each iterated value.
56+
*
57+
* @param value - iterated value
58+
* @param index - iterated value index
59+
* @param src - source array-like object
60+
* @returns iterator value
61+
*/
62+
type Tertiary = ( value: any, index: number, src: ArrayLike<any> ) => any;
63+
64+
/**
65+
* Map function invoked for each iterated value.
66+
*
67+
* @param value - iterated value
68+
* @param index - iterated value index
69+
* @param src - source array-like object
70+
* @returns iterator value
71+
*/
72+
type MapFunction = Nullary | Unary | Binary | Tertiary;
73+
74+
/**
75+
* Fills an array-like object view with values returned from an iterator.
76+
*
77+
* @param iterator - source iterator
78+
* @param out - output array
79+
* @param mapFcn - function to invoke for each iterated value
80+
* @param thisArg - execution context
81+
* @returns output array
82+
*
83+
* @example
84+
* var randu = require( `@stdlib/random/iter/randu` );
85+
* var Float64Array = require( `@stdlib/array/float64` );
86+
*
87+
* var iter = randu({
88+
* 'iter': 10
89+
* });
90+
*
91+
* var arr = iterator2arrayview( iter, new Float64Array( 20 ) );
92+
* // returns <Float64Array>
93+
*/
94+
declare function iterator2arrayview( iterator: Iterator, out: Collection, mapFcn?: MapFunction, thisArg?: any ): Collection; // tslint:disable-line:max-line-length
95+
96+
/**
97+
* Fills an array-like object view with values returned from an iterator.
98+
*
99+
* @param iterator - source iterator
100+
* @param out - output array
101+
* @param begin - starting index (inclusive) (default: 0)
102+
* @param mapFcn - function to invoke for each iterated value
103+
* @param thisArg - execution context
104+
* @returns output array
105+
*
106+
* @example
107+
* var randu = require( `@stdlib/random/iter/randu` );
108+
* var Float64Array = require( `@stdlib/array/float64` );
109+
*
110+
* var iter = randu({
111+
* 'iter': 10
112+
* });
113+
*
114+
* var arr = iterator2arrayview( iter, new Float64Array( 20 ), 5 );
115+
* // returns <Float64Array>
116+
*/
117+
declare function iterator2arrayview( iterator: Iterator, out: Collection, begin: number, mapFcn?: MapFunction, thisArg?: any ): Collection; // tslint:disable-line:max-line-length
118+
119+
/**
120+
* Fills an array-like object view with values returned from an iterator.
121+
*
122+
* @param iterator - source iterator
123+
* @param out - output array
124+
* @param begin - starting index (inclusive) (default: 0)
125+
* @param end - ending index (non-inclusive) (default: out.length)
126+
* @param mapFcn - function to invoke for each iterated value
127+
* @param thisArg - execution context
128+
* @returns output array
129+
*
130+
* @example
131+
* var randu = require( `@stdlib/random/iter/randu` );
132+
* var Float64Array = require( `@stdlib/array/float64` );
133+
*
134+
* var iter = randu({
135+
* 'iter': 10
136+
* });
137+
*
138+
* var arr = iterator2arrayview( iter, new Float64Array( 20 ), 5, 8 );
139+
* // returns <Float64Array>
140+
*/
141+
declare function iterator2arrayview( iterator: Iterator, out: Collection, begin: number, end: number, mapFcn?: MapFunction, thisArg?: any ): Collection; // tslint:disable-line:max-line-length
142+
143+
144+
// EXPORTS //
145+
146+
export = iterator2arrayview;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 randu = require( '@stdlib/random/iter/randu' );
20+
import iterator2arrayview = require( './index' );
21+
22+
/**
23+
* Multiplies a value by 10.
24+
*
25+
* @param v - iterated value
26+
* @returns new value
27+
*/
28+
function times10( v: number ): number {
29+
return v * 10.0;
30+
}
31+
32+
33+
// TESTS //
34+
35+
// The function returns a collection...
36+
{
37+
const iter = randu( { 'iter': 10 } );
38+
const out = new Float64Array( 10 );
39+
iterator2arrayview( iter, out ); // $ExpectType Collection
40+
iterator2arrayview( iter, out, times10 ); // $ExpectType Collection
41+
iterator2arrayview( iter, out, times10, {} ); // $ExpectType Collection
42+
iterator2arrayview( iter, out, 0, 10, times10 ); // $ExpectType Collection
43+
iterator2arrayview( iter, out, 0, 10, times10, {} ); // $ExpectType Collection
44+
}
45+
46+
// The compiler throws an error if the function is provided a first argument which is not an iterator...
47+
{
48+
iterator2arrayview( 'abc', [] ); // $ExpectError
49+
iterator2arrayview( 123, [] ); // $ExpectError
50+
iterator2arrayview( true, [] ); // $ExpectError
51+
iterator2arrayview( false, [] ); // $ExpectError
52+
iterator2arrayview( [], [] ); // $ExpectError
53+
iterator2arrayview( {}, [] ); // $ExpectError
54+
iterator2arrayview( null, [] ); // $ExpectError
55+
iterator2arrayview( undefined, [] ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided a second argument which is not a collection...
59+
{
60+
const iter = randu( { 'iter': 10 } );
61+
iterator2arrayview( iter, 123 ); // $ExpectError
62+
iterator2arrayview( iter, {} ); // $ExpectError
63+
iterator2arrayview( iter, true ); // $ExpectError
64+
iterator2arrayview( iter, false ); // $ExpectError
65+
iterator2arrayview( iter, null ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided a third argument which is not a number or map function...
69+
{
70+
const iter = randu( { 'iter': 10 } );
71+
iterator2arrayview( iter, [], 'abc' ); // $ExpectError
72+
iterator2arrayview( iter, [], [] ); // $ExpectError
73+
iterator2arrayview( iter, [], {} ); // $ExpectError
74+
iterator2arrayview( iter, [], true ); // $ExpectError
75+
iterator2arrayview( iter, [], false ); // $ExpectError
76+
iterator2arrayview( iter, [], null ); // $ExpectError
77+
}
78+
79+
// The compiler throws an error if the function is provided a `begin` argument and a fourth argument which is not a number or map function...
80+
{
81+
const iter = randu( { 'iter': 10 } );
82+
iterator2arrayview( iter, [], 2, 'abc' ); // $ExpectError
83+
iterator2arrayview( iter, [], 2, [] ); // $ExpectError
84+
iterator2arrayview( iter, [], 2, {} ); // $ExpectError
85+
iterator2arrayview( iter, [], 2, true ); // $ExpectError
86+
iterator2arrayview( iter, [], 2, false ); // $ExpectError
87+
iterator2arrayview( iter, [], 2, null ); // $ExpectError
88+
}
89+
90+
// The compiler throws an error if the function is provided an unsupported number of arguments...
91+
{
92+
const iter = randu( { 'iter': 10 } );
93+
iterator2arrayview(); // $ExpectError
94+
iterator2arrayview( iter ); // $ExpectError
95+
iterator2arrayview( iter, [], 0, 10, times10, {}, 123 ); // $ExpectError
96+
}

lib/node_modules/@stdlib/iter/to-array-view/package.json

+1
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)