@stdlib/utils
Version:
Standard utilities.
72 lines (63 loc) • 2.83 kB
text/typescript
/*
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import curryRight = require( './index' );
// TESTS //
// The function returns a function...
{
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...
{
curryRight( true ); // $ExpectError
curryRight( false ); // $ExpectError
curryRight( 5 ); // $ExpectError
curryRight( [] ); // $ExpectError
curryRight( {} ); // $ExpectError
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
}