@nlozgachev/pipelined 0.6.5 → 0.7.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/esm/src/Core/Logged.js +111 -0
- package/esm/src/Core/Predicate.js +133 -0
- package/esm/src/Core/Refinement.js +115 -0
- package/esm/src/Core/State.js +181 -0
- package/esm/src/Core/Task.js +36 -0
- package/esm/src/Core/index.js +4 -0
- package/package.json +1 -1
- package/script/src/Core/Logged.js +114 -0
- package/script/src/Core/Predicate.js +136 -0
- package/script/src/Core/Refinement.js +118 -0
- package/script/src/Core/State.js +184 -0
- package/script/src/Core/Task.js +36 -0
- package/script/src/Core/index.js +4 -0
- package/types/src/Core/InternalTypes.d.ts +3 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -1
- package/types/src/Core/Logged.d.ts +126 -0
- package/types/src/Core/Logged.d.ts.map +1 -0
- package/types/src/Core/Predicate.d.ts +161 -0
- package/types/src/Core/Predicate.d.ts.map +1 -0
- package/types/src/Core/Refinement.d.ts +138 -0
- package/types/src/Core/Refinement.d.ts.map +1 -0
- package/types/src/Core/State.d.ts +192 -0
- package/types/src/Core/State.d.ts.map +1 -0
- package/types/src/Core/Task.d.ts +30 -0
- package/types/src/Core/Task.d.ts.map +1 -1
- package/types/src/Core/index.d.ts +4 -0
- package/types/src/Core/index.d.ts.map +1 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { WithLog, WithValue } from "./InternalTypes.js";
|
|
2
|
+
/**
|
|
3
|
+
* A value paired with an accumulated log.
|
|
4
|
+
*
|
|
5
|
+
* `Logged<W, A>` pairs a result `A` with a sequence of log entries `W`. When
|
|
6
|
+
* you sequence two `Logged` computations with `chain`, the logs are
|
|
7
|
+
* automatically concatenated — you never have to thread the log array through
|
|
8
|
+
* your code manually.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const program = pipe(
|
|
13
|
+
* Logged.make<string, number>(0),
|
|
14
|
+
* Logged.chain(n => pipe(
|
|
15
|
+
* Logged.tell("start"),
|
|
16
|
+
* Logged.map(() => n + 1),
|
|
17
|
+
* )),
|
|
18
|
+
* Logged.chain(n => pipe(
|
|
19
|
+
* Logged.tell("done"),
|
|
20
|
+
* Logged.map(() => n * 10),
|
|
21
|
+
* )),
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* Logged.run(program); // [10, ["start", "done"]]
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export type Logged<L, A> = WithValue<A> & WithLog<L>;
|
|
28
|
+
export declare namespace Logged {
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a pure value into a `Logged` with an empty log.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* Logged.make<string, number>(42); // { value: 42, log: [] }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const make: <W, A>(value: A) => Logged<W, A>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a `Logged` that records a single log entry and produces no
|
|
40
|
+
* meaningful value. Use this to append to the log inside a `chain`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* Logged.tell("operation completed"); // { value: undefined, log: ["operation completed"] }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
const tell: <W>(entry: W) => Logged<W, undefined>;
|
|
48
|
+
/**
|
|
49
|
+
* Transforms the value inside a `Logged` without affecting the log.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* pipe(
|
|
54
|
+
* Logged.of<string, number>(5),
|
|
55
|
+
* Logged.map(n => n * 2),
|
|
56
|
+
* ); // { value: 10, log: [] }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
const map: <W, A, B>(f: (a: A) => B) => (data: Logged<W, A>) => Logged<W, B>;
|
|
60
|
+
/**
|
|
61
|
+
* Sequences two `Logged` computations, concatenating their logs.
|
|
62
|
+
* The value from the first is passed to `f`; the resulting log entries are
|
|
63
|
+
* appended after the entries from the first.
|
|
64
|
+
*
|
|
65
|
+
* Data-last — the first computation is the data being piped.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const result = pipe(
|
|
70
|
+
* Logged.of<string, number>(1),
|
|
71
|
+
* Logged.chain(n => pipe(Logged.tell("step"), Logged.map(() => n + 1))),
|
|
72
|
+
* Logged.chain(n => pipe(Logged.tell("done"), Logged.map(() => n * 10))),
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* Logged.run(result); // [20, ["step", "done"]]
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
const chain: <W, A, B>(f: (a: A) => Logged<W, B>) => (data: Logged<W, A>) => Logged<W, B>;
|
|
79
|
+
/**
|
|
80
|
+
* Applies a function wrapped in a `Logged` to a value wrapped in a `Logged`,
|
|
81
|
+
* concatenating both logs.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const fn: Logged<string, (n: number) => number> = {
|
|
86
|
+
* value: n => n * 2,
|
|
87
|
+
* log: ["fn-loaded"],
|
|
88
|
+
* };
|
|
89
|
+
* const arg: Logged<string, number> = { value: 5, log: ["arg-loaded"] };
|
|
90
|
+
*
|
|
91
|
+
* const result = pipe(fn, Logged.ap(arg));
|
|
92
|
+
* Logged.run(result); // [10, ["fn-loaded", "arg-loaded"]]
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
const ap: <W, A>(arg: Logged<W, A>) => <B>(data: Logged<W, (a: A) => B>) => Logged<W, B>;
|
|
96
|
+
/**
|
|
97
|
+
* Runs a side effect on the value without changing the `Logged`.
|
|
98
|
+
* Useful for debugging or inspecting intermediate values.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* pipe(
|
|
103
|
+
* Logged.of<string, number>(42),
|
|
104
|
+
* Logged.tap(n => console.log("value:", n)),
|
|
105
|
+
* );
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
const tap: <W, A>(f: (a: A) => void) => (data: Logged<W, A>) => Logged<W, A>;
|
|
109
|
+
/**
|
|
110
|
+
* Extracts the value and log as a `readonly [A, ReadonlyArray<W>]` tuple.
|
|
111
|
+
* Use this at the boundary where you need to consume both.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const result = pipe(
|
|
116
|
+
* Logged.of<string, number>(1),
|
|
117
|
+
* Logged.chain(n => pipe(Logged.tell("incremented"), Logged.map(() => n + 1))),
|
|
118
|
+
* );
|
|
119
|
+
*
|
|
120
|
+
* const [value, log] = Logged.run(result);
|
|
121
|
+
* // value = 2, log = ["incremented"]
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
const run: <W, A>(data: Logged<W, A>) => readonly [A, ReadonlyArray<W>];
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=Logged.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Logged.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Logged.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAErD,yBAAiB,MAAM,CAAC;IACtB;;;;;;;OAOG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAyB,CAAC;IAE3E;;;;;;;;OAQG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,MAAM,CA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refinement.js";
|
|
2
|
+
/**
|
|
3
|
+
* A boolean-valued function over a type `A`.
|
|
4
|
+
*
|
|
5
|
+
* A `Predicate<A>` is the simpler sibling of `Refinement<A, B>`: it tests whether a
|
|
6
|
+
* value satisfies a condition at runtime but carries no compile-time narrowing guarantee.
|
|
7
|
+
* Use it when you need to combine, negate, or adapt boolean checks as first-class values
|
|
8
|
+
* and do not require the extra type information that a `Refinement` provides.
|
|
9
|
+
*
|
|
10
|
+
* Every `Refinement<A, B>` is a `Predicate<A>` — convert with `Predicate.fromRefinement`
|
|
11
|
+
* when you want to compose a narrowing check alongside plain predicates.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const isAdult: Predicate<number> = n => n >= 18;
|
|
16
|
+
* const isRetired: Predicate<number> = n => n >= 65;
|
|
17
|
+
*
|
|
18
|
+
* const isWorkingAge: Predicate<number> = pipe(
|
|
19
|
+
* isAdult,
|
|
20
|
+
* Predicate.and(Predicate.not(isRetired))
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* isWorkingAge(30); // true
|
|
24
|
+
* isWorkingAge(15); // false
|
|
25
|
+
* isWorkingAge(70); // false
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type Predicate<A> = (a: A) => boolean;
|
|
29
|
+
export declare namespace Predicate {
|
|
30
|
+
/**
|
|
31
|
+
* Negates a predicate: the result passes exactly when the original fails.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const isBlank: Predicate<string> = s => s.trim().length === 0;
|
|
36
|
+
* const isNotBlank = Predicate.not(isBlank);
|
|
37
|
+
*
|
|
38
|
+
* isNotBlank("hello"); // true
|
|
39
|
+
* isNotBlank(" "); // false
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const not: <A>(p: Predicate<A>) => Predicate<A>;
|
|
43
|
+
/**
|
|
44
|
+
* Combines two predicates with logical AND: passes only when both hold.
|
|
45
|
+
*
|
|
46
|
+
* Data-last — the first predicate is the data being piped.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const isPositive: Predicate<number> = n => n > 0;
|
|
51
|
+
* const isEven: Predicate<number> = n => n % 2 === 0;
|
|
52
|
+
*
|
|
53
|
+
* const isPositiveEven: Predicate<number> = pipe(isPositive, Predicate.and(isEven));
|
|
54
|
+
*
|
|
55
|
+
* isPositiveEven(4); // true
|
|
56
|
+
* isPositiveEven(3); // false — positive but odd
|
|
57
|
+
* isPositiveEven(-2); // false — even but not positive
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
const and: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
61
|
+
/**
|
|
62
|
+
* Combines two predicates with logical OR: passes when either holds.
|
|
63
|
+
*
|
|
64
|
+
* Data-last — the first predicate is the data being piped.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const isChild: Predicate<number> = n => n < 13;
|
|
69
|
+
* const isSenior: Predicate<number> = n => n >= 65;
|
|
70
|
+
*
|
|
71
|
+
* const getsDiscount: Predicate<number> = pipe(isChild, Predicate.or(isSenior));
|
|
72
|
+
*
|
|
73
|
+
* getsDiscount(8); // true
|
|
74
|
+
* getsDiscount(70); // true
|
|
75
|
+
* getsDiscount(30); // false
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
const or: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
79
|
+
/**
|
|
80
|
+
* Adapts a `Predicate<A>` to work on a different input type `B` by applying `f`
|
|
81
|
+
* to extract the relevant `A` from a `B` before running the check.
|
|
82
|
+
*
|
|
83
|
+
* Data-last — the predicate is the data being piped; `f` is the extractor.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* type User = { name: string; age: number };
|
|
88
|
+
*
|
|
89
|
+
* const isAdult: Predicate<number> = n => n >= 18;
|
|
90
|
+
*
|
|
91
|
+
* // Lift isAdult to work on Users by extracting the age field
|
|
92
|
+
* const isAdultUser: Predicate<User> = pipe(
|
|
93
|
+
* isAdult,
|
|
94
|
+
* Predicate.using((u: User) => u.age)
|
|
95
|
+
* );
|
|
96
|
+
*
|
|
97
|
+
* isAdultUser({ name: "Alice", age: 30 }); // true
|
|
98
|
+
* isAdultUser({ name: "Bob", age: 15 }); // false
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
const using: <A, B>(f: (b: B) => A) => (p: Predicate<A>) => Predicate<B>;
|
|
102
|
+
/**
|
|
103
|
+
* Combines an array of predicates with AND: passes only when every predicate holds.
|
|
104
|
+
* Returns `true` for an empty array (vacuous truth).
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const checks: Predicate<string>[] = [
|
|
109
|
+
* s => s.length > 0,
|
|
110
|
+
* s => s.length <= 100,
|
|
111
|
+
* s => !s.includes("<"),
|
|
112
|
+
* ];
|
|
113
|
+
*
|
|
114
|
+
* Predicate.all(checks)("hello"); // true
|
|
115
|
+
* Predicate.all(checks)(""); // false — too short
|
|
116
|
+
* Predicate.all(checks)("<b>"); // false — contains "<"
|
|
117
|
+
* Predicate.all([])("anything"); // true
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
const all: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
121
|
+
/**
|
|
122
|
+
* Combines an array of predicates with OR: passes when at least one holds.
|
|
123
|
+
* Returns `false` for an empty array.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const acceptedFormats: Predicate<string>[] = [
|
|
128
|
+
* s => s.endsWith(".jpg"),
|
|
129
|
+
* s => s.endsWith(".png"),
|
|
130
|
+
* s => s.endsWith(".webp"),
|
|
131
|
+
* ];
|
|
132
|
+
*
|
|
133
|
+
* Predicate.any(acceptedFormats)("photo.jpg"); // true
|
|
134
|
+
* Predicate.any(acceptedFormats)("photo.gif"); // false
|
|
135
|
+
* Predicate.any([])("anything"); // false
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
const any: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
139
|
+
/**
|
|
140
|
+
* Converts a `Refinement<A, B>` into a `Predicate<A>`, discarding the compile-time
|
|
141
|
+
* narrowing. Use this when you want to combine a type guard with plain predicates
|
|
142
|
+
* using `and`, `or`, or `all`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const isString: Refinement<unknown, string> =
|
|
147
|
+
* Refinement.make(x => typeof x === "string");
|
|
148
|
+
*
|
|
149
|
+
* const isShortString: Predicate<unknown> = pipe(
|
|
150
|
+
* Predicate.fromRefinement(isString),
|
|
151
|
+
* Predicate.and(x => (x as string).length < 10)
|
|
152
|
+
* );
|
|
153
|
+
*
|
|
154
|
+
* isShortString("hi"); // true
|
|
155
|
+
* isShortString("a very long string that exceeds ten characters"); // false
|
|
156
|
+
* isShortString(42); // false
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
const fromRefinement: <A, B extends A>(r: Refinement<A, B>) => Predicate<A>;
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=Predicate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Predicate.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Predicate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE7C,yBAAiB,SAAS,CAAC;IACzB;;;;;;;;;;;OAWG;IACI,MAAM,GAAG,GAAI,CAAC,EAAE,GAA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with a runtime check.
|
|
5
|
+
*
|
|
6
|
+
* A `Refinement<A, B>` proves at compile time that a value of type `A` is actually
|
|
7
|
+
* the narrower type `B extends A`, backed by a runtime boolean test. Use it to
|
|
8
|
+
* express domain invariants (non-empty strings, positive numbers, valid emails) as
|
|
9
|
+
* first-class, composable values rather than one-off type guards scattered across
|
|
10
|
+
* the codebase.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmptyString" };
|
|
15
|
+
*
|
|
16
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
17
|
+
* Refinement.make(s => s.length > 0);
|
|
18
|
+
*
|
|
19
|
+
* pipe(
|
|
20
|
+
* "hello",
|
|
21
|
+
* Refinement.toFilter(isNonEmpty)
|
|
22
|
+
* ); // Some("hello")
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type Refinement<A, B extends A> = (a: A) => a is B;
|
|
26
|
+
export declare namespace Refinement {
|
|
27
|
+
/**
|
|
28
|
+
* Creates a `Refinement<A, B>` from a plain boolean predicate.
|
|
29
|
+
*
|
|
30
|
+
* This is an unsafe cast — the caller is responsible for ensuring that the
|
|
31
|
+
* predicate truly characterises values of type `B`. Use this only when
|
|
32
|
+
* bootstrapping a new refinement; prefer `compose`, `and`, or `or` to build
|
|
33
|
+
* derived refinements from existing ones.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* type PositiveNumber = number & { readonly _tag: "PositiveNumber" };
|
|
38
|
+
*
|
|
39
|
+
* const isPositive: Refinement<number, PositiveNumber> =
|
|
40
|
+
* Refinement.make(n => n > 0);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
const make: <A, B extends A>(f: (a: A) => boolean) => Refinement<A, B>;
|
|
44
|
+
/**
|
|
45
|
+
* Chains two refinements: if `ab` narrows `A` to `B` and `bc` narrows `B` to `C`,
|
|
46
|
+
* the result narrows `A` directly to `C`.
|
|
47
|
+
*
|
|
48
|
+
* Data-last — the first refinement `ab` is the data being piped.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmpty" };
|
|
53
|
+
* type TrimmedString = NonEmptyString & { readonly _tag: "Trimmed" };
|
|
54
|
+
*
|
|
55
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
56
|
+
* Refinement.make(s => s.length > 0);
|
|
57
|
+
* const isTrimmed: Refinement<NonEmptyString, TrimmedString> =
|
|
58
|
+
* Refinement.make(s => s === s.trim());
|
|
59
|
+
*
|
|
60
|
+
* const isNonEmptyTrimmed: Refinement<string, TrimmedString> = pipe(
|
|
61
|
+
* isNonEmpty,
|
|
62
|
+
* Refinement.compose(isTrimmed)
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
const compose: <A, B extends A, C extends B>(bc: Refinement<B, C>) => (ab: Refinement<A, B>) => Refinement<A, C>;
|
|
67
|
+
/**
|
|
68
|
+
* Intersects two refinements: the result narrows `A` to `B & C`, passing only
|
|
69
|
+
* when both refinements hold simultaneously.
|
|
70
|
+
*
|
|
71
|
+
* Data-last — the first refinement is the data being piped.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
|
|
76
|
+
* const isNonEmpty: Refinement<unknown, { length: number }> =
|
|
77
|
+
* Refinement.make(x => (x as any).length > 0);
|
|
78
|
+
*
|
|
79
|
+
* const isNonEmptyString = pipe(isString, Refinement.and(isNonEmpty));
|
|
80
|
+
* isNonEmptyString("hi"); // true
|
|
81
|
+
* isNonEmptyString(""); // false
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
const and: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B & C>;
|
|
85
|
+
/**
|
|
86
|
+
* Unions two refinements: the result narrows `A` to `B | C`, passing when either
|
|
87
|
+
* refinement holds.
|
|
88
|
+
*
|
|
89
|
+
* Data-last — the first refinement is the data being piped.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
|
|
94
|
+
* const isNumber: Refinement<unknown, number> = Refinement.make(x => typeof x === "number");
|
|
95
|
+
*
|
|
96
|
+
* const isStringOrNumber = pipe(isString, Refinement.or(isNumber));
|
|
97
|
+
* isStringOrNumber("hi"); // true
|
|
98
|
+
* isStringOrNumber(42); // true
|
|
99
|
+
* isStringOrNumber(true); // false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
const or: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B | C>;
|
|
103
|
+
/**
|
|
104
|
+
* Converts a `Refinement<A, B>` into a function `(a: A) => Option<B>`.
|
|
105
|
+
*
|
|
106
|
+
* Returns `Some(a)` when the refinement holds, `None` otherwise. Useful for
|
|
107
|
+
* integrating runtime validation into an `Option`-based pipeline.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* type PositiveNumber = number & { readonly _tag: "Positive" };
|
|
112
|
+
* const isPositive: Refinement<number, PositiveNumber> =
|
|
113
|
+
* Refinement.make(n => n > 0);
|
|
114
|
+
*
|
|
115
|
+
* pipe(-1, Refinement.toFilter(isPositive)); // None
|
|
116
|
+
* pipe(42, Refinement.toFilter(isPositive)); // Some(42)
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
const toFilter: <A, B extends A>(r: Refinement<A, B>) => (a: A) => Option<B>;
|
|
120
|
+
/**
|
|
121
|
+
* Converts a `Refinement<A, B>` into a function `(a: A) => Result<E, B>`.
|
|
122
|
+
*
|
|
123
|
+
* Returns `Ok(a)` when the refinement holds, `Err(onFail(a))` otherwise. Use
|
|
124
|
+
* this to surface validation failures as typed errors inside a `Result` pipeline.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmpty" };
|
|
129
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
130
|
+
* Refinement.make(s => s.length > 0);
|
|
131
|
+
*
|
|
132
|
+
* pipe("", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Err(...)
|
|
133
|
+
* pipe("hi", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Ok("hi")
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
const toResult: <A, B extends A, E>(r: Refinement<A, B>, onFail: (a: A) => E) => (a: A) => Result<E, B>;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=Refinement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Refinement.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Refinement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAE1D,yBAAiB,UAAU,CAAC;IAC1B;;;;;;;;;;;;;;;OAeG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,KAAG,UAAU,CAAC,CAAC,EAAE,CAAC,CACpD,CAAC;IAExB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACI,MAAM,OAAO,GACjB,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,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line without exposing mutation at call sites.
|
|
4
|
+
*
|
|
5
|
+
* At runtime a `State<S, A>` is just a function from an initial state to a pair
|
|
6
|
+
* `[value, nextState]`. Nothing runs until you supply the initial state with
|
|
7
|
+
* `State.run`, `State.evaluate`, or `State.execute`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* type Counter = number;
|
|
12
|
+
*
|
|
13
|
+
* const increment: State<Counter, undefined> = State.modify(n => n + 1);
|
|
14
|
+
* const getCount: State<Counter, Counter> = State.get();
|
|
15
|
+
*
|
|
16
|
+
* const program = pipe(
|
|
17
|
+
* increment,
|
|
18
|
+
* State.chain(() => increment),
|
|
19
|
+
* State.chain(() => getCount),
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* State.run(0)(program); // [2, 2] — value is 2, final state is 2
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type State<S, A> = (s: S) => readonly [A, S];
|
|
26
|
+
export declare namespace State {
|
|
27
|
+
/**
|
|
28
|
+
* Lifts a pure value into a State computation. The state passes through unchanged.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* State.run(10)(State.resolve(42)); // [42, 10] — value 42, state unchanged
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
const resolve: <S, A>(value: A) => State<S, A>;
|
|
36
|
+
/**
|
|
37
|
+
* Produces the current state as the value, without modifying it.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const readStack: State<string[], string[]> = State.get();
|
|
42
|
+
* State.run(["a", "b"])(readStack); // [["a", "b"], ["a", "b"]]
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
const get: <S>() => State<S, S>;
|
|
46
|
+
/**
|
|
47
|
+
* Reads a projection of the state without modifying it.
|
|
48
|
+
* Equivalent to `pipe(State.get(), State.map(f))` but more direct.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* type AppState = { count: number; label: string };
|
|
53
|
+
* const readCount: State<AppState, number> = State.gets(s => s.count);
|
|
54
|
+
* State.run({ count: 5, label: "x" })(readCount); // [5, { count: 5, label: "x" }]
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
const gets: <S, A>(f: (s: S) => A) => State<S, A>;
|
|
58
|
+
/**
|
|
59
|
+
* Replaces the current state with a new value. Produces no meaningful value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const reset: State<number, undefined> = State.put(0);
|
|
64
|
+
* State.run(99)(reset); // [undefined, 0]
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
const put: <S>(newState: S) => State<S, undefined>;
|
|
68
|
+
/**
|
|
69
|
+
* Applies a function to the current state to produce the next state.
|
|
70
|
+
* Produces no meaningful value.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const push = (item: string): State<string[], undefined> =>
|
|
75
|
+
* State.modify(stack => [...stack, item]);
|
|
76
|
+
*
|
|
77
|
+
* State.run(["a"])(push("b")); // [undefined, ["a", "b"]]
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
const modify: <S>(f: (s: S) => S) => State<S, undefined>;
|
|
81
|
+
/**
|
|
82
|
+
* Transforms the value produced by a State computation.
|
|
83
|
+
* The state transformation is unchanged.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const readLength: State<string[], number> = pipe(
|
|
88
|
+
* State.get<string[]>(),
|
|
89
|
+
* State.map(stack => stack.length),
|
|
90
|
+
* );
|
|
91
|
+
*
|
|
92
|
+
* State.run(["a", "b", "c"])(readLength); // [3, ["a", "b", "c"]]
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
const map: <S, A, B>(f: (a: A) => B) => (st: State<S, A>) => State<S, B>;
|
|
96
|
+
/**
|
|
97
|
+
* Sequences two State computations. The state output of the first is passed
|
|
98
|
+
* as the state input to the second.
|
|
99
|
+
*
|
|
100
|
+
* Data-last — the first computation is the data being piped.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const push = (item: string): State<string[], undefined> =>
|
|
105
|
+
* State.modify(stack => [...stack, item]);
|
|
106
|
+
*
|
|
107
|
+
* const program = pipe(
|
|
108
|
+
* push("a"),
|
|
109
|
+
* State.chain(() => push("b")),
|
|
110
|
+
* State.chain(() => State.get<string[]>()),
|
|
111
|
+
* );
|
|
112
|
+
*
|
|
113
|
+
* State.evaluate([])(program); // ["a", "b"]
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
const chain: <S, A, B>(f: (a: A) => State<S, B>) => (st: State<S, A>) => State<S, B>;
|
|
117
|
+
/**
|
|
118
|
+
* Applies a function wrapped in a State to a value wrapped in a State.
|
|
119
|
+
* The function computation runs first; its output state is the input to the
|
|
120
|
+
* argument computation.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const addCounted = (n: number) => (m: number) => n + m;
|
|
125
|
+
* const program = pipe(
|
|
126
|
+
* State.resolve<number, typeof addCounted>(addCounted),
|
|
127
|
+
* State.ap(State.gets((s: number) => s * 2)),
|
|
128
|
+
* State.ap(State.gets((s: number) => s)),
|
|
129
|
+
* );
|
|
130
|
+
*
|
|
131
|
+
* State.evaluate(3)(program); // 6 + 3 = 9
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
const ap: <S, A>(arg: State<S, A>) => <B>(fn: State<S, (a: A) => B>) => State<S, B>;
|
|
135
|
+
/**
|
|
136
|
+
* Runs a side effect on the produced value without changing the State computation.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* pipe(
|
|
141
|
+
* State.get<number>(),
|
|
142
|
+
* State.tap(n => console.log("current:", n)),
|
|
143
|
+
* State.chain(() => State.modify(n => n + 1)),
|
|
144
|
+
* );
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
const tap: <S, A>(f: (a: A) => void) => (st: State<S, A>) => State<S, A>;
|
|
148
|
+
/**
|
|
149
|
+
* Runs a State computation with an initial state, returning both the
|
|
150
|
+
* produced value and the final state as a pair.
|
|
151
|
+
*
|
|
152
|
+
* Data-last — the computation is the data being piped.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const program = pipe(
|
|
157
|
+
* State.modify<number>(n => n + 1),
|
|
158
|
+
* State.chain(() => State.get<number>()),
|
|
159
|
+
* );
|
|
160
|
+
*
|
|
161
|
+
* State.run(0)(program); // [1, 1]
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
const run: <S>(initialState: S) => <A>(st: State<S, A>) => readonly [A, S];
|
|
165
|
+
/**
|
|
166
|
+
* Runs a State computation with an initial state, returning only the
|
|
167
|
+
* produced value (discarding the final state).
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* State.evaluate([])(pipe(
|
|
172
|
+
* State.modify<string[]>(s => [...s, "x"]),
|
|
173
|
+
* State.chain(() => State.get<string[]>()),
|
|
174
|
+
* )); // ["x"]
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
const evaluate: <S>(initialState: S) => <A>(st: State<S, A>) => A;
|
|
178
|
+
/**
|
|
179
|
+
* Runs a State computation with an initial state, returning only the
|
|
180
|
+
* final state (discarding the produced value).
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* State.execute(0)(pipe(
|
|
185
|
+
* State.modify<number>(n => n + 10),
|
|
186
|
+
* State.chain(() => State.modify<number>(n => n * 2)),
|
|
187
|
+
* )); // 20
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
const execute: <S>(initialState: S) => <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CAAwB,CAAC;CAC/F"}
|
package/types/src/Core/Task.d.ts
CHANGED
|
@@ -171,6 +171,36 @@ export declare namespace Task {
|
|
|
171
171
|
when: (a: A) => boolean;
|
|
172
172
|
delay?: number;
|
|
173
173
|
}) => (task: Task<A>) => Task<A>;
|
|
174
|
+
/**
|
|
175
|
+
* Resolves with the value of the first Task to complete. All Tasks start
|
|
176
|
+
* immediately; the rest are abandoned once one resolves.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const fast = Task.from(() => new Promise<string>(r => setTimeout(() => r("fast"), 10)));
|
|
181
|
+
* const slow = Task.from(() => new Promise<string>(r => setTimeout(() => r("slow"), 200)));
|
|
182
|
+
*
|
|
183
|
+
* await Task.race([fast, slow])(); // "fast"
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
const race: <A>(tasks: ReadonlyArray<Task<A>>) => Task<A>;
|
|
187
|
+
/**
|
|
188
|
+
* Runs an array of Tasks one at a time in order, collecting all results.
|
|
189
|
+
* Each Task starts only after the previous one resolves.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* let log: number[] = [];
|
|
194
|
+
* const makeTask = (n: number) => Task.from(() => {
|
|
195
|
+
* log.push(n);
|
|
196
|
+
* return Promise.resolve(n);
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* await Task.sequential([makeTask(1), makeTask(2), makeTask(3)])();
|
|
200
|
+
* // log = [1, 2, 3] — tasks ran in order
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
const sequential: <A>(tasks: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>;
|
|
174
204
|
/**
|
|
175
205
|
* Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
|
|
176
206
|
* Task does not complete within the given time.
|