ok-fp 0.2.1 → 0.3.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 +15 -14
- package/dist/index.d.mts +1328 -0
- package/dist/index.mjs +636 -0
- package/package.json +8 -17
- package/dist/either.d.mts +0 -2
- package/dist/either.mjs +0 -204
- package/dist/helpers-qdt9BxJz.d.mts +0 -665
- package/dist/option.d.mts +0 -2
- package/dist/option.mjs +0 -177
- package/dist/task.d.mts +0 -146
- package/dist/task.mjs +0 -77
- package/dist/validation.d.mts +0 -232
- package/dist/validation.mjs +0 -165
package/dist/either.mjs
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
//#region src/either/either.ts
|
|
2
|
-
function createEither(value) {
|
|
3
|
-
const either = {
|
|
4
|
-
filterOrElse: (predicate, onLeft) => either.flatMap((right) => predicate(right) ? either : createEither({ left: onLeft() })),
|
|
5
|
-
map: (mapper) => either.flatMap((right) => createEither({ right: mapper(right) })),
|
|
6
|
-
orElse: (fallback) => either.match((left) => forceCast(fallback(left)), () => forceCast(either)),
|
|
7
|
-
ap: function(arg) {
|
|
8
|
-
return this.flatMap((fn) => arg.map((right) => fn(right)));
|
|
9
|
-
},
|
|
10
|
-
swap: () => either.match((left) => createEither({ right: left }), (right) => createEither({ left: right })),
|
|
11
|
-
zip: (eitherA) => either.map((value) => (a) => [value, a]).ap(eitherA),
|
|
12
|
-
flatten: function() {
|
|
13
|
-
return this.flatMap((value) => value);
|
|
14
|
-
},
|
|
15
|
-
flatMap: (mapper) => either.match(() => forceCast(either), (right) => forceCast(mapper(right))),
|
|
16
|
-
tap: (sideEffect) => {
|
|
17
|
-
either.match(() => {}, sideEffect);
|
|
18
|
-
return either;
|
|
19
|
-
},
|
|
20
|
-
match: (onLeft, onRight) => isLeft(value) ? onLeft(value.left) : onRight(value.right),
|
|
21
|
-
getOrElse: (fallback) => either.match((error) => fallback(error), (value) => value),
|
|
22
|
-
toResult: () => either.match((error) => ({
|
|
23
|
-
ok: false,
|
|
24
|
-
error
|
|
25
|
-
}), (value) => ({
|
|
26
|
-
ok: true,
|
|
27
|
-
value
|
|
28
|
-
}))
|
|
29
|
-
};
|
|
30
|
-
return either;
|
|
31
|
-
}
|
|
32
|
-
function isLeft(value) {
|
|
33
|
-
return typeof value === "object" && "left" in value;
|
|
34
|
-
}
|
|
35
|
-
function forceCast(either) {
|
|
36
|
-
return either;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
//#endregion
|
|
40
|
-
//#region src/either/constructors.ts
|
|
41
|
-
/**
|
|
42
|
-
* Creates an Either containing an error value (Left).
|
|
43
|
-
*
|
|
44
|
-
* @typeParam E - The type of the error value
|
|
45
|
-
* @typeParam T - The type of the success value (defaults to never since this is an error)
|
|
46
|
-
* @param left - The error value to wrap
|
|
47
|
-
* @returns Either containing the error value on the Left side
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* const error = left("Something went wrong"); // Either<string, never>
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
function left(left) {
|
|
55
|
-
return createEither({ left });
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Creates an Either containing a success value (Right).
|
|
59
|
-
*
|
|
60
|
-
* @typeParam T - The type of the success value
|
|
61
|
-
* @typeParam E - The type of the error value (defaults to never since this is a success)
|
|
62
|
-
* @param right - The success value to wrap
|
|
63
|
-
* @returns Either containing the success value on the Right side
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```typescript
|
|
67
|
-
* const success = right(42); // Either<never, number>
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
function right(right) {
|
|
71
|
-
return createEither({ right });
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Creates an Either from a potentially nullable value.
|
|
75
|
-
* If the value is null or undefined, creates a Left with the provided error.
|
|
76
|
-
* If the value is present, creates a Right with the value.
|
|
77
|
-
*
|
|
78
|
-
* @typeParam E - The type of the error value
|
|
79
|
-
* @typeParam T - The type of the success value
|
|
80
|
-
* @param nullable - The potentially null/undefined value
|
|
81
|
-
* @param onNullish - Function that provides the error value when nullable is null/undefined
|
|
82
|
-
* @returns Either containing the value if present, or the error if null/undefined
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* fromNullable("hello", () => "empty") // Right("hello")
|
|
87
|
-
* fromNullable(null, () => "empty") // Left("empty")
|
|
88
|
-
* fromNullable(undefined, () => "empty") // Left("empty")
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
function fromNullable(nullable, onNullish) {
|
|
92
|
-
return nullable != null ? right(nullable) : left(onNullish());
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Safely executes a function that might throw, converting exceptions to Either.
|
|
96
|
-
*
|
|
97
|
-
* @typeParam E - The type of the processed error value
|
|
98
|
-
* @typeParam T - The type of the success value
|
|
99
|
-
* @param fn - The function to execute that might throw
|
|
100
|
-
* @param onThrow - Function that processes the caught error into the desired error type
|
|
101
|
-
* @returns Either containing the result if successful, or the processed error if thrown
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```typescript
|
|
105
|
-
* const parseNumber = (str: string) =>
|
|
106
|
-
* tryCatch(
|
|
107
|
-
* () => JSON.parse(str),
|
|
108
|
-
* (err) => `Parse error: ${err.message}`
|
|
109
|
-
* );
|
|
110
|
-
*
|
|
111
|
-
* parseNumber("42") // Right(42)
|
|
112
|
-
* parseNumber("invalid") // Left("Parse error: ...")
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
function tryCatch(fn, onThrow) {
|
|
116
|
-
try {
|
|
117
|
-
return right(fn());
|
|
118
|
-
} catch (err) {
|
|
119
|
-
return left(onThrow(err));
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Creates an Either from an Option by providing an error value for the None case.
|
|
124
|
-
*
|
|
125
|
-
* @typeParam E - The type of the error value
|
|
126
|
-
* @typeParam T - The type of the success value
|
|
127
|
-
* @param opt - The Option to convert
|
|
128
|
-
* @param onNone - Function that provides the error value when Option is None
|
|
129
|
-
* @returns Either containing the Option's value if Some, or the error if None
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```typescript
|
|
133
|
-
* fromOption(some(42), () => "empty") // Right(42)
|
|
134
|
-
* fromOption(none(), () => "empty") // Left("empty")
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
function fromOption(opt, onNone) {
|
|
138
|
-
return opt.match(() => left(onNone()), (value) => right(value));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
//#endregion
|
|
142
|
-
//#region src/either/helpers.ts
|
|
143
|
-
/**
|
|
144
|
-
* Combines two Eithers using a mapping function.
|
|
145
|
-
* Returns the mapped result if both Eithers are Right, otherwise the first Left.
|
|
146
|
-
*
|
|
147
|
-
* @param eitherA - First Either to combine
|
|
148
|
-
* @param eitherB - Second Either to combine
|
|
149
|
-
* @param mapper - Function to combine both Right values
|
|
150
|
-
* @returns Either containing the mapped result, or the first Left
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```typescript
|
|
154
|
-
* map2(right("John"), right("Doe"), (first, last) => `${first} ${last}`)
|
|
155
|
-
* // Right("John Doe")
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
function map2(eitherA, eitherB, mapper) {
|
|
159
|
-
return eitherA.map((a) => (b) => mapper(a, b)).ap(eitherB);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Combines three Eithers using a mapping function.
|
|
163
|
-
* Returns the mapped result if all Eithers are Right, otherwise the first Left.
|
|
164
|
-
*
|
|
165
|
-
* @param eitherA - First Either to combine
|
|
166
|
-
* @param eitherB - Second Either to combine
|
|
167
|
-
* @param eitherC - Third Either to combine
|
|
168
|
-
* @param mapper - Function to combine all three Right values
|
|
169
|
-
* @returns Either containing the mapped result, or the first Left
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* ```typescript
|
|
173
|
-
* map3(right(1), right(2), right(3), (a, b, c) => a + b + c)
|
|
174
|
-
* // Right(6)
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
function map3(eitherA, eitherB, eitherC, mapper) {
|
|
178
|
-
return eitherA.map((a) => (b) => (c) => mapper(a, b, c)).ap(eitherB).ap(eitherC);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Converts an array of Eithers into an Either of array.
|
|
182
|
-
* Returns Right with all values if all Eithers are Right, otherwise the first Left.
|
|
183
|
-
*
|
|
184
|
-
* @param eithers - Array of Eithers to sequence
|
|
185
|
-
* @returns Either containing array of all values, or the first Left
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* ```typescript
|
|
189
|
-
* sequence([right(1), right(2), right(3)]) // Right([1, 2, 3])
|
|
190
|
-
* sequence([right(1), left("error")]) // Left("error")
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
function sequence(eithers) {
|
|
194
|
-
const out = [];
|
|
195
|
-
for (const either of eithers) {
|
|
196
|
-
const result = either.toResult();
|
|
197
|
-
if (!result.ok) return left(result.error);
|
|
198
|
-
out.push(result.value);
|
|
199
|
-
}
|
|
200
|
-
return right(out);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
//#endregion
|
|
204
|
-
export { fromNullable, fromOption, left, map2, map3, right, sequence, tryCatch };
|