@tempots/std 0.24.0 → 0.25.1

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.
@@ -1,265 +0,0 @@
1
- import { AsyncResult as c } from "./async-result.js";
2
- const a = {
3
- /**
4
- * Creates a valid `Validation`.
5
- * @returns A `Validation` that is `Valid`.
6
- */
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- valid: { type: "valid" },
9
- /**
10
- * Creates an invalid `Validation`.
11
- * @param error - The error associated with the invalid value.
12
- * @returns A `Validation` that is `Invalid`.
13
- */
14
- invalid(e) {
15
- return { type: "invalid", error: e };
16
- },
17
- /**
18
- * Checks if a `Validation` is `Valid`.
19
- * @param r - The `Validation` to check.
20
- * @returns `true` if the `Validation` is `Valid`, otherwise `false`.
21
- */
22
- isValid(e) {
23
- return e.type === "valid";
24
- },
25
- /**
26
- * Checks if a `Validation` is `Invalid`.
27
- * @param r - The `Validation` to check.
28
- * @returns `true` if the `Validation` is `Invalid`, otherwise `false`.
29
- */
30
- isInvalid(e) {
31
- return e.type === "invalid";
32
- },
33
- /**
34
- * Maps the value of a `Validation` to a new value.
35
- * @param r - The `Validation` to map.
36
- * @param valid - The mapping function for a valid value.
37
- * @param invalid - The mapping function for an invalid value.
38
- * @returns The mapped value.
39
- */
40
- match: (e, s, r) => a.isValid(e) ? s() : r(e.error),
41
- /**
42
- * Maps the value of a `Validation` to a new `Validation`.
43
- * @param validation - The `Validation` to map.
44
- * @param value - The value to map.
45
- * @returns A new `Validation` with the mapped value.
46
- */
47
- toResult: (e, s) => a.match(
48
- e,
49
- () => u.success(s),
50
- (r) => u.failure(r)
51
- ),
52
- /**
53
- * Execute a function when the `Validation` is valid.
54
- *
55
- * @param r - The `Validation` to check.
56
- * @param apply - The function to execute when the `Validation` is valid.
57
- * @returns The `Validation` object.
58
- */
59
- whenValid: (e, s) => (a.isValid(e) && s(), e),
60
- /**
61
- * Execute a function when the `Validation` is invalid.
62
- *
63
- * @param r - The `Validation` to check.
64
- * @param apply - The function to execute when the `Validation` is invalid.
65
- * @returns The `Validation` object.
66
- */
67
- whenInvalid: (e, s) => (a.isInvalid(e) && s(e.error), e)
68
- }, u = {
69
- /**
70
- * Creates a successful `Result`.
71
- * @param value - The value to wrap in a `Success` type.
72
- * @returns A `Result` that is a `Success`.
73
- * @public
74
- */
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- success(e) {
77
- return { type: "Success", value: e };
78
- },
79
- /**
80
- * Creates a failure `Result`.
81
- * @param error - The error to wrap in a `Failure` type.
82
- * @returns A `Result` that is a `Failure`.
83
- * @public
84
- */
85
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
- failure(e) {
87
- return { type: "Failure", error: e };
88
- },
89
- /**
90
- * Maps the value of a `Result` to a new value.
91
- * @param r - The `Result` to map.
92
- * @param f - The mapping function.
93
- * @returns A new `Result` with the mapped value.
94
- * @public
95
- */
96
- map: (e, s) => e.type === "Success" ? u.success(s(e.value)) : e,
97
- /**
98
- * Maps the value of a `Result` to a new `Result`.
99
- * @param r - The `Result` to map.
100
- * @param f - The mapping function.
101
- * @returns A new `Result` with the mapped value.
102
- * @public
103
- */
104
- flatMap: (e, s) => e.type === "Success" ? s(e.value) : e,
105
- /**
106
- * Converts a `Result` to an `AsyncResult`.
107
- * @param r - The `Result` to convert.
108
- * @returns An `AsyncResult` that is equivalent to the input `Result`.
109
- * @public
110
- */
111
- toAsync(e) {
112
- return u.match(
113
- e,
114
- (s) => c.success(s),
115
- (s) => c.failure(s)
116
- );
117
- },
118
- /**
119
- * Converts a `Result` to a `Validation`.
120
- * @param r - The `Result` to convert.
121
- * @returns A `Validation` that is equivalent to the input `Result`.
122
- * @public
123
- */
124
- toValidation(e) {
125
- return u.match(
126
- e,
127
- () => a.valid,
128
- (s) => a.invalid(s)
129
- );
130
- },
131
- /**
132
- * Checks if a `Result` is a success.
133
- * @param r - The `Result` to check.
134
- * @returns `true` if the `Result` is a `Success`, `false` otherwise.
135
- * @public
136
- */
137
- isSuccess(e) {
138
- return e.type === "Success";
139
- },
140
- /**
141
- * Checks if a `Result` is a failure.
142
- * @param r - The `Result` to check.
143
- * @returns `true` if the `Result` is a `Failure`, `false` otherwise.
144
- * @public
145
- */
146
- isFailure(e) {
147
- return e.type === "Failure";
148
- },
149
- /**
150
- * Gets the value of a `Result` if it is a `Success`, otherwise returns the provided default value.
151
- * @param r - The `Result` to get the value from.
152
- * @param alt - The default value to return if the `Result` is a `Failure`.
153
- * @returns The value of the `Result` if it is a `Success`, otherwise the default value.
154
- * @public
155
- */
156
- getOrElse(e, s) {
157
- return u.isSuccess(e) ? e.value : s;
158
- },
159
- /**
160
- * Gets the value of a `Result` if it is a `Success`, otherwise returns the result of the provided function.
161
- * @param r - The `Result` to get the value from.
162
- * @param altf - The function to call if the `Result` is a `Failure`.
163
- * @returns The value of the `Result` if it is a `Success`, otherwise the result of the function.
164
- * @public
165
- */
166
- getOrElseLazy(e, s) {
167
- return u.isSuccess(e) ? e.value : s();
168
- },
169
- /**
170
- * Gets the value of a `Result` if it is a `Success`, otherwise returns `null`.
171
- * @param r - The `Result` to get the value from.
172
- * @returns The value of the `Result` if it is a `Success`, otherwise `null`.
173
- * @public
174
- */
175
- getOrNull(e) {
176
- return u.isSuccess(e) ? e.value : null;
177
- },
178
- /**
179
- * Gets the value of a `Result` if it is a `Success`, otherwise returns `undefined`.
180
- * @param r - The `Result` to get the value from.
181
- * @returns The value of the `Result` if it is a `Success`, otherwise `undefined`.
182
- * @public
183
- */
184
- getOrUndefined(e) {
185
- return u.isSuccess(e) ? e.value : void 0;
186
- },
187
- /**
188
- * Gets the value of a `Result` if it is a `Success`, otherwise it throws the error contained in the `Failure`.
189
- * @param r - The `Result` to get the value from.
190
- * @returns The value of the `Result` if it is a `Success`.
191
- */
192
- getUnsafe: (e) => {
193
- if (u.isSuccess(e))
194
- return e.value;
195
- throw e.error;
196
- },
197
- /**
198
- * Based on the state of the result, it picks the appropriate function to call and returns the result.
199
- * @param success - The function to call if the result is a success.
200
- * @param failure - The function to call if the result is a failure.
201
- * @returns The result of calling the appropriate function based on the state of the result.
202
- * @public
203
- */
204
- match: (e, s, r) => u.isSuccess(e) ? s(e.value) : r(e.error),
205
- /**
206
- * Calls the provided function if the result is a success.
207
- * @param apply - The function to call if the result is a success.
208
- * @returns A function that takes a `Result` and calls the provided function if the result is a success.
209
- * @public
210
- */
211
- whenSuccess: (e, s) => (u.isSuccess(e) && s(e.value), e),
212
- whenFailure: (e, s) => (u.isFailure(e) && s(e.error), e),
213
- /**
214
- * Combines two results into a single result.
215
- * @param r1 - The first result.
216
- * @param r2 - The second result.
217
- * @param combineV - The function to combine two values.
218
- * @param combineE - The function to combine two errors.
219
- * @returns The combined result.
220
- * @public
221
- */
222
- combine: (e, s, r, i) => u.match(
223
- e,
224
- (t) => u.match(
225
- s,
226
- (l) => u.success(r(t, l)),
227
- (l) => u.failure(l)
228
- ),
229
- (t) => u.match(
230
- s,
231
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
232
- (l) => u.failure(t),
233
- (l) => u.failure(i(t, l))
234
- )
235
- ),
236
- /**
237
- * Compares two results for equality.
238
- * @param r1 - The first result.
239
- * @param r2 - The second result.
240
- * @param options - The options to use for comparison. By default, uses strict equality.
241
- * @returns `true` if the results are equal, `false` otherwise.
242
- */
243
- equals: (e, s, r = {
244
- valueEquals: (i, t) => i === t,
245
- errorEquals: (i, t) => i === t
246
- }) => e.type === "Success" && s.type === "Success" ? r.valueEquals(e.value, s.value) : e.type === "Failure" && s.type === "Failure" ? r.errorEquals(e.error, s.error) : !1,
247
- /**
248
- * Combines multiple results into a single result.
249
- * @param results - The results to combine.
250
- * @returns A single result that is a success if all the input results are successes, otherwise a failure.
251
- */
252
- all: (e) => {
253
- const s = [];
254
- for (const r of e)
255
- if (u.isSuccess(r))
256
- s.push(r.value);
257
- else
258
- return r;
259
- return u.success(s);
260
- }
261
- };
262
- export {
263
- u as R,
264
- a as V
265
- };
@@ -1 +0,0 @@
1
- "use strict";const c=require("./async-result.cjs"),l={valid:{type:"valid"},invalid(e){return{type:"invalid",error:e}},isValid(e){return e.type==="valid"},isInvalid(e){return e.type==="invalid"},match:(e,s,r)=>l.isValid(e)?s():r(e.error),toResult:(e,s)=>l.match(e,()=>u.success(s),r=>u.failure(r)),whenValid:(e,s)=>(l.isValid(e)&&s(),e),whenInvalid:(e,s)=>(l.isInvalid(e)&&s(e.error),e)},u={success(e){return{type:"Success",value:e}},failure(e){return{type:"Failure",error:e}},map:(e,s)=>e.type==="Success"?u.success(s(e.value)):e,flatMap:(e,s)=>e.type==="Success"?s(e.value):e,toAsync(e){return u.match(e,s=>c.AsyncResult.success(s),s=>c.AsyncResult.failure(s))},toValidation(e){return u.match(e,()=>l.valid,s=>l.invalid(s))},isSuccess(e){return e.type==="Success"},isFailure(e){return e.type==="Failure"},getOrElse(e,s){return u.isSuccess(e)?e.value:s},getOrElseLazy(e,s){return u.isSuccess(e)?e.value:s()},getOrNull(e){return u.isSuccess(e)?e.value:null},getOrUndefined(e){return u.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(u.isSuccess(e))return e.value;throw e.error},match:(e,s,r)=>u.isSuccess(e)?s(e.value):r(e.error),whenSuccess:(e,s)=>(u.isSuccess(e)&&s(e.value),e),whenFailure:(e,s)=>(u.isFailure(e)&&s(e.error),e),combine:(e,s,r,i)=>u.match(e,t=>u.match(s,a=>u.success(r(t,a)),a=>u.failure(a)),t=>u.match(s,a=>u.failure(t),a=>u.failure(i(t,a)))),equals:(e,s,r={valueEquals:(i,t)=>i===t,errorEquals:(i,t)=>i===t})=>e.type==="Success"&&s.type==="Success"?r.valueEquals(e.value,s.value):e.type==="Failure"&&s.type==="Failure"?r.errorEquals(e.error,s.error):!1,all:e=>{const s=[];for(const r of e)if(u.isSuccess(r))s.push(r.value);else return r;return u.success(s)}};exports.Result=u;exports.Validation=l;