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/option.mjs
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
//#region src/option/model.ts
|
|
2
|
-
const NONE = Symbol("None");
|
|
3
|
-
|
|
4
|
-
//#endregion
|
|
5
|
-
//#region src/option/option.ts
|
|
6
|
-
function createOption(optionValue) {
|
|
7
|
-
const option = {
|
|
8
|
-
filter: (predicate) => option.flatMap((value) => predicate(value) ? option : createOption(NONE)),
|
|
9
|
-
map: (mapper) => option.flatMap((value) => createOption({ some: mapper(value) })),
|
|
10
|
-
orElse: (fallback) => option.match(fallback, () => option),
|
|
11
|
-
ap: function(optA) {
|
|
12
|
-
return this.flatMap((fn) => optA.match(() => createOption(NONE), (arg) => createOption({ some: fn(arg) })));
|
|
13
|
-
},
|
|
14
|
-
zip: (optA) => option.map((value) => (a) => [value, a]).ap(optA),
|
|
15
|
-
flatten: function() {
|
|
16
|
-
return this.flatMap((value) => value);
|
|
17
|
-
},
|
|
18
|
-
flatMap: (mapper) => option.match(() => forceCast(option), (value) => mapper(value)),
|
|
19
|
-
tap: (sideEffect) => {
|
|
20
|
-
option.match(() => {}, sideEffect);
|
|
21
|
-
return option;
|
|
22
|
-
},
|
|
23
|
-
tapNone: (sideEffect) => {
|
|
24
|
-
option.match(sideEffect, () => {});
|
|
25
|
-
return option;
|
|
26
|
-
},
|
|
27
|
-
match: (onNone, onSome) => isSome(optionValue) ? onSome(optionValue.some) : onNone(),
|
|
28
|
-
getOrElse: (fallback) => option.match(fallback, (value) => value),
|
|
29
|
-
toNullable: () => option.match(() => null, (value) => value),
|
|
30
|
-
toArray: () => option.match(() => [], (val) => [val])
|
|
31
|
-
};
|
|
32
|
-
return option;
|
|
33
|
-
}
|
|
34
|
-
function isSome(option) {
|
|
35
|
-
return typeof option === "object" && "some" in option;
|
|
36
|
-
}
|
|
37
|
-
function forceCast(option) {
|
|
38
|
-
return option;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region src/option/constructors.ts
|
|
43
|
-
/**
|
|
44
|
-
* Creates an {@link Option} that contains a value.
|
|
45
|
-
*
|
|
46
|
-
* Use this to wrap an existing value into an `Option<T>` representing presence
|
|
47
|
-
* (as opposed to `none`, which represents absence).
|
|
48
|
-
*
|
|
49
|
-
* @typeParam T - Type of the wrapped value.
|
|
50
|
-
* @param some - The value to store in the option.
|
|
51
|
-
* @returns An {@link Option} containing the provided value.
|
|
52
|
-
*/
|
|
53
|
-
function some(some) {
|
|
54
|
-
return createOption({ some });
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Creates an {@link Option} representing the absence of a value.
|
|
58
|
-
*
|
|
59
|
-
* @typeParam T - The type of the value that would be contained if present.
|
|
60
|
-
* @returns An {@link Option} in the `None` state.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const value = none<number>();
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
function none() {
|
|
68
|
-
return createOption(NONE);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Creates an {@link Option} from a nullable value.
|
|
72
|
-
*
|
|
73
|
-
* If {@link nullable} is `null` or `undefined`, returns {@link none}.
|
|
74
|
-
* Otherwise, wraps the provided value in {@link some}.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* fromNullable(0).getOrElse(() => 123); // 0
|
|
78
|
-
* fromNullable(null).toNullable(); // null
|
|
79
|
-
*
|
|
80
|
-
* @typeParam T - The non-null value type to wrap.
|
|
81
|
-
* @param nullable - A value that may be `null` or `undefined`.
|
|
82
|
-
* @returns An {@link Option} that is `Some` when the value is present, otherwise `None`.
|
|
83
|
-
*/
|
|
84
|
-
function fromNullable(nullable) {
|
|
85
|
-
return nullable == null ? none() : some(nullable);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Creates an {@link Option} from an {@link Either} by extracting the Right value.
|
|
89
|
-
*
|
|
90
|
-
* If the Either is Right, returns Some containing the Right value.
|
|
91
|
-
* If the Either is Left, returns None (discarding the Left value).
|
|
92
|
-
*
|
|
93
|
-
* @typeParam L - The type of the Left value (error type) that will be discarded
|
|
94
|
-
* @typeParam R - The type of the Right value that will be preserved in the Option
|
|
95
|
-
* @param either - The Either to convert to an Option
|
|
96
|
-
* @returns Some containing the Right value, or None if the Either was Left
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```typescript
|
|
100
|
-
* const success = right(42);
|
|
101
|
-
* fromEither(success) // Some(42)
|
|
102
|
-
*
|
|
103
|
-
* const failure = left("error message");
|
|
104
|
-
* fromEither(failure) // None
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
function fromEither(either) {
|
|
108
|
-
return either.match(() => none(), (right) => some(right));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/option/helpers.ts
|
|
113
|
-
/**
|
|
114
|
-
* Combines two Options using a mapping function.
|
|
115
|
-
* Only applies the mapper if both Options contain values.
|
|
116
|
-
*
|
|
117
|
-
* @param optA - First Option to combine
|
|
118
|
-
* @param optB - Second Option to combine
|
|
119
|
-
* @param mapper - Function that combines both values
|
|
120
|
-
* @returns Option containing the combined result, or None if either Option is None
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* const greet = (first, last) => `Hi ${first} ${last}!`
|
|
125
|
-
* map2(some("John"), some("Doe"), greet) // Some("John Doe")
|
|
126
|
-
* map2(some("John"), none<string>(), greet) // None
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
function map2(optA, optB, mapper) {
|
|
130
|
-
return optA.map((a) => (b) => mapper(a, b)).ap(optB);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Combines three Options using a mapping function.
|
|
134
|
-
* Only applies the mapper if all three Options contain values.
|
|
135
|
-
*
|
|
136
|
-
* @param optA - First Option to combine
|
|
137
|
-
* @param optB - Second Option to combine
|
|
138
|
-
* @param optC - Third Option to combine
|
|
139
|
-
* @param mapper - Function that combines all three values
|
|
140
|
-
* @returns Option containing the combined result, or None if any Option is None
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```typescript
|
|
144
|
-
* const toDateString = (d, m, y) => `${d}/${m}/${y}`
|
|
145
|
-
* map3(some(15), some(6), some(2023), toDateString) // Some("15/6/2023")
|
|
146
|
-
* map3(some(15), some(6), none<number>(), toDateString) // None
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
|
-
function map3(optA, optB, optC, mapper) {
|
|
150
|
-
return optA.map((a) => (b) => (c) => mapper(a, b, c)).ap(optB).ap(optC);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Converts an array of Options into an Option of array.
|
|
154
|
-
* If all Options in the array contain values, returns Some containing an array of all values.
|
|
155
|
-
* If any Option in the array is None, returns None.
|
|
156
|
-
*
|
|
157
|
-
* @param opts - Array of Options to sequence
|
|
158
|
-
* @returns Some containing array of all values if all Options are Some, otherwise None
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* sequence([some(1), some(2), some(3)]) // Some([1, 2, 3])
|
|
163
|
-
* sequence([some(1), none(), some(3)]) // None
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
function sequence(opts) {
|
|
167
|
-
const out = [];
|
|
168
|
-
for (const opt of opts) {
|
|
169
|
-
const [value] = opt.toArray();
|
|
170
|
-
if (!value) return none();
|
|
171
|
-
out.push(value);
|
|
172
|
-
}
|
|
173
|
-
return some(out);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
//#endregion
|
|
177
|
-
export { NONE, fromEither, fromNullable, map2, map3, none, sequence, some };
|
package/dist/task.d.mts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
//#region src/task/model.d.ts
|
|
2
|
-
type TaskValue<T> = () => Promise<T>;
|
|
3
|
-
//#endregion
|
|
4
|
-
//#region src/task/task.d.ts
|
|
5
|
-
type Task<T> = {
|
|
6
|
-
/**
|
|
7
|
-
* Transforms the value produced by the Task using a mapping function.
|
|
8
|
-
*
|
|
9
|
-
* @typeParam U - The type of the transformed value
|
|
10
|
-
* @param mapper - Function to transform the resolved value
|
|
11
|
-
* @returns A new Task that resolves with the transformed value
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* task(5).map(x => x * 2).run() // Promise<10>
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
map: <U>(mapper: (value: T) => U) => Task<U>;
|
|
19
|
-
/**
|
|
20
|
-
* Applies a function wrapped in a Task to a value wrapped in a Task.
|
|
21
|
-
* Both Tasks are run concurrently.
|
|
22
|
-
*
|
|
23
|
-
* @typeParam A - The type of the argument value
|
|
24
|
-
* @typeParam U - The type of the function's return value
|
|
25
|
-
* @param arg - Task containing the argument to apply the function to
|
|
26
|
-
* @returns Task containing the function result
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
31
|
-
* task(add(5)).ap(task(3)).run() // Promise<8>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
ap: <A, U>(this: Task<(a: A) => U>, arg: Task<A>) => Task<U>;
|
|
35
|
-
/**
|
|
36
|
-
* Combines this Task with another Task into a tuple.
|
|
37
|
-
* Both Tasks are run concurrently.
|
|
38
|
-
*
|
|
39
|
-
* @typeParam A - The type of the value in the other Task
|
|
40
|
-
* @param taskA - The Task to combine with this one
|
|
41
|
-
* @returns Task containing a tuple of both values
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* task("Alice").zip(task(30)).run() // Promise<["Alice", 30]>
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
zip: <A>(taskA: Task<A>) => Task<readonly [T, A]>;
|
|
49
|
-
/**
|
|
50
|
-
* Flattens a nested Task structure by removing one level of nesting.
|
|
51
|
-
*
|
|
52
|
-
* @returns The inner Task result
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* task(task(42)).flatten().run() // Promise<42>
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
flatten: <U>(this: Task<Task<U>>) => Task<U>;
|
|
60
|
-
/**
|
|
61
|
-
* Chains Task-returning operations together (monadic bind).
|
|
62
|
-
*
|
|
63
|
-
* @typeParam U - The type of the value in the returned Task
|
|
64
|
-
* @param mapper - Function that takes a resolved value and returns a Task
|
|
65
|
-
* @returns The Task returned by mapper
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* task(10).flatMap(x => task(x * 2)).run() // Promise<20>
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
flatMap: <U>(mapper: (value: T) => Task<U>) => Task<U>;
|
|
73
|
-
/**
|
|
74
|
-
* Performs a side effect with the resolved value, returning the original Task unchanged.
|
|
75
|
-
*
|
|
76
|
-
* @param sideEffect - Function to execute with the resolved value (return value is ignored)
|
|
77
|
-
* @returns The same Task instance unchanged
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* task(42)
|
|
82
|
-
* .tap(value => console.log(`Got value: ${value}`))
|
|
83
|
-
* .run() // Logs: "Got value: 42", resolves to 42
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
tap: (sideEffect: (value: T) => unknown) => Task<T>;
|
|
87
|
-
/**
|
|
88
|
-
* Executes the Task and returns the resulting Promise.
|
|
89
|
-
*
|
|
90
|
-
* @returns A Promise that resolves with the Task's value
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* const result = await task(42).run(); // 42
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
run: () => Promise<T>;
|
|
98
|
-
};
|
|
99
|
-
//#endregion
|
|
100
|
-
//#region src/task/constructors.d.ts
|
|
101
|
-
/**
|
|
102
|
-
* Creates a Task that immediately resolves with the provided value.
|
|
103
|
-
*
|
|
104
|
-
* @typeParam T - The type of the value
|
|
105
|
-
* @param value - The value to wrap in a Task
|
|
106
|
-
* @returns Task that resolves with the given value
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* ```typescript
|
|
110
|
-
* const t = task(42); // Task<number>
|
|
111
|
-
* await t.run(); // 42
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
declare function task<T>(value: T): Task<T>;
|
|
115
|
-
/**
|
|
116
|
-
* Creates a Task from a lazy Promise-returning thunk.
|
|
117
|
-
* The thunk is not called until `run()` is invoked.
|
|
118
|
-
*
|
|
119
|
-
* @typeParam T - The type of the resolved value
|
|
120
|
-
* @param thunk - A function that returns a Promise
|
|
121
|
-
* @returns Task wrapping the lazy Promise
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```typescript
|
|
125
|
-
* const fetchUser = fromPromise(() => fetch("/api/user").then(r => r.json()));
|
|
126
|
-
* await fetchUser.run(); // fetches the user
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
declare function fromPromise<T>(thunk: () => Promise<T>): Task<T>;
|
|
130
|
-
//#endregion
|
|
131
|
-
//#region src/task/helpers.d.ts
|
|
132
|
-
/**
|
|
133
|
-
* Runs all Tasks concurrently and collects their results into an array.
|
|
134
|
-
* Equivalent to `Promise.all` for Tasks.
|
|
135
|
-
*
|
|
136
|
-
* @param tasks - Array of Tasks to run concurrently
|
|
137
|
-
* @returns Task that resolves with an array of all results
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```typescript
|
|
141
|
-
* await all([task(1), task(2), task(3)]).run() // [1, 2, 3]
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
declare function all<T>(tasks: Task<T>[]): Task<T[]>;
|
|
145
|
-
//#endregion
|
|
146
|
-
export { type Task, type TaskValue, all, fromPromise, task };
|
package/dist/task.mjs
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
//#region src/task/task.ts
|
|
2
|
-
function createTask(thunk) {
|
|
3
|
-
const t = {
|
|
4
|
-
map: (mapper) => createTask(() => thunk().then(mapper)),
|
|
5
|
-
ap: function(arg) {
|
|
6
|
-
return createTask(() => Promise.all([this.run(), arg.run()]).then(([fn, a]) => fn(a)));
|
|
7
|
-
},
|
|
8
|
-
zip: (taskA) => t.map((value) => (a) => [value, a]).ap(taskA),
|
|
9
|
-
flatten: function() {
|
|
10
|
-
return this.flatMap((value) => value);
|
|
11
|
-
},
|
|
12
|
-
flatMap: (mapper) => createTask(() => thunk().then((value) => mapper(value).run())),
|
|
13
|
-
tap: (sideEffect) => createTask(() => thunk().then((value) => {
|
|
14
|
-
sideEffect(value);
|
|
15
|
-
return value;
|
|
16
|
-
})),
|
|
17
|
-
run: () => thunk()
|
|
18
|
-
};
|
|
19
|
-
return t;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region src/task/constructors.ts
|
|
24
|
-
/**
|
|
25
|
-
* Creates a Task that immediately resolves with the provided value.
|
|
26
|
-
*
|
|
27
|
-
* @typeParam T - The type of the value
|
|
28
|
-
* @param value - The value to wrap in a Task
|
|
29
|
-
* @returns Task that resolves with the given value
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* const t = task(42); // Task<number>
|
|
34
|
-
* await t.run(); // 42
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
function task(value) {
|
|
38
|
-
return createTask(() => Promise.resolve(value));
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Creates a Task from a lazy Promise-returning thunk.
|
|
42
|
-
* The thunk is not called until `run()` is invoked.
|
|
43
|
-
*
|
|
44
|
-
* @typeParam T - The type of the resolved value
|
|
45
|
-
* @param thunk - A function that returns a Promise
|
|
46
|
-
* @returns Task wrapping the lazy Promise
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* const fetchUser = fromPromise(() => fetch("/api/user").then(r => r.json()));
|
|
51
|
-
* await fetchUser.run(); // fetches the user
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
function fromPromise(thunk) {
|
|
55
|
-
return createTask(thunk);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region src/task/helpers.ts
|
|
60
|
-
/**
|
|
61
|
-
* Runs all Tasks concurrently and collects their results into an array.
|
|
62
|
-
* Equivalent to `Promise.all` for Tasks.
|
|
63
|
-
*
|
|
64
|
-
* @param tasks - Array of Tasks to run concurrently
|
|
65
|
-
* @returns Task that resolves with an array of all results
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* await all([task(1), task(2), task(3)]).run() // [1, 2, 3]
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
function all(tasks) {
|
|
73
|
-
return createTask(() => Promise.all(tasks.map((t) => t.run())));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
//#endregion
|
|
77
|
-
export { all, fromPromise, task };
|
package/dist/validation.d.mts
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { l as Either, x as Option } from "./helpers-qdt9BxJz.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/validation/model.d.ts
|
|
4
|
-
type Valid<T> = {
|
|
5
|
-
readonly valid: T;
|
|
6
|
-
};
|
|
7
|
-
type Invalid<E> = {
|
|
8
|
-
readonly invalid: readonly E[];
|
|
9
|
-
};
|
|
10
|
-
type ValidationValue<E, T> = Valid<T> | Invalid<E>;
|
|
11
|
-
type ValidationResult<E, T> = {
|
|
12
|
-
readonly ok: true;
|
|
13
|
-
readonly value: T;
|
|
14
|
-
} | {
|
|
15
|
-
readonly ok: false;
|
|
16
|
-
readonly errors: readonly E[];
|
|
17
|
-
};
|
|
18
|
-
//#endregion
|
|
19
|
-
//#region src/validation/validation.d.ts
|
|
20
|
-
type Validation<E, T> = {
|
|
21
|
-
/**
|
|
22
|
-
* Filters the Valid value based on a predicate function.
|
|
23
|
-
* If the predicate fails, returns Invalid with the provided error.
|
|
24
|
-
* If this Validation is already Invalid, returns the same Invalid unchanged.
|
|
25
|
-
*
|
|
26
|
-
* @param predicate - Function that tests the valid value
|
|
27
|
-
* @param onInvalid - Function that provides the error value when predicate fails
|
|
28
|
-
* @returns The same Validation if Invalid or predicate passes, otherwise Invalid with the provided error
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```typescript
|
|
32
|
-
* const isPositive = (n: number) => n > 0;
|
|
33
|
-
* valid(5).filterOrElse(isPositive, () => "Must be positive") // Valid(5)
|
|
34
|
-
* valid(-3).filterOrElse(isPositive, () => "Must be positive") // Invalid(["Must be positive"])
|
|
35
|
-
* invalid("error").filterOrElse(isPositive, () => "Must be positive") // Invalid(["error"])
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
filterOrElse: (predicate: (value: T) => boolean, onInvalid: () => E) => Validation<E, T>;
|
|
39
|
-
/**
|
|
40
|
-
* Transforms the valid value using a mapping function.
|
|
41
|
-
* If this Validation is Invalid, returns the same Invalid unchanged.
|
|
42
|
-
*
|
|
43
|
-
* @param mapper - Function to transform the contained value
|
|
44
|
-
* @returns New Validation containing the transformed value, or the same Invalid
|
|
45
|
-
*/
|
|
46
|
-
map: <U>(mapper: (value: T) => U) => Validation<E, U>;
|
|
47
|
-
/**
|
|
48
|
-
* Applies a function wrapped in a Validation to a value wrapped in a Validation.
|
|
49
|
-
* Unlike Either, if both are Invalid, errors are accumulated (concatenated).
|
|
50
|
-
*
|
|
51
|
-
* @param arg - Validation containing the argument to apply the function to
|
|
52
|
-
* @returns Validation containing the result, or accumulated errors if any are Invalid
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
57
|
-
* valid(add(5)).ap(valid(3)) // Valid(8)
|
|
58
|
-
* valid(add(5)).ap(invalid("err")) // Invalid(["err"])
|
|
59
|
-
* invalid("e1").ap(invalid("e2")) // Invalid(["e1", "e2"]) ← errors accumulated!
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
ap: <EE, A, U>(this: Validation<E, (a: A) => U>, arg: Validation<EE, A>) => Validation<E | EE, U>;
|
|
63
|
-
/**
|
|
64
|
-
* Combines this Validation with another Validation into a tuple.
|
|
65
|
-
* If both are Valid, returns Valid containing a tuple of both values.
|
|
66
|
-
* If either is Invalid, accumulates all errors.
|
|
67
|
-
*
|
|
68
|
-
* @param valA - The Validation to combine with this one
|
|
69
|
-
* @returns Validation containing a tuple of both values, or accumulated errors
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* valid("Alice").zip(valid(30)) // Valid(["Alice", 30])
|
|
74
|
-
* valid("Alice").zip(invalid("No age")) // Invalid(["No age"])
|
|
75
|
-
* invalid("e1").zip(invalid("e2")) // Invalid(["e1", "e2"])
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
zip: <EE, A>(valA: Validation<EE, A>) => Validation<E | EE, readonly [T, A]>;
|
|
79
|
-
/**
|
|
80
|
-
* Pattern matches on the Validation, executing different functions based on its state.
|
|
81
|
-
*
|
|
82
|
-
* @param onInvalid - Function to execute if Validation is Invalid, receives the array of errors
|
|
83
|
-
* @param onValid - Function to execute if Validation is Valid, receives the value
|
|
84
|
-
* @returns The result of the executed function
|
|
85
|
-
*/
|
|
86
|
-
match: <U>(onInvalid: (errors: readonly E[]) => U, onValid: (value: T) => U) => U;
|
|
87
|
-
/**
|
|
88
|
-
* Extracts the valid value, or returns a fallback value if Invalid.
|
|
89
|
-
*
|
|
90
|
-
* @param fallback - Function that receives the errors and returns a default value
|
|
91
|
-
* @returns The valid value if present, otherwise the result of the fallback function
|
|
92
|
-
*/
|
|
93
|
-
getOrElse: (fallback: (errors: readonly E[]) => T) => T;
|
|
94
|
-
/**
|
|
95
|
-
* Performs a side effect if this Validation is Valid, returning the original Validation unchanged.
|
|
96
|
-
* If this Validation is Invalid, the side effect is not executed.
|
|
97
|
-
*
|
|
98
|
-
* @param sideEffect - Function to execute with the valid value (return value is ignored)
|
|
99
|
-
* @returns The same Validation instance unchanged
|
|
100
|
-
*/
|
|
101
|
-
tap: (sideEffect: (value: T) => void) => Validation<E, T>;
|
|
102
|
-
/**
|
|
103
|
-
* Performs a side effect if this Validation is Invalid, returning the original Validation unchanged.
|
|
104
|
-
* If this Validation is Valid, the side effect is not executed.
|
|
105
|
-
*
|
|
106
|
-
* @param sideEffect - Function to execute with the errors array (return value is ignored)
|
|
107
|
-
* @returns The same Validation instance unchanged
|
|
108
|
-
*/
|
|
109
|
-
tapInvalid: (sideEffect: (errors: readonly E[]) => void) => Validation<E, T>;
|
|
110
|
-
/**
|
|
111
|
-
* Converts the Validation to a ValidationResult type.
|
|
112
|
-
*
|
|
113
|
-
* @returns A ValidationResult object representing the Validation's state
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* valid(42).toResult() // { ok: true, value: 42 }
|
|
118
|
-
* invalid("error").toResult() // { ok: false, errors: ["error"] }
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
121
|
-
toResult: () => ValidationResult<E, T>;
|
|
122
|
-
};
|
|
123
|
-
//#endregion
|
|
124
|
-
//#region src/validation/constructors.d.ts
|
|
125
|
-
/**
|
|
126
|
-
* Creates a Validation containing a valid value.
|
|
127
|
-
*
|
|
128
|
-
* @param value - The valid value to wrap
|
|
129
|
-
* @returns Validation containing the valid value
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```typescript
|
|
133
|
-
* const v = valid(42); // Validation<never, number>
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
declare function valid<T, E = never>(value: T): Validation<E, T>;
|
|
137
|
-
/**
|
|
138
|
-
* Creates a Validation containing a single error.
|
|
139
|
-
*
|
|
140
|
-
* @param error - The error to wrap
|
|
141
|
-
* @returns Validation containing the error
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```typescript
|
|
145
|
-
* const v = invalid("Something went wrong"); // Validation<string, never>
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
declare function invalid<E, T = never>(error: E): Validation<E, T>;
|
|
149
|
-
/**
|
|
150
|
-
* Creates a Validation from an Either.
|
|
151
|
-
* Right values become Valid, Left values become Invalid (with a single error).
|
|
152
|
-
*
|
|
153
|
-
* @param either - The Either to convert
|
|
154
|
-
* @returns Validation equivalent of the Either
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* ```typescript
|
|
158
|
-
* fromEither(right(42)) // Valid(42)
|
|
159
|
-
* fromEither(left("error")) // Invalid(["error"])
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
declare function fromEither<E, T>(either: Either<E, T>): Validation<E, T>;
|
|
163
|
-
/**
|
|
164
|
-
* Creates a Validation from an Option.
|
|
165
|
-
* Some values become Valid, None becomes Invalid with the provided error.
|
|
166
|
-
*
|
|
167
|
-
* @param opt - The Option to convert
|
|
168
|
-
* @param onNone - Function that provides the error when Option is None
|
|
169
|
-
* @returns Validation equivalent of the Option
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* ```typescript
|
|
173
|
-
* fromOption(some(42), () => "missing") // Valid(42)
|
|
174
|
-
* fromOption(none(), () => "missing") // Invalid(["missing"])
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
declare function fromOption<E, T>(opt: Option<T>, onNone: () => E): Validation<E, T>;
|
|
178
|
-
//#endregion
|
|
179
|
-
//#region src/validation/helpers.d.ts
|
|
180
|
-
/**
|
|
181
|
-
* Combines two Validations using a mapping function.
|
|
182
|
-
* If both are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
183
|
-
*
|
|
184
|
-
* @param valA - First Validation to combine
|
|
185
|
-
* @param valB - Second Validation to combine
|
|
186
|
-
* @param mapper - Function to combine both valid values
|
|
187
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* map2(valid("John"), valid("Doe"), (first, last) => `${first} ${last}`)
|
|
192
|
-
* // Valid("John Doe")
|
|
193
|
-
* map2(invalid("e1"), invalid("e2"), mapper)
|
|
194
|
-
* // Invalid(["e1", "e2"])
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
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>;
|
|
198
|
-
/**
|
|
199
|
-
* Combines three Validations using a mapping function.
|
|
200
|
-
* If all are Valid, applies the mapper. If any are Invalid, accumulates all errors.
|
|
201
|
-
*
|
|
202
|
-
* @param valA - First Validation to combine
|
|
203
|
-
* @param valB - Second Validation to combine
|
|
204
|
-
* @param valC - Third Validation to combine
|
|
205
|
-
* @param mapper - Function to combine all three valid values
|
|
206
|
-
* @returns Validation containing the mapped result, or all accumulated errors
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```typescript
|
|
210
|
-
* map3(valid(1), valid(2), valid(3), (a, b, c) => a + b + c)
|
|
211
|
-
* // Valid(6)
|
|
212
|
-
* map3(invalid("e1"), valid(2), invalid("e3"), mapper)
|
|
213
|
-
* // Invalid(["e1", "e3"])
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
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>;
|
|
217
|
-
/**
|
|
218
|
-
* Converts an array of Validations into a Validation of array.
|
|
219
|
-
* Returns Valid with all values if all Validations are Valid, otherwise accumulates all errors.
|
|
220
|
-
*
|
|
221
|
-
* @param validations - Array of Validations to sequence
|
|
222
|
-
* @returns Validation containing array of all values, or all accumulated errors
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* sequence([valid(1), valid(2), valid(3)]) // Valid([1, 2, 3])
|
|
227
|
-
* sequence([valid(1), invalid("e1"), invalid("e2")]) // Invalid(["e1", "e2"])
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
declare function sequence<E, T>(validations: readonly Validation<E, T>[]): Validation<E, T[]>;
|
|
231
|
-
//#endregion
|
|
232
|
-
export { Invalid, Valid, type Validation, ValidationResult, ValidationValue, fromEither, fromOption, invalid, map2, map3, sequence, valid };
|