Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Dec 30, 2023
1 parent c7b0143 commit 7727ac3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 13 deletions.
40 changes: 37 additions & 3 deletions curry-right/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,32 @@

// TypeScript Version: 4.1

/**
* Utility type to reverse a tuple type.
*/
type ReverseTuple<T extends Array<any>> = T extends [infer First, ...infer Rest] ? [...ReverseTuple<Rest>, First] : []; // eslint-disable-line @typescript-eslint/no-explicit-any

/**
* Curry function type for curryRight.
*/
type CurryRightFunction<
TThis,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
> = ( this: TThis, ...args: TArgs ) => TReturn;

/**
* Curry function.
*
* @param v - curried function parameter
* @returns partially applied curry function or curried function result
*/
type Closure = ( v: any ) => any;
type Closure<
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
> = ReverseTuple<TArgs> extends [infer TFirst, ...infer TRest]
? ( v: TFirst ) => Closure<ReverseTuple<TRest>, TReturn>
: TReturn;

/**
* Transforms a function into a sequence of functions each accepting a single argument.
Expand All @@ -50,7 +69,15 @@ type Closure = ( v: any ) => any;
* var sum = f( 2 )( 3 );
* // returns 5
*/
declare function curryRight( fcn: Function, arity: number, thisArg?: any ): Closure;
declare function curryRight<
TThis extends object,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
>(
fcn: CurryRightFunction<TThis, TArgs, TReturn>,
arity: number,
thisArg?: TThis
): Closure<TArgs, TReturn>;

/**
* Transforms a function into a sequence of functions each accepting a single argument.
Expand All @@ -74,7 +101,14 @@ declare function curryRight( fcn: Function, arity: number, thisArg?: any ): Clos
* var sum = f( 2 )( 3 );
* // returns 5
*/
declare function curryRight( fcn: Function, thisArg?: any ): Closure;
declare function curryRight<
TThis extends object,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
>(
fcn: CurryRightFunction<TThis, TArgs, TReturn>,
thisArg?: TThis
): Closure<TArgs, TReturn>;


// EXPORTS //
Expand Down
33 changes: 30 additions & 3 deletions curry-right/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ import curryRight = require( './index' );

// The function returns a function...
{
curryRight( ( x: number, y: number ): number => x + y ); // $ExpectType Closure
curryRight( ( x: number, y: number ): number => x + y, 2 ); // $ExpectType Closure
curryRight( ( x: number, y: number ): number => x + y, 2, {} ); // $ExpectType Closure
curryRight( ( x: number, y: number ): number => x + y ); // $ExpectType (v: number) => (v: number) => number
curryRight( ( x: number, y: number ): number => x + y, 2 ); // $ExpectType (v: number) => (v: number) => number
curryRight( ( x: number, y: number ): number => x + y, 2, {} ); // $ExpectType (v: number) => (v: number) => number
curryRight( ( x: string, y: string ): string => x + y, {} ); // $ExpectType (v: string) => (v: string) => string
curryRight( ( x: string, y: string ): string => x + y, 2, {} ); // $ExpectType (v: string) => (v: string) => string
curryRight( ( str: string, n: number ): string => str.substring( n ) ); // $ExpectType (v: number) => (v: string) => string

const cxt = {
'count': 0,
'multiply': function multiply( this: { count: number }, x: number, y: number ): number {
this.count += 1; // eslint-disable-line no-invalid-this
return x * y;
}
};
curryRight( cxt.multiply ); // $ExpectType (v: number) => (v: number) => number
curryRight( cxt.multiply, 2 ); // $ExpectType (v: number) => (v: number) => number
curryRight( cxt.multiply, 2, { 'count': 2 } ); // $ExpectType (v: number) => (v: number) => number
}

// The compiler throws an error if the function is provided a first argument other than a function...
Expand All @@ -38,6 +52,19 @@ import curryRight = require( './index' );
curryRight( 'abc' ); // $ExpectError
}

// The compiler throws an error if the function is provided a this argument which does not constitute a valid `this` context...
{
const cxt = {
'count': 0,
'multiply': function multiply( this: { count: number }, x: number, y: number ): number {
this.count += 1; // eslint-disable-line no-invalid-this
return x * y;
}
};
curryRight( cxt.multiply, { 'COUNT': 2 } ); // $ExpectError
curryRight( cxt.multiply, 2, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided more than three arguments...
{
curryRight( ( x: number, y: number ): number => x + y, 2, {}, 4 ); // $ExpectError
Expand Down
35 changes: 32 additions & 3 deletions curry/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@

// TypeScript Version: 4.1

/**
* Curry function type.
*/
type CurryFunction<
TThis,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
> = ( this: TThis, ...args: TArgs ) => TReturn;

/**
* Curry function.
*
* @param v - curried function parameter
* @returns partially applied curry function or curried function result
*/
type Closure = ( v: any ) => any;
type Closure<
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
> = TArgs extends [infer TFirst, ...infer TRest]
? ( v: TFirst ) => Closure<TRest, TReturn>
: TReturn;

/**
* Transforms a function into a sequence of functions each accepting a single argument.
Expand All @@ -49,7 +63,15 @@ type Closure = ( v: any ) => any;
* var sum = f( 2 )( 3 );
* // returns 5
*/
declare function curry( fcn: Function, arity: number, thisArg?: any ): Closure;
declare function curry<
TThis extends object,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
>(
fcn: CurryFunction<TThis, TArgs, TReturn>,
arity: number,
thisArg?: TThis
): Closure<TArgs, TReturn>;

/**
* Transforms a function into a sequence of functions each accepting a single argument.
Expand All @@ -72,7 +94,14 @@ declare function curry( fcn: Function, arity: number, thisArg?: any ): Closure;
* var sum = f( 2 )( 3 );
* // returns 5
*/
declare function curry( fcn: Function, thisArg?: any ): Closure;
declare function curry<
TThis extends object,
TArgs extends Array<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
TReturn
>(
fcn: CurryFunction<TThis, TArgs, TReturn>,
thisArg?: TThis
): Closure<TArgs, TReturn>;


// EXPORTS //
Expand Down
33 changes: 29 additions & 4 deletions curry/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ import curry = require( './index' );

// The function returns a function...
{
curry( ( x: number, y: number ): number => x + y ); // $ExpectType Closure
curry( ( x: number, y: number ): number => x + y, 2 ); // $ExpectType Closure
curry( ( x: number, y: number ): number => x + y, {} ); // $ExpectType Closure
curry( ( x: number, y: number ): number => x + y, 2, {} ); // $ExpectType Closure
curry( ( x: number, y: number ): number => x + y ); // $ExpectType (v: number) => (v: number) => number
curry( ( x: number, y: number ): number => x + y, 2 ); // $ExpectType (v: number) => (v: number) => number
curry( ( x: string, y: string ): string => x + y, {} ); // $ExpectType (v: string) => (v: string) => string
curry( ( x: string, y: string ): string => x + y, 2, {} ); // $ExpectType (v: string) => (v: string) => string
curry( ( str: string, n: number ): string => str.substring( n ) ); // $ExpectType (v: string) => (v: number) => string

const cxt = {
'count': 0,
'multiply': function multiply( this: { count: number }, x: number, y: number ): number {
this.count += 1; // eslint-disable-line no-invalid-this
return x * y;
}
};
curry( cxt.multiply ); // $ExpectType (v: number) => (v: number) => number
curry( cxt.multiply, 2 ); // $ExpectType (v: number) => (v: number) => number
curry( cxt.multiply, 2, { 'count': 2 } ); // $ExpectType (v: number) => (v: number) => number
}

// The compiler throws an error if the function is provided a first argument other than a function...
Expand All @@ -39,6 +51,19 @@ import curry = require( './index' );
curry( 'abc' ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which does not constitute a valid `this` context...
{
const cxt = {
'count': 0,
'multiply': function multiply( this: { count: number }, x: number, y: number ): number {
this.count += 1; // eslint-disable-line no-invalid-this
return x * y;
}
};
curry( cxt.multiply, { 'COUNT': 2 } ); // $ExpectError
curry( cxt.multiply, 2, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided more than three arguments...
{
curry( ( x: number, y: number ): number => x + y, 2, {}, 4 ); // $ExpectError
Expand Down

0 comments on commit 7727ac3

Please sign in to comment.