Skip to content

Commit 5cf30d1

Browse files
committed
feat!: refactor declarations to use generics
1 parent c928438 commit 5cf30d1

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

lib/node_modules/@stdlib/utils/until-each/docs/types/index.d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type NullaryPredicate = () => boolean;
3535
* @param value - collection value
3636
* @returns boolean indicating whether an element in a collection passes a test
3737
*/
38-
type UnaryPredicate = ( value: any ) => boolean;
38+
type UnaryPredicate<T> = ( value: T ) => boolean;
3939

4040
/**
4141
* Checks whether an element in a collection passes a test.
@@ -44,7 +44,7 @@ type UnaryPredicate = ( value: any ) => boolean;
4444
* @param index - collection index
4545
* @returns boolean indicating whether an element in a collection passes a test
4646
*/
47-
type BinaryPredicate = ( value: any, index: number ) => boolean;
47+
type BinaryPredicate<T> = ( value: T, index: number ) => boolean;
4848

4949
/**
5050
* Checks whether an element in a collection passes a test.
@@ -54,7 +54,7 @@ type BinaryPredicate = ( value: any, index: number ) => boolean;
5454
* @param collection - input collection
5555
* @returns boolean indicating whether an element in a collection passes a test
5656
*/
57-
type TernaryPredicate = ( value: any, index: number, collection: Collection ) => boolean; // tslint-disable-line max-line-length
57+
type TernaryPredicate<T> = ( value: T, index: number, collection: Collection<T> ) => boolean; // tslint-disable-line max-line-length
5858

5959
/**
6060
* Checks whether an element in a collection passes a test.
@@ -64,7 +64,7 @@ type TernaryPredicate = ( value: any, index: number, collection: Collection ) =>
6464
* @param collection - input collection
6565
* @returns boolean indicating whether an element in a collection passes a test
6666
*/
67-
type Predicate = NullaryPredicate | UnaryPredicate | BinaryPredicate | TernaryPredicate; // tslint-disable-line max-line-length
67+
type Predicate<T> = NullaryPredicate | UnaryPredicate<T> | BinaryPredicate<T> | TernaryPredicate<T>; // tslint-disable-line max-line-length
6868

6969
/**
7070
* Function invoked for each collection element until test condition is true.
@@ -76,15 +76,15 @@ type Nullary = () => void;
7676
*
7777
* @param value - collection value
7878
*/
79-
type Unary = ( value: any ) => void;
79+
type Unary<T> = ( value: T ) => void;
8080

8181
/**
8282
* Function invoked for each collection element until test condition is true.
8383
*
8484
* @param value - collection value
8585
* @param index - collection index
8686
*/
87-
type Binary = ( value: any, index: number ) => void;
87+
type Binary<T> = ( value: T, index: number ) => void;
8888

8989
/**
9090
* Function invoked for each collection element until test condition is true.
@@ -93,7 +93,7 @@ type Binary = ( value: any, index: number ) => void;
9393
* @param index - collection index
9494
* @param collection - input collection
9595
*/
96-
type Ternary = ( value: any, index: number, collection: Collection ) => void;
96+
type Ternary<T> = ( value: T, index: number, collection: Collection<T> ) => void; // tslint-disable-line max-line-length
9797

9898
/**
9999
* Function invoked for each collection element until test condition is true.
@@ -102,7 +102,7 @@ type Ternary = ( value: any, index: number, collection: Collection ) => void;
102102
* @param index - collection index
103103
* @param collection - input collection
104104
*/
105-
type Callback = Nullary | Unary | Binary | Ternary;
105+
type Callback<T> = Nullary | Unary<T> | Binary<T> | Ternary<T>;
106106

107107
/**
108108
* Until a test condition is true, invokes a function once for each element in a collection.
@@ -134,7 +134,7 @@ type Callback = Nullary | Unary | Binary | Ternary;
134134
*
135135
* untilEach( arr, predicate, log );
136136
*/
137-
declare function untilEach( collection: Collection, predicate: Predicate, fcn: Callback, thisArg?: any ): Collection; // tslint-disable-line max-line-length
137+
declare function untilEach<T = any>( collection: Collection<T>, predicate: Predicate<T>, fcn: Callback<T>, thisArg?: any ): Collection<T>; // tslint-disable-line max-line-length
138138

139139

140140
// EXPORTS //

lib/node_modules/@stdlib/utils/until-each/docs/types/test.ts

+44-26
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,72 @@
1818

1919
import untilEach = require( './index' );
2020

21-
const log = ( v: any, index: number ): void => {
22-
console.log( `${index}: ${v}` );
23-
};
21+
/**
22+
* Test function.
23+
*
24+
* @param v - value
25+
* @param index - index
26+
*/
27+
function fcn( v: number, index: number ): void {
28+
if ( v !== v ) {
29+
throw new Error( 'beep' );
30+
}
31+
if ( index !== index ) {
32+
throw new Error( 'beep' );
33+
}
34+
}
35+
36+
/**
37+
* Test function.
38+
*
39+
* @param v - value
40+
* @returns result
41+
*/
42+
function isnan( v: number ): boolean {
43+
return ( v !== v );
44+
}
2445

25-
const isNotNaN = ( v: number ): boolean => {
26-
return ( v === v );
27-
};
2846

2947
// TESTS //
3048

3149
// The function returns the input collection...
3250
{
33-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, log ); // $ExpectType Collection
34-
untilEach( [ -1, 1, 2 ], isNotNaN, log, {} ); // $ExpectType Collection
51+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, fcn ); // $ExpectType Collection<number>
52+
untilEach( [ -1, 1, 2 ], isnan, fcn, {} ); // $ExpectType Collection<number>
3553
}
3654

3755
// The compiler throws an error if the function is provided a first argument which is not a collection...
3856
{
39-
untilEach( 2, isNotNaN, log ); // $ExpectError
40-
untilEach( false, isNotNaN, log ); // $ExpectError
41-
untilEach( true, isNotNaN, log ); // $ExpectError
42-
untilEach( {}, isNotNaN, log ); // $ExpectError
57+
untilEach( 2, isnan, fcn ); // $ExpectError
58+
untilEach( false, isnan, fcn ); // $ExpectError
59+
untilEach( true, isnan, fcn ); // $ExpectError
60+
untilEach( {}, isnan, fcn ); // $ExpectError
4361
}
4462

4563
// The compiler throws an error if the function is provided a second argument which is not a function...
4664
{
47-
untilEach( [ 0, 1, 1, NaN, 2 ], 2, log ); // $ExpectError
48-
untilEach( [ 0, 1, 1, NaN, 2 ], false, log ); // $ExpectError
49-
untilEach( [ 0, 1, 1, NaN, 2 ], true, log ); // $ExpectError
50-
untilEach( [ 0, 1, 1, NaN, 2 ], 'abc', log ); // $ExpectError
51-
untilEach( [ 0, 1, 1, NaN, 2 ], {}, log ); // $ExpectError
52-
untilEach( [ 0, 1, 1, NaN, 2 ], [], log ); // $ExpectError
65+
untilEach( [ 0, 1, 1, NaN, 2 ], 2, fcn ); // $ExpectError
66+
untilEach( [ 0, 1, 1, NaN, 2 ], false, fcn ); // $ExpectError
67+
untilEach( [ 0, 1, 1, NaN, 2 ], true, fcn ); // $ExpectError
68+
untilEach( [ 0, 1, 1, NaN, 2 ], 'abc', fcn ); // $ExpectError
69+
untilEach( [ 0, 1, 1, NaN, 2 ], {}, fcn ); // $ExpectError
70+
untilEach( [ 0, 1, 1, NaN, 2 ], [], fcn ); // $ExpectError
5371
}
5472

5573
// The compiler throws an error if the function is provided a third argument which is not a function...
5674
{
57-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, 2 ); // $ExpectError
58-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, false ); // $ExpectError
59-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, true ); // $ExpectError
60-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, 'abc' ); // $ExpectError
61-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, {} ); // $ExpectError
62-
untilEach( [ 0, 1, 1, NaN, 2 ], isNotNaN, [] ); // $ExpectError
75+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, 2 ); // $ExpectError
76+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, false ); // $ExpectError
77+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, true ); // $ExpectError
78+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, 'abc' ); // $ExpectError
79+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, {} ); // $ExpectError
80+
untilEach( [ 0, 1, 1, NaN, 2 ], isnan, [] ); // $ExpectError
6381
}
6482

6583
// The compiler throws an error if the function is provided an invalid number of arguments...
6684
{
6785
untilEach(); // $ExpectError
6886
untilEach( [ 1, 2, 3 ] ); // $ExpectError
69-
untilEach( [ 1, 2, 3 ], isNotNaN ); // $ExpectError
70-
untilEach( [ 1, 2, 3 ], isNotNaN, log, {}, 3 ); // $ExpectError
87+
untilEach( [ 1, 2, 3 ], isnan ); // $ExpectError
88+
untilEach( [ 1, 2, 3 ], isnan, fcn, {}, 3 ); // $ExpectError
7189
}

0 commit comments

Comments
 (0)