Skip to content

Commit

Permalink
fix: update import path for Collection type definition
Browse files Browse the repository at this point in the history
Ref: bde4671
  • Loading branch information
kgryte committed Aug 22, 2023
1 parent 73b6076 commit a151e17
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* limitations under the License.
*/

// TypeScript Version: 2.0
// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/object';
import { Collection } from '@stdlib/types/array';

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -35,7 +35,7 @@ type NullaryPredicate = () => boolean;
* @param value - collection value
* @returns boolean indicating whether an element in a collection passes a test
*/
type UnaryPredicate = ( value: any ) => boolean;
type UnaryPredicate<T> = ( value: T ) => boolean;

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

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

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

/**
* Function invoked for each collection element while a test condition is true.
Expand All @@ -76,15 +76,15 @@ type Nullary = () => void;
*
* @param value - collection value
*/
type Unary = ( value: any ) => void;
type Unary<T> = ( value: T ) => void;

/**
* Function invoked for each collection element while a test condition is true.
*
* @param value - collection value
* @param index - collection index
*/
type Binary = ( value: any, index: number ) => void;
type Binary<T> = ( value: T, index: number ) => void;

/**
* Function invoked for each collection element while a test condition is true.
Expand All @@ -93,7 +93,7 @@ type Binary = ( value: any, index: number ) => void;
* @param index - collection index
* @param collection - input collection
*/
type Ternary = ( value: any, index: number, collection: Collection ) => void;
type Ternary<T> = ( value: T, index: number, collection: Collection<T> ) => void;

/**
* Function invoked for each collection element while a test condition is true.
Expand All @@ -102,7 +102,7 @@ type Ternary = ( value: any, index: number, collection: Collection ) => void;
* @param index - collection index
* @param collection - input collection
*/
type Callback = Nullary | Unary | Binary | Ternary;
type Callback<T> = Nullary | Unary<T> | Binary<T> | Ternary<T>;

/**
* While a test condition is true, invokes a function once for each element in a collection, iterating from right to left.
Expand All @@ -121,7 +121,6 @@ type Callback = Nullary | Unary | Binary | Ternary;
*
* - If provided an empty collection, the function invokes the provided function with the collection index set to `undefined`.
*
*
* @param collection - input collection
* @param fcn - function to invoke
* @param predicate - function which indicates whether to continue iterating over a collection
Expand All @@ -141,7 +140,7 @@ type Callback = Nullary | Unary | Binary | Ternary;
*
* doWhileEachRight( arr, log, predicate );
*/
declare function doWhileEachRight( collection: Collection, fcn: Callback, predicate: Predicate, thisArg?: any ): Collection; // tslint-disable-line max-line-length
declare function doWhileEachRight<T = unknown>( collection: Collection<T>, fcn: Callback<T>, predicate: Predicate<T>, thisArg?: ThisParameterType<Callback<T>> ): Collection<T>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@

import doWhileEachRight = require( './index' );

const log = ( v: any, index: number ): void => {
console.log( `${index}: ${v}` );
};
/**
* Dummy function.
*/
function fcn( v: any, index: number ): number {
if ( v !== v ) {
throw new Error( 'beep' );
}
return index;
}

const isNotNaN = ( v: number ): boolean => {
/**
* Dummy function.
*/
function isNotNaN( v: number ): boolean {
return ( v === v );
};
}


// TESTS //

// The function returns the input collection...
{
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, isNotNaN ); // $ExpectType Collection
doWhileEachRight( [ -1, 1, 2 ], log, isNotNaN, {} ); // $ExpectType Collection
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, isNotNaN ); // $ExpectType Collection<number>
doWhileEachRight( [ -1, 1, 2 ], fcn, isNotNaN, {} ); // $ExpectType Collection<number>
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
{
doWhileEachRight( 2, log, isNotNaN ); // $ExpectError
doWhileEachRight( false, log, isNotNaN ); // $ExpectError
doWhileEachRight( true, log, isNotNaN ); // $ExpectError
doWhileEachRight( {}, log, isNotNaN ); // $ExpectError
doWhileEachRight( 2, fcn, isNotNaN ); // $ExpectError
doWhileEachRight( false, fcn, isNotNaN ); // $ExpectError
doWhileEachRight( true, fcn, isNotNaN ); // $ExpectError
doWhileEachRight( {}, fcn, isNotNaN ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a function...
Expand All @@ -54,18 +64,18 @@ const isNotNaN = ( v: number ): boolean => {

// The compiler throws an error if the function is provided a third argument which is not a function...
{
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, 2 ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, false ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, true ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, 'abc' ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, {} ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], log, [] ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, 2 ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, false ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, true ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, 'abc' ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, {} ); // $ExpectError
doWhileEachRight( [ 0, 1, 1, NaN, 2 ], fcn, [] ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid number of arguments...
{
doWhileEachRight(); // $ExpectError
doWhileEachRight( [ 1, 2, 3 ] ); // $ExpectError
doWhileEachRight( [ 1, 2, 3 ], log ); // $ExpectError
doWhileEachRight( [ 1, 2, 3 ], log, isNotNaN, {}, 3 ); // $ExpectError
doWhileEachRight( [ 1, 2, 3 ], fcn ); // $ExpectError
doWhileEachRight( [ 1, 2, 3 ], fcn, isNotNaN, {}, 3 ); // $ExpectError
}

0 comments on commit a151e17

Please sign in to comment.