Skip to content

Commit 6e52135

Browse files
committed
Add Typescript definition
1 parent 5a61306 commit 6e52135

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
26+
// Define a union type representing both iterable and non-iterable iterators:
27+
type Iterator = Iter | IterableIterator;
28+
29+
/**
30+
* Map function invoked for each iterated value.
31+
*
32+
* @returns iterator value
33+
*/
34+
type Nullary = () => any;
35+
36+
/**
37+
* Map function invoked for each iterated value.
38+
*
39+
* @param value - iterated value
40+
* @returns iterator value
41+
*/
42+
type Unary = ( value: any ) => any;
43+
44+
/**
45+
* Map function invoked for each iterated value.
46+
*
47+
* @param value - iterated value
48+
* @param index - iterated value index
49+
* @returns iterator value
50+
*/
51+
type Binary = ( value: any, index: number ) => any;
52+
53+
/**
54+
* Map function invoked for each iterated value.
55+
*
56+
* @param value - iterated value
57+
* @param index - iterated value index
58+
* @param n - iteration count (zero-based)
59+
* @returns iterator value
60+
*/
61+
type Tertiary = ( value: any, index: number, n: number ) => any;
62+
63+
/**
64+
* Map function invoked for each iterated value.
65+
*
66+
* @param value - iterated value
67+
* @param index - iterated value index
68+
* @param n - iteration count (zero-based)
69+
* @param src - source array-like object
70+
* @returns iterator value
71+
*/
72+
type Quaternary = ( value: any, index: number, n: number, src: ArrayLike<any> ) => any; // tslint:disable-line:max-line-length
73+
74+
/**
75+
* Map function invoked for each iterated value.
76+
*
77+
* @param value - iterated value
78+
* @param index - iterated value index
79+
* @param n - iteration count (zero-based)
80+
* @param src - source array-like object
81+
* @returns iterator value
82+
*/
83+
type MapFunction = Nullary | Unary | Binary | Tertiary | Quaternary;
84+
85+
/**
86+
* Returns an iterator which iterates over elements in an array-like object according to specified stride parameters.
87+
*
88+
* @param N - number of values to iterate
89+
* @param src - input value
90+
* @param stride - stride length
91+
* @param offset - starting index
92+
* @param mapFcn - function to invoke for each iterated value
93+
* @param thisArg - execution context
94+
* @throws first argument must be a nonnegative integer
95+
* @throws third argument must be an integer
96+
* @throws fourth argument must be a nonnegative integer
97+
* @returns iterator
98+
*
99+
* @example
100+
* var values = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
101+
*
102+
* var N = 4;
103+
* var stride = -2;
104+
* var offset = 6;
105+
*
106+
* var iter = stridedarray2iterator( N, values, stride, offset );
107+
*
108+
* var v = iter.next().value;
109+
* // returns 7
110+
*
111+
* v = iter.next().value;
112+
* // returns 5
113+
*
114+
* v = iter.next().value;
115+
* // returns 3
116+
*
117+
* // ...
118+
*/
119+
declare function stridedarray2iterator( N: number, src: ArrayLike<any>, stride: number, offset: number, mapFcn?: MapFunction, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
120+
121+
122+
// EXPORTS //
123+
124+
export = stridedarray2iterator;
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) 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 stridedarray2iterator = require( './index' );
20+
21+
/**
22+
* Multiplies a value by 10.
23+
*
24+
* @param v - iterated value
25+
* @returns new value
26+
*/
27+
function times10( v: number ): number {
28+
return v * 10.0;
29+
}
30+
31+
32+
// TESTS //
33+
34+
// The function returns an iterator...
35+
{
36+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectType Iterator
37+
stridedarray2iterator( 4, [ 1, 2, 3, 4, 5, 6, 7, 8 ], -2, 6, times10 ); // $ExpectType Iterator
38+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, times10, {} ); // $ExpectType Iterator
39+
}
40+
41+
// The compiler throws an error if the function is provided a first argument which is not a number...
42+
{
43+
stridedarray2iterator( 'abc', [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
44+
stridedarray2iterator( true, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
45+
stridedarray2iterator( false, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
46+
stridedarray2iterator( {}, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
47+
stridedarray2iterator( null, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
48+
stridedarray2iterator( undefined, [ 1, 2, 3, 4 ], -2, 3 ); // $ExpectError
49+
}
50+
51+
// The compiler throws an error if the function is provided a second argument which is not array-like...
52+
{
53+
stridedarray2iterator( 2, 123, -2, 3 ); // $ExpectError
54+
stridedarray2iterator( 2, true, -2, 3 ); // $ExpectError
55+
stridedarray2iterator( 2, false, -2, 3 ); // $ExpectError
56+
stridedarray2iterator( 2, {}, -2, 3 ); // $ExpectError
57+
stridedarray2iterator( 2, null, -2, 3 ); // $ExpectError
58+
stridedarray2iterator( 2, undefined, -2, 3 ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided a third argument which is not a number...
62+
{
63+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], 'abc', 3 ); // $ExpectError
64+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], true, 3 ); // $ExpectError
65+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], false, 3 ); // $ExpectError
66+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], {}, 3 ); // $ExpectError
67+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], null, 3 ); // $ExpectError
68+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], undefined, 3 ); // $ExpectError
69+
}
70+
71+
// The compiler throws an error if the function is provided a fourth argument which is not a number...
72+
{
73+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 'abc' ); // $ExpectError
74+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, true ); // $ExpectError
75+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, false ); // $ExpectError
76+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, {} ); // $ExpectError
77+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, null ); // $ExpectError
78+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, undefined ); // $ExpectError
79+
}
80+
81+
// The compiler throws an error if the function is provided a fifth argument which is not a map function...
82+
{
83+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, 'abc' ); // $ExpectError
84+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, 123 ); // $ExpectError
85+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, [] ); // $ExpectError
86+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, {} ); // $ExpectError
87+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, true ); // $ExpectError
88+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, false ); // $ExpectError
89+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, null ); // $ExpectError
90+
}
91+
92+
// The compiler throws an error if the function is provided an unsupported number of arguments...
93+
{
94+
stridedarray2iterator(); // $ExpectError
95+
stridedarray2iterator( 2 ); // $ExpectError
96+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ] ); // $ExpectError
97+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2 ); // $ExpectError
98+
stridedarray2iterator( 2, [ 1, 2, 3, 4 ], -2, 3, times10, {}, 123 ); // $ExpectError
99+
}

lib/node_modules/@stdlib/array/to-strided-iterator/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)