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 Jul 19, 2023
1 parent f52ecbe commit a52b687
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
27 changes: 25 additions & 2 deletions key-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@
* limitations under the License.
*/

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

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

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

/**
* Function invoked for each collection element returning a key.
*
* @param value - collection value
*/
type Unary<T, V> = ( this: T, value: V ) => string;

/**
* Function invoked for each collection element returning a key.
*
* @param value - collection value
* @param index - collection index
*/
type Binary<T, V> = ( this: T, value: V, index: number ) => string;

/**
* Function invoked for each collection element returning a key.
*
* @param value - collection value
* @param index - collection index
*/
type Callback<T, V> = Unary<T, V> | Binary<T, V>;

/**
* Converts a collection to an object whose keys are determined by a provided function and whose values are the collection values.
*
Expand Down Expand Up @@ -55,7 +78,7 @@ import { Collection } from '@stdlib/types/object';
* var obj = keyBy( collection, toKey );
* // returns { 'beep': { 'name': 'beep', 'a': 1 }, 'boop': { 'name': 'boop', 'b': 2 } }
*/
declare function keyBy( collection: Collection, fcn: Function, thisArg?: any ): any; // tslint-disable-line max-line-length
declare function keyBy<T, V>( collection: Collection<V>, fcn: Callback<T, V>, thisArg?: ThisParameterType<Callback<T, V>> ): Record<string, V>; // tslint-disable-line max-line-length


// EXPORTS //
Expand Down
14 changes: 12 additions & 2 deletions key-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ const toKey = ( value: Foo ) => {
return value.name;
};

const funcs = {
'count': 0,
'toKey': function toKey( this: { count: number; }, value: Foo ): string {
this.count += 1;
return value.name;
}
};


// TESTS //

// The function returns an object...
{
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey ); // $ExpectType any
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey, {} ); // $ExpectType any
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey, {} ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], funcs.toKey, { 'count': 0 } ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
Expand Down
11 changes: 7 additions & 4 deletions omit-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Nullary = () => boolean;
* @param key - iteration key
* @returns boolean indicating whether an iterated property passes a test
*/
type Unary = ( key: any ) => boolean;
type Unary<K> = ( key: K ) => boolean;

/**
* Checks whether an iterated property passes a test.
Expand All @@ -40,7 +40,7 @@ type Unary = ( key: any ) => boolean;
* @param value - property value
* @returns boolean indicating whether an iterated property passes a test
*/
type Binary = ( key: any, value: any ) => boolean;
type Binary<K, V> = ( key: K, value: V ) => boolean;

/**
* Checks whether an iterated value passes a test.
Expand All @@ -49,7 +49,7 @@ type Binary = ( key: any, value: any ) => boolean;
* @param value - property value
* @returns boolean indicating whether an iterated property passes a test
*/
type Predicate = Nullary | Unary | Binary;
type Predicate<K, V> = Nullary | Unary<K> | Binary<K, V>;

/**
* Returns a partial object copy excluding properties for which a predicate returns a truthy value.
Expand All @@ -76,7 +76,10 @@ type Predicate = Nullary | Unary | Binary;
* var obj2 = omitBy( obj1, predicate );
* // returns { 'a': 1 }
*/
declare function omitBy( obj: any, predicate: Predicate ): Object;
declare function omitBy<T extends object>(
obj: T,
predicate: Predicate<keyof T, T[keyof T]>
): Partial<T>;


// EXPORTS //
Expand Down
3 changes: 2 additions & 1 deletion omit-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import omitBy = require( './index' );

// The function returns an object...
{
omitBy( { 'a': 1, 'b': 2 }, ( _: number, value: number ): boolean => value > 1 ); // $ExpectType Object
omitBy( { 'a': 1, 'b': 2 }, ( _: string, value: number ): boolean => value > 1 ); // $ExpectType Partial<{ a: number; b: number; }>
omitBy( { 'a': true, 'b': 3, 'c': 'foo' }, ( _: string, value: string | number | boolean ): boolean => !!value ); // $ExpectType Partial<{ a: boolean; b: number; c: string; }>
}

// The compiler throws an error if the function is provided a second argument which is not a predicate function...
Expand Down
10 changes: 5 additions & 5 deletions pick-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

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

/**
* Checks whether an iterated property passes a test.
Expand All @@ -31,7 +31,7 @@ type Nullary = () => boolean;
* @param key - iteration key
* @returns boolean indicating whether an iterated property passes a test
*/
type Unary = ( key: any ) => boolean;
type Unary<T = unknown> = ( key: T ) => boolean;

/**
* Checks whether an iterated property passes a test.
Expand All @@ -40,7 +40,7 @@ type Unary = ( key: any ) => boolean;
* @param value - property value
* @returns boolean indicating whether an iterated property passes a test
*/
type Binary = ( key: any, value: any ) => boolean;
type Binary<T = unknown, U = unknown> = ( key: T, value: U ) => boolean;

/**
* Checks whether an iterated value passes a test.
Expand All @@ -49,7 +49,7 @@ type Binary = ( key: any, value: any ) => boolean;
* @param value - property value
* @returns boolean indicating whether an iterated property passes a test
*/
type Predicate = Nullary | Unary | Binary;
type Predicate<T = unknown, U = unknown> = Nullary | Unary<T> | Binary<T, U>;

/**
* Returns a partial object copy containing properties for which a predicate returns a truthy value.
Expand All @@ -71,7 +71,7 @@ type Predicate = Nullary | Unary | Binary;
* var obj2 = pickBy( obj1, predicate );
* // returns { 'b': 2 }
*/
declare function pickBy( obj: any, predicate: Predicate ): Object;
declare function pickBy<T extends object, K extends keyof T>( obj: T, predicate: Predicate<K, T[K]> ): Partial<T>; // tslint-disable-line max-line-length


// EXPORTS //
Expand Down
3 changes: 2 additions & 1 deletion pick-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import pickBy = require( './index' );

// The function returns an object...
{
pickBy( { 'a': 1, 'b': 2 }, ( _: number, value: number ): boolean => value > 1 ); // $ExpectType Object
pickBy( { 'a': 1, 'b': 2 }, ( _: string, value: number ): boolean => value > 1 ); // $ExpectType Partial<{ a: number; b: number; }>
pickBy( { 'a': 1, 'b': 2 }, () => true ); // $ExpectType Partial<{ a: number; b: number; }>
}

// The compiler throws an error if the function is provided a second argument which is not a predicate function...
Expand Down
4 changes: 2 additions & 2 deletions while/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

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

/**
* Checks whether an iteration number passes a test.
Expand Down Expand Up @@ -51,7 +51,7 @@ type Predicate = ( i: number ) => boolean;
*
* whilst( predicate, beep );
*/
declare function whilst( fcn: Function, predicate: Predicate, thisArg?: any ): void; // tslint:disable-line: max-line-length
declare function whilst<T extends Function>( predicate: Predicate, fcn: T, thisArg?: ThisParameterType<T> ): void; // tslint:disable-line: max-line-length


// EXPORTS //
Expand Down
30 changes: 15 additions & 15 deletions while/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ function noop() {

// The function does not return a value...
{
whilst( noop, predicate ); // $ExpectType void
whilst( noop, predicate, {} ); // $ExpectType void
whilst( predicate, noop ); // $ExpectType void
whilst( predicate, noop, {} ); // $ExpectType void
}

// The compiler throws an error if the function is provided a first argument which is not a function...
{
whilst( 'abc', predicate ); // $ExpectError
whilst( 5, predicate ); // $ExpectError
whilst( true, predicate ); // $ExpectError
whilst( false, predicate ); // $ExpectError
whilst( [], predicate ); // $ExpectError
whilst( {}, predicate ); // $ExpectError
whilst( 'abc', noop ); // $ExpectError
whilst( 5, noop ); // $ExpectError
whilst( true, noop ); // $ExpectError
whilst( false, noop ); // $ExpectError
whilst( [], noop ); // $ExpectError
whilst( {}, noop ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a function...
{
whilst( noop, 'abc' ); // $ExpectError
whilst( noop, 5 ); // $ExpectError
whilst( noop, true ); // $ExpectError
whilst( noop, false ); // $ExpectError
whilst( noop, [] ); // $ExpectError
whilst( noop, {} ); // $ExpectError
whilst( predicate, 'abc' ); // $ExpectError
whilst( predicate, 5 ); // $ExpectError
whilst( predicate, true ); // $ExpectError
whilst( predicate, false ); // $ExpectError
whilst( predicate, [] ); // $ExpectError
whilst( predicate, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided fewer than two arguments...
{
whilst(); // $ExpectError
whilst( noop ); // $ExpectError
whilst( predicate ); // $ExpectError
}

0 comments on commit a52b687

Please sign in to comment.