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 19, 2023
1 parent 6e8d636 commit 3c46abc
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bifurcate-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ declare function bifurcateBy<T = unknown>( collection: Collection<T>, options: I
* 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> ];
declare function bifurcateBy<T = unknown>( collection: Collection<T>, options: ValuesOptions<T> | BaseOptions<T>, predicate: Predicate<T> ): [ Array<T>, Array<T> ];

/**
* Splits values into two groups according to a predicate function.
Expand Down
119 changes: 97 additions & 22 deletions find/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 {
/**
* Limits the number of returned elements.
*/
k?: number;
}

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

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

/**
* Interface defining function options when returning indices and values.
*/
interface IndicesAndValuesOptions extends BaseOptions {
/**
* Specifies that indices and values should be returned.
*/
returns: '*';
}

/**
Expand All @@ -50,7 +75,7 @@ type Nullary = () => boolean;
* @param value - collection value
* @returns boolean indicating whether an element in a collection passes a test
*/
type Unary = ( value: any ) => boolean;
type Unary<T> = ( value: T ) => boolean;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -59,7 +84,7 @@ type Unary = ( value: any ) => boolean;
* @param index - collection index
* @returns boolean indicating whether an element in a collection passes a test
*/
type Binary = ( value: any, index: number ) => boolean;
type Binary<T> = ( value: T, index: number ) => boolean;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -69,7 +94,7 @@ type Binary = ( value: any, index: number ) => boolean;
* @param collection - input collection
* @returns boolean indicating whether an element in a collection passes a test
*/
type Ternary = ( value: any, index: number, collection: Collection ) => boolean; // tslint-disable-line max-line-length
type Ternary<T> = ( value: T, index: number, collection: Collection<T> ) => boolean;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -79,34 +104,72 @@ type Ternary = ( value: any, index: number, collection: Collection ) => boolean;
* @param collection - input collection
* @returns boolean indicating whether an element in a collection passes a test
*/
type Callback = Nullary | Unary | Binary | Ternary;
type Callback<T> = Nullary | Unary<T> | Binary<T> | Ternary<T>;

/**
* Finds elements in an array-like object that satisfy a test condition.
*
* @param arr - object from which elements will be tested
* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition.
* @returns array of indices, element values, or arrays of index-value pairs
* @returns array of indices
*
* @example
* function condition( val ) {
* return val > 20;
* }
*
* var data = [ 30, 20, 50, 60, 10 ];
* var vals = find( data, condition );
* // returns [ 0, 2, 3 ]
*
* @example
* function condition( val ) {
* return val > 20;
* return val > 1000;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var vals = find( data, condition );
* // returns []
*/
declare function find<T = unknown>( arr: Collection<T> | string, clbk: Callback<T> ): Array<number>;

/**
* Finds elements in an array-like object that satisfy a test condition.
*
* @param arr - object from which elements will be tested
* @param options - function options
* @param options.k - limits the number of returned elements (default: arr.length)
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'indices')
* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition.
* @returns array of indices
*
* @example
* function condition( val ) {
* return val > 1000;
* return val > 20;
* }
*
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': 2,
* 'returns': 'indices'
* };
* var vals = find( data, opts, condition );
* // returns [ 0, 2 ]
*
* @example
* function condition( val ) {
* return val > 20;
* }
*
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': -2,
* 'returns': 'indices'
* };
* var vals = find( data, opts, condition );
* // returns [ 3, 2 ]
*/
declare function find( arr: Collection, clbk: Callback ): Array<any> | Array<[ number, any ]>; // tslint-disable-line max-line-length
declare function find<T = unknown>( arr: Collection<T> | string, options: IndicesOptions | BaseOptions, clbk: Callback<T> ): Array<number>;

/**
* Finds elements in an array-like object that satisfy a test condition.
Expand All @@ -116,9 +179,13 @@ declare function find( arr: Collection, clbk: Callback ): Array<any> | Array<[ n
* @param options.k - limits the number of returned elements (default: arr.length)
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'indices')
* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition.
* @returns array of indices, element values, or arrays of index-value pairs
* @returns array of values
*
* @example
* function condition( val ) {
* return val > 20;
* }
*
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': 2,
Expand All @@ -127,37 +194,45 @@ declare function find( arr: Collection, clbk: Callback ): Array<any> | Array<[ n
* var vals = find( data, opts, condition );
* // returns [ 30, 50 ]
*
* @example
* function condition( val ) {
* return val > 20;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': -2,
* 'returns': 'values'
* };
* var vals = find( data, opts, condition );
* // returns [ 60, 50 ]
*/
declare function find<T = unknown>( arr: Collection<T> | string, options: ValuesOptions, clbk: Callback<T> ): Array<T>;

/**
* Finds elements in an array-like object that satisfy a test condition.
*
* @param arr - object from which elements will be tested
* @param options - function options
* @param options.k - limits the number of returned elements (default: arr.length)
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'indices')
* @param clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition.
* @returns array of index-value pairs
*
* @example
* function condition( val ) {
* return val > 20;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': -2,
* 'returns': '*'
* };
* var vals = find( data, opts, condition );
* // returns [ [3, 60], [2, 50] ]
*
* function condition( val ) {
* return val > 20;
* }
*/
declare function find( arr: Collection | string, options: Options, clbk: Callback ): Array<any> | Array<[ number, any ]>; // tslint-disable-line max-line-length
declare function find<T = unknown>( arr: Collection<T> | string, options: IndicesAndValuesOptions, clbk: Callback<T> ): Array<[ number, T ]>;


// EXPORTS //
Expand Down
33 changes: 29 additions & 4 deletions find/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,39 @@

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

const condition = ( val: number ): boolean => val > 20;
/**
* Condition function.
*
* @param v - value
* @returns boolean result
*/
function condition( v: number ): boolean {
return ( v > 20 );
}


// TESTS //

// The function returns an array of indices, element values, or arrays of index-value pairs...
{
const arr = [ 30, 20, 50, 60, 10 ];
find( arr, condition ); // $ExpectType any[] | [number, any][]
const opts = {

find( arr, condition ); // $ExpectType number[]

const opts1 = {
'returns': 'indices' as 'indices'
};
find( arr, opts, condition ); // $ExpectType any[] | [number, any][]
find( arr, opts1, condition ); // $ExpectType number[]

const opts2 = {
'returns': 'values' as 'values'
};
find( arr, opts2, condition ); // $ExpectType number[]

const opts3 = {
'returns': '*' as '*'
};
find( arr, opts3, condition ); // $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 +63,7 @@ const condition = ( val: number ): boolean => val > 20;
// The compiler throws an error if the function is provided a last argument which is not a function...
{
const arr = [ 30, 20, 50, 60, 10 ];

find( arr, false ); // $ExpectError
find( arr, true ); // $ExpectError
find( arr, 32 ); // $ExpectError
Expand All @@ -61,12 +82,14 @@ const condition = ( val: number ): boolean => val > 20;
// The compiler throws an error if the function is provided an options argument which is not an object...
{
const arr = [ 30, 20, 50, 60, 10 ];

find( arr, null, condition ); // $ExpectError
}

// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
{
const arr = [ 30, 20, 50, 60, 10 ];

find( arr, { 'returns': '5' }, condition ); // $ExpectError
find( arr, { 'returns': 123 }, condition ); // $ExpectError
find( arr, { 'returns': [] }, condition ); // $ExpectError
Expand All @@ -77,6 +100,7 @@ const condition = ( val: number ): boolean => val > 20;
// The compiler throws an error if the function is provided a `k` option which is not a number...
{
const arr = [ 30, 20, 50, 60, 10 ];

find( arr, { 'k': '5' }, condition ); // $ExpectError
find( arr, { 'k': false }, condition ); // $ExpectError
find( arr, { 'k': true }, condition ); // $ExpectError
Expand All @@ -88,6 +112,7 @@ const condition = ( val: number ): boolean => val > 20;
// The compiler throws an error if the function is provided an invalid number of arguments...
{
const arr = [ 30, 20, 50, 60, 10 ];

find(); // $ExpectError
find( arr ); // $ExpectError
find( arr, {}, condition, 16 ); // $ExpectError
Expand Down
2 changes: 1 addition & 1 deletion group-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ declare function groupBy<T = unknown>( collection: Collection<T>, options: Indic
* var out = groupBy( arr, opts, indicator );
* // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
*/
declare function groupBy<T = unknown>( collection: Collection<T>, options: ValuesOptions<T>, indicator: Indicator<T> ): ValuesResults<T>;
declare function groupBy<T = unknown>( collection: Collection<T>, options: ValuesOptions<T> | BaseOptions<T>, indicator: Indicator<T> ): ValuesResults<T>;

/**
* Groups values according to an indicator function.
Expand Down
6 changes: 3 additions & 3 deletions tabulate-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

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

/**
* Interface defining function options.
Expand Down Expand Up @@ -103,7 +103,7 @@ type Indicator<T> = Nullary | Unary<T> | Binary<T>;
* var out = tabulateBy( arr, indicator );
* // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ]
*/
declare function tabulateBy<T = any>( collection: Collection<T>, indicator: Indicator<T> ): Array<TableEntry<T>>; // tslint-disable-line max-line-length
declare function tabulateBy<T = unknown>( collection: Collection<T>, indicator: Indicator<T> ): Array<TableEntry<T>>;

/**
* Generates a frequency table according to a provided function.
Expand Down Expand Up @@ -143,7 +143,7 @@ declare function tabulateBy<T = any>( collection: Collection<T>, indicator: Indi
* var out = tabulateBy( arr, opts, indicator );
* // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ]
*/
declare function tabulateBy<T = any>( collection: Collection<T>, options: Options, indicator: Indicator<T> ): Array<TableEntry<T>>; // tslint-disable-line max-line-length
declare function tabulateBy<T = unknown>( collection: Collection<T>, options: Options, indicator: Indicator<T> ): Array<TableEntry<T>>;


// EXPORTS //
Expand Down

0 comments on commit 3c46abc

Please sign in to comment.