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/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 };
|