ok-fp 0.2.2 → 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 +6 -6
- package/dist/index.d.mts +1328 -0
- package/dist/index.mjs +636 -0
- package/package.json +8 -21
- package/dist/constructors-pI0lvrQE.mjs +0 -142
- package/dist/either-CWllQk7N.d.mts +0 -213
- package/dist/either.d.mts +0 -3
- package/dist/either.mjs +0 -65
- package/dist/helpers-Bw8McftR.d.mts +0 -455
- package/dist/option.d.mts +0 -2
- package/dist/option.mjs +0 -177
- package/dist/task-D9kWv4yq.d.mts +0 -100
- package/dist/task-DQTXV7xv.mjs +0 -23
- package/dist/task.d.mts +0 -49
- package/dist/task.mjs +0 -57
- package/dist/taskEither.d.mts +0 -212
- package/dist/taskEither.mjs +0 -80
- package/dist/validation.d.mts +0 -233
- package/dist/validation.mjs +0 -165
package/dist/validation.d.mts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { g as Option } from "./helpers-Bw8McftR.mjs";
|
|
2
|
-
import { t as Either } from "./either-CWllQk7N.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/validation/model.d.ts
|
|
5
|
-
type Valid<T> = {
|
|
6
|
-
readonly valid: T;
|
|
7
|
-
};
|
|
8
|
-
type Invalid<E> = {
|
|
9
|
-
readonly invalid: readonly E[];
|
|
10
|
-
};
|
|
11
|
-
type ValidationValue<E, T> = Valid<T> | Invalid<E>;
|
|
12
|
-
type ValidationResult<E, T> = {
|
|
13
|
-
readonly ok: true;
|
|
14
|
-
readonly value: T;
|
|
15
|
-
} | {
|
|
16
|
-
readonly ok: false;
|
|
17
|
-
readonly errors: readonly E[];
|
|
18
|
-
};
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/validation/validation.d.ts
|
|
21
|
-
type Validation<E, T> = {
|
|
22
|
-
/**
|
|
23
|
-
* Filters the Valid value based on a predicate function.
|
|
24
|
-
* If the predicate fails, returns Invalid with the provided error.
|
|
25
|
-
* If this Validation is already Invalid, returns the same Invalid unchanged.
|
|
26
|
-
*
|
|
27
|
-
* @param predicate - Function that tests the valid value
|
|
28
|
-
* @param onInvalid - Function that provides the error value when predicate fails
|
|
29
|
-
* @returns The same Validation if Invalid or predicate passes, otherwise Invalid with the provided error
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* const isPositive = (n: number) => n > 0;
|
|
34
|
-
* valid(5).filterOrElse(isPositive, () => "Must be positive") // Valid(5)
|
|
35
|
-
* valid(-3).filterOrElse(isPositive, () => "Must be positive") // Invalid(["Must be positive"])
|
|
36
|
-
* invalid("error").filterOrElse(isPositive, () => "Must be positive") // Invalid(["error"])
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
filterOrElse: (predicate: (value: T) => boolean, onInvalid: () => E) => Validation<E, T>;
|
|
40
|
-
/**
|
|
41
|
-
* Transforms the valid value using a mapping function.
|
|
42
|
-
* If this Validation is Invalid, returns the same Invalid unchanged.
|
|
43
|
-
*
|
|
44
|
-
* @param mapper - Function to transform the contained value
|
|
45
|
-
* @returns New Validation containing the transformed value, or the same Invalid
|
|
46
|
-
*/
|
|
47
|
-
map: <U>(mapper: (value: T) => U) => Validation<E, U>;
|
|
48
|
-
/**
|
|
49
|
-
* Applies a function wrapped in a Validation to a value wrapped in a Validation.
|
|
50
|
-
* Unlike Either, if both are Invalid, errors are accumulated (concatenated).
|
|
51
|
-
*
|
|
52
|
-
* @param arg - Validation containing the argument to apply the function to
|
|
53
|
-
* @returns Validation containing the result, or accumulated errors if any are Invalid
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
58
|
-
* valid(add(5)).ap(valid(3)) // Valid(8)
|
|
59
|
-
* valid(add(5)).ap(invalid("err")) // Invalid(["err"])
|
|
60
|
-
* invalid("e1").ap(invalid("e2")) // Invalid(["e1", "e2"]) ← errors accumulated!
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
ap: <EE, A, U>(this: Validation<E, (a: A) => U>, arg: Validation<EE, A>) => Validation<E | EE, U>;
|
|
64
|
-
/**
|
|
65
|
-
* Combines this Validation with another Validation into a tuple.
|
|
66
|
-
* If both are Valid, returns Valid containing a tuple of both values.
|
|
67
|
-
* If either is Invalid, accumulates all errors.
|
|
68
|
-
*
|
|
69
|
-
* @param valA - The Validation to combine with this one
|
|
70
|
-
* @returns Validation containing a tuple of both values, or accumulated errors
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* valid("Alice").zip(valid(30)) // Valid(["Alice", 30])
|
|
75
|
-
* valid("Alice").zip(invalid("No age")) // Invalid(["No age"])
|
|
76
|
-
* invalid("e1").zip(invalid("e2")) // Invalid(["e1", "e2"])
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
zip: <EE, A>(valA: Validation<EE, A>) => Validation<E | EE, readonly [T, A]>;
|
|
80
|
-
/**
|
|
81
|
-
* Pattern matches on the Validation, executing different functions based on its state.
|
|
82
|
-
*
|
|
83
|
-
* @param onInvalid - Function to execute if Validation is Invalid, receives the array of errors
|
|
84
|
-
* @param onValid - Function to execute if Validation is Valid, receives the value
|
|
85
|
-
* @returns The result of the executed function
|
|
86
|
-
*/
|
|
87
|
-
match: <U>(onInvalid: (errors: readonly E[]) => U, onValid: (value: T) => U) => U;
|
|
88
|
-
/**
|
|
89
|
-
* Extracts the valid value, or returns a fallback value if Invalid.
|
|
90
|
-
*
|
|
91
|
-
* @param fallback - Function that receives the errors and returns a default value
|
|
92
|
-
* @returns The valid value if present, otherwise the result of the fallback function
|
|
93
|
-
*/
|
|
94
|
-
getOrElse: (fallback: (errors: readonly E[]) => T) => T;
|
|
95
|
-
/**
|
|
96
|
-
* Performs a side effect if this Validation is Valid, returning the original Validation unchanged.
|
|
97
|
-
* If this Validation is Invalid, the side effect is not executed.
|
|
98
|
-
*
|
|
99
|
-
* @param sideEffect - Function to execute with the valid value (return value is ignored)
|
|
100
|
-
* @returns The same Validation instance unchanged
|
|
101
|
-
*/
|
|
102
|
-
tap: (sideEffect: (value: T) => void) => Validation<E, T>;
|
|
103
|
-
/**
|
|
104
|
-
* Performs a side effect if this Validation is Invalid, returning the original Validation unchanged.
|
|
105
|
-
* If this Validation is Valid, the side effect is not executed.
|
|
106
|
-
*
|
|
107
|
-
* @param sideEffect - Function to execute with the errors array (return value is ignored)
|
|
108
|
-
* @returns The same Validation instance unchanged
|
|
109
|
-
*/
|
|
110
|
-
tapInvalid: (sideEffect: (errors: readonly E[]) => void) => Validation<E, T>;
|
|
111
|
-
/**
|
|
112
|
-
* Converts the Validation to a ValidationResult type.
|
|
113
|
-
*
|
|
114
|
-
* @returns A ValidationResult object representing the Validation's state
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* valid(42).toResult() // { ok: true, value: 42 }
|
|
119
|
-
* invalid("error").toResult() // { ok: false, errors: ["error"] }
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
toResult: () => ValidationResult<E, T>;
|
|
123
|
-
};
|
|
124
|
-
//#endregion
|
|
125
|
-
//#region src/validation/constructors.d.ts
|
|
126
|
-
/**
|
|
127
|
-
* Creates a Validation containing a valid value.
|
|
128
|
-
*
|
|
129
|
-
* @param value - The valid value to wrap
|
|
130
|
-
* @returns Validation containing the valid value
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```typescript
|
|
134
|
-
* const v = valid(42); // Validation<never, number>
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
declare function valid<T, E = never>(value: T): Validation<E, T>;
|
|
138
|
-
/**
|
|
139
|
-
* Creates a Validation containing a single error.
|
|
140
|
-
*
|
|
141
|
-
* @param error - The error to wrap
|
|
142
|
-
* @returns Validation containing the error
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* const v = invalid("Something went wrong"); // Validation<string, never>
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
|
-
declare function invalid<E, T = never>(error: E): Validation<E, T>;
|
|
150
|
-
/**
|
|
151
|
-
* Creates a Validation from an Either.
|
|
152
|
-
* Right values become Valid, Left values become Invalid (with a single error).
|
|
153
|
-
*
|
|
154
|
-
* @param either - The Either to convert
|
|
155
|
-
* @returns Validation equivalent of the Either
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* fromEither(right(42)) // Valid(42)
|
|
160
|
-
* fromEither(left("error")) // Invalid(["error"])
|
|
161
|
-
* ```
|
|
162
|
-
*/
|
|
163
|
-
declare function fromEither<E, T>(either: Either<E, T>): Validation<E, T>;
|
|
164
|
-
/**
|
|
165
|
-
* Creates a Validation from an Option.
|
|
166
|
-
* Some values become Valid, None becomes Invalid with the provided error.
|
|
167
|
-
*
|
|
168
|
-
* @param opt - The Option to convert
|
|
169
|
-
* @param onNone - Function that provides the error when Option is None
|
|
170
|
-
* @returns Validation equivalent of the Option
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* ```typescript
|
|
174
|
-
* fromOption(some(42), () => "missing") // Valid(42)
|
|
175
|
-
* fromOption(none(), () => "missing") // Invalid(["missing"])
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
declare function fromOption<E, T>(opt: Option<T>, onNone: () => E): Validation<E, T>;
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region src/validation/helpers.d.ts
|
|
181
|
-
/**
|
|
182
|
-
* Combines two Validations using a mapping function.
|
|
183
|
-
* If both are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
184
|
-
*
|
|
185
|
-
* @param valA - First Validation to combine
|
|
186
|
-
* @param valB - Second Validation to combine
|
|
187
|
-
* @param mapper - Function to combine both valid values
|
|
188
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
189
|
-
*
|
|
190
|
-
* @example
|
|
191
|
-
* ```typescript
|
|
192
|
-
* map2(valid("John"), valid("Doe"), (first, last) => `${first} ${last}`)
|
|
193
|
-
* // Valid("John Doe")
|
|
194
|
-
* map2(invalid("e1"), invalid("e2"), mapper)
|
|
195
|
-
* // Invalid(["e1", "e2"])
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
declare function map2<EA, A, EB, B, C>(valA: Validation<EA, A>, valB: Validation<EB, B>, mapper: (a: A, b: B) => C): Validation<EA | EB, C>;
|
|
199
|
-
/**
|
|
200
|
-
* Combines three Validations using a mapping function.
|
|
201
|
-
* If all are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
202
|
-
*
|
|
203
|
-
* @param valA - First Validation to combine
|
|
204
|
-
* @param valB - Second Validation to combine
|
|
205
|
-
* @param valC - Third Validation to combine
|
|
206
|
-
* @param mapper - Function to combine all three valid values
|
|
207
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* ```typescript
|
|
211
|
-
* map3(valid(1), valid(2), valid(3), (a, b, c) => a + b + c)
|
|
212
|
-
* // Valid(6)
|
|
213
|
-
* map3(invalid("e1"), valid(2), invalid("e3"), mapper)
|
|
214
|
-
* // Invalid(["e1", "e3"])
|
|
215
|
-
* ```
|
|
216
|
-
*/
|
|
217
|
-
declare function map3<EA, A, EB, B, EC, C, D>(valA: Validation<EA, A>, valB: Validation<EB, B>, valC: Validation<EC, C>, mapper: (a: A, b: B, c: C) => D): Validation<EA | EB | EC, D>;
|
|
218
|
-
/**
|
|
219
|
-
* Converts an array of Validations into a Validation of array.
|
|
220
|
-
* Returns Valid with all values if all Validations are Valid, otherwise accumulates all errors.
|
|
221
|
-
*
|
|
222
|
-
* @param validations - Array of Validations to sequence
|
|
223
|
-
* @returns Validation containing array of all values, or all accumulated errors
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```typescript
|
|
227
|
-
* sequence([valid(1), valid(2), valid(3)]) // Valid([1, 2, 3])
|
|
228
|
-
* sequence([valid(1), invalid("e1"), invalid("e2")]) // Invalid(["e1", "e2"])
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
declare function sequence<E, T>(validations: readonly Validation<E, T>[]): Validation<E, T[]>;
|
|
232
|
-
//#endregion
|
|
233
|
-
export { Invalid, Valid, type Validation, ValidationResult, ValidationValue, fromEither, fromOption, invalid, map2, map3, sequence, valid };
|
package/dist/validation.mjs
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
//#region src/validation/validation.ts
|
|
2
|
-
function createValidation(value) {
|
|
3
|
-
const validation = {
|
|
4
|
-
filterOrElse: (predicate, onInvalid) => validation.match(() => validation, (v) => predicate(v) ? validation : createValidation({ invalid: [onInvalid()] })),
|
|
5
|
-
map: (mapper) => validation.match(() => forceCast(validation), (v) => createValidation({ valid: mapper(v) })),
|
|
6
|
-
ap: function(arg) {
|
|
7
|
-
return this.match((errors1) => arg.match((errors2) => createValidation({ invalid: [...errors1, ...errors2] }), () => createValidation({ invalid: errors1 })), (fn) => arg.match((errors2) => createValidation({ invalid: errors2 }), (v) => createValidation({ valid: fn(v) })));
|
|
8
|
-
},
|
|
9
|
-
zip: (valA) => validation.map((value) => (a) => [value, a]).ap(valA),
|
|
10
|
-
match: (onInvalid, onValid) => isValid(value) ? onValid(value.valid) : onInvalid(value.invalid),
|
|
11
|
-
getOrElse: (fallback) => validation.match((errors) => fallback(errors), (v) => v),
|
|
12
|
-
tap: (sideEffect) => {
|
|
13
|
-
validation.match(() => {}, sideEffect);
|
|
14
|
-
return validation;
|
|
15
|
-
},
|
|
16
|
-
tapInvalid: (sideEffect) => {
|
|
17
|
-
validation.match(sideEffect, () => {});
|
|
18
|
-
return validation;
|
|
19
|
-
},
|
|
20
|
-
toResult: () => validation.match((errors) => ({
|
|
21
|
-
ok: false,
|
|
22
|
-
errors
|
|
23
|
-
}), (value) => ({
|
|
24
|
-
ok: true,
|
|
25
|
-
value
|
|
26
|
-
}))
|
|
27
|
-
};
|
|
28
|
-
return validation;
|
|
29
|
-
}
|
|
30
|
-
function isValid(value) {
|
|
31
|
-
return typeof value === "object" && "valid" in value;
|
|
32
|
-
}
|
|
33
|
-
function forceCast(v) {
|
|
34
|
-
return v;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/validation/constructors.ts
|
|
39
|
-
/**
|
|
40
|
-
* Creates a Validation containing a valid value.
|
|
41
|
-
*
|
|
42
|
-
* @param value - The valid value to wrap
|
|
43
|
-
* @returns Validation containing the valid value
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* const v = valid(42); // Validation<never, number>
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
function valid(value) {
|
|
51
|
-
return createValidation({ valid: value });
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Creates a Validation containing a single error.
|
|
55
|
-
*
|
|
56
|
-
* @param error - The error to wrap
|
|
57
|
-
* @returns Validation containing the error
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* const v = invalid("Something went wrong"); // Validation<string, never>
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
function invalid(error) {
|
|
65
|
-
return createValidation({ invalid: [error] });
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Creates a Validation from an Either.
|
|
69
|
-
* Right values become Valid, Left values become Invalid (with a single error).
|
|
70
|
-
*
|
|
71
|
-
* @param either - The Either to convert
|
|
72
|
-
* @returns Validation equivalent of the Either
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```typescript
|
|
76
|
-
* fromEither(right(42)) // Valid(42)
|
|
77
|
-
* fromEither(left("error")) // Invalid(["error"])
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
function fromEither(either) {
|
|
81
|
-
return either.match((error) => invalid(error), (value) => valid(value));
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Creates a Validation from an Option.
|
|
85
|
-
* Some values become Valid, None becomes Invalid with the provided error.
|
|
86
|
-
*
|
|
87
|
-
* @param opt - The Option to convert
|
|
88
|
-
* @param onNone - Function that provides the error when Option is None
|
|
89
|
-
* @returns Validation equivalent of the Option
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* fromOption(some(42), () => "missing") // Valid(42)
|
|
94
|
-
* fromOption(none(), () => "missing") // Invalid(["missing"])
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
function fromOption(opt, onNone) {
|
|
98
|
-
return opt.match(() => invalid(onNone()), (value) => valid(value));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
//#endregion
|
|
102
|
-
//#region src/validation/helpers.ts
|
|
103
|
-
/**
|
|
104
|
-
* Combines two Validations using a mapping function.
|
|
105
|
-
* If both are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
106
|
-
*
|
|
107
|
-
* @param valA - First Validation to combine
|
|
108
|
-
* @param valB - Second Validation to combine
|
|
109
|
-
* @param mapper - Function to combine both valid values
|
|
110
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* map2(valid("John"), valid("Doe"), (first, last) => `${first} ${last}`)
|
|
115
|
-
* // Valid("John Doe")
|
|
116
|
-
* map2(invalid("e1"), invalid("e2"), mapper)
|
|
117
|
-
* // Invalid(["e1", "e2"])
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
function map2(valA, valB, mapper) {
|
|
121
|
-
return valA.map((a) => (b) => mapper(a, b)).ap(valB);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Combines three Validations using a mapping function.
|
|
125
|
-
* If all are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
126
|
-
*
|
|
127
|
-
* @param valA - First Validation to combine
|
|
128
|
-
* @param valB - Second Validation to combine
|
|
129
|
-
* @param valC - Third Validation to combine
|
|
130
|
-
* @param mapper - Function to combine all three valid values
|
|
131
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* ```typescript
|
|
135
|
-
* map3(valid(1), valid(2), valid(3), (a, b, c) => a + b + c)
|
|
136
|
-
* // Valid(6)
|
|
137
|
-
* map3(invalid("e1"), valid(2), invalid("e3"), mapper)
|
|
138
|
-
* // Invalid(["e1", "e3"])
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
function map3(valA, valB, valC, mapper) {
|
|
142
|
-
return valA.map((a) => (b) => (c) => mapper(a, b, c)).ap(valB).ap(valC);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Converts an array of Validations into a Validation of array.
|
|
146
|
-
* Returns Valid with all values if all Validations are Valid, otherwise accumulates all errors.
|
|
147
|
-
*
|
|
148
|
-
* @param validations - Array of Validations to sequence
|
|
149
|
-
* @returns Validation containing array of all values, or all accumulated errors
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* ```typescript
|
|
153
|
-
* sequence([valid(1), valid(2), valid(3)]) // Valid([1, 2, 3])
|
|
154
|
-
* sequence([valid(1), invalid("e1"), invalid("e2")]) // Invalid(["e1", "e2"])
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
function sequence(validations) {
|
|
158
|
-
const errors = [];
|
|
159
|
-
const values = [];
|
|
160
|
-
for (const v of validations) v.match((errs) => errors.push(...errs), (value) => values.push(value));
|
|
161
|
-
return errors.length > 0 ? createValidation({ invalid: errors }) : createValidation({ valid: values });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
//#endregion
|
|
165
|
-
export { fromEither, fromOption, invalid, map2, map3, sequence, valid };
|