Skip to content

Commit

Permalink
Added embedded docs for main modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Sep 17, 2024
1 parent bfb8451 commit 869cd1c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/asStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import {Arg0, Ret} from './defs';

export = asStream;

/**
* Wraps a function in a duplex stream
* @param fn function to wrap
* @param options options for the wrapping duplex stream
* @returns a duplex stream
*/
declare function asStream<F extends (chunk: any, encoding?: string) => unknown>(
fn: F,
options?: DuplexOptions
Expand Down
11 changes: 11 additions & 0 deletions src/fun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import type {FnList} from './gen';

export = fun;

/**
* Returns a wrapped identity function. Rarely used.
*/
declare function fun(): (arg: any) => Promise<Many<any>>;

/**
* Returns a function that applies the given functions in sequence wrapping them as
* an asynchronous function.
* @param fns functions to be wrapped
* @returns an asynchronous function
* @remark It collects values and return them as a {@link Many}.
*/
declare function fun<L extends unknown[]>(
...fns: FnList<Arg0<L>, L>
): AsFlatList<L> extends readonly [Fn, ...Fn[]]
Expand Down
17 changes: 17 additions & 0 deletions src/gen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type {Arg0, Ret, AsFlatList, Fn} from './defs';

export = gen;

/**
* Returns a type, which was expected from a list item.
* It is used to highlight mismatches between argument types and return types in a list.
*/
export type FnItem<I, F> = F extends readonly [infer F1, ...infer R]
? F1 extends (null | undefined)
? readonly [F1, ...FnList<I, R>]
Expand All @@ -16,13 +20,26 @@ export type FnItem<I, F> = F extends readonly [infer F1, ...infer R]
? F
: never;

/**
* Replicates a tuple verifying the types of the list items so arguments match returns.
* The replicated tuple is used to highlight mismatches between list items.
*/
export type FnList<I, L> = L extends readonly [infer F1, ...infer R]
? F1 extends (null | undefined)
? readonly [F1, ...FnList<I, R>]
: readonly [FnItem<I, F1>, ...FnList<Ret<F1, I>, R>]
: L;

/**
* Returns a wrapped identity function. Rarely used.
*/
declare function gen(): (arg: any) => AsyncGenerator<any, void, unknown>;
/**
* Returns a function that applies the given functions in sequence wrapping them as
* an asynchronous generator.
* @param fns functions to be wrapped
* @returns an asynchronous generator
*/
declare function gen<L extends readonly unknown[]>(
...fns: FnList<Arg0<L>, L>
): AsFlatList<L> extends readonly [Fn, ...Fn[]]
Expand Down
46 changes: 46 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,52 @@ import asStream from './asStream';

export = chain;

/**
* Represents a typed duplex stream as a pair of readable and writable streams.
*/
export type DuplexStream<W = any, R = any> = {
readable: ReadableStream<R>;
writable: WritableStream<W>;
};

/**
* Options for the chain function, which is based on `DuplexOptions`.
*/
export interface ChainOptions extends DuplexOptions {
/** If `true`, no groupings will be done. Each function will be a separate stream object. */
noGroupings?: boolean;
/** If `true`, event bindings to the chain stream object will be skipped. */
skipEvents?: boolean;
}

/**
* The tuple type for a chain function with one item.
*/
type ChainSteams1 = [Readable | Writable | Duplex | Transform];
/**
* The tuple type for a chain function with multiple items.
*/
type ChainSteams = [
Readable | Duplex | Transform,
...(Duplex | Transform)[],
Writable | Duplex | Transform
];

/**
* Represents the output of the chain function. It is based on `Duplex` with extra properties.
*/
export interface ChainOutput<W, R> extends Duplex {
/** Internal list of streams. */
streams: ChainSteams1 | ChainSteams;
/** The first stream, which can be used to feed the chain and to attach event handlers. */
input: Readable | Writable | Duplex | Transform;
/** The last stream, which can be used to consume results and to attach event handlers. */
output: Readable | Writable | Duplex | Transform;
}

/**
* Returns the first argument of a chain, a stream, or a function.
*/
export type Arg0<F> = F extends TypedTransform<infer W, any>
? W
: F extends TypedDuplex<infer W, any>
Expand Down Expand Up @@ -87,6 +110,9 @@ export type Arg0<F> = F extends TypedTransform<infer W, any>
? Parameters<F>[0]
: never;

/**
* Returns the return type of a chain, a stream, or a function.
*/
export type Ret<F, Default = any> = F extends TypedTransform<any, infer R>
? R
: F extends TypedDuplex<any, infer R>
Expand Down Expand Up @@ -119,6 +145,10 @@ export type Ret<F, Default = any> = F extends TypedTransform<any, infer R>
? OutputType<F>
: never;

/**
* Represents an item in the chain function.
* It is used to highlight mismatches between argument types and return types in a list.
*/
export type ChainItem<I, F> =
F extends TypedTransform<infer W, infer R>
? I extends W
Expand Down Expand Up @@ -170,12 +200,22 @@ export type ChainItem<I, F> =
: (arg: I, ...rest: readonly unknown[]) => ReturnType<F>
: never;

/**
* Replicates a tuple verifying the types of the list items so arguments match returns.
* The replicated tuple is used to highlight mismatches between list items.
*/
export type ChainList<I, L> = L extends readonly [infer F1, ...infer R]
? F1 extends (null | undefined)
? readonly [F1, ...ChainList<I, R>]
: readonly [ChainItem<I, F1>, ...ChainList<Ret<F1, I>, R>]
: L;

/**
* Takes a function or an iterable and returns the underlying function.
* @param fn function or iterable
* @returns the underlying function
* @remarks In the case of a function, it returns the argument. For iterables it returns the function associated with `Symbol.iterator` or `Symbol.asyncIterator`.
*/
declare function dataSource<F>(
fn: F
): F extends AsyncIterable<infer T>
Expand All @@ -186,6 +226,12 @@ declare function dataSource<F>(
? F
: never;

/**
* Creates a stream object out of a list of functions and streams.
* @param fns array of functions, streams, or other arrays
* @returns a duplex stream with additional properties
* @remarks This is the main function of this library.
*/
declare function chain<L extends readonly unknown[]>(
...fns: ChainList<Arg0<L>, L>
): ChainOutput<Arg0<L>, Ret<L>>;
Expand Down
12 changes: 12 additions & 0 deletions src/typed-streams.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import {Duplex, Readable, Transform, Writable} from 'node:stream';

/**
* Technical class to add input/output types to duplex streams.
*/
export declare class TypedDuplex<W = any, R = W> extends Duplex {
__streamTypeR(): R {
return null as R;
Expand All @@ -11,12 +14,18 @@ export declare class TypedDuplex<W = any, R = W> extends Duplex {
}
}

/**
* Technical class to add output type to readable streams.
*/
export declare class TypedReadable<R = any> extends Readable {
__streamTypeR(): R {
return null as R;
}
}

/**
* Technical class to add input/output types to transform streams.
*/
export declare class TypedTransform<W = any, R = W> extends Transform {
__streamTypeR(): R {
return null as R;
Expand All @@ -26,6 +35,9 @@ export declare class TypedTransform<W = any, R = W> extends Transform {
}
}

/**
* Technical class to add input type to writable streams.
*/
export declare class TypedWritable<W = any> extends Writable {
__streamTypeW(): W {
return null as W;
Expand Down

0 comments on commit 869cd1c

Please sign in to comment.