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 Aug 18, 2023
1 parent 58cbb30 commit 6e8d636
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 48 deletions.
107 changes: 94 additions & 13 deletions bifurcate-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,46 @@

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

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

/**
* Interface defining function options.
* Interface defining base function options.
*/
interface Options {
interface BaseOptions<T> {
/**
* Execution context.
*/
thisArg?: any;
thisArg?: ThisParameterType<Predicate<T>>;
}

/**
* Interface defining function options when returning indices.
*/
interface IndicesOptions<T> extends BaseOptions<T> {
/**
* Specifies that indices should be returned.
*/
returns: 'indices';
}

/**
* Interface defining function options when returning values.
*/
interface ValuesOptions<T> extends BaseOptions<T> {
/**
* Specifies that values should be returned.
*/
returns: 'values';
}

/**
* Interface defining function options when returning indices and values.
*/
interface IndicesAndValuesOptions<T> extends BaseOptions<T> {
/**
* If `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned.
* Specifies that indices and values should be returned.
*/
returns?: 'values' | 'indices' | '*';
returns: '*';
}

/**
Expand All @@ -50,7 +75,7 @@ type Nullary = () => boolean;
* @param value - collection value
* @returns boolean indicating whether an element in a collection should be placed in the first or second group
*/
type Unary = ( value: any ) => boolean;
type Unary<T> = ( value: T ) => boolean;

/**
* Returns a boolean indicating which group an element in an collection belongs to.
Expand All @@ -59,7 +84,7 @@ type Unary = ( value: any ) => boolean;
* @param index - collection index
* @returns boolean indicating whether an element in a collection should be placed in the first or second group
*/
type Binary = ( value: any, index: number ) => boolean;
type Binary<T> = ( value: T, index: number ) => boolean;

/**
* Returns a boolean indicating which group an element in an collection belongs to.
Expand All @@ -68,7 +93,7 @@ type Binary = ( value: any, index: number ) => boolean;
* @param index - collection index
* @returns boolean indicating whether an element in a collection should be placed in the first or second group
*/
type Predicate = Nullary | Unary | Binary;
type Predicate<T> = Nullary | Unary<T> | Binary<T>;

/**
* Splits values into two groups according to a predicate function.
Expand All @@ -84,7 +109,6 @@ type Predicate = Nullary | Unary | Binary;
*
* - If provided an empty collection, the function returns an empty array.
*
*
* @param collection - input collection
* @param predicate - predicate function indicating which group an element in the input collection belongs to
* @returns group results
Expand All @@ -98,7 +122,7 @@ type Predicate = Nullary | Unary | Binary;
* var out = bifurcateBy( arr, predicate );
* // returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
*/
declare function bifurcateBy( collection: Collection, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length
declare function bifurcateBy<T = unknown>( collection: Collection<T>, predicate: Predicate<T> ): [ Array<T>, Array<T> ];

/**
* Splits values into two groups according to a predicate function.
Expand All @@ -114,7 +138,6 @@ declare function bifurcateBy( collection: Collection, predicate: Predicate ): Ar
*
* - If provided an empty collection, the function returns an empty array.
*
*
* @param collection - input collection
* @param options - function options
* @param options.thisArg - execution context
Expand All @@ -133,6 +156,64 @@ declare function bifurcateBy( collection: Collection, predicate: Predicate ): Ar
* };
* var out = bifurcateBy( arr, opts, predicate );
* // returns [ [ 0, 1, 3 ], [ 2 ] ]
*/
declare function bifurcateBy<T = unknown>( collection: Collection<T>, options: IndicesOptions<T>, predicate: Predicate<T> ): [ Array<number>, Array<number> ];

/**
* Splits values into two groups according to a predicate function.
*
* ## Notes
*
* - When invoked, the predicate function is provided two arguments:
*
* - `value`: collection value
* - `index`: collection index
*
* - If a predicate function returns a truthy value, a collection value is placed in the first group; otherwise, a collection value is placed in the second group.
*
* - If provided an empty collection, the function returns an empty array.
*
* @param collection - input collection
* @param options - function options
* @param options.thisArg - execution context
* @param options.returns - if `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned (default: 'values')
* @param predicate - predicate function indicating which group an element in the input collection belongs to
* @returns group results
*
* @example
* function predicate( v ) {
* return v[ 0 ] === 'b';
* }
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
*
* var opts = {
* 'returns': 'values'
* };
* var out = bifurcateBy( arr, opts, predicate );
* // returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
*/
declare function bifurcateBy<T = unknown>( collection: Collection<T>, options: ValuesOptions<T>, predicate: Predicate<T> ): [ Array<T>, Array<T> ];

/**
* Splits values into two groups according to a predicate function.
*
* ## Notes
*
* - When invoked, the predicate function is provided two arguments:
*
* - `value`: collection value
* - `index`: collection index
*
* - If a predicate function returns a truthy value, a collection value is placed in the first group; otherwise, a collection value is placed in the second group.
*
* - If provided an empty collection, the function returns an empty array.
*
* @param collection - input collection
* @param options - function options
* @param options.thisArg - execution context
* @param options.returns - if `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned (default: 'values')
* @param predicate - predicate function indicating which group an element in the input collection belongs to
* @returns group results
*
* @example
* function predicate( v ) {
Expand All @@ -146,7 +227,7 @@ declare function bifurcateBy( collection: Collection, predicate: Predicate ): Ar
* var out = bifurcateBy( arr, opts, predicate );
* // returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
*/
declare function bifurcateBy( collection: Collection, options: Options, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length
declare function bifurcateBy<T = unknown>( collection: Collection<T>, options: IndicesAndValuesOptions<T>, predicate: Predicate<T> ): [ Array<[ number, T ]>, Array<[ number, T ]> ];


// EXPORTS //
Expand Down
21 changes: 17 additions & 4 deletions bifurcate-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@

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

const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
/**
* Predicate function.
*
* @param v - value
* @returns boolean result
*/
function predicate( v: string ): boolean {
return ( v[ 0 ] === 'b' );
}


// TESTS //

// The function returns an array of arrays...
{
bifurcateBy( [ 'beep', 'boop', 'foo', 'bar' ], predicate ); // $ExpectType any[][]
bifurcateBy( [], predicate ); // $ExpectType any[][]
bifurcateBy( [ 'beep', 'boop', 'foo', 'bar' ], predicate ); // $ExpectType [string[], string[]]
bifurcateBy( [], predicate ); // $ExpectType [string[], string[]]

const opts = {
'returns': 'indices' as 'indices'
};
bifurcateBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, predicate ); // $ExpectType any[][]
bifurcateBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, predicate ); // $ExpectType [number[], number[]]
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
Expand All @@ -43,6 +52,7 @@ const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
// The compiler throws an error if the function is provided a last argument which is not a function...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

bifurcateBy( arr, false ); // $ExpectError
bifurcateBy( arr, true ); // $ExpectError
bifurcateBy( arr, 32 ); // $ExpectError
Expand All @@ -61,12 +71,14 @@ const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
// The compiler throws an error if the function is provided an options argument which is not an object...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

bifurcateBy( arr, null, predicate ); // $ExpectError
}

// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

bifurcateBy( arr, { 'returns': '5' }, predicate ); // $ExpectError
bifurcateBy( arr, { 'returns': 123 }, predicate ); // $ExpectError
bifurcateBy( arr, { 'returns': [] }, predicate ); // $ExpectError
Expand All @@ -77,6 +89,7 @@ const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
// The compiler throws an error if the function is provided an invalid number of arguments...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

bifurcateBy(); // $ExpectError
bifurcateBy( arr ); // $ExpectError
bifurcateBy( arr, {}, predicate, 16 ); // $ExpectError
Expand Down
4 changes: 2 additions & 2 deletions count-by/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

When invoked, the indicator function is provided two arguments:

- `value`: collection value
- `index`: collection index
- value: collection value
- index: collection index

The value returned by an indicator function should be a value which can be
serialized as an object key.
Expand Down
28 changes: 19 additions & 9 deletions count-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@

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

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

/**
* Interface defining function options.
*/
interface Options {
interface Options<T> {
/**
* Execution context.
*/
thisArg?: any;
thisArg?: ThisParameterType<Indicator<T>>;
}

/**
* Specifies which group an element in the input collection belongs to.
*
* @returns object key
*/
type Nullary = () => string | symbol;
type Nullary = () => string | symbol | number;

/**
* Specifies which group an element in the input collection belongs to.
*
* @param value - collection value
* @returns object key
*/
type Unary = ( value: any ) => string | symbol;
type Unary<T> = ( value: T ) => string | symbol | number;

/**
* Specifies which group an element in the input collection belongs to.
Expand All @@ -54,7 +54,7 @@ type Unary = ( value: any ) => string | symbol;
* @param index - collection index
* @returns object key
*/
type Binary = ( value: any, index: number ) => string | symbol;
type Binary<T> = ( value: T, index: number ) => string | symbol | number;

/**
* Specifies which group an element in the input collection belongs to.
Expand All @@ -63,7 +63,17 @@ type Binary = ( value: any, index: number ) => string | symbol;
* @param index - collection index
* @returns object key
*/
type Indicator = Nullary | Unary | Binary;
type Indicator<T> = Nullary | Unary<T> | Binary<T>;

/**
* Interface describing returned results.
*/
interface Results<T> {
/**
* Object properties.
*/
[key: string | symbol | number]: T;
}

/**
* Groups values according to an indicator function and returns group counts.
Expand Down Expand Up @@ -92,7 +102,7 @@ type Indicator = Nullary | Unary | Binary;
* var out = countBy( arr, indicator );
* // returns { 'b': 3, 'f': 1 }
*/
declare function countBy( collection: Collection, indicator: Indicator ): any;
declare function countBy<T = unknown>( collection: Collection<T>, indicator: Indicator<T> ): Results<T>;

/**
* Groups values according to an indicator function and returns group counts.
Expand Down Expand Up @@ -123,7 +133,7 @@ declare function countBy( collection: Collection, indicator: Indicator ): any;
* var out = countBy( arr, indicator );
* // returns { 'b': 3, 'f': 1 }
*/
declare function countBy( collection: Collection, options: Options, indicator: Indicator ): any; // tslint-disable-line max-line-length
declare function countBy<T = unknown>( collection: Collection<T>, options: Options<T>, indicator: Indicator<T> ): Results<T>;


// EXPORTS //
Expand Down
20 changes: 16 additions & 4 deletions count-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@

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

const indicator = ( v: string ): string => v[ 0 ];
/**
* Indicator function.
*
* @param v - value
* @returns indicator
*/
function indicator( v: string ): string {
return v[ 0 ];
}


// TESTS //

// The function returns an object...
{
countBy( [ 'beep', 'boop', 'foo', 'bar' ], indicator ); // $ExpectType any
countBy( [], indicator ); // $ExpectType any
countBy( [ 'beep', 'boop', 'foo', 'bar' ], indicator ); // $ExpectType Results<string>
countBy( [], indicator ); // $ExpectType Results<string>

const opts = {
'thisArg': {}
};
countBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, indicator ); // $ExpectType any
countBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, indicator ); // $ExpectType Results<string>
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
Expand All @@ -43,6 +52,7 @@ const indicator = ( v: string ): string => v[ 0 ];
// The compiler throws an error if the function is provided a last argument which is not a function...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

countBy( arr, false ); // $ExpectError
countBy( arr, true ); // $ExpectError
countBy( arr, 32 ); // $ExpectError
Expand All @@ -61,12 +71,14 @@ const indicator = ( v: string ): string => v[ 0 ];
// The compiler throws an error if the function is provided an options argument which is not an object...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

countBy( arr, null, indicator ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid number of arguments...
{
const arr = [ 'beep', 'boop', 'foo', 'bar' ];

countBy(); // $ExpectError
countBy( arr ); // $ExpectError
countBy( arr, {}, indicator, 16 ); // $ExpectError
Expand Down
Loading

0 comments on commit 6e8d636

Please sign in to comment.