@nlozgachev/pipekit 0.1.6
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 +182 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/Composition/compose.js +3 -0
- package/esm/src/Composition/curry.js +42 -0
- package/esm/src/Composition/flip.js +20 -0
- package/esm/src/Composition/flow.js +8 -0
- package/esm/src/Composition/fn.js +85 -0
- package/esm/src/Composition/index.js +10 -0
- package/esm/src/Composition/memoize.js +66 -0
- package/esm/src/Composition/not.js +25 -0
- package/esm/src/Composition/pipe.js +3 -0
- package/esm/src/Composition/tap.js +33 -0
- package/esm/src/Composition/uncurry.js +32 -0
- package/esm/src/Core/Arr.js +461 -0
- package/esm/src/Core/InternalTypes.js +1 -0
- package/esm/src/Core/Option.js +195 -0
- package/esm/src/Core/Rec.js +167 -0
- package/esm/src/Core/RemoteData.js +210 -0
- package/esm/src/Core/Result.js +173 -0
- package/esm/src/Core/Task.js +108 -0
- package/esm/src/Core/TaskResult.js +63 -0
- package/esm/src/Core/Validation.js +215 -0
- package/esm/src/Core/index.js +8 -0
- package/esm/src/Types/NonEmptyList.js +14 -0
- package/esm/src/Types/index.js +1 -0
- package/package.json +60 -0
- package/script/mod.js +19 -0
- package/script/package.json +3 -0
- package/script/src/Composition/compose.js +6 -0
- package/script/src/Composition/curry.js +48 -0
- package/script/src/Composition/flip.js +24 -0
- package/script/src/Composition/flow.js +11 -0
- package/script/src/Composition/fn.js +98 -0
- package/script/src/Composition/index.js +26 -0
- package/script/src/Composition/memoize.js +71 -0
- package/script/src/Composition/not.js +29 -0
- package/script/src/Composition/pipe.js +6 -0
- package/script/src/Composition/tap.js +37 -0
- package/script/src/Composition/uncurry.js +38 -0
- package/script/src/Core/Arr.js +464 -0
- package/script/src/Core/InternalTypes.js +2 -0
- package/script/src/Core/Option.js +198 -0
- package/script/src/Core/Rec.js +170 -0
- package/script/src/Core/RemoteData.js +213 -0
- package/script/src/Core/Result.js +176 -0
- package/script/src/Core/Task.js +111 -0
- package/script/src/Core/TaskResult.js +66 -0
- package/script/src/Core/Validation.js +218 -0
- package/script/src/Core/index.js +24 -0
- package/script/src/Types/NonEmptyList.js +18 -0
- package/script/src/Types/index.js +17 -0
- package/types/mod.d.ts +4 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/Composition/compose.d.ts +33 -0
- package/types/src/Composition/compose.d.ts.map +1 -0
- package/types/src/Composition/curry.d.ts +43 -0
- package/types/src/Composition/curry.d.ts.map +1 -0
- package/types/src/Composition/flip.d.ts +21 -0
- package/types/src/Composition/flip.d.ts.map +1 -0
- package/types/src/Composition/flow.d.ts +56 -0
- package/types/src/Composition/flow.d.ts.map +1 -0
- package/types/src/Composition/fn.d.ts +76 -0
- package/types/src/Composition/fn.d.ts.map +1 -0
- package/types/src/Composition/index.d.ts +11 -0
- package/types/src/Composition/index.d.ts.map +1 -0
- package/types/src/Composition/memoize.d.ts +46 -0
- package/types/src/Composition/memoize.d.ts.map +1 -0
- package/types/src/Composition/not.d.ts +26 -0
- package/types/src/Composition/not.d.ts.map +1 -0
- package/types/src/Composition/pipe.d.ts +56 -0
- package/types/src/Composition/pipe.d.ts.map +1 -0
- package/types/src/Composition/tap.d.ts +31 -0
- package/types/src/Composition/tap.d.ts.map +1 -0
- package/types/src/Composition/uncurry.d.ts +54 -0
- package/types/src/Composition/uncurry.d.ts.map +1 -0
- package/types/src/Core/Arr.d.ts +355 -0
- package/types/src/Core/Arr.d.ts.map +1 -0
- package/types/src/Core/InternalTypes.d.ts +14 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -0
- package/types/src/Core/Option.d.ts +214 -0
- package/types/src/Core/Option.d.ts.map +1 -0
- package/types/src/Core/Rec.d.ts +121 -0
- package/types/src/Core/Rec.d.ts.map +1 -0
- package/types/src/Core/RemoteData.d.ts +196 -0
- package/types/src/Core/RemoteData.d.ts.map +1 -0
- package/types/src/Core/Result.d.ts +185 -0
- package/types/src/Core/Result.d.ts.map +1 -0
- package/types/src/Core/Task.d.ts +125 -0
- package/types/src/Core/Task.d.ts.map +1 -0
- package/types/src/Core/TaskResult.d.ts +78 -0
- package/types/src/Core/TaskResult.d.ts.map +1 -0
- package/types/src/Core/Validation.d.ts +217 -0
- package/types/src/Core/Validation.d.ts.map +1 -0
- package/types/src/Core/index.d.ts +9 -0
- package/types/src/Core/index.d.ts.map +1 -0
- package/types/src/Types/NonEmptyList.d.ts +29 -0
- package/types/src/Types/NonEmptyList.d.ts.map +1 -0
- package/types/src/Types/index.d.ts +2 -0
- package/types/src/Types/index.d.ts.map +1 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Option } from "./Option.js";
|
|
2
|
+
/**
|
|
3
|
+
* Functional record/object utilities that compose well with pipe.
|
|
4
|
+
* All functions are data-last and curried where applicable.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* pipe(
|
|
9
|
+
* { a: 1, b: 2, c: 3 },
|
|
10
|
+
* Rec.filter(n => n > 1),
|
|
11
|
+
* Rec.map(n => n * 10)
|
|
12
|
+
* ); // { b: 20, c: 30 }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export var Rec;
|
|
16
|
+
(function (Rec) {
|
|
17
|
+
/**
|
|
18
|
+
* Transforms each value in a record.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* pipe({ a: 1, b: 2 }, Rec.map(n => n * 2)); // { a: 2, b: 4 }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
Rec.map = (f) => (data) => {
|
|
26
|
+
const result = {};
|
|
27
|
+
for (const key of Object.keys(data)) {
|
|
28
|
+
result[key] = f(data[key]);
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Transforms each value in a record, also receiving the key.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* pipe({ a: 1, b: 2 }, Rec.mapWithKey((k, v) => `${k}:${v}`));
|
|
38
|
+
* // { a: "a:1", b: "b:2" }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
Rec.mapWithKey = (f) => (data) => {
|
|
42
|
+
const result = {};
|
|
43
|
+
for (const key of Object.keys(data)) {
|
|
44
|
+
result[key] = f(key, data[key]);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Filters values in a record by a predicate.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.filter(n => n > 1)); // { b: 2, c: 3 }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
Rec.filter = (predicate) => (data) => {
|
|
57
|
+
const result = {};
|
|
58
|
+
for (const key of Object.keys(data)) {
|
|
59
|
+
if (predicate(data[key]))
|
|
60
|
+
result[key] = data[key];
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Filters values in a record by a predicate that also receives the key.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* pipe({ a: 1, b: 2 }, Rec.filterWithKey((k, v) => k !== "a" && v > 0));
|
|
70
|
+
* // { b: 2 }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
Rec.filterWithKey = (predicate) => (data) => {
|
|
74
|
+
const result = {};
|
|
75
|
+
for (const key of Object.keys(data)) {
|
|
76
|
+
if (predicate(key, data[key]))
|
|
77
|
+
result[key] = data[key];
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Looks up a value by key, returning Option.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* pipe({ a: 1, b: 2 }, Rec.lookup("a")); // Some(1)
|
|
87
|
+
* pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option.toSome(data[key]) : Option.toNone();
|
|
91
|
+
/**
|
|
92
|
+
* Returns all keys of a record.
|
|
93
|
+
*/
|
|
94
|
+
Rec.keys = (data) => Object.keys(data);
|
|
95
|
+
/**
|
|
96
|
+
* Returns all values of a record.
|
|
97
|
+
*/
|
|
98
|
+
Rec.values = (data) => Object.values(data);
|
|
99
|
+
/**
|
|
100
|
+
* Returns all key-value pairs of a record.
|
|
101
|
+
*/
|
|
102
|
+
Rec.entries = (data) => Object.entries(data);
|
|
103
|
+
/**
|
|
104
|
+
* Creates a record from key-value pairs.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* Rec.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
Rec.fromEntries = (data) => Object.fromEntries(data);
|
|
112
|
+
/**
|
|
113
|
+
* Picks specific keys from a record.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.pick("a", "c")); // { a: 1, c: 3 }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
Rec.pick = (...pickedKeys) => (data) => {
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const key of pickedKeys) {
|
|
123
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
124
|
+
result[key] = data[key];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Omits specific keys from a record.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.omit("b")); // { a: 1, c: 3 }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
Rec.omit = (...omittedKeys) => (data) => {
|
|
138
|
+
const omitSet = new Set(omittedKeys);
|
|
139
|
+
const result = {};
|
|
140
|
+
for (const key of Object.keys(data)) {
|
|
141
|
+
if (!omitSet.has(key)) {
|
|
142
|
+
result[key] = data[key];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Merges two records. Values from the second record take precedence.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* pipe({ a: 1, b: 2 }, Rec.merge({ b: 3, c: 4 })); // { a: 1, b: 3, c: 4 }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
Rec.merge = (other) => (data) => ({
|
|
156
|
+
...data,
|
|
157
|
+
...other,
|
|
158
|
+
});
|
|
159
|
+
/**
|
|
160
|
+
* Returns true if the record has no keys.
|
|
161
|
+
*/
|
|
162
|
+
Rec.isEmpty = (data) => Object.keys(data).length === 0;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the number of keys in a record.
|
|
165
|
+
*/
|
|
166
|
+
Rec.size = (data) => Object.keys(data).length;
|
|
167
|
+
})(Rec || (Rec = {}));
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
export var RemoteData;
|
|
2
|
+
(function (RemoteData) {
|
|
3
|
+
/**
|
|
4
|
+
* Creates a NotAsked RemoteData.
|
|
5
|
+
*/
|
|
6
|
+
RemoteData.notAsked = () => ({ kind: "NotAsked" });
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Loading RemoteData.
|
|
9
|
+
*/
|
|
10
|
+
RemoteData.loading = () => ({ kind: "Loading" });
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Failure RemoteData with the given error.
|
|
13
|
+
*/
|
|
14
|
+
RemoteData.failure = (error) => ({
|
|
15
|
+
kind: "Failure",
|
|
16
|
+
error,
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Success RemoteData with the given value.
|
|
20
|
+
*/
|
|
21
|
+
RemoteData.success = (value) => ({
|
|
22
|
+
kind: "Success",
|
|
23
|
+
value,
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Wraps a value in a Success RemoteData. Alias for `success`.
|
|
27
|
+
*/
|
|
28
|
+
RemoteData.of = RemoteData.success;
|
|
29
|
+
/**
|
|
30
|
+
* Type guard that checks if a RemoteData is NotAsked.
|
|
31
|
+
*/
|
|
32
|
+
RemoteData.isNotAsked = (data) => data.kind === "NotAsked";
|
|
33
|
+
/**
|
|
34
|
+
* Type guard that checks if a RemoteData is Loading.
|
|
35
|
+
*/
|
|
36
|
+
RemoteData.isLoading = (data) => data.kind === "Loading";
|
|
37
|
+
/**
|
|
38
|
+
* Type guard that checks if a RemoteData is Failure.
|
|
39
|
+
*/
|
|
40
|
+
RemoteData.isFailure = (data) => data.kind === "Failure";
|
|
41
|
+
/**
|
|
42
|
+
* Type guard that checks if a RemoteData is Success.
|
|
43
|
+
*/
|
|
44
|
+
RemoteData.isSuccess = (data) => data.kind === "Success";
|
|
45
|
+
/**
|
|
46
|
+
* Transforms the success value inside a RemoteData.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* pipe(RemoteData.of(5), RemoteData.map(n => n * 2)); // Success(10)
|
|
51
|
+
* pipe(RemoteData.loading(), RemoteData.map(n => n * 2)); // Loading
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
RemoteData.map = (f) => (data) => RemoteData.isSuccess(data) ? RemoteData.success(f(data.value)) : data;
|
|
55
|
+
/**
|
|
56
|
+
* Transforms the error value inside a RemoteData.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* pipe(RemoteData.failure("oops"), RemoteData.mapError(e => e.toUpperCase())); // Failure("OOPS")
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
RemoteData.mapError = (f) => (data) => RemoteData.isFailure(data) ? RemoteData.failure(f(data.error)) : data;
|
|
64
|
+
/**
|
|
65
|
+
* Chains RemoteData computations. If the input is Success, passes the value to f.
|
|
66
|
+
* Otherwise, propagates the current state.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* pipe(
|
|
71
|
+
* RemoteData.of(5),
|
|
72
|
+
* RemoteData.chain(n => n > 0 ? RemoteData.of(n) : RemoteData.failure("negative"))
|
|
73
|
+
* );
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
RemoteData.chain = (f) => (data) => RemoteData.isSuccess(data) ? f(data.value) : data;
|
|
77
|
+
/**
|
|
78
|
+
* Applies a function wrapped in a RemoteData to a value wrapped in a RemoteData.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
83
|
+
* pipe(
|
|
84
|
+
* RemoteData.of(add),
|
|
85
|
+
* RemoteData.ap(RemoteData.of(5)),
|
|
86
|
+
* RemoteData.ap(RemoteData.of(3))
|
|
87
|
+
* ); // Success(8)
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
RemoteData.ap = (arg) => (data) => {
|
|
91
|
+
if (RemoteData.isSuccess(data) && RemoteData.isSuccess(arg)) {
|
|
92
|
+
return RemoteData.success(data.value(arg.value));
|
|
93
|
+
}
|
|
94
|
+
if (RemoteData.isFailure(data))
|
|
95
|
+
return data;
|
|
96
|
+
if (RemoteData.isFailure(arg))
|
|
97
|
+
return arg;
|
|
98
|
+
if (RemoteData.isLoading(data) || RemoteData.isLoading(arg))
|
|
99
|
+
return RemoteData.loading();
|
|
100
|
+
return RemoteData.notAsked();
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Extracts the value from a RemoteData by providing handlers for all four cases.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* pipe(
|
|
108
|
+
* userData,
|
|
109
|
+
* RemoteData.fold(
|
|
110
|
+
* () => "Not asked",
|
|
111
|
+
* () => "Loading...",
|
|
112
|
+
* e => `Error: ${e}`,
|
|
113
|
+
* value => `Got: ${value}`
|
|
114
|
+
* )
|
|
115
|
+
* );
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
RemoteData.fold = (onNotAsked, onLoading, onFailure, onSuccess) => (data) => {
|
|
119
|
+
switch (data.kind) {
|
|
120
|
+
case "NotAsked":
|
|
121
|
+
return onNotAsked();
|
|
122
|
+
case "Loading":
|
|
123
|
+
return onLoading();
|
|
124
|
+
case "Failure":
|
|
125
|
+
return onFailure(data.error);
|
|
126
|
+
case "Success":
|
|
127
|
+
return onSuccess(data.value);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Pattern matches on a RemoteData, returning the result of the matching case.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* pipe(
|
|
136
|
+
* userData,
|
|
137
|
+
* RemoteData.match({
|
|
138
|
+
* notAsked: () => "Click to load",
|
|
139
|
+
* loading: () => "Loading...",
|
|
140
|
+
* failure: e => `Error: ${e}`,
|
|
141
|
+
* success: user => `Hello, ${user.name}!`
|
|
142
|
+
* })
|
|
143
|
+
* );
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
RemoteData.match = (cases) => (data) => {
|
|
147
|
+
switch (data.kind) {
|
|
148
|
+
case "NotAsked":
|
|
149
|
+
return cases.notAsked();
|
|
150
|
+
case "Loading":
|
|
151
|
+
return cases.loading();
|
|
152
|
+
case "Failure":
|
|
153
|
+
return cases.failure(data.error);
|
|
154
|
+
case "Success":
|
|
155
|
+
return cases.success(data.value);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Returns the success value or a default value if the RemoteData is not Success.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* pipe(RemoteData.of(5), RemoteData.getOrElse(0)); // 5
|
|
164
|
+
* pipe(RemoteData.loading(), RemoteData.getOrElse(0)); // 0
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
RemoteData.getOrElse = (defaultValue) => (data) => RemoteData.isSuccess(data) ? data.value : defaultValue;
|
|
168
|
+
/**
|
|
169
|
+
* Executes a side effect on the success value without changing the RemoteData.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* pipe(
|
|
174
|
+
* RemoteData.of(5),
|
|
175
|
+
* RemoteData.tap(n => console.log("Value:", n)),
|
|
176
|
+
* RemoteData.map(n => n * 2)
|
|
177
|
+
* );
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
RemoteData.tap = (f) => (data) => {
|
|
181
|
+
if (RemoteData.isSuccess(data))
|
|
182
|
+
f(data.value);
|
|
183
|
+
return data;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Recovers from a Failure state by providing a fallback RemoteData.
|
|
187
|
+
*/
|
|
188
|
+
RemoteData.recover = (fallback) => (data) => RemoteData.isFailure(data) ? fallback(data.error) : data;
|
|
189
|
+
/**
|
|
190
|
+
* Converts a RemoteData to an Option.
|
|
191
|
+
* Success becomes Some, all other states become None.
|
|
192
|
+
*/
|
|
193
|
+
RemoteData.toOption = (data) => RemoteData.isSuccess(data) ? { kind: "Some", value: data.value } : { kind: "None" };
|
|
194
|
+
/**
|
|
195
|
+
* Converts a RemoteData to a Result.
|
|
196
|
+
* Success becomes Ok, Failure becomes Err.
|
|
197
|
+
* NotAsked and Loading become Err with the provided fallback error.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* pipe(
|
|
202
|
+
* RemoteData.of(42),
|
|
203
|
+
* RemoteData.toResult(() => "not loaded")
|
|
204
|
+
* ); // Ok(42)
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
RemoteData.toResult = (onNotReady) => (data) => RemoteData.isSuccess(data)
|
|
208
|
+
? { kind: "Ok", value: data.value }
|
|
209
|
+
: { kind: "Error", error: RemoteData.isFailure(data) ? data.error : onNotReady() };
|
|
210
|
+
})(RemoteData || (RemoteData = {}));
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
export var Result;
|
|
2
|
+
(function (Result) {
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a value in a successful Result (Ok).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* Result.of(42); // Ok(42)
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
Result.of = (value) => Result.toOk(value);
|
|
12
|
+
/**
|
|
13
|
+
* Creates a failed Result with the given error.
|
|
14
|
+
*/
|
|
15
|
+
Result.toErr = (error) => ({ kind: "Error", error });
|
|
16
|
+
/**
|
|
17
|
+
* Creates a successful Result with the given value.
|
|
18
|
+
*/
|
|
19
|
+
Result.toOk = (value) => ({ kind: "Ok", value });
|
|
20
|
+
/**
|
|
21
|
+
* Type guard that checks if an Result is Ok.
|
|
22
|
+
*/
|
|
23
|
+
Result.isOk = (data) => data.kind === "Ok";
|
|
24
|
+
/**
|
|
25
|
+
* Type guard that checks if an Result is Err.
|
|
26
|
+
*/
|
|
27
|
+
Result.isErr = (data) => data.kind === "Error";
|
|
28
|
+
/**
|
|
29
|
+
* Creates an Result from a function that may throw.
|
|
30
|
+
* Catches any errors and transforms them using the onError function.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const parseJson = (s: string): Result<string, unknown> =>
|
|
35
|
+
* Result.tryCatch(
|
|
36
|
+
* () => JSON.parse(s),
|
|
37
|
+
* (e) => `Parse error: ${e}`
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
Result.tryCatch = (f, onError) => {
|
|
42
|
+
try {
|
|
43
|
+
return Result.toOk(f());
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return Result.toErr(onError(e));
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Transforms the success value inside an Result.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* pipe(Result.of(5), Result.map(n => n * 2)); // Ok(10)
|
|
55
|
+
* pipe(Result.toErr("error"), Result.map(n => n * 2)); // Err("error")
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
Result.map = (f) => (data) => Result.isOk(data) ? Result.toOk(f(data.value)) : data;
|
|
59
|
+
/**
|
|
60
|
+
* Transforms the error value inside an Result.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* pipe(Result.toErr("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
Result.mapError = (f) => (data) => Result.isErr(data) ? Result.toErr(f(data.error)) : data;
|
|
68
|
+
/**
|
|
69
|
+
* Chains Result computations. If the first is Ok, passes the value to f.
|
|
70
|
+
* If the first is Err, propagates the error.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const validatePositive = (n: number): Result<string, number> =>
|
|
75
|
+
* n > 0 ? Result.of(n) : Result.toErr("Must be positive");
|
|
76
|
+
*
|
|
77
|
+
* pipe(Result.of(5), Result.chain(validatePositive)); // Ok(5)
|
|
78
|
+
* pipe(Result.of(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
Result.chain = (f) => (data) => Result.isOk(data) ? f(data.value) : data;
|
|
82
|
+
/**
|
|
83
|
+
* Extracts the value from an Result by providing handlers for both cases.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* pipe(
|
|
88
|
+
* Result.of(5),
|
|
89
|
+
* Result.fold(
|
|
90
|
+
* e => `Error: ${e}`,
|
|
91
|
+
* n => `Value: ${n}`
|
|
92
|
+
* )
|
|
93
|
+
* ); // "Value: 5"
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
Result.fold = (onErr, onOk) => (data) => Result.isOk(data) ? onOk(data.value) : onErr(data.error);
|
|
97
|
+
/**
|
|
98
|
+
* Pattern matches on a Result, returning the result of the matching case.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* pipe(
|
|
103
|
+
* result,
|
|
104
|
+
* Result.match({
|
|
105
|
+
* ok: value => `Got ${value}`,
|
|
106
|
+
* err: error => `Failed: ${error}`
|
|
107
|
+
* })
|
|
108
|
+
* );
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
Result.match = (cases) => (data) => Result.isOk(data) ? cases.ok(data.value) : cases.err(data.error);
|
|
112
|
+
/**
|
|
113
|
+
* Returns the success value or a default value if the Result is an error.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* pipe(Result.of(5), Result.getOrElse(0)); // 5
|
|
118
|
+
* pipe(Result.toErr("error"), Result.getOrElse(0)); // 0
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
Result.getOrElse = (defaultValue) => (data) => Result.isOk(data) ? data.value : defaultValue;
|
|
122
|
+
/**
|
|
123
|
+
* Executes a side effect on the success value without changing the Result.
|
|
124
|
+
* Useful for logging or debugging.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* pipe(
|
|
129
|
+
* Result.of(5),
|
|
130
|
+
* Result.tap(n => console.log("Value:", n)),
|
|
131
|
+
* Result.map(n => n * 2)
|
|
132
|
+
* );
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
Result.tap = (f) => (data) => {
|
|
136
|
+
if (Result.isOk(data))
|
|
137
|
+
f(data.value);
|
|
138
|
+
return data;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Recovers from an error by providing a fallback Result.
|
|
142
|
+
*/
|
|
143
|
+
Result.recover = (fallback) => (data) => Result.isOk(data) ? data : fallback();
|
|
144
|
+
/**
|
|
145
|
+
* Recovers from an error unless it matches the blocked error.
|
|
146
|
+
*/
|
|
147
|
+
Result.recoverUnless = (blockedErr, fallback) => (data) => Result.isErr(data) && data.error !== blockedErr ? fallback() : data;
|
|
148
|
+
/**
|
|
149
|
+
* Converts a Result to an Option.
|
|
150
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* Result.toOption(Result.toOk(42)); // Some(42)
|
|
155
|
+
* Result.toOption(Result.toErr("oops")); // None
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
Result.toOption = (data) => Result.isOk(data) ? { kind: "Some", value: data.value } : { kind: "None" };
|
|
159
|
+
/**
|
|
160
|
+
* Applies a function wrapped in an Result to a value wrapped in an Result.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
165
|
+
* pipe(
|
|
166
|
+
* Result.of(add),
|
|
167
|
+
* Result.ap(Result.of(5)),
|
|
168
|
+
* Result.ap(Result.of(3))
|
|
169
|
+
* ); // Ok(8)
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.toOk(data.value(arg.value)) : Result.isErr(data) ? data : arg;
|
|
173
|
+
})(Result || (Result = {}));
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export var Task;
|
|
2
|
+
(function (Task) {
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a value in a Task that immediately resolves to that value.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const task = Task.of(42);
|
|
9
|
+
* task().then(console.log); // 42
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
Task.of = (value) => () => Promise.resolve(value);
|
|
13
|
+
/**
|
|
14
|
+
* Creates a Task that will reject with the given error.
|
|
15
|
+
*/
|
|
16
|
+
Task.fail = (error) => () => Promise.reject(error);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a Task from a function that returns a Promise.
|
|
19
|
+
* Alias for directly creating a Task.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const fetchUser = Task.from(() => fetch('/user').then(r => r.json()));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
Task.from = (f) => f;
|
|
27
|
+
/**
|
|
28
|
+
* Transforms the value inside a Task.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* pipe(
|
|
33
|
+
* Task.of(5),
|
|
34
|
+
* Task.map(n => n * 2)
|
|
35
|
+
* )(); // Promise<10>
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
Task.map = (f) => (data) => () => data().then(f);
|
|
39
|
+
/**
|
|
40
|
+
* Chains Task computations. If the first succeeds, passes the value to f.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const fetchUser = (id: string): Task<User> => () => fetch(`/users/${id}`).then(r => r.json());
|
|
45
|
+
* const fetchPosts = (user: User): Task<Post[]> => () => fetch(`/posts?userId=${user.id}`).then(r => r.json());
|
|
46
|
+
*
|
|
47
|
+
* pipe(
|
|
48
|
+
* fetchUser("123"),
|
|
49
|
+
* Task.chain(fetchPosts)
|
|
50
|
+
* )(); // Promise<Post[]>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
Task.chain = (f) => (data) => () => data().then((a) => f(a)());
|
|
54
|
+
/**
|
|
55
|
+
* Applies a function wrapped in a Task to a value wrapped in a Task.
|
|
56
|
+
* Both Tasks run in parallel.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
61
|
+
* pipe(
|
|
62
|
+
* Task.of(add),
|
|
63
|
+
* Task.ap(Task.of(5)),
|
|
64
|
+
* Task.ap(Task.of(3))
|
|
65
|
+
* )(); // Promise<8>
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
Task.ap = (arg) => (data) => () => Promise.all([data(), arg()]).then(([f, a]) => f(a));
|
|
69
|
+
/**
|
|
70
|
+
* Executes a side effect on the value without changing the Task.
|
|
71
|
+
* Useful for logging or debugging.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* pipe(
|
|
76
|
+
* fetchData,
|
|
77
|
+
* Task.tap(data => console.log("Fetched:", data)),
|
|
78
|
+
* Task.map(transform)
|
|
79
|
+
* );
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
Task.tap = (f) => (data) => () => data().then((a) => {
|
|
83
|
+
f(a);
|
|
84
|
+
return a;
|
|
85
|
+
});
|
|
86
|
+
/**
|
|
87
|
+
* Runs multiple Tasks in parallel and collects their results.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* Task.all([fetchUser, fetchPosts, fetchComments])();
|
|
92
|
+
* // Promise<[User, Post[], Comment[]]>
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
Task.all = (tasks) => () => Promise.all(tasks.map((t) => t()));
|
|
96
|
+
/**
|
|
97
|
+
* Delays the execution of a Task by the specified milliseconds.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* pipe(
|
|
102
|
+
* Task.of(42),
|
|
103
|
+
* Task.delay(1000)
|
|
104
|
+
* )(); // Resolves after 1 second
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
Task.delay = (ms) => (data) => () => new Promise((resolve, reject) => setTimeout(() => data().then(resolve, reject), ms));
|
|
108
|
+
})(Task || (Task = {}));
|