Skip to content

Commit

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

Ref: bde4671
  • Loading branch information
kgryte committed Sep 20, 2023
1 parent 030ddd0 commit 5c93dea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
20 changes: 10 additions & 10 deletions lib/node_modules/@stdlib/utils/async/reduce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var reduceAsync = require( '@stdlib/utils/async/reduce' );

#### reduceAsync( collection, initial, \[options,] reducer, done )

Applies a `function` against an accumulator and each element in a `collection` and returns the accumulated result.
Applies a function against an accumulator and each element in a `collection` and returns the accumulated result.

```javascript
function reducer( acc, value, index, next ) {
Expand Down Expand Up @@ -102,9 +102,9 @@ reduceAsync( arr, acc, reducer, done );

The function accepts the following `options`:

- `limit`: the maximum number of pending invocations at any one time. If provided, the function sets `options.series=false`. Default: `infinity`.
- `series`: `boolean` indicating whether to sequentially invoke `reducer` for each `collection` element. If `true`, the function sets `options.limit=1`. Default: `true`.
- `thisArg`: the execution context for `reducer`.
- **limit**: the maximum number of pending invocations at any one time. If provided, the function sets `options.series=false`. Default: `infinity`.
- **series**: boolean indicating whether to sequentially invoke `reducer` for each `collection` element. If `true`, the function sets `options.limit=1`. Default: `true`.
- **thisArg**: the execution context for `reducer`.

By default, all elements are processed **sequentially**, which means that the function **does** guarantee completion order. To process each `collection` element concurrently, set the `series` option to `false`.

Expand Down Expand Up @@ -224,11 +224,11 @@ function done( error, acc ) {

When invoked, `reducer` is provided a maximum of five arguments:

- `accumulator`: accumulated value.
- `value`: collection value.
- `index`: collection index.
- `collection`: the input `collection`.
- `next`: a callback which should be called once `reducer` has finished processing a collection `value`.
- **accumulator**: accumulated value.
- **value**: collection value.
- **index**: collection index.
- **collection**: the input `collection`.
- **next**: a callback which should be called once `reducer` has finished processing a collection `value`.

The actual number of provided arguments depends on function `length`. If `reducer` accepts three arguments, `reducer` is provided `accumulator`, `value` and `next`. If `reducer` accepts four arguments, `reducer` is provided `accumulator`, `value`, `index`, and `next`. For every other `reducer` signature, `reducer` is provided all five arguments.

Expand Down Expand Up @@ -270,7 +270,7 @@ reduceAsync( arr, acc, reducer, done );

#### reduceAsync.factory( \[options,] reducer )

Returns a `function` which invokes a function once for each element in a `collection`.
Returns a function which invokes a function once for each element in a `collection`.

```javascript
function reducer( acc, value, index, next ) {
Expand Down
43 changes: 19 additions & 24 deletions lib/node_modules/@stdlib/utils/async/reduce/docs/types/index.d.ts
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, U, V> {
/**
* The maximum number of pending invocations at any one time.
* Execution context.
*/
limit?: number;
thisArg?: ThisParameterType<Reducer<T, U, V>>;

/**
* Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true).
* The maximum number of pending invocations at any one time.
*/
series?: boolean;
limit?: number;

/**
* Execution context.
* Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true.
*/
thisArg?: any;
series?: boolean;
}

/**
Expand All @@ -60,15 +60,15 @@ type Unary = ( error: Error | null ) => void;
* @param error - encountered error or null
* @param accumulator - accumulated value
*/
type Binary = ( error: Error | null, accumulator: any ) => void;
type Binary<U> = ( error: Error | null, accumulator: U ) => void;

/**
* Callback function.
*
* @param error - encountered error or null
* @param accumulator - accumulated value
*/
type Callback = Nullary | Unary | Binary;
type Callback<U> = Nullary | Unary | Binary<U>;

/**
* Reducer function invoked for each element in a collection.
Expand All @@ -77,7 +77,7 @@ type Callback = Nullary | Unary | Binary;
* @param value - collection value
* @param next - a callback to be invoked after processing a collection `value`
*/
type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void;
type TernaryReducer<T, U, V> = ( this: V, accumulator: U, value: T, next: Callback<U> ) => void;

/**
* Reducer function invoked for each element in a collection.
Expand All @@ -87,7 +87,7 @@ type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void;
* @param index - collection index
* @param next - a callback to be invoked after processing a collection `value`
*/
type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length
type QuaternaryReducer <T, U, V> = ( this: V, accumulator: U, value: T, index: number, next: Callback<U> ) => void;

/**
* Reducer function invoked for each element in a collection.
Expand All @@ -98,7 +98,7 @@ type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Ca
* @param collection - the input collection
* @param next - a callback to be invoked after processing a collection `value`
*/
type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length
type QuinaryReducer<T, U, V> = ( this: V, accumulator: U, value: T, index: number, collection: Collection<T>, next: Callback<U> ) => void;

/**
* Reducer function invoked for each element in a collection.
Expand All @@ -109,7 +109,7 @@ type QuinaryReducer = ( accumulator: any, value: any, index: number, collection:
* @param collection - the input collection
* @param next - a callback to be invoked after processing a collection `value`
*/
type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer;
type Reducer<T, U, V> = TernaryReducer<T, U, V> | QuaternaryReducer<T, U, V> | QuinaryReducer<T, U, V>;

/**
* Applies a function against an accumulator and each element in a collection and return the accumulated result.
Expand All @@ -118,7 +118,7 @@ type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer;
* @param initial - initial value
* @param done - function to invoke upon completion
*/
type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length
type FactoryFunction<T, U> = ( collection: Collection<T>, initial: U, done: Callback<U> ) => void;

/**
* Interface for `reduceAsync`.
Expand All @@ -132,7 +132,6 @@ interface ReduceAsync {
* - If a provided 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 initial - initial value
* @param options - function options
Expand Down Expand Up @@ -177,7 +176,7 @@ interface ReduceAsync {
* };
* reduceAsync( files, acc, {}, read, done );
*/
( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length
<T = unknown, U = unknown, V = unknown>( collection: Collection<T>, initial: U, options: Options<T, U, V>, reducer: Reducer<T, U, V>, done: Callback<U> ): void;

/**
* Applies a function against an accumulator and each element in a collection and return the accumulated result.
Expand All @@ -187,7 +186,6 @@ interface ReduceAsync {
* - If a provided 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 initial - initial value
* @param reducer - function to invoke for each element in a collection
Expand Down Expand Up @@ -227,7 +225,7 @@ interface ReduceAsync {
* };
* reduceAsync( files, acc, read, done );
*/
( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length
<T = unknown, U = unknown, V = unknown>( collection: Collection<T>, initial: U, reducer: Reducer<T, U, V>, done: Callback<U> ): void; // tslint:disable-line:no-unnecessary-generics

/**
* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.
Expand All @@ -237,7 +235,6 @@ interface ReduceAsync {
* - If a provided 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 @@ -291,7 +288,7 @@ interface ReduceAsync {
* };
* reduceAsync( files, acc, done );
*/
factory( options: Options, reducer: Reducer ): FactoryFunction;
factory<T = unknown, U = unknown, V = unknown>( options: Options<T, U, V>, reducer: Reducer<T, U, V> ): FactoryFunction<T, U>;

/**
* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.
Expand All @@ -301,7 +298,6 @@ interface ReduceAsync {
* - If a provided 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 reducer - function to invoke for each element in a collection
* @throws must provide valid options
* @returns function which invokes the provided function once for each element in a collection
Expand Down Expand Up @@ -347,7 +343,7 @@ interface ReduceAsync {
* };
* reduceAsync( files, acc, done );
*/
factory( reducer: Reducer ): FactoryFunction;
factory<T = unknown, U = unknown, V = unknown>( reducer: Reducer<T, U, V> ): FactoryFunction<T, U>; // tslint:disable-line:no-unnecessary-generics
}

/**
Expand All @@ -358,7 +354,6 @@ interface ReduceAsync {
* - If a provided 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 initial - initial value
* @param options - function options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ const done = ( error: Error | null, acc: any ) => {

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

// 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 5c93dea

Please sign in to comment.