All files / src combine.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 382x                                                       2x 10x 10x 10x   10x 10x 10x 10x  
import { compute } from "./compute";
import { type Config, type OwnedReadable, type ReadableLike } from "./typings";
 
export type MapReadablesToValues<TDepValues extends readonly ReadableLike[]> = {
  [K in keyof TDepValues]: TDepValues[K] extends ReadableLike<infer V> ? V : never;
};
 
export interface Combine {
  /**
   * Combines an array of {@link ReadableLike}s into a single {@link Readable} with the array of values.
   * @param deps An array of {@link ReadableLike}s to combine.
   * @returns A {@link Readable} with the combined values.
   */
  <TDeps extends readonly ReadableLike[] = ReadableLike[]>(deps: TDeps): OwnedReadable<MapReadablesToValues<TDeps>>;
  /**
   * Combines an array of {@link ReadableLike}s into a single {@link Readable} with transformed value.
   * @param deps An array of {@link ReadableLike}s to combine.
   * @param transform A pure function that takes an array of values and returns a new value.
   * @param config custom config for the combined Readable.
   * @returns A {@link Readable} with the transformed values.
   */
  <TDeps extends readonly ReadableLike[] = ReadableLike[], TValue = any>(
    deps: TDeps,
    transform: (deps: MapReadablesToValues<TDeps>) => TValue,
    config?: Config<TValue>,
  ): OwnedReadable<TValue>;
}
 
export const combine: Combine = <TDeps extends readonly ReadableLike[], TValue = any>(
  deps: TDeps,
  transform?: (deps: MapReadablesToValues<TDeps>) => TValue,
  config?: Config<TValue>,
) =>
  compute(
    get => (transform ? transform(deps.map(get) as MapReadablesToValues<TDeps>) : (deps.map(get) as TValue)),
    config,
  );