@tempots/std 0.25.1 → 0.26.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/async-result.cjs +1 -1
- package/async-result.d.ts +13 -12
- package/async-result.js +66 -59
- package/index.cjs +1 -1
- package/index.js +1 -1
- package/json.cjs +1 -1
- package/json.js +1 -1
- package/package.json +1 -1
- package/{result-7Po2Mxhx.js → result-BYto972d.js} +6 -4
- package/result-DBJ3htTg.cjs +1 -0
- package/result.cjs +1 -1
- package/result.d.ts +3 -1
- package/result.js +1 -1
- package/validation.cjs +1 -1
- package/validation.d.ts +3 -1
- package/validation.js +1 -1
- package/result-bSKKrSLB.cjs +0 -1
package/async-result.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s={notAsked:{type:"NotAsked"},loading(e=void 0){return{type:"Loading",previousValue:e}},success(e){return{type:"AsyncSuccess",value:e}},failure(e){return{type:"AsyncFailure",error:e}},isSuccess(e){return e.type==="AsyncSuccess"},isFailure(e){return e.type==="AsyncFailure"},isNotAsked(e){return e.type==="NotAsked"},isLoading(e){return e.type==="Loading"},getOrElse(e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s={notAsked:{type:"NotAsked"},loading(e=void 0){return{type:"Loading",previousValue:e}},success(e){return{type:"AsyncSuccess",value:e}},failure(e){return{type:"AsyncFailure",error:e}},isSuccess(e){return e.type==="AsyncSuccess"},isFailure(e){return e.type==="AsyncFailure"},isNotAsked(e){return e.type==="NotAsked"},isLoading(e){return e.type==="Loading"},getOrElse(e,u){return s.isSuccess(e)?e.value:s.isLoading(e)&&e.previousValue!==void 0?e.previousValue:u},getOrElseLazy(e,u){return s.isSuccess(e)?e.value:s.isLoading(e)&&e.previousValue!==void 0?e.previousValue:u()},getOrNull(e){return s.isSuccess(e)?e.value:s.isLoading(e)&&e.previousValue!==void 0?e.previousValue:null},getOrUndefined(e){if(s.isSuccess(e))return e.value;if(s.isLoading(e)&&e.previousValue!==void 0)return e.previousValue},getUnsafe:e=>{if(s.isSuccess(e))return e.value;if(s.isLoading(e)&&e.previousValue!==void 0)return e.previousValue;throw s.isFailure(e)?e.error:new Error("Cannot get value from a not-asked or loading result without previous value")},match:(e,{success:u,failure:r,loading:i,notAsked:a=i})=>s.isSuccess(e)?u(e.value):s.isFailure(e)?r(e.error):s.isNotAsked(e)?a():i(e.previousValue),effect:(e,u)=>{switch(e.type){case"AsyncSuccess":u.success?u.success(e.value):u.else?.();break;case"AsyncFailure":u.failure?u.failure(e.error):u.else?.();break;case"Loading":u.loading?u.loading(e.previousValue):u.else?.();break;case"NotAsked":u.notAsked?u.notAsked():u.else?.();break}return e},whenSuccess:(e,u)=>(s.isSuccess(e)&&u(e.value),e),whenFailure:(e,u)=>(s.isFailure(e)&&u(e.error),e),equals:(e,u,r={valueEquals:(i,a)=>i===a,errorEquals:(i,a)=>i===a})=>e.type==="AsyncSuccess"&&u.type==="AsyncSuccess"?r.valueEquals(e.value,u.value):e.type==="AsyncFailure"&&u.type==="AsyncFailure"?r.errorEquals(e.error,u.error):e.type==="Loading"&&u.type==="Loading"?r.valueEquals(e.previousValue,u.previousValue):e.type==="NotAsked"&&u.type==="NotAsked",all:e=>{const u=[];for(const r of e)if(s.isSuccess(r))u.push(r.value);else return r;return s.success(u)},ofPromise:async e=>{try{const u=await e;return s.success(u)}catch(u){return s.failure(u instanceof Error?u:new Error(String(u)))}},map:(e,u)=>{switch(e.type){case"AsyncSuccess":return s.success(u(e.value));case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(e.error);case"Loading":return s.loading(e.previousValue!=null?u(e.previousValue):void 0)}},flatMap:(e,u)=>{switch(e.type){case"AsyncSuccess":return u(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(e.error);case"Loading":return s.loading()}},mapError:(e,u)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(u(e.error));case"Loading":return s.loading(e.previousValue)}},flatMapError:(e,u)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return u(e.error);case"Loading":return s.loading(e.previousValue)}},toResult:e=>{switch(e.type){case"AsyncSuccess":return{type:"Success",value:e.value};case"AsyncFailure":return{type:"Failure",error:e.error};case"NotAsked":case"Loading":return}},isSettled(e){return e.type==="AsyncSuccess"||e.type==="AsyncFailure"},recover:(e,u)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.success(u(e.error));case"Loading":return s.loading(e.previousValue)}},ap:(e,u)=>s.isSuccess(e)&&s.isSuccess(u)?s.success(e.value(u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(u)?s.failure(u.error):s.isLoading(e)||s.isLoading(u)?s.loading():s.notAsked,map2:(e,u,r)=>s.isSuccess(e)&&s.isSuccess(u)?s.success(r(e.value,u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(u)?s.failure(u.error):s.isLoading(e)||s.isLoading(u)?s.loading():s.notAsked,map3:(e,u,r,i)=>s.isSuccess(e)&&s.isSuccess(u)&&s.isSuccess(r)?s.success(i(e.value,u.value,r.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(u)?s.failure(u.error):s.isFailure(r)?s.failure(r.error):s.isLoading(e)||s.isLoading(u)||s.isLoading(r)?s.loading():s.notAsked};exports.AsyncResult=s;
|
package/async-result.d.ts
CHANGED
|
@@ -118,40 +118,39 @@ export declare const AsyncResult: {
|
|
|
118
118
|
*/
|
|
119
119
|
isLoading<V, E>(r: AsyncResult<V, E>): r is Loading<V>;
|
|
120
120
|
/**
|
|
121
|
-
* Gets the value if the result is a success; otherwise, returns the alternative value.
|
|
121
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns the alternative value.
|
|
122
122
|
* @param r - The result.
|
|
123
123
|
* @param alt - The alternative value.
|
|
124
|
-
* @returns The value if the result is a success; otherwise, the alternative value.
|
|
124
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, the alternative value.
|
|
125
125
|
* @public
|
|
126
126
|
*/
|
|
127
127
|
getOrElse<V, E>(r: AsyncResult<V, E>, alt: V): V;
|
|
128
128
|
/**
|
|
129
|
-
* Gets the value if the result is a success; otherwise, returns the value from the alternative function.
|
|
129
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns the value from the alternative function.
|
|
130
130
|
* @param r - The result.
|
|
131
131
|
* @param altf - The alternative function.
|
|
132
|
-
* @returns The value if the result is a success; otherwise, the value from the alternative
|
|
132
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, the value from the alternative function.
|
|
133
133
|
* @public
|
|
134
|
-
* function.
|
|
135
134
|
*/
|
|
136
135
|
getOrElseLazy<V, E>(r: AsyncResult<V, E>, altf: () => V): V;
|
|
137
136
|
/**
|
|
138
|
-
* Gets the value if the result is a success; otherwise, returns `null`.
|
|
137
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns `null`.
|
|
139
138
|
* @param r - The result.
|
|
140
|
-
* @returns The value if the result is a success; otherwise, `null`.
|
|
139
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, `null`.
|
|
141
140
|
* @public
|
|
142
141
|
*/
|
|
143
142
|
getOrNull<V, E>(r: AsyncResult<V, E>): V | null;
|
|
144
143
|
/**
|
|
145
|
-
* Gets the value if the result is a success; otherwise, returns `undefined`.
|
|
144
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns `undefined`.
|
|
146
145
|
* @param r - The result.
|
|
147
|
-
* @returns The value if the result is a success; otherwise, `undefined`.
|
|
146
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, `undefined`.
|
|
148
147
|
* @public
|
|
149
148
|
*/
|
|
150
149
|
getOrUndefined<V, E>(r: AsyncResult<V, E>): Maybe<V>;
|
|
151
150
|
/**
|
|
152
|
-
* Gets the value of a `AsyncResult` if it is a `Success
|
|
151
|
+
* Gets the value of a `AsyncResult` if it is a `Success` or loading with a previous value, otherwise it throws the error contained in the `Failure`.
|
|
153
152
|
* @param r - The `AsyncResult` to get the value from.
|
|
154
|
-
* @returns The value of the `AsyncResult` if it is a `Success
|
|
153
|
+
* @returns The value of the `AsyncResult` if it is a `Success` or loading with previous value.
|
|
155
154
|
*/
|
|
156
155
|
getUnsafe: <V, E>(r: AsyncResult<V, E>) => V;
|
|
157
156
|
/**
|
|
@@ -172,8 +171,9 @@ export declare const AsyncResult: {
|
|
|
172
171
|
/**
|
|
173
172
|
* Executes side effects based on the state of the result.
|
|
174
173
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
174
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
175
175
|
* @param r - The result.
|
|
176
|
-
* @param handlers - An object with optional handlers for each state.
|
|
176
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
177
177
|
* @returns The result that was passed in, allowing for chaining.
|
|
178
178
|
* @public
|
|
179
179
|
*/
|
|
@@ -182,6 +182,7 @@ export declare const AsyncResult: {
|
|
|
182
182
|
failure?: (error: E) => void;
|
|
183
183
|
loading?: (previousValue?: V) => void;
|
|
184
184
|
notAsked?: () => void;
|
|
185
|
+
else?: () => void;
|
|
185
186
|
}) => AsyncResult<V, E>;
|
|
186
187
|
/**
|
|
187
188
|
* When the result is a success, it applies the function to the value.
|
package/async-result.js
CHANGED
|
@@ -70,53 +70,59 @@ const s = {
|
|
|
70
70
|
return e.type === "Loading";
|
|
71
71
|
},
|
|
72
72
|
/**
|
|
73
|
-
* Gets the value if the result is a success; otherwise, returns the alternative value.
|
|
73
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns the alternative value.
|
|
74
74
|
* @param r - The result.
|
|
75
75
|
* @param alt - The alternative value.
|
|
76
|
-
* @returns The value if the result is a success; otherwise, the alternative value.
|
|
76
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, the alternative value.
|
|
77
77
|
* @public
|
|
78
78
|
*/
|
|
79
|
-
getOrElse(e,
|
|
80
|
-
return s.isSuccess(e) ? e.value :
|
|
79
|
+
getOrElse(e, u) {
|
|
80
|
+
return s.isSuccess(e) ? e.value : s.isLoading(e) && e.previousValue !== void 0 ? e.previousValue : u;
|
|
81
81
|
},
|
|
82
82
|
/**
|
|
83
|
-
* Gets the value if the result is a success; otherwise, returns the value from the alternative function.
|
|
83
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns the value from the alternative function.
|
|
84
84
|
* @param r - The result.
|
|
85
85
|
* @param altf - The alternative function.
|
|
86
|
-
* @returns The value if the result is a success; otherwise, the value from the alternative
|
|
86
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, the value from the alternative function.
|
|
87
87
|
* @public
|
|
88
|
-
* function.
|
|
89
88
|
*/
|
|
90
|
-
getOrElseLazy(e,
|
|
91
|
-
return s.isSuccess(e) ? e.value :
|
|
89
|
+
getOrElseLazy(e, u) {
|
|
90
|
+
return s.isSuccess(e) ? e.value : s.isLoading(e) && e.previousValue !== void 0 ? e.previousValue : u();
|
|
92
91
|
},
|
|
93
92
|
/**
|
|
94
|
-
* Gets the value if the result is a success; otherwise, returns `null`.
|
|
93
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns `null`.
|
|
95
94
|
* @param r - The result.
|
|
96
|
-
* @returns The value if the result is a success; otherwise, `null`.
|
|
95
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, `null`.
|
|
97
96
|
* @public
|
|
98
97
|
*/
|
|
99
98
|
getOrNull(e) {
|
|
100
|
-
return s.isSuccess(e) ? e.value : null;
|
|
99
|
+
return s.isSuccess(e) ? e.value : s.isLoading(e) && e.previousValue !== void 0 ? e.previousValue : null;
|
|
101
100
|
},
|
|
102
101
|
/**
|
|
103
|
-
* Gets the value if the result is a success; otherwise, returns `undefined`.
|
|
102
|
+
* Gets the value if the result is a success or loading with a previous value; otherwise, returns `undefined`.
|
|
104
103
|
* @param r - The result.
|
|
105
|
-
* @returns The value if the result is a success; otherwise, `undefined`.
|
|
104
|
+
* @returns The value if the result is a success or loading with previous value; otherwise, `undefined`.
|
|
106
105
|
* @public
|
|
107
106
|
*/
|
|
108
107
|
getOrUndefined(e) {
|
|
109
|
-
|
|
108
|
+
if (s.isSuccess(e))
|
|
109
|
+
return e.value;
|
|
110
|
+
if (s.isLoading(e) && e.previousValue !== void 0)
|
|
111
|
+
return e.previousValue;
|
|
110
112
|
},
|
|
111
113
|
/**
|
|
112
|
-
* Gets the value of a `AsyncResult` if it is a `Success
|
|
114
|
+
* Gets the value of a `AsyncResult` if it is a `Success` or loading with a previous value, otherwise it throws the error contained in the `Failure`.
|
|
113
115
|
* @param r - The `AsyncResult` to get the value from.
|
|
114
|
-
* @returns The value of the `AsyncResult` if it is a `Success
|
|
116
|
+
* @returns The value of the `AsyncResult` if it is a `Success` or loading with previous value.
|
|
115
117
|
*/
|
|
116
118
|
getUnsafe: (e) => {
|
|
117
119
|
if (s.isSuccess(e))
|
|
118
120
|
return e.value;
|
|
119
|
-
|
|
121
|
+
if (s.isLoading(e) && e.previousValue !== void 0)
|
|
122
|
+
return e.previousValue;
|
|
123
|
+
throw s.isFailure(e) ? e.error : new Error(
|
|
124
|
+
"Cannot get value from a not-asked or loading result without previous value"
|
|
125
|
+
);
|
|
120
126
|
},
|
|
121
127
|
/**
|
|
122
128
|
* Based on the state of the result, it picks the appropriate function to call and returns the result.
|
|
@@ -128,32 +134,33 @@ const s = {
|
|
|
128
134
|
* @public
|
|
129
135
|
*/
|
|
130
136
|
match: (e, {
|
|
131
|
-
success:
|
|
132
|
-
failure:
|
|
133
|
-
loading:
|
|
134
|
-
notAsked:
|
|
135
|
-
}) => s.isSuccess(e) ?
|
|
137
|
+
success: u,
|
|
138
|
+
failure: r,
|
|
139
|
+
loading: i,
|
|
140
|
+
notAsked: a = i
|
|
141
|
+
}) => s.isSuccess(e) ? u(e.value) : s.isFailure(e) ? r(e.error) : s.isNotAsked(e) ? a() : i(e.previousValue),
|
|
136
142
|
/**
|
|
137
143
|
* Executes side effects based on the state of the result.
|
|
138
144
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
145
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
139
146
|
* @param r - The result.
|
|
140
|
-
* @param handlers - An object with optional handlers for each state.
|
|
147
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
141
148
|
* @returns The result that was passed in, allowing for chaining.
|
|
142
149
|
* @public
|
|
143
150
|
*/
|
|
144
|
-
effect: (e,
|
|
151
|
+
effect: (e, u) => {
|
|
145
152
|
switch (e.type) {
|
|
146
153
|
case "AsyncSuccess":
|
|
147
|
-
|
|
154
|
+
u.success ? u.success(e.value) : u.else?.();
|
|
148
155
|
break;
|
|
149
156
|
case "AsyncFailure":
|
|
150
|
-
|
|
157
|
+
u.failure ? u.failure(e.error) : u.else?.();
|
|
151
158
|
break;
|
|
152
159
|
case "Loading":
|
|
153
|
-
|
|
160
|
+
u.loading ? u.loading(e.previousValue) : u.else?.();
|
|
154
161
|
break;
|
|
155
162
|
case "NotAsked":
|
|
156
|
-
|
|
163
|
+
u.notAsked ? u.notAsked() : u.else?.();
|
|
157
164
|
break;
|
|
158
165
|
}
|
|
159
166
|
return e;
|
|
@@ -166,7 +173,7 @@ const s = {
|
|
|
166
173
|
* @returns The result that was passed in.
|
|
167
174
|
* @public
|
|
168
175
|
*/
|
|
169
|
-
whenSuccess: (e,
|
|
176
|
+
whenSuccess: (e, u) => (s.isSuccess(e) && u(e.value), e),
|
|
170
177
|
/**
|
|
171
178
|
* When the result is a failure, it applies the function to the error.
|
|
172
179
|
*
|
|
@@ -175,7 +182,7 @@ const s = {
|
|
|
175
182
|
* @returns The result that was passed in.
|
|
176
183
|
* @public
|
|
177
184
|
*/
|
|
178
|
-
whenFailure: (e,
|
|
185
|
+
whenFailure: (e, u) => (s.isFailure(e) && u(e.error), e),
|
|
179
186
|
/**
|
|
180
187
|
* Compares two results for equality.
|
|
181
188
|
* @param r1 - The first result.
|
|
@@ -183,23 +190,23 @@ const s = {
|
|
|
183
190
|
* @param options - The options to use for comparison. By default, uses strict equality.
|
|
184
191
|
* @returns `true` if the results are equal, `false` otherwise.
|
|
185
192
|
*/
|
|
186
|
-
equals: (e,
|
|
187
|
-
valueEquals: (
|
|
188
|
-
errorEquals: (
|
|
189
|
-
}) => e.type === "AsyncSuccess" &&
|
|
193
|
+
equals: (e, u, r = {
|
|
194
|
+
valueEquals: (i, a) => i === a,
|
|
195
|
+
errorEquals: (i, a) => i === a
|
|
196
|
+
}) => e.type === "AsyncSuccess" && u.type === "AsyncSuccess" ? r.valueEquals(e.value, u.value) : e.type === "AsyncFailure" && u.type === "AsyncFailure" ? r.errorEquals(e.error, u.error) : e.type === "Loading" && u.type === "Loading" ? r.valueEquals(e.previousValue, u.previousValue) : e.type === "NotAsked" && u.type === "NotAsked",
|
|
190
197
|
/**
|
|
191
198
|
* Combines multiple results into a single result.
|
|
192
199
|
* @param results - The results to combine.
|
|
193
200
|
* @returns A single result that is a success if all the input results are successes, otherwise a failure.
|
|
194
201
|
*/
|
|
195
202
|
all: (e) => {
|
|
196
|
-
const
|
|
197
|
-
for (const
|
|
198
|
-
if (s.isSuccess(
|
|
199
|
-
|
|
203
|
+
const u = [];
|
|
204
|
+
for (const r of e)
|
|
205
|
+
if (s.isSuccess(r))
|
|
206
|
+
u.push(r.value);
|
|
200
207
|
else
|
|
201
|
-
return
|
|
202
|
-
return s.success(
|
|
208
|
+
return r;
|
|
209
|
+
return s.success(u);
|
|
203
210
|
},
|
|
204
211
|
/**
|
|
205
212
|
* Converts a Promise to an AsyncResult.
|
|
@@ -208,10 +215,10 @@ const s = {
|
|
|
208
215
|
*/
|
|
209
216
|
ofPromise: async (e) => {
|
|
210
217
|
try {
|
|
211
|
-
const
|
|
212
|
-
return s.success(
|
|
213
|
-
} catch (
|
|
214
|
-
return s.failure(
|
|
218
|
+
const u = await e;
|
|
219
|
+
return s.success(u);
|
|
220
|
+
} catch (u) {
|
|
221
|
+
return s.failure(u instanceof Error ? u : new Error(String(u)));
|
|
215
222
|
}
|
|
216
223
|
},
|
|
217
224
|
/**
|
|
@@ -223,17 +230,17 @@ const s = {
|
|
|
223
230
|
* @returns A new `AsyncResult` with the mapped value if successful, otherwise the original state.
|
|
224
231
|
* @public
|
|
225
232
|
*/
|
|
226
|
-
map: (e,
|
|
233
|
+
map: (e, u) => {
|
|
227
234
|
switch (e.type) {
|
|
228
235
|
case "AsyncSuccess":
|
|
229
|
-
return s.success(
|
|
236
|
+
return s.success(u(e.value));
|
|
230
237
|
case "NotAsked":
|
|
231
238
|
return s.notAsked;
|
|
232
239
|
case "AsyncFailure":
|
|
233
240
|
return s.failure(e.error);
|
|
234
241
|
case "Loading":
|
|
235
242
|
return s.loading(
|
|
236
|
-
e.previousValue != null ?
|
|
243
|
+
e.previousValue != null ? u(e.previousValue) : void 0
|
|
237
244
|
);
|
|
238
245
|
}
|
|
239
246
|
},
|
|
@@ -245,10 +252,10 @@ const s = {
|
|
|
245
252
|
* @returns The result of the mapping function if successful, otherwise the original state.
|
|
246
253
|
* @public
|
|
247
254
|
*/
|
|
248
|
-
flatMap: (e,
|
|
255
|
+
flatMap: (e, u) => {
|
|
249
256
|
switch (e.type) {
|
|
250
257
|
case "AsyncSuccess":
|
|
251
|
-
return
|
|
258
|
+
return u(e.value);
|
|
252
259
|
case "NotAsked":
|
|
253
260
|
return s.notAsked;
|
|
254
261
|
case "AsyncFailure":
|
|
@@ -265,14 +272,14 @@ const s = {
|
|
|
265
272
|
* @returns A new `AsyncResult` with the mapped error if failed, otherwise the original state.
|
|
266
273
|
* @public
|
|
267
274
|
*/
|
|
268
|
-
mapError: (e,
|
|
275
|
+
mapError: (e, u) => {
|
|
269
276
|
switch (e.type) {
|
|
270
277
|
case "AsyncSuccess":
|
|
271
278
|
return s.success(e.value);
|
|
272
279
|
case "NotAsked":
|
|
273
280
|
return s.notAsked;
|
|
274
281
|
case "AsyncFailure":
|
|
275
|
-
return s.failure(
|
|
282
|
+
return s.failure(u(e.error));
|
|
276
283
|
case "Loading":
|
|
277
284
|
return s.loading(e.previousValue);
|
|
278
285
|
}
|
|
@@ -285,14 +292,14 @@ const s = {
|
|
|
285
292
|
* @returns The result of the recovery function if failed, otherwise the original state.
|
|
286
293
|
* @public
|
|
287
294
|
*/
|
|
288
|
-
flatMapError: (e,
|
|
295
|
+
flatMapError: (e, u) => {
|
|
289
296
|
switch (e.type) {
|
|
290
297
|
case "AsyncSuccess":
|
|
291
298
|
return s.success(e.value);
|
|
292
299
|
case "NotAsked":
|
|
293
300
|
return s.notAsked;
|
|
294
301
|
case "AsyncFailure":
|
|
295
|
-
return
|
|
302
|
+
return u(e.error);
|
|
296
303
|
case "Loading":
|
|
297
304
|
return s.loading(e.previousValue);
|
|
298
305
|
}
|
|
@@ -331,14 +338,14 @@ const s = {
|
|
|
331
338
|
* @returns A successful `AsyncResult` with the alternative value if failed, otherwise the original state.
|
|
332
339
|
* @public
|
|
333
340
|
*/
|
|
334
|
-
recover: (e,
|
|
341
|
+
recover: (e, u) => {
|
|
335
342
|
switch (e.type) {
|
|
336
343
|
case "AsyncSuccess":
|
|
337
344
|
return s.success(e.value);
|
|
338
345
|
case "NotAsked":
|
|
339
346
|
return s.notAsked;
|
|
340
347
|
case "AsyncFailure":
|
|
341
|
-
return s.success(
|
|
348
|
+
return s.success(u(e.error));
|
|
342
349
|
case "Loading":
|
|
343
350
|
return s.loading(e.previousValue);
|
|
344
351
|
}
|
|
@@ -351,7 +358,7 @@ const s = {
|
|
|
351
358
|
* @returns A new `AsyncResult` with the result of applying the function to the value.
|
|
352
359
|
* @public
|
|
353
360
|
*/
|
|
354
|
-
ap: (e,
|
|
361
|
+
ap: (e, u) => s.isSuccess(e) && s.isSuccess(u) ? s.success(e.value(u.value)) : s.isFailure(e) ? s.failure(e.error) : s.isFailure(u) ? s.failure(u.error) : s.isLoading(e) || s.isLoading(u) ? s.loading() : s.notAsked,
|
|
355
362
|
/**
|
|
356
363
|
* Maps two `AsyncResult` values using a function.
|
|
357
364
|
* @param r1 - The first `AsyncResult`.
|
|
@@ -360,7 +367,7 @@ const s = {
|
|
|
360
367
|
* @returns A new `AsyncResult` with the result of applying the function to both values.
|
|
361
368
|
* @public
|
|
362
369
|
*/
|
|
363
|
-
map2: (e,
|
|
370
|
+
map2: (e, u, r) => s.isSuccess(e) && s.isSuccess(u) ? s.success(r(e.value, u.value)) : s.isFailure(e) ? s.failure(e.error) : s.isFailure(u) ? s.failure(u.error) : s.isLoading(e) || s.isLoading(u) ? s.loading() : s.notAsked,
|
|
364
371
|
/**
|
|
365
372
|
* Maps three `AsyncResult` values using a function.
|
|
366
373
|
* @param r1 - The first `AsyncResult`.
|
|
@@ -370,7 +377,7 @@ const s = {
|
|
|
370
377
|
* @returns A new `AsyncResult` with the result of applying the function to all three values.
|
|
371
378
|
* @public
|
|
372
379
|
*/
|
|
373
|
-
map3: (e,
|
|
380
|
+
map3: (e, u, r, i) => s.isSuccess(e) && s.isSuccess(u) && s.isSuccess(r) ? s.success(i(e.value, u.value, r.value)) : s.isFailure(e) ? s.failure(e.error) : s.isFailure(u) ? s.failure(u.error) : s.isFailure(r) ? s.failure(r.error) : s.isLoading(e) || s.isLoading(u) || s.isLoading(r) ? s.loading() : s.notAsked
|
|
374
381
|
};
|
|
375
382
|
export {
|
|
376
383
|
s as AsyncResult
|
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./array.cjs"),b=require("./async-result.cjs"),a=require("./bigint.cjs"),u=require("./boolean.cjs"),t=require("./date.cjs"),A=require("./deferred.cjs"),g=require("./equal.cjs"),p=require("./function.cjs"),l=require("./iterator.cjs"),y=require("./json.cjs"),m=require("./map.cjs"),i=require("./number.cjs"),s=require("./object.cjs"),S=require("./promise.cjs"),c=require("./random.cjs"),C=require("./regexp.cjs"),f=require("./result-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./array.cjs"),b=require("./async-result.cjs"),a=require("./bigint.cjs"),u=require("./boolean.cjs"),t=require("./date.cjs"),A=require("./deferred.cjs"),g=require("./equal.cjs"),p=require("./function.cjs"),l=require("./iterator.cjs"),y=require("./json.cjs"),m=require("./map.cjs"),i=require("./number.cjs"),s=require("./object.cjs"),S=require("./promise.cjs"),c=require("./random.cjs"),C=require("./regexp.cjs"),f=require("./result-DBJ3htTg.cjs"),n=require("./set.cjs"),e=require("./string.cjs"),d=require("./timer.cjs"),o=require("./url.cjs");exports.applyArrayDiffOperations=r.applyArrayDiffOperations;exports.areArraysEqual=r.areArraysEqual;exports.arrayDiffOperations=r.arrayDiffOperations;exports.arrayHasValues=r.arrayHasValues;exports.arrayHead=r.arrayHead;exports.arrayTail=r.arrayTail;exports.buildArray=r.buildArray;exports.chunk=r.chunk;exports.compareArrays=r.compareArrays;exports.fillArray=r.fillArray;exports.filterMapArray=r.filterMapArray;exports.filterNullsFromArray=r.filterNullsFromArray;exports.groupBy=r.groupBy;exports.isArrayEmpty=r.isArrayEmpty;exports.joinArrayWithConjunction=r.joinArrayWithConjunction;exports.partition=r.partition;exports.range=r.range;exports.rankArray=r.rankArray;exports.removeAllFromArray=r.removeAllFromArray;exports.removeAllFromArrayByPredicate=r.removeAllFromArrayByPredicate;exports.removeOneFromArray=r.removeOneFromArray;exports.removeOneFromArrayByPredicate=r.removeOneFromArrayByPredicate;exports.uniqueByPrimitive=r.uniqueByPrimitive;exports.AsyncResult=b.AsyncResult;exports.biAbs=a.biAbs;exports.biCeilDiv=a.biCeilDiv;exports.biCompare=a.biCompare;exports.biFloorDiv=a.biFloorDiv;exports.biGcd=a.biGcd;exports.biIsEven=a.biIsEven;exports.biIsNegative=a.biIsNegative;exports.biIsOdd=a.biIsOdd;exports.biIsOne=a.biIsOne;exports.biIsPositive=a.biIsPositive;exports.biIsPrime=a.biIsPrime;exports.biIsZero=a.biIsZero;exports.biLcm=a.biLcm;exports.biMax=a.biMax;exports.biMin=a.biMin;exports.biNextPrime=a.biNextPrime;exports.biPow=a.biPow;exports.biPrevPrime=a.biPrevPrime;exports.booleanToInt=u.booleanToInt;exports.canParseBoolean=u.canParseBoolean;exports.compareBooleans=u.compareBooleans;exports.parseBoolean=u.parseBoolean;exports.xorBoolean=u.xorBoolean;exports.addDays=t.addDays;exports.addHours=t.addHours;exports.addMinutes=t.addMinutes;exports.compareDates=t.compareDates;exports.diffInDays=t.diffInDays;exports.diffInHours=t.diffInHours;exports.endOfDay=t.endOfDay;exports.endOfWeek=t.endOfWeek;exports.isSameDay=t.isSameDay;exports.isSameHour=t.isSameHour;exports.isSameMinute=t.isSameMinute;exports.isSameMonth=t.isSameMonth;exports.isSameSecond=t.isSameSecond;exports.isSameWeek=t.isSameWeek;exports.isSameYear=t.isSameYear;exports.isValidDate=t.isValidDate;exports.isWeekend=t.isWeekend;exports.startOfDay=t.startOfDay;exports.startOfWeek=t.startOfWeek;exports.deferred=A.deferred;exports.deepEqual=g.deepEqual;exports.looseEqual=g.looseEqual;exports.strictEqual=g.strictEqual;exports.compose=p.compose;exports.curryLeft=p.curryLeft;exports.flip=p.flip;exports.identity=p.identity;exports.memoize=p.memoize;exports.negate=p.negate;exports.once=p.once;exports.partial=p.partial;exports.pipe=p.pipe;exports.chain=l.chain;exports.every=l.every;exports.filter=l.filter;exports.find=l.find;exports.map=l.map;exports.reduce=l.reduce;exports.skip=l.skip;exports.some=l.some;exports.take=l.take;exports.toArray=l.toArray;exports.isJSON=y.isJSON;exports.isJSONArray=y.isJSONArray;exports.isJSONObject=y.isJSONObject;exports.isJSONPrimitive=y.isJSONPrimitive;exports.parseJSON=y.parseJSON;exports.mapEntries=m.mapEntries;exports.mapFilter=m.mapFilter;exports.mapFromEntries=m.mapFromEntries;exports.mapGroupBy=m.mapGroupBy;exports.mapIsEmpty=m.mapIsEmpty;exports.mapKeys=m.mapKeys;exports.mapMap=m.mapMap;exports.mapMerge=m.mapMerge;exports.mapToObject=m.mapToObject;exports.mapValues=m.mapValues;exports.EPSILON=i.EPSILON;exports.angleDifference=i.angleDifference;exports.ceilTo=i.ceilTo;exports.clamp=i.clamp;exports.clampInt=i.clampInt;exports.clampSym=i.clampSym;exports.compareNumbers=i.compareNumbers;exports.floorTo=i.floorTo;exports.interpolate=i.interpolate;exports.interpolateAngle=i.interpolateAngle;exports.interpolateAngleCCW=i.interpolateAngleCCW;exports.interpolateAngleCW=i.interpolateAngleCW;exports.interpolateWidestAngle=i.interpolateWidestAngle;exports.nearEqual=i.nearEqual;exports.nearEqualAngles=i.nearEqualAngles;exports.nearZero=i.nearZero;exports.root=i.root;exports.roundTo=i.roundTo;exports.toHex=i.toHex;exports.widestAngleDifference=i.widestAngleDifference;exports.wrap=i.wrap;exports.wrapCircular=i.wrapCircular;exports.deepClone=s.deepClone;exports.isEmptyObject=s.isEmptyObject;exports.isObject=s.isObject;exports.mergeObjects=s.mergeObjects;exports.objectEntries=s.objectEntries;exports.objectFromEntries=s.objectFromEntries;exports.objectKeys=s.objectKeys;exports.objectValues=s.objectValues;exports.omit=s.omit;exports.pick=s.pick;exports.removeObjectFields=s.removeObjectFields;exports.sameObjectKeys=s.sameObjectKeys;exports.sleep=S.sleep;exports.randomBytes=c.randomBytes;exports.randomChoice=c.randomChoice;exports.randomChoices=c.randomChoices;exports.randomFloat=c.randomFloat;exports.randomHex=c.randomHex;exports.randomInt=c.randomInt;exports.randomUuid=c.randomUuid;exports.seedRandom=c.seedRandom;exports.shuffle=c.shuffle;exports.shuffled=c.shuffled;exports.mapRegExp=C.mapRegExp;exports.Result=f.Result;exports.Validation=f.Validation;exports.setDifference=n.setDifference;exports.setFilter=n.setFilter;exports.setFromArray=n.setFromArray;exports.setIntersection=n.setIntersection;exports.setIsEmpty=n.setIsEmpty;exports.setIsSubset=n.setIsSubset;exports.setIsSuperset=n.setIsSuperset;exports.setMap=n.setMap;exports.setSymmetricDifference=n.setSymmetricDifference;exports.setToArray=n.setToArray;exports.setUnion=n.setUnion;exports.canonicalizeNewlines=e.canonicalizeNewlines;exports.capitalize=e.capitalize;exports.capitalizeWords=e.capitalizeWords;exports.chunkString=e.chunkString;exports.collapseText=e.collapseText;exports.compareCaseInsensitive=e.compareCaseInsensitive;exports.compareStrings=e.compareStrings;exports.containsAllText=e.containsAllText;exports.containsAllTextCaseInsensitive=e.containsAllTextCaseInsensitive;exports.containsAnyText=e.containsAnyText;exports.containsAnyTextCaseInsensitive=e.containsAnyTextCaseInsensitive;exports.countStringOccurrences=e.countStringOccurrences;exports.dasherize=e.dasherize;exports.decodeBase64=e.decodeBase64;exports.deleteFirstFromString=e.deleteFirstFromString;exports.deleteStringAfter=e.deleteStringAfter;exports.deleteStringBefore=e.deleteStringBefore;exports.deleteSubstring=e.deleteSubstring;exports.ellipsis=e.ellipsis;exports.ellipsisMiddle=e.ellipsisMiddle;exports.encodeBase64=e.encodeBase64;exports.filterCharcodes=e.filterCharcodes;exports.filterChars=e.filterChars;exports.humanize=e.humanize;exports.ifEmptyString=e.ifEmptyString;exports.isAlpha=e.isAlpha;exports.isAlphaNum=e.isAlphaNum;exports.isBreakingWhitespace=e.isBreakingWhitespace;exports.isDigitsOnly=e.isDigitsOnly;exports.isEmptyString=e.isEmptyString;exports.isLowerCase=e.isLowerCase;exports.isSpaceAt=e.isSpaceAt;exports.isUpperCase=e.isUpperCase;exports.jsQuote=e.jsQuote;exports.lowerCaseFirst=e.lowerCaseFirst;exports.lpad=e.lpad;exports.mapChars=e.mapChars;exports.quote=e.quote;exports.randomStringSequence=e.randomStringSequence;exports.randomStringSequenceBase64=e.randomStringSequenceBase64;exports.randomSubString=e.randomSubString;exports.reverseString=e.reverseString;exports.rpad=e.rpad;exports.smartQuote=e.smartQuote;exports.splitStringOnFirst=e.splitStringOnFirst;exports.splitStringOnLast=e.splitStringOnLast;exports.splitStringOnce=e.splitStringOnce;exports.stringEndsWithAny=e.stringEndsWithAny;exports.stringHasContent=e.stringHasContent;exports.stringHashCode=e.stringHashCode;exports.stringStartsWithAny=e.stringStartsWithAny;exports.stringToCharcodes=e.stringToCharcodes;exports.stringsDifferAtIndex=e.stringsDifferAtIndex;exports.substringAfter=e.substringAfter;exports.substringAfterLast=e.substringAfterLast;exports.substringBefore=e.substringBefore;exports.substringBeforeLast=e.substringBeforeLast;exports.surroundString=e.surroundString;exports.textContainsCaseInsensitive=e.textContainsCaseInsensitive;exports.textEndsWithAnyCaseInsensitive=e.textEndsWithAnyCaseInsensitive;exports.textEndsWithCaseInsensitive=e.textEndsWithCaseInsensitive;exports.textStartsWithAnyCaseInsensitive=e.textStartsWithAnyCaseInsensitive;exports.textStartsWithCaseInsensitive=e.textStartsWithCaseInsensitive;exports.textToLines=e.textToLines;exports.trimChars=e.trimChars;exports.trimCharsLeft=e.trimCharsLeft;exports.trimCharsRight=e.trimCharsRight;exports.trimStringSlice=e.trimStringSlice;exports.underscore=e.underscore;exports.upperCaseFirst=e.upperCaseFirst;exports.wrapColumns=e.wrapColumns;exports.wrapLine=e.wrapLine;exports.debounce=d.debounce;exports.delayed=d.delayed;exports.delayedAnimationFrame=d.delayedAnimationFrame;exports.interval=d.interval;exports.intervalAnimationFrame=d.intervalAnimationFrame;exports.throttle=d.throttle;exports.buildUrl=o.buildUrl;exports.getBaseName=o.getBaseName;exports.getFileExtension=o.getFileExtension;exports.getFileName=o.getFileName;exports.getQueryParams=o.getQueryParams;exports.isValidUrl=o.isValidUrl;exports.joinPaths=o.joinPaths;exports.normalizePath=o.normalizePath;exports.parseUrl=o.parseUrl;exports.removeQueryParam=o.removeQueryParam;exports.setQueryParam=o.setQueryParam;
|
package/index.js
CHANGED
|
@@ -14,7 +14,7 @@ import { deepClone as jr, isEmptyObject as Nr, isObject as Tr, mergeObjects as k
|
|
|
14
14
|
import { sleep as Jr } from "./promise.js";
|
|
15
15
|
import { randomBytes as Kr, randomChoice as Gr, randomChoices as Zr, randomFloat as Yr, randomHex as Xr, randomInt as _r, randomUuid as $r, seedRandom as et, shuffle as rt, shuffled as tt } from "./random.js";
|
|
16
16
|
import { mapRegExp as at } from "./regexp.js";
|
|
17
|
-
import { R as nt, V as ot } from "./result-
|
|
17
|
+
import { R as nt, V as ot } from "./result-BYto972d.js";
|
|
18
18
|
import { setDifference as lt, setFilter as pt, setFromArray as dt, setIntersection as ct, setIsEmpty as ft, setIsSubset as yt, setIsSuperset as ut, setMap as gt, setSymmetricDifference as At, setToArray as St, setUnion as bt } from "./set.js";
|
|
19
19
|
import { canonicalizeNewlines as Ct, capitalize as ht, capitalizeWords as Ot, chunkString as It, collapseText as vt, compareCaseInsensitive as Et, compareStrings as Ft, containsAllText as Bt, containsAllTextCaseInsensitive as Dt, containsAnyText as Pt, containsAnyTextCaseInsensitive as Wt, countStringOccurrences as jt, dasherize as Nt, decodeBase64 as Tt, deleteFirstFromString as kt, deleteStringAfter as qt, deleteStringBefore as Lt, deleteSubstring as Mt, ellipsis as wt, ellipsisMiddle as Ht, encodeBase64 as zt, filterCharcodes as Vt, filterChars as Rt, humanize as Ut, ifEmptyString as Jt, isAlpha as Qt, isAlphaNum as Kt, isBreakingWhitespace as Gt, isDigitsOnly as Zt, isEmptyString as Yt, isLowerCase as Xt, isSpaceAt as _t, isUpperCase as $t, jsQuote as ei, lowerCaseFirst as ri, lpad as ti, mapChars as ii, quote as ai, randomStringSequence as si, randomStringSequenceBase64 as ni, randomSubString as oi, reverseString as mi, rpad as li, smartQuote as pi, splitStringOnFirst as di, splitStringOnLast as ci, splitStringOnce as fi, stringEndsWithAny as yi, stringHasContent as ui, stringHashCode as gi, stringStartsWithAny as Ai, stringToCharcodes as Si, stringsDifferAtIndex as bi, substringAfter as xi, substringAfterLast as Ci, substringBefore as hi, substringBeforeLast as Oi, surroundString as Ii, textContainsCaseInsensitive as vi, textEndsWithAnyCaseInsensitive as Ei, textEndsWithCaseInsensitive as Fi, textStartsWithAnyCaseInsensitive as Bi, textStartsWithCaseInsensitive as Di, textToLines as Pi, trimChars as Wi, trimCharsLeft as ji, trimCharsRight as Ni, trimStringSlice as Ti, underscore as ki, upperCaseFirst as qi, wrapColumns as Li, wrapLine as Mi } from "./string.js";
|
|
20
20
|
import { debounce as Hi, delayed as zi, delayedAnimationFrame as Vi, interval as Ri, intervalAnimationFrame as Ui, throttle as Ji } from "./timer.js";
|
package/json.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./result-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./result-DBJ3htTg.cjs"),e=r=>typeof r=="object"&&!Array.isArray(r)&&r!=null&&Object.values(r).every(t),i=r=>Array.isArray(r)&&r.every(t),t=r=>o(r)||e(r)||i(r),o=r=>typeof r=="string"||typeof r=="boolean"||typeof r=="number"||r==null,n=r=>{try{return s.Result.success(JSON.parse(r))}catch(c){return s.Result.failure(c)}};exports.isJSON=t;exports.isJSONArray=i;exports.isJSONObject=e;exports.isJSONPrimitive=o;exports.parseJSON=n;
|
package/json.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { R as t } from "./result-
|
|
1
|
+
import { R as t } from "./result-BYto972d.js";
|
|
2
2
|
const e = (r) => typeof r == "object" && !Array.isArray(r) && r != null && Object.values(r).every(s), c = (r) => Array.isArray(r) && r.every(s), s = (r) => i(r) || e(r) || c(r), i = (r) => typeof r == "string" || typeof r == "boolean" || typeof r == "number" || r == null, y = (r) => {
|
|
3
3
|
try {
|
|
4
4
|
return t.success(JSON.parse(r));
|
package/package.json
CHANGED
|
@@ -52,12 +52,13 @@ const i = {
|
|
|
52
52
|
/**
|
|
53
53
|
* Executes side effects based on the state of the validation.
|
|
54
54
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
55
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
55
56
|
* @param v - The validation.
|
|
56
|
-
* @param handlers - An object with optional handlers for each state.
|
|
57
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
57
58
|
* @returns The validation that was passed in, allowing for chaining.
|
|
58
59
|
* @public
|
|
59
60
|
*/
|
|
60
|
-
effect: (e, r) => (e.type === "valid" ? r.valid?.() : r.invalid
|
|
61
|
+
effect: (e, r) => (e.type === "valid" ? r.valid ? r.valid() : r.else?.() : r.invalid ? r.invalid(e.error) : r.else?.(), e),
|
|
61
62
|
/**
|
|
62
63
|
* Execute a function when the `Validation` is valid.
|
|
63
64
|
*
|
|
@@ -305,12 +306,13 @@ const i = {
|
|
|
305
306
|
/**
|
|
306
307
|
* Executes side effects based on the state of the result.
|
|
307
308
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
309
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
308
310
|
* @param r - The result.
|
|
309
|
-
* @param handlers - An object with optional handlers for each state.
|
|
311
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
310
312
|
* @returns The result that was passed in, allowing for chaining.
|
|
311
313
|
* @public
|
|
312
314
|
*/
|
|
313
|
-
effect: (e, r) => (e.type === "Success" ? r.success
|
|
315
|
+
effect: (e, r) => (e.type === "Success" ? r.success ? r.success(e.value) : r.else?.() : r.failure ? r.failure(e.error) : r.else?.(), e),
|
|
314
316
|
/**
|
|
315
317
|
* Calls the provided function if the result is a success.
|
|
316
318
|
* @param apply - The function to call if the result is a success.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const n=require("./async-result.cjs"),i={valid:{type:"valid"},invalid(e){return{type:"invalid",error:e}},isValid(e){return e.type==="valid"},isInvalid(e){return e.type==="invalid"},match:(e,r,u)=>i.isValid(e)?r():u(e.error),toResult:(e,r)=>i.match(e,()=>s.success(r),u=>s.failure(u)),effect:(e,r)=>(e.type==="valid"?r.valid?r.valid():r.else?.():r.invalid?r.invalid(e.error):r.else?.(),e),whenValid:(e,r)=>(i.isValid(e)&&r(),e),whenInvalid:(e,r)=>(i.isInvalid(e)&&r(e.error),e),mapError:(e,r)=>e.type==="invalid"?i.invalid(r(e.error)):e,flatMapError:(e,r)=>e.type==="invalid"?r(e.error):e,combine:(e,r,u)=>i.isValid(e)&&i.isValid(r)?i.valid:i.isInvalid(e)&&i.isInvalid(r)?i.invalid(u(e.error,r.error)):i.isInvalid(e)?e:r,all:e=>{for(const r of e)if(i.isInvalid(r))return r;return i.valid},allErrors:e=>{const r=[];for(const u of e)i.isInvalid(u)&&r.push(u.error);return r.length>0?i.invalid(r):i.valid},equals:(e,r,u=(t,l)=>t===l)=>e.type==="valid"&&r.type==="valid"?!0:e.type==="invalid"&&r.type==="invalid"?u(e.error,r.error):!1,recover:e=>i.valid,getError:e=>{if(i.isInvalid(e))return e.error},getErrorOrElse:(e,r)=>i.isInvalid(e)?e.error:r},s={success(e){return{type:"Success",value:e}},failure(e){return{type:"Failure",error:e}},map:(e,r)=>e.type==="Success"?s.success(r(e.value)):e,flatMap:(e,r)=>e.type==="Success"?r(e.value):e,toAsync(e){return s.match(e,r=>n.AsyncResult.success(r),r=>n.AsyncResult.failure(r))},toValidation(e){return s.match(e,()=>i.valid,r=>i.invalid(r))},isSuccess(e){return e.type==="Success"},isFailure(e){return e.type==="Failure"},getOrElse(e,r){return s.isSuccess(e)?e.value:r},getOrElseLazy(e,r){return s.isSuccess(e)?e.value:r()},getOrNull(e){return s.isSuccess(e)?e.value:null},getOrUndefined(e){return s.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(s.isSuccess(e))return e.value;throw e.error},match:(e,r,u)=>s.isSuccess(e)?r(e.value):u(e.error),effect:(e,r)=>(e.type==="Success"?r.success?r.success(e.value):r.else?.():r.failure?r.failure(e.error):r.else?.(),e),whenSuccess:(e,r)=>(s.isSuccess(e)&&r(e.value),e),whenFailure:(e,r)=>(s.isFailure(e)&&r(e.error),e),combine:(e,r,u,t)=>s.match(e,l=>s.match(r,a=>s.success(u(l,a)),a=>s.failure(a)),l=>s.match(r,a=>s.failure(l),a=>s.failure(t(l,a)))),equals:(e,r,u={valueEquals:(t,l)=>t===l,errorEquals:(t,l)=>t===l})=>e.type==="Success"&&r.type==="Success"?u.valueEquals(e.value,r.value):e.type==="Failure"&&r.type==="Failure"?u.errorEquals(e.error,r.error):!1,all:e=>{const r=[];for(const u of e)if(s.isSuccess(u))r.push(u.value);else return u;return s.success(r)},mapError:(e,r)=>e.type==="Failure"?s.failure(r(e.error)):e,flatMapError:(e,r)=>e.type==="Failure"?r(e.error):e,recover:(e,r)=>e.type==="Failure"?{type:"Success",value:r(e.error)}:e,ap:(e,r)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(e.value(r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map2:(e,r,u)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(u(e.value,r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map3:(e,r,u,t)=>s.isSuccess(e)&&s.isSuccess(r)&&s.isSuccess(u)?s.success(t(e.value,r.value,u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.failure(u.error),ofPromise:async e=>{try{const r=await e;return s.success(r)}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}},swap:e=>s.isSuccess(e)?s.failure(e.value):s.success(e.error),fromNullable:(e,r)=>e==null?s.failure(r):s.success(e),fromNullableLazy:(e,r)=>e==null?s.failure(r()):s.success(e),tryCatch:e=>{try{return s.success(e())}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}}};exports.Result=s;exports.Validation=i;
|
package/result.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./async-result.cjs");const e=require("./result-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./async-result.cjs");const e=require("./result-DBJ3htTg.cjs");exports.Result=e.Result;
|
package/result.d.ts
CHANGED
|
@@ -143,14 +143,16 @@ export declare const Result: {
|
|
|
143
143
|
/**
|
|
144
144
|
* Executes side effects based on the state of the result.
|
|
145
145
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
146
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
146
147
|
* @param r - The result.
|
|
147
|
-
* @param handlers - An object with optional handlers for each state.
|
|
148
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
148
149
|
* @returns The result that was passed in, allowing for chaining.
|
|
149
150
|
* @public
|
|
150
151
|
*/
|
|
151
152
|
effect: <V, E>(r: Result<V, E>, handlers: {
|
|
152
153
|
success?: (value: V) => void;
|
|
153
154
|
failure?: (error: E) => void;
|
|
155
|
+
else?: () => void;
|
|
154
156
|
}) => Result<V, E>;
|
|
155
157
|
/**
|
|
156
158
|
* Calls the provided function if the result is a success.
|
package/result.js
CHANGED
package/validation.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./result-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./result-DBJ3htTg.cjs");exports.Validation=i.Validation;
|
package/validation.d.ts
CHANGED
|
@@ -75,14 +75,16 @@ export declare const Validation: {
|
|
|
75
75
|
/**
|
|
76
76
|
* Executes side effects based on the state of the validation.
|
|
77
77
|
* Unlike `match`, all handlers are optional, allowing you to react only to specific states.
|
|
78
|
+
* The `else` handler is called when no specific handler is provided for the current state.
|
|
78
79
|
* @param v - The validation.
|
|
79
|
-
* @param handlers - An object with optional handlers for each state.
|
|
80
|
+
* @param handlers - An object with optional handlers for each state and an optional `else` fallback.
|
|
80
81
|
* @returns The validation that was passed in, allowing for chaining.
|
|
81
82
|
* @public
|
|
82
83
|
*/
|
|
83
84
|
effect: <E>(v: Validation<E>, handlers: {
|
|
84
85
|
valid?: () => void;
|
|
85
86
|
invalid?: (error: E) => void;
|
|
87
|
+
else?: () => void;
|
|
86
88
|
}) => Validation<E>;
|
|
87
89
|
/**
|
|
88
90
|
* Execute a function when the `Validation` is valid.
|
package/validation.js
CHANGED
package/result-bSKKrSLB.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const n=require("./async-result.cjs"),i={valid:{type:"valid"},invalid(e){return{type:"invalid",error:e}},isValid(e){return e.type==="valid"},isInvalid(e){return e.type==="invalid"},match:(e,r,u)=>i.isValid(e)?r():u(e.error),toResult:(e,r)=>i.match(e,()=>s.success(r),u=>s.failure(u)),effect:(e,r)=>(e.type==="valid"?r.valid?.():r.invalid?.(e.error),e),whenValid:(e,r)=>(i.isValid(e)&&r(),e),whenInvalid:(e,r)=>(i.isInvalid(e)&&r(e.error),e),mapError:(e,r)=>e.type==="invalid"?i.invalid(r(e.error)):e,flatMapError:(e,r)=>e.type==="invalid"?r(e.error):e,combine:(e,r,u)=>i.isValid(e)&&i.isValid(r)?i.valid:i.isInvalid(e)&&i.isInvalid(r)?i.invalid(u(e.error,r.error)):i.isInvalid(e)?e:r,all:e=>{for(const r of e)if(i.isInvalid(r))return r;return i.valid},allErrors:e=>{const r=[];for(const u of e)i.isInvalid(u)&&r.push(u.error);return r.length>0?i.invalid(r):i.valid},equals:(e,r,u=(t,l)=>t===l)=>e.type==="valid"&&r.type==="valid"?!0:e.type==="invalid"&&r.type==="invalid"?u(e.error,r.error):!1,recover:e=>i.valid,getError:e=>{if(i.isInvalid(e))return e.error},getErrorOrElse:(e,r)=>i.isInvalid(e)?e.error:r},s={success(e){return{type:"Success",value:e}},failure(e){return{type:"Failure",error:e}},map:(e,r)=>e.type==="Success"?s.success(r(e.value)):e,flatMap:(e,r)=>e.type==="Success"?r(e.value):e,toAsync(e){return s.match(e,r=>n.AsyncResult.success(r),r=>n.AsyncResult.failure(r))},toValidation(e){return s.match(e,()=>i.valid,r=>i.invalid(r))},isSuccess(e){return e.type==="Success"},isFailure(e){return e.type==="Failure"},getOrElse(e,r){return s.isSuccess(e)?e.value:r},getOrElseLazy(e,r){return s.isSuccess(e)?e.value:r()},getOrNull(e){return s.isSuccess(e)?e.value:null},getOrUndefined(e){return s.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(s.isSuccess(e))return e.value;throw e.error},match:(e,r,u)=>s.isSuccess(e)?r(e.value):u(e.error),effect:(e,r)=>(e.type==="Success"?r.success?.(e.value):r.failure?.(e.error),e),whenSuccess:(e,r)=>(s.isSuccess(e)&&r(e.value),e),whenFailure:(e,r)=>(s.isFailure(e)&&r(e.error),e),combine:(e,r,u,t)=>s.match(e,l=>s.match(r,a=>s.success(u(l,a)),a=>s.failure(a)),l=>s.match(r,a=>s.failure(l),a=>s.failure(t(l,a)))),equals:(e,r,u={valueEquals:(t,l)=>t===l,errorEquals:(t,l)=>t===l})=>e.type==="Success"&&r.type==="Success"?u.valueEquals(e.value,r.value):e.type==="Failure"&&r.type==="Failure"?u.errorEquals(e.error,r.error):!1,all:e=>{const r=[];for(const u of e)if(s.isSuccess(u))r.push(u.value);else return u;return s.success(r)},mapError:(e,r)=>e.type==="Failure"?s.failure(r(e.error)):e,flatMapError:(e,r)=>e.type==="Failure"?r(e.error):e,recover:(e,r)=>e.type==="Failure"?{type:"Success",value:r(e.error)}:e,ap:(e,r)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(e.value(r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map2:(e,r,u)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(u(e.value,r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map3:(e,r,u,t)=>s.isSuccess(e)&&s.isSuccess(r)&&s.isSuccess(u)?s.success(t(e.value,r.value,u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.failure(u.error),ofPromise:async e=>{try{const r=await e;return s.success(r)}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}},swap:e=>s.isSuccess(e)?s.failure(e.value):s.success(e.error),fromNullable:(e,r)=>e==null?s.failure(r):s.success(e),fromNullableLazy:(e,r)=>e==null?s.failure(r()):s.success(e),tryCatch:e=>{try{return s.success(e())}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}}};exports.Result=s;exports.Validation=i;
|