You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Validates the `Right` value based on a predicate. If the predicate returns `true`, keeps the value. If it returns `false`, converts to `Left` with the provided error. Does nothing for `Left`.
105
+
106
+
```ts
107
+
import { Left, Right } from'holo-fn/either';
108
+
109
+
const result1 =newRight<string, number>(25).validate((n) =>n>=18, 'Must be 18+');
110
+
console.log(result1.unwrapOr(0)); // 25
111
+
112
+
const result2 =newRight<string, number>(15).validate((n) =>n>=18, 'Must be 18+');
113
+
console.log(result2.isLeft()); // true
114
+
console.log(result2.unwrapOr(0)); // 0
115
+
116
+
const result3 =newLeft<string, number>('Already failed').validate((n) =>n>=18, 'Must be 18+');
117
+
console.log(result3.isLeft()); // true (keeps original error)
101
118
102
-
console.log(result2); // 0
103
119
```
104
120
105
121
### `unwrapOr(defaultValue: R): R`
@@ -387,6 +403,69 @@ console.log(result); // 10
387
403
388
404
---
389
405
406
+
### `validate`
407
+
408
+
Curried version of `validate` for `Either`. This allows filtering/validating values in a functional pipeline with custom error values.
Validates the `Ok` value based on a predicate. If the predicate returns `true`, keeps the value. If it returns `false`, converts to `Err` with the provided error. Does nothing for `Err`.
62
+
63
+
```ts
64
+
import { Ok, Err } from"holo-fn/result";
65
+
66
+
const result1 =newOk(25).validate((n) =>n>=18, 'Must be 18+');
67
+
console.log(result1.unwrapOr(0)); // 25
68
+
69
+
const result2 =newOk(15).validate((n) =>n>=18, 'Must be 18+');
70
+
console.log(result2.isErr()); // true
71
+
72
+
const result3 =newErr<number, string>('Already failed').validate((n) =>n>=18, 'Must be 18+');
73
+
console.log(result3.isErr()); // true (keeps original error)
74
+
```
75
+
60
76
### `unwrapOr(defaultValue: T): T`
61
77
Returns the value of `Ok`, or the default value for `Err`.
62
78
@@ -277,6 +293,69 @@ console.log(result); // 15
277
293
278
294
---
279
295
296
+
### `validate`
297
+
298
+
Curried version of `validate` for `Result`. This allows filtering/validating values in a functional pipeline with custom error messages.
0 commit comments