Skip to content

Commit

Permalink
fix: update import path for Collection type definition and use gene…
Browse files Browse the repository at this point in the history
…rics

Ref: bde4671
  • Loading branch information
kgryte committed Sep 10, 2023
1 parent 33fb7ed commit 6b80df1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@

/// <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, V> {
/**
* The maximum number of pending invocations at any one time.
*/
limit?: number;

/**
* Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: false).
* Boolean indicating whether to sequentially invoke the `predicate` function for each `collection` element. If `true`, the function sets `options.limit=1`.
*/
series?: boolean;

/**
* Execution context.
*/
thisArg?: any;
thisArg?: ThisParameterType<Predicate<T, V>>;
}

/**
Expand Down Expand Up @@ -76,7 +76,7 @@ type Callback = Nullary | Unary | Binary;
* @param value - collection value
* @param next - callback which should be called once the predicate function has finished processing a collection value
*/
type BinaryPredicate = ( value: any, next: Callback ) => void;
type BinaryPredicate<T, V> = ( this: V, value: T, next: Callback ) => void;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -85,7 +85,7 @@ type BinaryPredicate = ( value: any, next: Callback ) => void;
* @param index - collection index
* @param next - callback which should be called once the `predicate` function has finished processing a collection `value`
*/
type TernaryPredicate = ( value: any, index: number, next: Callback ) => void;
type TernaryPredicate<T, V> = ( this: V, value: T, index: number, next: Callback ) => void;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -95,7 +95,7 @@ type TernaryPredicate = ( value: any, index: number, next: Callback ) => void;
* @param collection - input collection
* @param next - callback which should be called once the `predicate` function has finished processing a collection `value`
*/
type QuaternaryPredicate = ( value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length
type QuaternaryPredicate<T, V> = ( this: V, value: T, index: number, collection: Collection<T>, next: Callback ) => void;

/**
* Checks whether an element in a collection passes a test.
Expand All @@ -105,16 +105,16 @@ type QuaternaryPredicate = ( value: any, index: number, collection: Collection,
* @param collection - input collection
* @param next - callback which should be called once the `predicate` function has finished processing a collection `value`
*/
type Predicate = BinaryPredicate | TernaryPredicate | QuaternaryPredicate;
type Predicate<T, V> = BinaryPredicate<T, V> | TernaryPredicate<T, V> | QuaternaryPredicate<T, V>;

/**
* Tests whether at least one element in a collection passes a test implemented by a predicate function.
* Tests whether at least `n` elements in a collection pass a test implemented by a predicate function.
*
* @param collection - input collection
* @param n - number of elements
* @param done - function to invoke upon completion
*/
type FactoryFunction = ( collection: Collection, n: number, done: Callback ) => void; // tslint-disable-line max-line-length
type FactoryFunction<T> = ( collection: Collection<T>, n: number, done: Callback ) => void;

/**
* Interface for `someByRightAsync`.
Expand All @@ -128,7 +128,6 @@ interface SomeByRightAsync {
* - If a predicate function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
*
*
* @param collection - input collection
* @param n - number of elements
* @param options - function options
Expand Down Expand Up @@ -175,7 +174,7 @@ interface SomeByRightAsync {
*
* someByRightAsync( files, 2, predicate, done );
*/
( collection: Collection, n: number, options: Options, predicate: Predicate, done: Callback ): void; // tslint-disable-line max-line-length
<T = unknown, V = unknown>( collection: Collection<T>, n: number, options: Options<T, V>, predicate: Predicate<T, V>, done: Callback ): void;

/**
* Tests whether a collection contains at least `n` elements which pass a test implemented by a predicate function, iterating from right to left.
Expand All @@ -185,7 +184,6 @@ interface SomeByRightAsync {
* - If a predicate function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
*
*
* @param collection - input collection
* @param n - number of elements
* @param predicate - predicate function to invoke for each element in a collection
Expand Down Expand Up @@ -228,7 +226,7 @@ interface SomeByRightAsync {
*
* someByRightAsync( files, 2, predicate, done );
*/
( collection: Collection, n: number, predicate: Predicate, done: Callback ): void; // tslint-disable-line max-line-length
<T = unknown, V = unknown>( collection: Collection<T>, n: number, predicate: Predicate<T, V>, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics

/**
* Returns a function for testing whether a collection contains at least `n` elements which pass a test implemented by a predicate function, iterating from right to left.
Expand All @@ -238,7 +236,6 @@ interface SomeByRightAsync {
* - If a predicate function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
*
*
* @param options - function options
* @param options.thisArg - execution context
* @param options.limit - maximum number of pending invocations at any one time
Expand Down Expand Up @@ -292,7 +289,7 @@ interface SomeByRightAsync {
* // Try to read each element in `files`:
* someByRightAsync( files, 2, done );
*/
factory( options: Options, predicate: Predicate ): FactoryFunction;
factory<T = unknown, V = unknown>( options: Options<T, V>, predicate: Predicate<T, V> ): FactoryFunction<T>;

/**
* Returns a function for testing whether a collection contains at least `n` elements which pass a test implemented by a predicate function, iterating from right to left.
Expand All @@ -302,7 +299,6 @@ interface SomeByRightAsync {
* - If a predicate function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
*
*
* @param predicate - predicate function to invoke for each element in a collection
* @throws must provide valid options
* @returns function which invokes the predicate function once for each element in a collection
Expand Down Expand Up @@ -352,7 +348,7 @@ interface SomeByRightAsync {
* // Try to read each element in `files`:
* someByRightAsync( files, 2, done );
*/
factory( predicate: Predicate ): FactoryFunction;
factory<T = unknown, V = unknown>( predicate: Predicate<T, V> ): FactoryFunction<T>; // tslint:disable-line:no-unnecessary-generics
}

/**
Expand All @@ -363,7 +359,6 @@ interface SomeByRightAsync {
* - If a predicate function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
*
*
* @param collection - input collection
* @param n - number of elements
* @param options - function options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ const done = ( error: Error | null, bool: boolean ) => {

// Attached to main export is a `factory` method which returns a function...
{
someByRightAsync.factory( isPositive ); // $ExpectType FactoryFunction
someByRightAsync.factory( { 'series': true }, isPositive ); // $ExpectType FactoryFunction
someByRightAsync.factory( isPositive ); // $ExpectType FactoryFunction<number>
someByRightAsync.factory( { 'series': true }, isPositive ); // $ExpectType FactoryFunction<number>
}

// The compiler throws an error if the `factory` method is provided an options argument which is not an object...
Expand Down

0 comments on commit 6b80df1

Please sign in to comment.