@nlozgachev/pipelined 0.43.0 → 0.45.0
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 +17 -16
- package/dist/{InternalTypes-DsCqxWZm.d.mts → InternalTypes-CLE7qlOc.d.mts} +33 -28
- package/dist/{InternalTypes-DuzMFAfJ.d.ts → InternalTypes-Mssktd7z.d.ts} +33 -28
- package/dist/{Validation-BOPLiDqa.d.ts → Validation-BMsvixWH.d.ts} +586 -554
- package/dist/{Validation-Do6uWLLZ.d.mts → Validation-v38R0qH-.d.mts} +586 -554
- package/dist/{chunk-W6RWKBDX.mjs → chunk-2LKJF45J.mjs} +160 -68
- package/dist/{chunk-X6XQX3OZ.mjs → chunk-KOYYDQH4.mjs} +2 -2
- package/dist/{chunk-CHRXZIJU.mjs → chunk-VSU36S2K.mjs} +435 -309
- package/dist/{chunk-74JKKJ4R.mjs → chunk-XTVF5R6R.mjs} +8 -5
- package/dist/composition.d.mts +10 -10
- package/dist/composition.d.ts +10 -10
- package/dist/composition.js +9 -6
- package/dist/composition.mjs +2 -2
- package/dist/core.d.mts +453 -402
- package/dist/core.d.ts +453 -402
- package/dist/core.js +445 -313
- package/dist/core.mjs +8 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +605 -381
- package/dist/index.mjs +10 -4
- package/dist/types.d.mts +23 -21
- package/dist/types.d.ts +23 -21
- package/dist/types.js +8 -5
- package/dist/types.mjs +1 -1
- package/dist/utils.d.mts +468 -218
- package/dist/utils.d.ts +468 -218
- package/dist/utils.js +406 -238
- package/dist/utils.mjs +3 -3
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ npm add @nlozgachev/pipelined
|
|
|
17
17
|
In mainstream TypeScript, code is often burdened by implicit control flow: unchecked exceptions,
|
|
18
18
|
manual null propagation, and unhandled asynchronous failures. `pipelined` turns these complex
|
|
19
19
|
runtime states into simple, transparent data structures that compose. By representing optionality as
|
|
20
|
-
`Maybe`, failures as `Result`, lazy asynchronous pipelines as `
|
|
20
|
+
`Maybe`, failures as `Result`, lazy asynchronous pipelines as `Task.Result`, and repeated stateful
|
|
21
21
|
interactions as `Op`, the library helps disentangle business logic from control mechanics.
|
|
22
22
|
|
|
23
23
|
## Documentation
|
|
@@ -55,18 +55,18 @@ Every step that sees `None` is skipped. The fallback runs once, at the end.
|
|
|
55
55
|
## Example: typed async errors
|
|
56
56
|
|
|
57
57
|
In JavaScript, asynchronous exceptions bypass the static type system, leaving unhandled rejections
|
|
58
|
-
as invisible runtime risks. `
|
|
58
|
+
as invisible runtime risks. `Task.Result<E, A>` represents fallible asynchronous computations as
|
|
59
59
|
lazy, infallible tasks that resolve to a typed `Result`. The error type is explicitly tracked in the
|
|
60
60
|
function signature, ensuring that failures are handled before compile time:
|
|
61
61
|
|
|
62
62
|
```ts
|
|
63
63
|
import { pipe } from "@nlozgachev/pipelined/composition";
|
|
64
|
-
import { Result,
|
|
64
|
+
import { Result, Task } from "@nlozgachev/pipelined/core";
|
|
65
65
|
|
|
66
66
|
type ApiError = { status: number; message: string };
|
|
67
67
|
|
|
68
|
-
const fetchUser = (id: string):
|
|
69
|
-
|
|
68
|
+
const fetchUser = (id: string): Task.Result<ApiError, User> =>
|
|
69
|
+
Task.Result.tryCatch(
|
|
70
70
|
(signal) =>
|
|
71
71
|
fetch(`/users/${id}`, { signal }).then((r) => {
|
|
72
72
|
if (!r.ok) throw { status: r.status, message: r.statusText };
|
|
@@ -75,8 +75,8 @@ const fetchUser = (id: string): TaskResult<ApiError, User> =>
|
|
|
75
75
|
(e) => e as ApiError,
|
|
76
76
|
);
|
|
77
77
|
|
|
78
|
-
const fetchPosts = (userId: string):
|
|
79
|
-
|
|
78
|
+
const fetchPosts = (userId: string): Task.Result<ApiError, Post[]> =>
|
|
79
|
+
Task.Result.tryCatch(
|
|
80
80
|
(signal) =>
|
|
81
81
|
fetch(`/users/${userId}/posts`, { signal }).then((r) => r.json()),
|
|
82
82
|
(e) => e as ApiError,
|
|
@@ -86,10 +86,10 @@ const fetchPosts = (userId: string): TaskResult<ApiError, Post[]> =>
|
|
|
86
86
|
const userWithPosts = (id: string) =>
|
|
87
87
|
pipe(
|
|
88
88
|
fetchUser(id),
|
|
89
|
-
|
|
89
|
+
Task.Result.chain((user) =>
|
|
90
90
|
pipe(
|
|
91
91
|
fetchPosts(user.id),
|
|
92
|
-
|
|
92
|
+
Task.Result.map((posts) => ({ ...user, posts })),
|
|
93
93
|
)
|
|
94
94
|
),
|
|
95
95
|
);
|
|
@@ -103,7 +103,7 @@ const controller = new AbortController();
|
|
|
103
103
|
const fetchUserWithPosts = userWithPosts("42"); // build the lazy task
|
|
104
104
|
const result = await fetchUserWithPosts(controller.signal); // run it — signal controls cancellation
|
|
105
105
|
|
|
106
|
-
if (Result.
|
|
106
|
+
if (Result.is.ok(result)) {
|
|
107
107
|
render(result.value); // { ...User, posts: Post[] }
|
|
108
108
|
} else {
|
|
109
109
|
showError(result.error); // ApiError — typed, not unknown
|
|
@@ -140,7 +140,7 @@ const cheapestByCategory = (items: RawItem[]) =>
|
|
|
140
140
|
items,
|
|
141
141
|
Arr.filterMap(normalise), // parse + drop unparseable prices in one pass
|
|
142
142
|
Arr.sortBy((a, b) => a.price - b.price), // ascending price
|
|
143
|
-
Arr.groupBy((item) => item.category), // Record<string,
|
|
143
|
+
Arr.groupBy((item) => item.category), // Record<string, Arr.NonEmpty<Item>>
|
|
144
144
|
Rec.map((group) => Arr.head(group)), // cheapest per category — Maybe<Item>
|
|
145
145
|
);
|
|
146
146
|
```
|
|
@@ -314,7 +314,7 @@ provides a strongly-typed, immutable two-element pair.
|
|
|
314
314
|
### Asynchronous operations
|
|
315
315
|
|
|
316
316
|
`Task` represents a lazy, infallible asynchronous computation. Fallible asynchronous workflows are
|
|
317
|
-
handled by `
|
|
317
|
+
handled by `Task.Result`, `Task.Maybe`, and `Task.Validation`. For managing stateful, recurring
|
|
318
318
|
asynchronous operations with complex scheduling, `Op` implements named concurrency strategies such
|
|
319
319
|
as `restartable`, `exclusive`, `debounced`, `throttled`, and `queue`, handling retries, timeouts,
|
|
320
320
|
and signal propagation automatically. `Deferred` represents a lightweight, infallible asynchronous
|
|
@@ -342,11 +342,12 @@ Functions are composed using `pipe` and `flow`, which are enriched with high-lev
|
|
|
342
342
|
helpers like `when`, `unless`, `either`, `safe`, and `async` to support robust, expressive
|
|
343
343
|
pipelines.
|
|
344
344
|
|
|
345
|
-
### Nominal branding, durations, and non-empty
|
|
345
|
+
### Nominal branding, durations, and non-empty collections
|
|
346
346
|
|
|
347
|
-
Compile-time nominal typing with zero runtime overhead is provided by `Brand
|
|
348
|
-
models and converts time durations (seconds, milliseconds, etc.)
|
|
349
|
-
|
|
347
|
+
Compile-time nominal typing with zero runtime overhead is provided by `Brand`. `Duration` safely
|
|
348
|
+
models and converts time durations (seconds, milliseconds, etc.). `Arr.NonEmpty` and `Rec.NonEmpty`
|
|
349
|
+
guarantee that an array or record is never empty, eliminating defensive length/emptiness checks at
|
|
350
|
+
runtime.
|
|
350
351
|
|
|
351
352
|
Every utility in the library is benchmarked against its native equivalent. The data-last currying
|
|
352
353
|
adds a small function call overhead, which is the expected cost of composability. For operations
|
|
@@ -7,7 +7,7 @@ declare const _deferred: unique symbol;
|
|
|
7
7
|
* Two design choices work together to make the guarantee structural rather than documentary:
|
|
8
8
|
*
|
|
9
9
|
* - The phantom `[_deferred]` symbol makes the type **nominal**: only values produced by
|
|
10
|
-
* `Deferred.
|
|
10
|
+
* `Deferred.from.Promise` satisfy it. A plain object `{ then: ... }` does not.
|
|
11
11
|
* - The single-parameter `.then()` **excludes rejection handlers** by construction. There is
|
|
12
12
|
* no second argument to pass, so chaining and `.catch()` are impossible.
|
|
13
13
|
*
|
|
@@ -16,7 +16,7 @@ declare const _deferred: unique symbol;
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
|
-
* const value = await Deferred.
|
|
19
|
+
* const value = await Deferred.from.Promise(Promise.resolve(42));
|
|
20
20
|
* // value === 42
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
@@ -25,31 +25,35 @@ type Deferred<A> = {
|
|
|
25
25
|
readonly then: (onfulfilled: (value: A) => unknown) => void;
|
|
26
26
|
};
|
|
27
27
|
declare namespace Deferred {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
28
|
+
namespace from {
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a `Promise` or `Deferred` into a `Deferred`, structurally excluding rejection handlers,
|
|
31
|
+
* `.catch()`, `.finally()`, and chainable `.then()`.
|
|
32
|
+
*
|
|
33
|
+
* **Precondition**: `p` must never reject. If `p` rejects, the returned `Deferred` will
|
|
34
|
+
* never resolve — `await`-ing it will hang indefinitely. Use `Task.Result.tryCatch` to
|
|
35
|
+
* handle operations that may fail before converting to a `Deferred`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const d = Deferred.from.Promise(Promise.resolve("hello"));
|
|
40
|
+
* const value = await d; // "hello"
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
const Promise: <A>(p: Thenable<A>) => Deferred<A>;
|
|
44
|
+
}
|
|
45
|
+
namespace to {
|
|
46
|
+
/**
|
|
47
|
+
* Converts a `Deferred` back into a `Promise`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const p = Deferred.to.Promise(Deferred.from.Promise(Promise.resolve(42)));
|
|
52
|
+
* // p is Promise<42>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const Promise: <A>(d: Deferred<A>) => globalThis.Promise<A>;
|
|
56
|
+
}
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
/**
|
|
@@ -114,5 +118,6 @@ type WithCooldown = {
|
|
|
114
118
|
type WithMinInterval = {
|
|
115
119
|
readonly minInterval?: Duration;
|
|
116
120
|
};
|
|
121
|
+
type NonEmpty<T extends string> = `NonEmpty${T}`;
|
|
117
122
|
|
|
118
|
-
export { type Awaitable as A, Deferred as D, type
|
|
123
|
+
export { type Awaitable as A, Deferred as D, type NonEmpty as N, type RetryOptions as R, type Thenable as T, type WithConcurrency as W, type NonEmptyArr as a, type TimeoutOptions as b, type WithCooldown as c, type WithDuration as d, type WithError as e, type WithErrors as f, type WithFirst as g, type WithKind as h, type WithLog as i, type WithMinInterval as j, type WithN as k, type WithSecond as l, type WithSize as m, type WithTimeout as n, type WithValue as o };
|
|
@@ -7,7 +7,7 @@ declare const _deferred: unique symbol;
|
|
|
7
7
|
* Two design choices work together to make the guarantee structural rather than documentary:
|
|
8
8
|
*
|
|
9
9
|
* - The phantom `[_deferred]` symbol makes the type **nominal**: only values produced by
|
|
10
|
-
* `Deferred.
|
|
10
|
+
* `Deferred.from.Promise` satisfy it. A plain object `{ then: ... }` does not.
|
|
11
11
|
* - The single-parameter `.then()` **excludes rejection handlers** by construction. There is
|
|
12
12
|
* no second argument to pass, so chaining and `.catch()` are impossible.
|
|
13
13
|
*
|
|
@@ -16,7 +16,7 @@ declare const _deferred: unique symbol;
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
|
-
* const value = await Deferred.
|
|
19
|
+
* const value = await Deferred.from.Promise(Promise.resolve(42));
|
|
20
20
|
* // value === 42
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
@@ -25,31 +25,35 @@ type Deferred<A> = {
|
|
|
25
25
|
readonly then: (onfulfilled: (value: A) => unknown) => void;
|
|
26
26
|
};
|
|
27
27
|
declare namespace Deferred {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
28
|
+
namespace from {
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a `Promise` or `Deferred` into a `Deferred`, structurally excluding rejection handlers,
|
|
31
|
+
* `.catch()`, `.finally()`, and chainable `.then()`.
|
|
32
|
+
*
|
|
33
|
+
* **Precondition**: `p` must never reject. If `p` rejects, the returned `Deferred` will
|
|
34
|
+
* never resolve — `await`-ing it will hang indefinitely. Use `Task.Result.tryCatch` to
|
|
35
|
+
* handle operations that may fail before converting to a `Deferred`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const d = Deferred.from.Promise(Promise.resolve("hello"));
|
|
40
|
+
* const value = await d; // "hello"
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
const Promise: <A>(p: Thenable<A>) => Deferred<A>;
|
|
44
|
+
}
|
|
45
|
+
namespace to {
|
|
46
|
+
/**
|
|
47
|
+
* Converts a `Deferred` back into a `Promise`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const p = Deferred.to.Promise(Deferred.from.Promise(Promise.resolve(42)));
|
|
52
|
+
* // p is Promise<42>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const Promise: <A>(d: Deferred<A>) => globalThis.Promise<A>;
|
|
56
|
+
}
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
/**
|
|
@@ -114,5 +118,6 @@ type WithCooldown = {
|
|
|
114
118
|
type WithMinInterval = {
|
|
115
119
|
readonly minInterval?: Duration;
|
|
116
120
|
};
|
|
121
|
+
type NonEmpty<T extends string> = `NonEmpty${T}`;
|
|
117
122
|
|
|
118
|
-
export { type Awaitable as A, Deferred as D, type
|
|
123
|
+
export { type Awaitable as A, Deferred as D, type NonEmpty as N, type RetryOptions as R, type Thenable as T, type WithConcurrency as W, type NonEmptyArr as a, type TimeoutOptions as b, type WithCooldown as c, type WithDuration as d, type WithError as e, type WithErrors as f, type WithFirst as g, type WithKind as h, type WithLog as i, type WithMinInterval as j, type WithN as k, type WithSecond as l, type WithSize as m, type WithTimeout as n, type WithValue as o };
|