@tempots/std 0.11.0 → 0.13.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/array.cjs +1 -1
- package/array.d.ts +325 -35
- package/array.js +148 -152
- package/async-result.cjs +1 -1
- package/async-result.d.ts +152 -13
- package/async-result.js +112 -8
- package/bigint.cjs +1 -1
- package/bigint.d.ts +155 -18
- package/bigint.js +37 -37
- package/boolean.cjs +1 -1
- package/boolean.d.ts +39 -7
- package/boolean.js +10 -10
- package/domain.d.ts +146 -5
- package/equal.d.ts +26 -0
- package/function.cjs +1 -1
- package/function.d.ts +24 -18
- package/function.js +10 -29
- package/index.cjs +1 -0
- package/index.d.ts +13 -0
- package/index.js +179 -0
- package/json.d.ts +20 -1
- package/number.cjs +1 -1
- package/number.d.ts +347 -57
- package/number.js +31 -32
- package/object.cjs +1 -1
- package/object.d.ts +53 -5
- package/object.js +15 -15
- package/package.json +8 -8
- package/regexp.cjs +1 -1
- package/regexp.d.ts +5 -4
- package/regexp.js +8 -8
- package/result-Czm7RKNP.js +230 -0
- package/result-DzdZiQoR.cjs +1 -0
- package/result.cjs +1 -1
- package/result.d.ts +130 -6
- package/result.js +3 -54
- package/string.cjs +4 -4
- package/string.d.ts +532 -95
- package/string.js +192 -205
- package/validation.cjs +1 -1
- package/validation.d.ts +72 -4
- package/validation.js +2 -23
- package/maybe.cjs +0 -1
- package/maybe.d.ts +0 -9
- package/maybe.js +0 -9
package/object.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
function s(e) {
|
|
2
2
|
return Object.keys(e);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
7
|
-
for (const n of
|
|
4
|
+
function u(e, t) {
|
|
5
|
+
const c = s(e), r = s(t);
|
|
6
|
+
if (c.length !== r.length) return !1;
|
|
7
|
+
for (const n of c)
|
|
8
8
|
if (!(n in t)) return !1;
|
|
9
9
|
return !0;
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
function o(e) {
|
|
12
12
|
return e != null && Object.getPrototypeOf(e) === Object.prototype;
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function i(e, ...t) {
|
|
15
15
|
return s(e).reduce((r, n) => (t.includes(n) || (r[n] = e[n]), r), {});
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function f(e, t) {
|
|
18
18
|
return Object.assign({}, e, t);
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
20
|
+
function O(e) {
|
|
21
|
+
return Object.keys(e).length === 0;
|
|
22
22
|
}
|
|
23
23
|
export {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
O as isEmptyObject,
|
|
25
|
+
o as isObject,
|
|
26
|
+
f as mergeObjects,
|
|
27
|
+
s as objectKeys,
|
|
28
|
+
i as removeObjectFields,
|
|
29
|
+
u as sameObjectKeys
|
|
30
30
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tempots/std",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"priority": 8,
|
|
5
5
|
"description": "Std library for TypeScript. Natural complement to the Tempo libraries.",
|
|
6
6
|
"keywords": [
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
},
|
|
22
22
|
"type": "module",
|
|
23
23
|
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./index.js",
|
|
26
|
+
"require": "./index.cjs"
|
|
27
|
+
},
|
|
24
28
|
"./array": {
|
|
25
29
|
"import": "./array.js",
|
|
26
30
|
"require": "./array.cjs"
|
|
@@ -53,10 +57,6 @@
|
|
|
53
57
|
"import": "./json.js",
|
|
54
58
|
"require": "./json.cjs"
|
|
55
59
|
},
|
|
56
|
-
"./maybe": {
|
|
57
|
-
"import": "./maybe.js",
|
|
58
|
-
"require": "./maybe.cjs"
|
|
59
|
-
},
|
|
60
60
|
"./number": {
|
|
61
61
|
"import": "./number.js",
|
|
62
62
|
"require": "./number.cjs"
|
|
@@ -89,6 +89,9 @@
|
|
|
89
89
|
},
|
|
90
90
|
"typesVersions": {
|
|
91
91
|
"*": {
|
|
92
|
+
".": [
|
|
93
|
+
"./index.d.ts"
|
|
94
|
+
],
|
|
92
95
|
"array": [
|
|
93
96
|
"./array.d.ts"
|
|
94
97
|
],
|
|
@@ -113,9 +116,6 @@
|
|
|
113
116
|
"json": [
|
|
114
117
|
"./json.d.ts"
|
|
115
118
|
],
|
|
116
|
-
"maybe": [
|
|
117
|
-
"./maybe.d.ts"
|
|
118
|
-
],
|
|
119
119
|
"number": [
|
|
120
120
|
"./number.d.ts"
|
|
121
121
|
],
|
package/regexp.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(i,s,u){const n=[];let l=0,e;if(s.global)for(s.lastIndex=0;(e=s.exec(i))!==null;)n.push(i.substring(l,e.index)),n.push(u(...e)),l=e.index+e[0].length;else for(;(e=s.exec(i.substring(l)))!==null;)n.push(i.substring(l,l+e.index)),n.push(u(...e)),l+=e.index+e[0].length;return n.push(i.substring(l)),n.join("")}exports.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(i,s,u){const n=[];let l=0,e;if(s.global)for(s.lastIndex=0;(e=s.exec(i))!==null;)n.push(i.substring(l,e.index)),n.push(u(...e)),l=e.index+e[0].length;else for(;(e=s.exec(i.substring(l)))!==null;)n.push(i.substring(l,l+e.index)),n.push(u(...e)),l+=e.index+e[0].length;return n.push(i.substring(l)),n.join("")}exports.mapRegExp=g;
|
package/regexp.d.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
5
|
* Map the function `f` on each occurance matched by the pattern.
|
|
6
|
-
* @param f
|
|
7
|
-
* @param pattern
|
|
8
|
-
* @param subject
|
|
6
|
+
* @param f - The function to apply to each match.
|
|
7
|
+
* @param pattern - The pattern to match.
|
|
8
|
+
* @param subject - The string to search.
|
|
9
|
+
* @public
|
|
9
10
|
*/
|
|
10
|
-
export declare function
|
|
11
|
+
export declare function mapRegExp(subject: string, pattern: RegExp, f: (...s: string[]) => string): string;
|
package/regexp.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
function h(
|
|
2
|
-
const
|
|
1
|
+
function h(s, i, u) {
|
|
2
|
+
const e = [];
|
|
3
3
|
let n = 0, l;
|
|
4
4
|
if (i.global)
|
|
5
|
-
for (i.lastIndex = 0; (l = i.exec(
|
|
6
|
-
|
|
5
|
+
for (i.lastIndex = 0; (l = i.exec(s)) !== null; )
|
|
6
|
+
e.push(s.substring(n, l.index)), e.push(u(...l)), n = l.index + l[0].length;
|
|
7
7
|
else
|
|
8
|
-
for (; (l = i.exec(
|
|
9
|
-
|
|
10
|
-
return
|
|
8
|
+
for (; (l = i.exec(s.substring(n))) !== null; )
|
|
9
|
+
e.push(s.substring(n, n + l.index)), e.push(u(...l)), n += l.index + l[0].length;
|
|
10
|
+
return e.push(s.substring(n)), e.join("");
|
|
11
11
|
}
|
|
12
12
|
export {
|
|
13
|
-
h as
|
|
13
|
+
h as mapRegExp
|
|
14
14
|
};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { AsyncResult as l } from "./async-result.js";
|
|
2
|
+
const t = {
|
|
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) => t.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) => t.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) => (t.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) => (t.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) => l.success(s),
|
|
115
|
+
(s) => l.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
|
+
() => t.valid,
|
|
128
|
+
(s) => t.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
|
+
* Based on the state of the result, it picks the appropriate function to call and returns the result.
|
|
189
|
+
* @param success - The function to call if the result is a success.
|
|
190
|
+
* @param failure - The function to call if the result is a failure.
|
|
191
|
+
* @returns The result of calling the appropriate function based on the state of the result.
|
|
192
|
+
* @public
|
|
193
|
+
*/
|
|
194
|
+
match: (e, s, r) => u.isSuccess(e) ? s(e.value) : r(e.error),
|
|
195
|
+
/**
|
|
196
|
+
* Calls the provided function if the result is a success.
|
|
197
|
+
* @param apply - The function to call if the result is a success.
|
|
198
|
+
* @returns A function that takes a `Result` and calls the provided function if the result is a success.
|
|
199
|
+
* @public
|
|
200
|
+
*/
|
|
201
|
+
whenSuccess: (e, s) => (u.isSuccess(e) && s(e.value), e),
|
|
202
|
+
whenFailure: (e, s) => (u.isFailure(e) && s(e.error), e),
|
|
203
|
+
/**
|
|
204
|
+
* Combines two results into a single result.
|
|
205
|
+
* @param r1 - The first result.
|
|
206
|
+
* @param r2 - The second result.
|
|
207
|
+
* @param combineV - The function to combine two values.
|
|
208
|
+
* @param combineE - The function to combine two errors.
|
|
209
|
+
* @returns The combined result.
|
|
210
|
+
* @public
|
|
211
|
+
*/
|
|
212
|
+
combine: (e, s, r, c) => u.match(
|
|
213
|
+
e,
|
|
214
|
+
(a) => u.match(
|
|
215
|
+
s,
|
|
216
|
+
(i) => u.success(r(a, i)),
|
|
217
|
+
(i) => u.failure(i)
|
|
218
|
+
),
|
|
219
|
+
(a) => u.match(
|
|
220
|
+
s,
|
|
221
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
222
|
+
(i) => u.failure(a),
|
|
223
|
+
(i) => u.failure(c(a, i))
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
};
|
|
227
|
+
export {
|
|
228
|
+
u as R,
|
|
229
|
+
t as V
|
|
230
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const a=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,s,t)=>i.isValid(e)?s():t(e.error),toResult:(e,s)=>i.match(e,()=>u.success(s),t=>u.failure(t)),whenValid:(e,s)=>(i.isValid(e)&&s(),e),whenInvalid:(e,s)=>(i.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=>a.AsyncResult.success(s),s=>a.AsyncResult.failure(s))},toValidation(e){return u.match(e,()=>i.valid,s=>i.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},match:(e,s,t)=>u.isSuccess(e)?s(e.value):t(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,t,c)=>u.match(e,l=>u.match(s,r=>u.success(t(l,r)),r=>u.failure(r)),l=>u.match(s,r=>u.failure(l),r=>u.failure(c(l,r))))};exports.Result=u;exports.Validation=i;
|
package/result.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./async-result.cjs");const e=require("./result-DzdZiQoR.cjs");exports.Result=e.Result;
|
package/result.d.ts
CHANGED
|
@@ -1,32 +1,156 @@
|
|
|
1
1
|
import { AsyncResult } from './async-result';
|
|
2
|
+
import { Maybe } from './domain';
|
|
3
|
+
import { Validation } from './validation';
|
|
2
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a successful result.
|
|
7
|
+
* @typeParam V - The type of the value.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
3
10
|
export interface Success<V> {
|
|
4
11
|
type: 'Success';
|
|
5
12
|
value: V;
|
|
6
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Represents a failure result.
|
|
16
|
+
* @typeParam E - The type of the error.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
7
19
|
export interface Failure<E> {
|
|
8
20
|
type: 'Failure';
|
|
9
21
|
error: E;
|
|
10
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Represents a result that can either be a success or a failure.
|
|
25
|
+
* @typeParam V - The type of the value in case of success.
|
|
26
|
+
* @typeParam E - The type of the error in case of failure.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
11
29
|
export type Result<V, E> = Success<V> | Failure<E>;
|
|
30
|
+
/**
|
|
31
|
+
* Represents a promise that resolves to a `Result` type.
|
|
32
|
+
* @typeParam V - The type of the value contained in the `Result`.
|
|
33
|
+
* @typeParam E - The type of the error contained in the `Result`.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
12
36
|
export type PromiseResult<V, E> = PromiseLike<Result<V, E>>;
|
|
37
|
+
/**
|
|
38
|
+
* Utility functions for working with `Result` types.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
13
41
|
export declare const Result: {
|
|
42
|
+
/**
|
|
43
|
+
* Creates a successful `Result`.
|
|
44
|
+
* @param value - The value to wrap in a `Success` type.
|
|
45
|
+
* @returns A `Result` that is a `Success`.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
14
48
|
success<V>(value: V): Result<V, any>;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a failure `Result`.
|
|
51
|
+
* @param error - The error to wrap in a `Failure` type.
|
|
52
|
+
* @returns A `Result` that is a `Failure`.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
15
55
|
failure<E>(error: E): Result<any, E>;
|
|
16
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Maps the value of a `Result` to a new value.
|
|
58
|
+
* @param r - The `Result` to map.
|
|
59
|
+
* @param f - The mapping function.
|
|
60
|
+
* @returns A new `Result` with the mapped value.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
17
63
|
map: <V1, V2, E>(r: Result<V1, E>, f: (value: V1) => V2) => Result<V2, E>;
|
|
18
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Maps the value of a `Result` to a new `Result`.
|
|
66
|
+
* @param r - The `Result` to map.
|
|
67
|
+
* @param f - The mapping function.
|
|
68
|
+
* @returns A new `Result` with the mapped value.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
19
71
|
flatMap: <V1, V2, E>(r: Result<V1, E>, f: (value: V1) => Result<V2, E>) => Result<V2, E>;
|
|
72
|
+
/**
|
|
73
|
+
* Converts a `Result` to an `AsyncResult`.
|
|
74
|
+
* @param r - The `Result` to convert.
|
|
75
|
+
* @returns An `AsyncResult` that is equivalent to the input `Result`.
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
20
78
|
toAsync<V, E>(r: Result<V, E>): AsyncResult<V, E>;
|
|
79
|
+
/**
|
|
80
|
+
* Converts a `Result` to a `Validation`.
|
|
81
|
+
* @param r - The `Result` to convert.
|
|
82
|
+
* @returns A `Validation` that is equivalent to the input `Result`.
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
toValidation<V, E>(r: Result<V, E>): Validation<E>;
|
|
86
|
+
/**
|
|
87
|
+
* Checks if a `Result` is a success.
|
|
88
|
+
* @param r - The `Result` to check.
|
|
89
|
+
* @returns `true` if the `Result` is a `Success`, `false` otherwise.
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
21
92
|
isSuccess<V, E>(r: Result<V, E>): r is Success<V>;
|
|
93
|
+
/**
|
|
94
|
+
* Checks if a `Result` is a failure.
|
|
95
|
+
* @param r - The `Result` to check.
|
|
96
|
+
* @returns `true` if the `Result` is a `Failure`, `false` otherwise.
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
22
99
|
isFailure<V, E>(r: Result<V, E>): r is Failure<E>;
|
|
100
|
+
/**
|
|
101
|
+
* Gets the value of a `Result` if it is a `Success`, otherwise returns the provided default value.
|
|
102
|
+
* @param r - The `Result` to get the value from.
|
|
103
|
+
* @param alt - The default value to return if the `Result` is a `Failure`.
|
|
104
|
+
* @returns The value of the `Result` if it is a `Success`, otherwise the default value.
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
23
107
|
getOrElse<V, E>(r: Result<V, E>, alt: V): V;
|
|
108
|
+
/**
|
|
109
|
+
* Gets the value of a `Result` if it is a `Success`, otherwise returns the result of the provided function.
|
|
110
|
+
* @param r - The `Result` to get the value from.
|
|
111
|
+
* @param altf - The function to call if the `Result` is a `Failure`.
|
|
112
|
+
* @returns The value of the `Result` if it is a `Success`, otherwise the result of the function.
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
24
115
|
getOrElseLazy<V, E>(r: Result<V, E>, altf: () => V): V;
|
|
116
|
+
/**
|
|
117
|
+
* Gets the value of a `Result` if it is a `Success`, otherwise returns `null`.
|
|
118
|
+
* @param r - The `Result` to get the value from.
|
|
119
|
+
* @returns The value of the `Result` if it is a `Success`, otherwise `null`.
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
25
122
|
getOrNull<V, E>(r: Result<V, E>): V | null;
|
|
26
|
-
|
|
27
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Gets the value of a `Result` if it is a `Success`, otherwise returns `undefined`.
|
|
125
|
+
* @param r - The `Result` to get the value from.
|
|
126
|
+
* @returns The value of the `Result` if it is a `Success`, otherwise `undefined`.
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
getOrUndefined<V, E>(r: Result<V, E>): Maybe<V>;
|
|
130
|
+
/**
|
|
131
|
+
* Based on the state of the result, it picks the appropriate function to call and returns the result.
|
|
132
|
+
* @param success - The function to call if the result is a success.
|
|
133
|
+
* @param failure - The function to call if the result is a failure.
|
|
134
|
+
* @returns The result of calling the appropriate function based on the state of the result.
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
28
137
|
match: <V1, V2, E>(r: Result<V1, E>, success: (value: V1) => V2, failure: (error: E) => V2) => V2;
|
|
29
|
-
|
|
30
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Calls the provided function if the result is a success.
|
|
140
|
+
* @param apply - The function to call if the result is a success.
|
|
141
|
+
* @returns A function that takes a `Result` and calls the provided function if the result is a success.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
whenSuccess: <V, E>(r: Result<V, E>, apply: (v: V) => void) => Result<V, E>;
|
|
145
|
+
whenFailure: <V, E>(r: Result<V, E>, apply: (e: E) => void) => Result<V, E>;
|
|
146
|
+
/**
|
|
147
|
+
* Combines two results into a single result.
|
|
148
|
+
* @param r1 - The first result.
|
|
149
|
+
* @param r2 - The second result.
|
|
150
|
+
* @param combineV - The function to combine two values.
|
|
151
|
+
* @param combineE - The function to combine two errors.
|
|
152
|
+
* @returns The combined result.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
31
155
|
combine: <V, E>(r1: Result<V, E>, r2: Result<V, E>, combineV: (v1: V, v2: V) => V, combineE: (e1: E, e2: E) => E) => Result<V, E>;
|
|
32
156
|
};
|
package/result.js
CHANGED
|
@@ -1,56 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
success(e) {
|
|
4
|
-
return { type: "Success", value: e };
|
|
5
|
-
},
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
failure(e) {
|
|
8
|
-
return { type: "Failure", error: e };
|
|
9
|
-
},
|
|
10
|
-
cmap: (e) => (u) => u.type === "Success" ? s.success(e(u.value)) : u,
|
|
11
|
-
map: (e, u) => e.type === "Success" ? s.success(u(e.value)) : e,
|
|
12
|
-
cflatMap: (e) => (u) => u.type === "Success" ? e(u.value) : u,
|
|
13
|
-
flatMap: (e, u) => e.type === "Success" ? u(e.value) : e,
|
|
14
|
-
toAsync(e) {
|
|
15
|
-
return e;
|
|
16
|
-
},
|
|
17
|
-
isSuccess(e) {
|
|
18
|
-
return e.type === "Success";
|
|
19
|
-
},
|
|
20
|
-
isFailure(e) {
|
|
21
|
-
return e.type === "Failure";
|
|
22
|
-
},
|
|
23
|
-
getOrElse(e, u) {
|
|
24
|
-
return s.isSuccess(e) ? e.value : u;
|
|
25
|
-
},
|
|
26
|
-
getOrElseLazy(e, u) {
|
|
27
|
-
return s.isSuccess(e) ? e.value : u();
|
|
28
|
-
},
|
|
29
|
-
getOrNull(e) {
|
|
30
|
-
return s.isSuccess(e) ? e.value : null;
|
|
31
|
-
},
|
|
32
|
-
getOrUndefined(e) {
|
|
33
|
-
return s.isSuccess(e) ? e.value : void 0;
|
|
34
|
-
},
|
|
35
|
-
cmatch: (e, u) => (c) => s.isSuccess(c) ? e(c.value) : u(c.error),
|
|
36
|
-
match: (e, u, c) => s.isSuccess(e) ? u(e.value) : c(e.error),
|
|
37
|
-
whenSuccess: (e) => (u) => (s.isSuccess(u) && e(u.value), u),
|
|
38
|
-
whenFailure: (e) => (u) => (s.isFailure(u) && e(u.error), u),
|
|
39
|
-
combine: (e, u, c, l) => s.match(
|
|
40
|
-
e,
|
|
41
|
-
(t) => s.match(
|
|
42
|
-
u,
|
|
43
|
-
(r) => s.success(c(t, r)),
|
|
44
|
-
(r) => s.failure(r)
|
|
45
|
-
),
|
|
46
|
-
(t) => s.match(
|
|
47
|
-
u,
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
|
-
(r) => s.failure(t),
|
|
50
|
-
(r) => s.failure(l(t, r))
|
|
51
|
-
)
|
|
52
|
-
)
|
|
53
|
-
};
|
|
1
|
+
import "./async-result.js";
|
|
2
|
+
import { R as e } from "./result-Czm7RKNP.js";
|
|
54
3
|
export {
|
|
55
|
-
|
|
4
|
+
e as Result
|
|
56
5
|
};
|