Skip to content

Commit

Permalink
Showing 4 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ jobs:
# Publish package to npm:
- name: 'Publish package to npm'
uses: JS-DevTools/npm-publish@v1
uses: JS-DevTools/npm-publish@v2
with:
token: ${{ secrets.NPM_TOKEN }}
access: public
4 changes: 4 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -16,19 +16,23 @@ Joey Reed <[email protected]>
Jordan-Gallivan <[email protected]>
Joris Labie <[email protected]>
Justin Dennison <[email protected]>
KATTA NAGA NITHIN <[email protected]>
Marcus <[email protected]>
Matt Cochrane <[email protected]>
Milan Raj <[email protected]>
Momtchil Momtchev <[email protected]>
Naresh Jagadeesan <[email protected]>
Ognjen Jevremović <[email protected]>
Philipp Burckhardt <[email protected]>
Pranav <[email protected]>
Ricky Reusser <[email protected]>
Roman Stetsyk <[email protected]>
Ryan Seal <[email protected]>
Seyyed Parsa Neshaei <[email protected]>
Shraddheya Shendre <[email protected]>
Stephannie Jiménez Gacha <[email protected]>
Yernar Yergaziyev <[email protected]>
dorrin-sot <[email protected]>
drunken_devv <[email protected]>
orimiles5 <[email protected]>
rei2hu <[email protected]>
18 changes: 9 additions & 9 deletions while-each-right/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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.
@@ -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.
@@ -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<T> ) => boolean; // tslint-disable-line max-line-length

/**
* Checks whether an element in a collection passes a test.
@@ -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>; // tslint-disable-line max-line-length

/**
* Function invoked for each collection element passing a test.
@@ -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 passing a test.
*
* @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 passing a test.
@@ -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; // tslint-disable-line max-line-length

/**
* Function invoked for each collection element passing a test.
@@ -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.
@@ -137,7 +137,7 @@ type Callback = Nullary | Unary | Binary | Ternary;
*
* whileEachRight( arr, predicate, log );
*/
declare function whileEachRight( collection: Collection, predicate: Predicate, fcn: Callback, thisArg?: any ): Collection; // tslint-disable-line max-line-length
declare function whileEachRight<T = any>( collection: Collection<T>, predicate: Predicate<T>, fcn: Callback<T>, thisArg?: any ): Collection<T>; // tslint-disable-line max-line-length


// EXPORTS //
53 changes: 29 additions & 24 deletions while-each-right/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -18,54 +18,59 @@

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

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

const isNotNaN = ( v: number ): boolean => {
return ( v === v );
const isnan = ( v: number ): boolean => {
return ( v !== v );
};

// TESTS //

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

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

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

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

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

0 comments on commit fdcb3e0

Please sign in to comment.