@nlozgachev/pipekit 0.2.0 → 0.4.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 +18 -13
- package/esm/src/Composition/flip.js +2 -2
- package/esm/src/Composition/fn.js +1 -1
- package/esm/src/Composition/tap.js +1 -1
- package/esm/src/Core/Arr.js +4 -4
- package/esm/src/Core/Option.js +13 -22
- package/esm/src/Core/RemoteData.js +9 -13
- package/esm/src/Core/Result.js +12 -21
- package/esm/src/Core/Task.js +26 -23
- package/esm/src/Core/TaskOption.js +8 -6
- package/esm/src/Core/TaskResult.js +10 -6
- package/esm/src/Core/TaskValidation.js +12 -8
- package/esm/src/Core/These.js +122 -123
- package/esm/src/Core/Validation.js +42 -43
- package/package.json +1 -1
- package/script/src/Composition/flip.js +2 -2
- package/script/src/Composition/fn.js +1 -1
- package/script/src/Composition/tap.js +1 -1
- package/script/src/Core/Arr.js +4 -4
- package/script/src/Core/Option.js +13 -22
- package/script/src/Core/RemoteData.js +9 -13
- package/script/src/Core/Result.js +12 -21
- package/script/src/Core/Task.js +26 -23
- package/script/src/Core/TaskOption.js +8 -6
- package/script/src/Core/TaskResult.js +10 -6
- package/script/src/Core/TaskValidation.js +12 -8
- package/script/src/Core/These.js +122 -123
- package/script/src/Core/Validation.js +42 -43
- package/types/src/Composition/flip.d.ts +2 -2
- package/types/src/Composition/fn.d.ts +1 -1
- package/types/src/Composition/pipe.d.ts +1 -1
- package/types/src/Composition/tap.d.ts +1 -1
- package/types/src/Composition/uncurry.d.ts +3 -3
- package/types/src/Core/Arr.d.ts +4 -4
- package/types/src/Core/InternalTypes.d.ts +6 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -1
- package/types/src/Core/Option.d.ts +14 -23
- package/types/src/Core/Option.d.ts.map +1 -1
- package/types/src/Core/RemoteData.d.ts +9 -13
- package/types/src/Core/RemoteData.d.ts.map +1 -1
- package/types/src/Core/Result.d.ts +12 -21
- package/types/src/Core/Result.d.ts.map +1 -1
- package/types/src/Core/Task.d.ts +35 -32
- package/types/src/Core/Task.d.ts.map +1 -1
- package/types/src/Core/TaskOption.d.ts +1 -1
- package/types/src/Core/TaskOption.d.ts.map +1 -1
- package/types/src/Core/TaskResult.d.ts +1 -1
- package/types/src/Core/TaskResult.d.ts.map +1 -1
- package/types/src/Core/TaskValidation.d.ts +10 -6
- package/types/src/Core/TaskValidation.d.ts.map +1 -1
- package/types/src/Core/These.d.ts +108 -100
- package/types/src/Core/These.d.ts.map +1 -1
- package/types/src/Core/Validation.d.ts +38 -42
- package/types/src/Core/Validation.d.ts.map +1 -1
|
@@ -1,124 +1,140 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { WithError, WithKind, WithValue } from "./InternalTypes.js";
|
|
1
|
+
import { WithFirst, WithKind, WithSecond } from "./InternalTypes.js";
|
|
3
2
|
/**
|
|
4
|
-
* These<
|
|
5
|
-
* value (
|
|
6
|
-
*
|
|
3
|
+
* These<A, B> is an inclusive-OR type: it holds a first value (A), a second
|
|
4
|
+
* value (B), or both simultaneously. Neither side carries a success/failure
|
|
5
|
+
* connotation — it is a neutral pair where any combination is valid.
|
|
7
6
|
*
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* - Both(
|
|
7
|
+
* - First(a) — only a first value
|
|
8
|
+
* - Second(b) — only a second value
|
|
9
|
+
* - Both(a, b) — first and second values simultaneously
|
|
10
|
+
*
|
|
11
|
+
* A common use: lenient parsers or processors that carry a diagnostic note
|
|
12
|
+
* alongside a result, without losing either piece of information.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* const parse = (s: string): These<
|
|
15
|
-
* const
|
|
16
|
-
*
|
|
17
|
-
* if (
|
|
18
|
-
* return These.
|
|
16
|
+
* const parse = (s: string): These<number, string> => {
|
|
17
|
+
* const trimmed = s.trim();
|
|
18
|
+
* const n = parseFloat(trimmed);
|
|
19
|
+
* if (isNaN(n)) return These.second("Not a number");
|
|
20
|
+
* if (s !== trimmed) return These.both(n, "Leading/trailing whitespace trimmed");
|
|
21
|
+
* return These.first(n);
|
|
19
22
|
* };
|
|
20
23
|
* ```
|
|
21
24
|
*/
|
|
22
|
-
export type These<
|
|
23
|
-
export type
|
|
25
|
+
export type These<A, B> = TheseFirst<A> | TheseSecond<B> | TheseBoth<A, B>;
|
|
26
|
+
export type TheseFirst<T> = WithKind<"First"> & WithFirst<T>;
|
|
27
|
+
export type TheseSecond<T> = WithKind<"Second"> & WithSecond<T>;
|
|
28
|
+
export type TheseBoth<First, Second> = WithKind<"Both"> & WithFirst<First> & WithSecond<Second>;
|
|
24
29
|
export declare namespace These {
|
|
25
30
|
/**
|
|
26
|
-
* Creates a These holding only
|
|
31
|
+
* Creates a These holding only a first value.
|
|
27
32
|
*
|
|
28
33
|
* @example
|
|
29
34
|
* ```ts
|
|
30
|
-
* These.
|
|
35
|
+
* These.first(42); // { kind: "First", first: 42 }
|
|
31
36
|
* ```
|
|
32
37
|
*/
|
|
33
|
-
const
|
|
38
|
+
const first: <A>(value: A) => TheseFirst<A>;
|
|
34
39
|
/**
|
|
35
|
-
* Creates a These holding only a
|
|
40
|
+
* Creates a These holding only a second value.
|
|
36
41
|
*
|
|
37
42
|
* @example
|
|
38
43
|
* ```ts
|
|
39
|
-
* These.
|
|
44
|
+
* These.second("warning"); // { kind: "Second", second: "warning" }
|
|
40
45
|
* ```
|
|
41
46
|
*/
|
|
42
|
-
const
|
|
47
|
+
const second: <B>(value: B) => TheseSecond<B>;
|
|
43
48
|
/**
|
|
44
|
-
* Creates a These holding both
|
|
49
|
+
* Creates a These holding both a first and a second value simultaneously.
|
|
45
50
|
*
|
|
46
51
|
* @example
|
|
47
52
|
* ```ts
|
|
48
|
-
* These.both("Deprecated API used",
|
|
53
|
+
* These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
49
54
|
* ```
|
|
50
55
|
*/
|
|
51
|
-
const both: <
|
|
56
|
+
const both: <A, B>(first: A, second: B) => TheseBoth<A, B>;
|
|
52
57
|
/**
|
|
53
|
-
* Type guard — checks if a These holds only
|
|
58
|
+
* Type guard — checks if a These holds only a first value.
|
|
54
59
|
*/
|
|
55
|
-
const
|
|
60
|
+
const isFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A>;
|
|
56
61
|
/**
|
|
57
|
-
* Type guard — checks if a These holds only a
|
|
62
|
+
* Type guard — checks if a These holds only a second value.
|
|
58
63
|
*/
|
|
59
|
-
const
|
|
64
|
+
const isSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B>;
|
|
60
65
|
/**
|
|
61
|
-
* Type guard — checks if a These holds both
|
|
66
|
+
* Type guard — checks if a These holds both values simultaneously.
|
|
62
67
|
*/
|
|
63
|
-
const isBoth: <
|
|
68
|
+
const isBoth: <A, B>(data: These<A, B>) => data is TheseBoth<A, B>;
|
|
64
69
|
/**
|
|
65
|
-
* Returns true if the These contains a
|
|
70
|
+
* Returns true if the These contains a first value (First or Both).
|
|
66
71
|
*/
|
|
67
|
-
const
|
|
72
|
+
const hasFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A> | TheseBoth<A, B>;
|
|
68
73
|
/**
|
|
69
|
-
* Returns true if the These contains
|
|
74
|
+
* Returns true if the These contains a second value (Second or Both).
|
|
70
75
|
*/
|
|
71
|
-
const
|
|
76
|
+
const hasSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B> | TheseBoth<A, B>;
|
|
72
77
|
/**
|
|
73
|
-
* Transforms the
|
|
78
|
+
* Transforms the first value, leaving the second unchanged.
|
|
74
79
|
*
|
|
75
80
|
* @example
|
|
76
81
|
* ```ts
|
|
77
|
-
* pipe(These.
|
|
78
|
-
* pipe(These.both("warn"
|
|
79
|
-
* pipe(These.
|
|
82
|
+
* pipe(These.first(5), These.mapFirst(n => n * 2)); // First(10)
|
|
83
|
+
* pipe(These.both(5, "warn"), These.mapFirst(n => n * 2)); // Both(10, "warn")
|
|
84
|
+
* pipe(These.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
80
85
|
* ```
|
|
81
86
|
*/
|
|
82
|
-
const
|
|
87
|
+
const mapFirst: <A, C>(f: (a: A) => C) => <B>(data: These<A, B>) => These<C, B>;
|
|
83
88
|
/**
|
|
84
|
-
* Transforms the
|
|
89
|
+
* Transforms the second value, leaving the first unchanged.
|
|
85
90
|
*
|
|
86
91
|
* @example
|
|
87
92
|
* ```ts
|
|
88
|
-
* pipe(These.
|
|
89
|
-
* pipe(These.both("warn"
|
|
93
|
+
* pipe(These.second("warn"), These.mapSecond(e => e.toUpperCase())); // Second("WARN")
|
|
94
|
+
* pipe(These.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
90
95
|
* ```
|
|
91
96
|
*/
|
|
92
|
-
const
|
|
97
|
+
const mapSecond: <B, D>(f: (b: B) => D) => <A>(data: These<A, B>) => These<A, D>;
|
|
93
98
|
/**
|
|
94
|
-
* Transforms both the
|
|
99
|
+
* Transforms both the first and second values independently.
|
|
95
100
|
*
|
|
96
101
|
* @example
|
|
97
102
|
* ```ts
|
|
98
103
|
* pipe(
|
|
99
|
-
* These.both("warn"
|
|
100
|
-
* These.
|
|
101
|
-
* ); // Both("WARN"
|
|
104
|
+
* These.both(5, "warn"),
|
|
105
|
+
* These.mapBoth(n => n * 2, e => e.toUpperCase())
|
|
106
|
+
* ); // Both(10, "WARN")
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
const mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (data: These<A, B>) => These<C, D>;
|
|
110
|
+
/**
|
|
111
|
+
* Chains These computations by passing the first value to f.
|
|
112
|
+
* Second propagates unchanged; First and Both apply f to the first value.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const double = (n: number): These<number, string> => These.first(n * 2);
|
|
117
|
+
*
|
|
118
|
+
* pipe(These.first(5), These.chainFirst(double)); // First(10)
|
|
119
|
+
* pipe(These.both(5, "warn"), These.chainFirst(double)); // First(10)
|
|
120
|
+
* pipe(These.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
102
121
|
* ```
|
|
103
122
|
*/
|
|
104
|
-
const
|
|
123
|
+
const chainFirst: <A, B, C>(f: (a: A) => These<C, B>) => (data: These<A, B>) => These<C, B>;
|
|
105
124
|
/**
|
|
106
|
-
* Chains These computations by passing the
|
|
107
|
-
*
|
|
108
|
-
* - Ok(a) applies f(a) directly.
|
|
109
|
-
* - Both(e, a): applies f(a); if the result is Ok(b), returns Both(e, b)
|
|
110
|
-
* to preserve the warning. Otherwise returns f(a) as-is.
|
|
125
|
+
* Chains These computations by passing the second value to f.
|
|
126
|
+
* First propagates unchanged; Second and Both apply f to the second value.
|
|
111
127
|
*
|
|
112
128
|
* @example
|
|
113
129
|
* ```ts
|
|
114
|
-
* const
|
|
130
|
+
* const shout = (s: string): These<number, string> => These.second(s.toUpperCase());
|
|
115
131
|
*
|
|
116
|
-
* pipe(These.
|
|
117
|
-
* pipe(These.both("warn"
|
|
118
|
-
* pipe(These.
|
|
132
|
+
* pipe(These.second("warn"), These.chainSecond(shout)); // Second("WARN")
|
|
133
|
+
* pipe(These.both(5, "warn"), These.chainSecond(shout)); // Second("WARN")
|
|
134
|
+
* pipe(These.first(5), These.chainSecond(shout)); // First(5)
|
|
119
135
|
* ```
|
|
120
136
|
*/
|
|
121
|
-
const
|
|
137
|
+
const chainSecond: <A, B, D>(f: (b: B) => These<A, D>) => (data: These<A, B>) => These<A, D>;
|
|
122
138
|
/**
|
|
123
139
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
124
140
|
*
|
|
@@ -127,14 +143,14 @@ export declare namespace These {
|
|
|
127
143
|
* pipe(
|
|
128
144
|
* these,
|
|
129
145
|
* These.fold(
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* (
|
|
146
|
+
* a => `First: ${a}`,
|
|
147
|
+
* b => `Second: ${b}`,
|
|
148
|
+
* (a, b) => `Both: ${a} / ${b}`
|
|
133
149
|
* )
|
|
134
150
|
* );
|
|
135
151
|
* ```
|
|
136
152
|
*/
|
|
137
|
-
const fold: <
|
|
153
|
+
const fold: <A, B, C>(onFirst: (a: A) => C, onSecond: (b: B) => C, onBoth: (a: A, b: B) => C) => (data: These<A, B>) => C;
|
|
138
154
|
/**
|
|
139
155
|
* Pattern matches on a These, returning the result of the matching case.
|
|
140
156
|
*
|
|
@@ -143,71 +159,63 @@ export declare namespace These {
|
|
|
143
159
|
* pipe(
|
|
144
160
|
* these,
|
|
145
161
|
* These.match({
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* both: (
|
|
162
|
+
* first: a => `First: ${a}`,
|
|
163
|
+
* second: b => `Second: ${b}`,
|
|
164
|
+
* both: (a, b) => `Both: ${a} / ${b}`
|
|
149
165
|
* })
|
|
150
166
|
* );
|
|
151
167
|
* ```
|
|
152
168
|
*/
|
|
153
|
-
const match: <
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
both: (
|
|
157
|
-
}) => (data: These<
|
|
169
|
+
const match: <A, B, C>(cases: {
|
|
170
|
+
first: (a: A) => C;
|
|
171
|
+
second: (b: B) => C;
|
|
172
|
+
both: (a: A, b: B) => C;
|
|
173
|
+
}) => (data: These<A, B>) => C;
|
|
158
174
|
/**
|
|
159
|
-
* Returns the
|
|
175
|
+
* Returns the first value, or a default if the These has no first value.
|
|
160
176
|
*
|
|
161
177
|
* @example
|
|
162
178
|
* ```ts
|
|
163
|
-
* pipe(These.
|
|
164
|
-
* pipe(These.both("warn"
|
|
165
|
-
* pipe(These.
|
|
179
|
+
* pipe(These.first(5), These.getFirstOrElse(0)); // 5
|
|
180
|
+
* pipe(These.both(5, "warn"), These.getFirstOrElse(0)); // 5
|
|
181
|
+
* pipe(These.second("warn"), These.getFirstOrElse(0)); // 0
|
|
166
182
|
* ```
|
|
167
183
|
*/
|
|
168
|
-
const
|
|
169
|
-
/**
|
|
170
|
-
* Executes a side effect on the success value without changing the These.
|
|
171
|
-
* Useful for logging or debugging.
|
|
172
|
-
*/
|
|
173
|
-
const tap: <A>(f: (a: A) => void) => <E>(data: These<E, A>) => These<E, A>;
|
|
184
|
+
const getFirstOrElse: <A>(defaultValue: A) => <B>(data: These<A, B>) => A;
|
|
174
185
|
/**
|
|
175
|
-
*
|
|
176
|
-
* - Err(e) → Ok(e)
|
|
177
|
-
* - Ok(a) → Err(a)
|
|
178
|
-
* - Both(e, a) → Both(a, e)
|
|
186
|
+
* Returns the second value, or a default if the These has no second value.
|
|
179
187
|
*
|
|
180
188
|
* @example
|
|
181
189
|
* ```ts
|
|
182
|
-
* These.
|
|
183
|
-
* These.
|
|
184
|
-
* These.
|
|
190
|
+
* pipe(These.second("warn"), These.getSecondOrElse("none")); // "warn"
|
|
191
|
+
* pipe(These.both(5, "warn"), These.getSecondOrElse("none")); // "warn"
|
|
192
|
+
* pipe(These.first(5), These.getSecondOrElse("none")); // "none"
|
|
185
193
|
* ```
|
|
186
194
|
*/
|
|
187
|
-
const
|
|
195
|
+
const getSecondOrElse: <B>(defaultValue: B) => <A>(data: These<A, B>) => B;
|
|
188
196
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
197
|
+
* Executes a side effect on the first value without changing the These.
|
|
198
|
+
* Useful for logging or debugging.
|
|
191
199
|
*
|
|
192
200
|
* @example
|
|
193
201
|
* ```ts
|
|
194
|
-
* These.
|
|
195
|
-
* These.toOption(These.both("warn", 42)); // Some(42)
|
|
196
|
-
* These.toOption(These.err("err")); // None
|
|
202
|
+
* pipe(These.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
197
203
|
* ```
|
|
198
204
|
*/
|
|
199
|
-
const
|
|
205
|
+
const tap: <A>(f: (a: A) => void) => <B>(data: These<A, B>) => These<A, B>;
|
|
200
206
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
207
|
+
* Swaps the roles of first and second values.
|
|
208
|
+
* - First(a) → Second(a)
|
|
209
|
+
* - Second(b) → First(b)
|
|
210
|
+
* - Both(a, b) → Both(b, a)
|
|
203
211
|
*
|
|
204
212
|
* @example
|
|
205
213
|
* ```ts
|
|
206
|
-
* These.
|
|
207
|
-
* These.
|
|
208
|
-
* These.
|
|
214
|
+
* These.swap(These.first(5)); // Second(5)
|
|
215
|
+
* These.swap(These.second("warn")); // First("warn")
|
|
216
|
+
* These.swap(These.both(5, "warn")); // Both("warn", 5)
|
|
209
217
|
* ```
|
|
210
218
|
*/
|
|
211
|
-
const
|
|
219
|
+
const swap: <A, B>(data: These<A, B>) => These<B, A>;
|
|
212
220
|
}
|
|
213
221
|
//# sourceMappingURL=These.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"These.d.ts","sourceRoot":"","sources":["../../../src/src/Core/These.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"These.d.ts","sourceRoot":"","sources":["../../../src/src/Core/These.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE3E,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;AAC7D,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAChE,MAAM,MAAM,SAAS,CAAC,KAAK,EAAE,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC,MA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;;;;;;OAYG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAIxD,CAAC;CACH"}
|
|
@@ -11,14 +11,14 @@ import { WithErrors, WithKind, WithValue } from "./InternalTypes.js";
|
|
|
11
11
|
* @example
|
|
12
12
|
* ```ts
|
|
13
13
|
* const validateName = (name: string): Validation<string, string> =>
|
|
14
|
-
* name.length > 0 ? Validation.
|
|
14
|
+
* name.length > 0 ? Validation.valid(name) : Validation.invalid("Name is required");
|
|
15
15
|
*
|
|
16
16
|
* const validateAge = (age: number): Validation<string, number> =>
|
|
17
|
-
* age >= 0 ? Validation.
|
|
17
|
+
* age >= 0 ? Validation.valid(age) : Validation.invalid("Age must be positive");
|
|
18
18
|
*
|
|
19
19
|
* // Accumulates all errors using ap
|
|
20
20
|
* pipe(
|
|
21
|
-
* Validation.
|
|
21
|
+
* Validation.valid((name: string) => (age: number) => ({ name, age })),
|
|
22
22
|
* Validation.ap(validateName("")),
|
|
23
23
|
* Validation.ap(validateAge(-1))
|
|
24
24
|
* );
|
|
@@ -34,47 +34,43 @@ export declare namespace Validation {
|
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* ```ts
|
|
37
|
-
* Validation.
|
|
37
|
+
* Validation.valid(42); // Valid(42)
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
const
|
|
40
|
+
const valid: <E, A>(value: A) => Validation<E, A>;
|
|
41
41
|
/**
|
|
42
|
-
* Creates
|
|
43
|
-
*/
|
|
44
|
-
const toValid: <A>(value: A) => Valid<A>;
|
|
45
|
-
/**
|
|
46
|
-
* Type guard that checks if a Validation is valid.
|
|
47
|
-
*/
|
|
48
|
-
const isValid: <E, A>(data: Validation<E, A>) => data is Valid<A>;
|
|
49
|
-
/**
|
|
50
|
-
* Creates an invalid Validation with the given errors.
|
|
42
|
+
* Creates an invalid Validation from a single error.
|
|
51
43
|
*
|
|
52
44
|
* @example
|
|
53
45
|
* ```ts
|
|
54
|
-
* Validation.
|
|
46
|
+
* Validation.invalid("Invalid input");
|
|
55
47
|
* ```
|
|
56
48
|
*/
|
|
57
|
-
const
|
|
58
|
-
/**
|
|
59
|
-
* Type guard that checks if a Validation is invalid.
|
|
60
|
-
*/
|
|
61
|
-
const isInvalid: <E, A>(data: Validation<E, A>) => data is Invalid<E>;
|
|
49
|
+
const invalid: <E>(error: E) => Invalid<E>;
|
|
62
50
|
/**
|
|
63
|
-
* Creates an invalid Validation from
|
|
51
|
+
* Creates an invalid Validation from multiple errors.
|
|
64
52
|
*
|
|
65
53
|
* @example
|
|
66
54
|
* ```ts
|
|
67
|
-
* Validation.
|
|
55
|
+
* Validation.invalidAll(["Invalid input"]);
|
|
68
56
|
* ```
|
|
69
57
|
*/
|
|
70
|
-
const
|
|
58
|
+
const invalidAll: <E>(errors: NonEmptyList<E>) => Invalid<E>;
|
|
59
|
+
/**
|
|
60
|
+
* Type guard that checks if a Validation is valid.
|
|
61
|
+
*/
|
|
62
|
+
const isValid: <E, A>(data: Validation<E, A>) => data is Valid<A>;
|
|
63
|
+
/**
|
|
64
|
+
* Type guard that checks if a Validation is invalid.
|
|
65
|
+
*/
|
|
66
|
+
const isInvalid: <E, A>(data: Validation<E, A>) => data is Invalid<E>;
|
|
71
67
|
/**
|
|
72
68
|
* Transforms the success value inside a Validation.
|
|
73
69
|
*
|
|
74
70
|
* @example
|
|
75
71
|
* ```ts
|
|
76
|
-
* pipe(Validation.
|
|
77
|
-
* pipe(Validation.
|
|
72
|
+
* pipe(Validation.valid(5), Validation.map(n => n * 2)); // Valid(10)
|
|
73
|
+
* pipe(Validation.invalid("oops"), Validation.map(n => n * 2)); // Invalid(["oops"])
|
|
78
74
|
* ```
|
|
79
75
|
*/
|
|
80
76
|
const map: <A, B>(f: (a: A) => B) => <E>(data: Validation<E, A>) => Validation<E, B>;
|
|
@@ -87,10 +83,10 @@ export declare namespace Validation {
|
|
|
87
83
|
* @example
|
|
88
84
|
* ```ts
|
|
89
85
|
* const validatePositive = (n: number): Validation<string, number> =>
|
|
90
|
-
* n > 0 ? Validation.
|
|
86
|
+
* n > 0 ? Validation.valid(n) : Validation.invalid("Must be positive");
|
|
91
87
|
*
|
|
92
|
-
* pipe(Validation.
|
|
93
|
-
* pipe(Validation.
|
|
88
|
+
* pipe(Validation.valid(5), Validation.chain(validatePositive)); // Valid(5)
|
|
89
|
+
* pipe(Validation.valid(-1), Validation.chain(validatePositive)); // Invalid(["Must be positive"])
|
|
94
90
|
* ```
|
|
95
91
|
*/
|
|
96
92
|
const chain: <E, A, B>(f: (a: A) => Validation<E, B>) => (data: Validation<E, A>) => Validation<E, B>;
|
|
@@ -102,15 +98,15 @@ export declare namespace Validation {
|
|
|
102
98
|
* ```ts
|
|
103
99
|
* const add = (a: number) => (b: number) => a + b;
|
|
104
100
|
* pipe(
|
|
105
|
-
* Validation.
|
|
106
|
-
* Validation.ap(Validation.
|
|
107
|
-
* Validation.ap(Validation.
|
|
101
|
+
* Validation.valid(add),
|
|
102
|
+
* Validation.ap(Validation.valid(5)),
|
|
103
|
+
* Validation.ap(Validation.valid(3))
|
|
108
104
|
* ); // Valid(8)
|
|
109
105
|
*
|
|
110
106
|
* pipe(
|
|
111
|
-
* Validation.
|
|
112
|
-
* Validation.ap(Validation.
|
|
113
|
-
* Validation.ap(Validation.
|
|
107
|
+
* Validation.valid(add),
|
|
108
|
+
* Validation.ap(Validation.invalid<string, number>("bad a")),
|
|
109
|
+
* Validation.ap(Validation.invalid<string, number>("bad b"))
|
|
114
110
|
* ); // Invalid(["bad a", "bad b"])
|
|
115
111
|
* ```
|
|
116
112
|
*/
|
|
@@ -121,7 +117,7 @@ export declare namespace Validation {
|
|
|
121
117
|
* @example
|
|
122
118
|
* ```ts
|
|
123
119
|
* pipe(
|
|
124
|
-
* Validation.
|
|
120
|
+
* Validation.valid(42),
|
|
125
121
|
* Validation.fold(
|
|
126
122
|
* errors => `Errors: ${errors.join(", ")}`,
|
|
127
123
|
* value => `Value: ${value}`
|
|
@@ -153,8 +149,8 @@ export declare namespace Validation {
|
|
|
153
149
|
*
|
|
154
150
|
* @example
|
|
155
151
|
* ```ts
|
|
156
|
-
* pipe(Validation.
|
|
157
|
-
* pipe(Validation.
|
|
152
|
+
* pipe(Validation.valid(5), Validation.getOrElse(0)); // 5
|
|
153
|
+
* pipe(Validation.invalid("oops"), Validation.getOrElse(0)); // 0
|
|
158
154
|
* ```
|
|
159
155
|
*/
|
|
160
156
|
const getOrElse: <E, A>(defaultValue: A) => (data: Validation<E, A>) => A;
|
|
@@ -164,7 +160,7 @@ export declare namespace Validation {
|
|
|
164
160
|
* @example
|
|
165
161
|
* ```ts
|
|
166
162
|
* pipe(
|
|
167
|
-
* Validation.
|
|
163
|
+
* Validation.valid(5),
|
|
168
164
|
* Validation.tap(n => console.log("Value:", n)),
|
|
169
165
|
* Validation.map(n => n * 2)
|
|
170
166
|
* );
|
|
@@ -187,13 +183,13 @@ export declare namespace Validation {
|
|
|
187
183
|
* @example
|
|
188
184
|
* ```ts
|
|
189
185
|
* Validation.combine(
|
|
190
|
-
* Validation.
|
|
191
|
-
* Validation.
|
|
186
|
+
* Validation.invalid("Error 1"),
|
|
187
|
+
* Validation.invalid("Error 2")
|
|
192
188
|
* ); // Invalid(["Error 1", "Error 2"])
|
|
193
189
|
*
|
|
194
190
|
* Validation.combine(
|
|
195
|
-
* Validation.
|
|
196
|
-
* Validation.
|
|
191
|
+
* Validation.valid("a"),
|
|
192
|
+
* Validation.valid("b")
|
|
197
193
|
* ); // Valid("b")
|
|
198
194
|
* ```
|
|
199
195
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Validation.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,QA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