@jesscss/awaitable-pipe 2.0.0-alpha.6 → 2.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # awaitable-pipe
1
+ # @jesscss/awaitable-pipe
2
2
 
3
- ![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)
3
+ A tiny, strongly-typed pipe that stays synchronous until a step returns a Promise — with one optional error handler.
4
4
 
5
- A tiny, zero-dependency pipe with friendly types that “just works”: it stays sync when everything is sync, and turns into a Promise only when something is async. No wrappers, no ceremony.
5
+ It’s a small utility from the [Jess](https://github.com/jesscss/jess) project (Less.js v5, a ground-up rewrite of the Less CSS preprocessor), where hot paths call functions that are usually — but not always — synchronous. It’s zero-dependency and usable on its own: it stays sync when everything is sync, and turns into a Promise only when something is async. No wrappers, no ceremony.
6
6
 
7
7
  - **Stays sync when it can**: all-sync pipelines return a plain value
8
8
  - **Goes async when it must**: any async input/step returns a Promise
@@ -12,12 +12,12 @@ A tiny, zero-dependency pipe with friendly types that “just works”: it stays
12
12
 
13
13
  ## Install
14
14
 
15
- ```bash
16
- pnpm add @jesscss/awaitable-pipe
17
- # or
18
- npm i @jesscss/awaitable-pipe
15
+ ```sh
16
+ npm install @jesscss/awaitable-pipe
19
17
  ```
20
18
 
19
+ Published to npm under both the `latest` and `alpha` dist-tags.
20
+
21
21
  ## Quick Start
22
22
 
23
23
  ```ts
@@ -181,5 +181,16 @@ const sum = await serialReduce(items, 0, async (acc, n, i) => {
181
181
  // sum === 6
182
182
  ```
183
183
 
184
+ ## Status
185
+
186
+ Alpha, as part of the Jess monorepo. Please
187
+ [report issues](https://github.com/jesscss/jess/issues).
188
+
189
+ ## Links
190
+
191
+ - Repository: <https://github.com/jesscss/jess>
192
+ - Documentation: <https://jesscss.github.io/> (currently pre-alpha content)
193
+
184
194
  ## License
185
- MIT
195
+
196
+ [MIT](https://github.com/jesscss/jess/blob/dev/LICENSE)
@@ -0,0 +1,16 @@
1
+ import type { MaybePromise } from './utils.js';
2
+ export { serialForEach, serialReduce } from './utils.js';
3
+ export type StepErrorOptions<TIn, R> = {
4
+ onError?: (error: unknown, input: TIn) => void;
5
+ fallback?: R | ((error: unknown, input: TIn) => R);
6
+ rethrow?: boolean;
7
+ };
8
+ export declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options: StepErrorOptions<TIn, R> & {
9
+ rethrow: true;
10
+ }): (input: TIn) => MaybePromise<R>;
11
+ export declare function tryStep<R>(fn: () => MaybePromise<R>, options: StepErrorOptions<undefined, R> & {
12
+ rethrow: true;
13
+ }): () => MaybePromise<R>;
14
+ export declare function tryStep<R>(fn: () => MaybePromise<R>, options?: StepErrorOptions<undefined, R | undefined>): () => MaybePromise<R | undefined>;
15
+ export declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options?: StepErrorOptions<TIn, R | undefined>): (input: TIn) => MaybePromise<R | undefined>;
16
+ export declare function guard<T>(predicate: (value: T) => MaybePromise<boolean>, errorFactory?: (value: T) => unknown): (value: T) => MaybePromise<T>;
package/lib/index.cjs CHANGED
@@ -142,8 +142,11 @@ function safePipe(...args) {
142
142
  }
143
143
  //#endregion
144
144
  //#region src/helpers.ts
145
+ function isStepFallbackFactory(fallback) {
146
+ return typeof fallback === "function";
147
+ }
145
148
  function resolveStepFallback(fallback, error, input) {
146
- return typeof fallback === "function" ? fallback(error, input) : fallback;
149
+ return isStepFallbackFactory(fallback) ? fallback(error, input) : fallback;
147
150
  }
148
151
  function isNoArgFunction(fn) {
149
152
  return fn.length === 0;
package/lib/index.d.ts CHANGED
@@ -1,63 +1,5 @@
1
- //#region src/pipe.d.ts
2
- type Unwrap<T> = T extends Promise<infer U> ? U : T;
3
- type RetOf<F> = F extends ((a: any) => infer R) ? R : never;
4
- type ParamOf<F> = F extends ((...args: infer P) => any) ? (P extends [infer A, ...any[]] ? A : never) : never;
5
- type Apply<In, F> = [ParamOf<F>] extends [never] ? RetOf<F> : In extends Promise<any> ? Promise<Awaited<RetOf<F>>> : RetOf<F>;
6
- type PipeResult<In, Fns extends any[], Acc = In> = Fns extends [infer F, ...infer Rest] ? PipeResult<Apply<Acc, F>, Rest> : Acc;
7
- declare function pipe<A, R1>(fn1: () => A, fn2: (a: Unwrap<A>) => R1): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1]>;
8
- declare function pipe<A, R1>(fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]>;
9
- declare function pipe<A, R1, R2>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2]>;
10
- declare function pipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
11
- declare function pipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
12
- declare function pipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
13
- declare function pipe<T, R1>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1): PipeResult<T, [(a: Unwrap<T>) => R1]>;
14
- declare function pipe<T, R1, R2>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2]>;
15
- declare function pipe<T, R1, R2, R3>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
16
- declare function pipe<T, R1, R2, R3, R4>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
17
- declare function pipe<T, R1, R2, R3, R4, R5>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4, fn5: (e: Unwrap<R4>) => R5): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
18
- declare function pipe<A>(input: A | Promise<A> | (() => A) | (() => Promise<A>), ...fns: Array<(a: A) => A | Promise<A>>): A | Promise<A>;
19
- type SafePipeOptions<R = unknown> = {
20
- onError?: (error: unknown) => void;
21
- fallback?: R | (() => R);
22
- };
23
- declare function safePipe<A, R1>(options: SafePipeOptions<any>, fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
24
- declare function safePipe<A, R1, R2>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
25
- declare function safePipe<A, R1, R2, R3>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
26
- declare function safePipe<A, R1>(fn1: () => A, fn2: (a: A) => R1): PipeResult<undefined, [() => A, (a: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
27
- declare function safePipe<A, R1, R2>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
28
- declare function safePipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
29
- declare function safePipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]> extends Promise<any> ? Promise<R4 | undefined> : R4 | undefined;
30
- declare function safePipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]> extends Promise<any> ? Promise<R5 | undefined> : R5 | undefined;
31
- //#endregion
32
- //#region src/utils.d.ts
33
- type MaybePromise<T> = T | Promise<T>;
34
- declare function isThenable(x: unknown): x is Promise<unknown>;
35
- declare function isPromise<T = unknown>(x: unknown): x is Promise<T>;
36
- /**
37
- * Serial forEach over an array where the step may return a Promise.
38
- * Runs synchronously until an async step is encountered, then switches to async for the remainder.
39
- */
40
- declare function serialForEach<T>(items: readonly T[], step: (item: T, index: number) => MaybePromise<void | undefined>): MaybePromise<void | undefined>;
41
- /**
42
- * Serial reduce over an array where the step may return a Promise.
43
- * Runs synchronously until an async step is encountered, then switches to async for the remainder.
44
- */
45
- declare function serialReduce<T, A>(items: readonly T[], seed: A, step: (acc: A, item: T, index: number) => MaybePromise<A>): MaybePromise<A>;
46
- //#endregion
47
- //#region src/helpers.d.ts
48
- type StepErrorOptions<TIn, R> = {
49
- onError?: (error: unknown, input: TIn) => void;
50
- fallback?: R | ((error: unknown, input: TIn) => R);
51
- rethrow?: boolean;
52
- };
53
- declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options: StepErrorOptions<TIn, R> & {
54
- rethrow: true;
55
- }): (input: TIn) => MaybePromise<R>;
56
- declare function tryStep<R>(fn: () => MaybePromise<R>, options: StepErrorOptions<undefined, R> & {
57
- rethrow: true;
58
- }): () => MaybePromise<R>;
59
- declare function tryStep<R>(fn: () => MaybePromise<R>, options?: StepErrorOptions<undefined, R | undefined>): () => MaybePromise<R | undefined>;
60
- declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options?: StepErrorOptions<TIn, R | undefined>): (input: TIn) => MaybePromise<R | undefined>;
61
- declare function guard<T>(predicate: (value: T) => MaybePromise<boolean>, errorFactory?: (value: T) => unknown): (value: T) => MaybePromise<T>;
62
- //#endregion
63
- export { type MaybePromise, type SafePipeOptions, guard, isPromise, isThenable, pipe, safePipe, serialForEach, serialReduce, tryStep };
1
+ export type { SafePipeOptions } from './pipe.js';
2
+ export { pipe, safePipe } from './pipe.js';
3
+ export { tryStep, guard, serialForEach, serialReduce } from './helpers.js';
4
+ export type { MaybePromise } from './utils.js';
5
+ export { isThenable, isPromise } from './utils.js';
package/lib/index.js CHANGED
@@ -141,8 +141,11 @@ function safePipe(...args) {
141
141
  }
142
142
  //#endregion
143
143
  //#region src/helpers.ts
144
+ function isStepFallbackFactory(fallback) {
145
+ return typeof fallback === "function";
146
+ }
144
147
  function resolveStepFallback(fallback, error, input) {
145
- return typeof fallback === "function" ? fallback(error, input) : fallback;
148
+ return isStepFallbackFactory(fallback) ? fallback(error, input) : fallback;
146
149
  }
147
150
  function isNoArgFunction(fn) {
148
151
  return fn.length === 0;
package/lib/pipe.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ type Unwrap<T> = T extends Promise<infer U> ? U : T;
2
+ type RetOf<F> = F extends (a: any) => infer R ? R : never;
3
+ type ParamOf<F> = F extends (...args: infer P) => any ? (P extends [infer A, ...any[]] ? A : never) : never;
4
+ type Apply<In, F> = [
5
+ ParamOf<F>
6
+ ] extends [never] ? RetOf<F> : In extends Promise<any> ? Promise<Awaited<RetOf<F>>> : RetOf<F>;
7
+ type PipeResult<In, Fns extends any[], Acc = In> = Fns extends [infer F, ...infer Rest] ? PipeResult<Apply<Acc, F>, Rest> : Acc;
8
+ export declare function pipe<A, R1>(fn1: () => A, fn2: (a: Unwrap<A>) => R1): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1]>;
9
+ export declare function pipe<A, R1>(fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]>;
10
+ export declare function pipe<A, R1, R2>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2]>;
11
+ export declare function pipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
12
+ export declare function pipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
13
+ export declare function pipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
14
+ export declare function pipe<T, R1>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1): PipeResult<T, [(a: Unwrap<T>) => R1]>;
15
+ export declare function pipe<T, R1, R2>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2]>;
16
+ export declare function pipe<T, R1, R2, R3>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
17
+ export declare function pipe<T, R1, R2, R3, R4>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
18
+ export declare function pipe<T, R1, R2, R3, R4, R5>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4, fn5: (e: Unwrap<R4>) => R5): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
19
+ export declare function pipe<A>(input: A | Promise<A> | (() => A) | (() => Promise<A>), ...fns: Array<(a: A) => A | Promise<A>>): A | Promise<A>;
20
+ export type SafePipeOptions<R = unknown> = {
21
+ onError?: (error: unknown) => void;
22
+ fallback?: R | (() => R);
23
+ };
24
+ export declare function safePipe<A, R1>(options: SafePipeOptions<any>, fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
25
+ export declare function safePipe<A, R1, R2>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
26
+ export declare function safePipe<A, R1, R2, R3>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
27
+ export declare function safePipe<A, R1>(fn1: () => A, fn2: (a: A) => R1): PipeResult<undefined, [() => A, (a: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
28
+ export declare function safePipe<A, R1, R2>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
29
+ export declare function safePipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
30
+ export declare function safePipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]> extends Promise<any> ? Promise<R4 | undefined> : R4 | undefined;
31
+ export declare function safePipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]> extends Promise<any> ? Promise<R5 | undefined> : R5 | undefined;
32
+ export {};
package/lib/utils.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export type MaybePromise<T> = T | Promise<T>;
2
+ export declare function isThenable<T = unknown>(x: T | Promise<T>): x is Promise<T>;
3
+ export declare function isThenable<T = unknown>(x: unknown): x is Promise<T>;
4
+ export declare function isPromise<T = unknown>(x: unknown): x is Promise<T>;
5
+ /**
6
+ * Serial forEach over an array where the step may return a Promise.
7
+ * Runs synchronously until an async step is encountered, then switches to async for the remainder.
8
+ */
9
+ export declare function serialForEach<T>(items: readonly T[], step: (item: T, index: number) => MaybePromise<void | undefined>): MaybePromise<void | undefined>;
10
+ /**
11
+ * Serial reduce over an array where the step may return a Promise.
12
+ * Runs synchronously until an async step is encountered, then switches to async for the remainder.
13
+ */
14
+ export declare function serialReduce<T, A>(items: readonly T[], seed: A, step: (acc: A, item: T, index: number) => MaybePromise<A>): MaybePromise<A>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jesscss/awaitable-pipe",
3
- "version": "2.0.0-alpha.6",
3
+ "version": "2.0.0-alpha.8",
4
4
  "description": "A tiny, strongly-typed pipe that stays sync when possible and becomes a Promise when needed. With an optional single-point error handler.",
5
5
  "type": "module",
6
6
  "main": "lib/index.cjs",
@@ -8,7 +8,6 @@
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./lib/index.d.ts",
11
- "source": "./src/index.ts",
12
11
  "import": "./lib/index.js",
13
12
  "require": "./lib/index.cjs"
14
13
  },
@@ -47,6 +46,6 @@
47
46
  "test": "vitest run",
48
47
  "test:types": "tsc -p tsconfig.json --noEmit",
49
48
  "test:watch": "vitest",
50
- "compile": "tsdown --tsconfig tsconfig.build.json"
49
+ "compile": "tsdown --tsconfig tsconfig.build.json --no-dts && tsc -p tsconfig.build.json --emitDeclarationOnly --noCheck"
51
50
  }
52
51
  }
package/lib/index.d.cts DELETED
@@ -1,63 +0,0 @@
1
- //#region src/pipe.d.ts
2
- type Unwrap<T> = T extends Promise<infer U> ? U : T;
3
- type RetOf<F> = F extends ((a: any) => infer R) ? R : never;
4
- type ParamOf<F> = F extends ((...args: infer P) => any) ? (P extends [infer A, ...any[]] ? A : never) : never;
5
- type Apply<In, F> = [ParamOf<F>] extends [never] ? RetOf<F> : In extends Promise<any> ? Promise<Awaited<RetOf<F>>> : RetOf<F>;
6
- type PipeResult<In, Fns extends any[], Acc = In> = Fns extends [infer F, ...infer Rest] ? PipeResult<Apply<Acc, F>, Rest> : Acc;
7
- declare function pipe<A, R1>(fn1: () => A, fn2: (a: Unwrap<A>) => R1): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1]>;
8
- declare function pipe<A, R1>(fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]>;
9
- declare function pipe<A, R1, R2>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2]>;
10
- declare function pipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
11
- declare function pipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
12
- declare function pipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: Unwrap<A>) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: Unwrap<A>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
13
- declare function pipe<T, R1>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1): PipeResult<T, [(a: Unwrap<T>) => R1]>;
14
- declare function pipe<T, R1, R2>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2]>;
15
- declare function pipe<T, R1, R2, R3>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]>;
16
- declare function pipe<T, R1, R2, R3, R4>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]>;
17
- declare function pipe<T, R1, R2, R3, R4, R5>(input: T | Promise<T> | (() => T) | (() => Promise<T>), fn1: (a: Unwrap<T>) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3, fn4: (d: Unwrap<R3>) => R4, fn5: (e: Unwrap<R4>) => R5): PipeResult<T, [(a: Unwrap<T>) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]>;
18
- declare function pipe<A>(input: A | Promise<A> | (() => A) | (() => Promise<A>), ...fns: Array<(a: A) => A | Promise<A>>): A | Promise<A>;
19
- type SafePipeOptions<R = unknown> = {
20
- onError?: (error: unknown) => void;
21
- fallback?: R | (() => R);
22
- };
23
- declare function safePipe<A, R1>(options: SafePipeOptions<any>, fn1: (a?: A) => R1): PipeResult<undefined, [(a?: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
24
- declare function safePipe<A, R1, R2>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
25
- declare function safePipe<A, R1, R2, R3>(options: SafePipeOptions<any>, fn1: (a?: A) => R1, fn2: (b: Unwrap<R1>) => R2, fn3: (c: Unwrap<R2>) => R3): PipeResult<undefined, [(a?: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
26
- declare function safePipe<A, R1>(fn1: () => A, fn2: (a: A) => R1): PipeResult<undefined, [() => A, (a: A) => R1]> extends Promise<any> ? Promise<R1 | undefined> : R1 | undefined;
27
- declare function safePipe<A, R1, R2>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2]> extends Promise<any> ? Promise<R2 | undefined> : R2 | undefined;
28
- declare function safePipe<A, R1, R2, R3>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3]> extends Promise<any> ? Promise<R3 | undefined> : R3 | undefined;
29
- declare function safePipe<A, R1, R2, R3, R4>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4]> extends Promise<any> ? Promise<R4 | undefined> : R4 | undefined;
30
- declare function safePipe<A, R1, R2, R3, R4, R5>(fn1: () => A, fn2: (a: A) => R1, fn3: (b: Unwrap<R1>) => R2, fn4: (c: Unwrap<R2>) => R3, fn5: (d: Unwrap<R3>) => R4, fn6: (e: Unwrap<R4>) => R5): PipeResult<undefined, [() => A, (a: A) => R1, (b: Unwrap<R1>) => R2, (c: Unwrap<R2>) => R3, (d: Unwrap<R3>) => R4, (e: Unwrap<R4>) => R5]> extends Promise<any> ? Promise<R5 | undefined> : R5 | undefined;
31
- //#endregion
32
- //#region src/utils.d.ts
33
- type MaybePromise<T> = T | Promise<T>;
34
- declare function isThenable(x: unknown): x is Promise<unknown>;
35
- declare function isPromise<T = unknown>(x: unknown): x is Promise<T>;
36
- /**
37
- * Serial forEach over an array where the step may return a Promise.
38
- * Runs synchronously until an async step is encountered, then switches to async for the remainder.
39
- */
40
- declare function serialForEach<T>(items: readonly T[], step: (item: T, index: number) => MaybePromise<void | undefined>): MaybePromise<void | undefined>;
41
- /**
42
- * Serial reduce over an array where the step may return a Promise.
43
- * Runs synchronously until an async step is encountered, then switches to async for the remainder.
44
- */
45
- declare function serialReduce<T, A>(items: readonly T[], seed: A, step: (acc: A, item: T, index: number) => MaybePromise<A>): MaybePromise<A>;
46
- //#endregion
47
- //#region src/helpers.d.ts
48
- type StepErrorOptions<TIn, R> = {
49
- onError?: (error: unknown, input: TIn) => void;
50
- fallback?: R | ((error: unknown, input: TIn) => R);
51
- rethrow?: boolean;
52
- };
53
- declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options: StepErrorOptions<TIn, R> & {
54
- rethrow: true;
55
- }): (input: TIn) => MaybePromise<R>;
56
- declare function tryStep<R>(fn: () => MaybePromise<R>, options: StepErrorOptions<undefined, R> & {
57
- rethrow: true;
58
- }): () => MaybePromise<R>;
59
- declare function tryStep<R>(fn: () => MaybePromise<R>, options?: StepErrorOptions<undefined, R | undefined>): () => MaybePromise<R | undefined>;
60
- declare function tryStep<TIn, R>(fn: (input: TIn) => MaybePromise<R>, options?: StepErrorOptions<TIn, R | undefined>): (input: TIn) => MaybePromise<R | undefined>;
61
- declare function guard<T>(predicate: (value: T) => MaybePromise<boolean>, errorFactory?: (value: T) => unknown): (value: T) => MaybePromise<T>;
62
- //#endregion
63
- export { type MaybePromise, type SafePipeOptions, guard, isPromise, isThenable, pipe, safePipe, serialForEach, serialReduce, tryStep };