Skip to content

Commit 9340097

Browse files
committed
Add Typescript definition
1 parent e20380a commit 9340097

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
* Interface defining function options.
31+
*/
32+
interface Options {
33+
/**
34+
* Number of iterations.
35+
*/
36+
iter?: number;
37+
38+
/**
39+
* Iteration direction (default: 1).
40+
*/
41+
dir?: number;
42+
}
43+
44+
/**
45+
* Map function invoked for each iterated value.
46+
*
47+
* @returns iterator value
48+
*/
49+
type Nullary = () => any;
50+
51+
/**
52+
* Map function invoked for each iterated value.
53+
*
54+
* @param value - iterated value
55+
* @returns iterator value
56+
*/
57+
type Unary = ( value: any ) => any;
58+
59+
/**
60+
* Map function invoked for each iterated value.
61+
*
62+
* @param value - iterated value
63+
* @param index - iterated value index
64+
* @returns iterator value
65+
*/
66+
type Binary = ( value: any, index: number ) => any;
67+
68+
/**
69+
* Map function invoked for each iterated value.
70+
*
71+
* @param value - iterated value
72+
* @param index - iterated value index
73+
* @param n - iteration count
74+
* @returns iterator value
75+
*/
76+
type Tertiary = ( value: any, index: number, n: number ) => any;
77+
78+
/**
79+
* Map function invoked for each iterated value.
80+
*
81+
* @param value - iterated value
82+
* @param index - iterated value index
83+
* @param n - iteration count
84+
* @param src - source array-like object
85+
* @returns iterator value
86+
*/
87+
type Quaternary = ( value: any, index: number, n: number, src: ArrayLike<any> ) => any; // tslint-disable-line max-line-length
88+
89+
/**
90+
* Map function invoked for each iterated value.
91+
*
92+
* @param value - iterated value
93+
* @param index - iterated value index
94+
* @param n - iteration count
95+
* @param src - source array-like object
96+
* @returns iterator value
97+
*/
98+
type MapFunction = Nullary | Unary | Binary | Tertiary | Quaternary;
99+
100+
/**
101+
* Returns an iterator which repeatedly iterates over each element in an array-like object.
102+
*
103+
* @param src - input value
104+
* @param mapFcn - function to invoke for each iterated value
105+
* @param thisArg - execution context
106+
* @throws must provide valid options
107+
* @returns iterator
108+
*
109+
* @example
110+
* var iter = circarray2iterator( [ 1, 2, 3, 4 ] );
111+
*
112+
* var v = iter.next().value;
113+
* // returns 1
114+
*
115+
* v = iter.next().value;
116+
* // returns 2
117+
*
118+
* v = iter.next().value;
119+
* // returns 3
120+
*
121+
* // ...
122+
*/
123+
declare function circarray2iterator( src: ArrayLike<any>, mapFcn?: MapFunction, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
124+
125+
/**
126+
* Returns an iterator which repeatedly iterates over each element in an array-like object.
127+
*
128+
* @param src - input value
129+
* @param options - function options
130+
* @param options.iter - number of iterations
131+
* @param options.dir - iteration direction
132+
* @param mapFcn - function to invoke for each iterated value
133+
* @param thisArg - execution context
134+
* @throws must provide valid options
135+
* @returns iterator
136+
*
137+
* @example
138+
* var opts = {
139+
* 'dir': -1
140+
* };
141+
* var it = circarray2iterator( [ 1, 2, 3, 4 ], opts );
142+
* // returns <Object>
143+
*
144+
* var v = it.next().value;
145+
* // returns 4
146+
*
147+
* v = it.next().value;
148+
* // returns 3
149+
*
150+
* v = it.next().value;
151+
* // returns 2
152+
*/
153+
declare function circarray2iterator( src: ArrayLike<any>, options: Options, mapFcn?: MapFunction, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
154+
155+
156+
// EXPORTS //
157+
158+
export = circarray2iterator;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 circarray2iterator = 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+
circarray2iterator( [ 1, 2, 3, 4 ] ); // $ExpectType Iterator
37+
circarray2iterator( [ 1, 2, 3, 4 ], times10 ); // $ExpectType Iterator
38+
circarray2iterator( [ 1, 2, 3, 4 ], times10, {} ); // $ExpectType Iterator
39+
}
40+
41+
// The compiler throws an error if the function is provided a first argument which is not array-like...
42+
{
43+
circarray2iterator( 123 ); // $ExpectError
44+
circarray2iterator( true ); // $ExpectError
45+
circarray2iterator( false ); // $ExpectError
46+
circarray2iterator( {} ); // $ExpectError
47+
circarray2iterator( null ); // $ExpectError
48+
circarray2iterator( undefined ); // $ExpectError
49+
}
50+
51+
// The compiler throws an error if the function is provided a second argument which is not an options object or function...
52+
{
53+
circarray2iterator( [ 1, 2, 3, 4 ], 'abc' ); // $ExpectError
54+
circarray2iterator( [ 1, 2, 3, 4 ], 123 ); // $ExpectError
55+
circarray2iterator( [ 1, 2, 3, 4 ], [] ); // $ExpectError
56+
circarray2iterator( [ 1, 2, 3, 4 ], true ); // $ExpectError
57+
circarray2iterator( [ 1, 2, 3, 4 ], false ); // $ExpectError
58+
circarray2iterator( [ 1, 2, 3, 4 ], null ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided a `dir` option which is not a number...
62+
{
63+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': 'abc' } ); // $ExpectError
64+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': true } ); // $ExpectError
65+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': false } ); // $ExpectError
66+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': null } ); // $ExpectError
67+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': [] } ); // $ExpectError
68+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': {} } ); // $ExpectError
69+
circarray2iterator( [ 1, 2, 3, 4 ], { 'dir': ( x: number ): number => x } ); // $ExpectError
70+
}
71+
72+
// The compiler throws an error if the function is provided an `iter` option which is not a number...
73+
{
74+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': 'abc' } ); // $ExpectError
75+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': true } ); // $ExpectError
76+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': false } ); // $ExpectError
77+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': null } ); // $ExpectError
78+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': [] } ); // $ExpectError
79+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': {} } ); // $ExpectError
80+
circarray2iterator( [ 1, 2, 3, 4 ], { 'iter': ( x: number ): number => x } ); // $ExpectError
81+
}
82+
83+
// The compiler throws an error if the function is provided an unsupported number of arguments...
84+
{
85+
circarray2iterator(); // $ExpectError
86+
circarray2iterator( [ 1, 2, 3, 4 ], times10, {}, 123 ); // $ExpectError
87+
}

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