@terrygonguet/utils 0.0.10 → 0.0.12
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/dist/async.js +62 -3
- package/dist/functional.js +207 -149
- package/package.json +12 -7
- package/src/async.ts +111 -2
- package/src/functional/index.ts +26 -7
- package/src/functional/maybe.ts +81 -60
- package/src/functional/result.ts +92 -65
- package/types/async.d.ts +14 -1
- package/types/functional/index.d.ts +8 -3
- package/types/functional/maybe.d.ts +35 -18
- package/types/functional/result.d.ts +44 -20
- /package/dist/{main.js → index.js} +0 -0
- /package/src/{main.ts → index.ts} +0 -0
- /package/types/{main.d.ts → index.d.ts} +0 -0
package/dist/async.js
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { findIndex as g } from "./functional.js";
|
|
2
|
+
function h(n) {
|
|
3
|
+
return new Promise((t) => setTimeout(t, n));
|
|
4
|
+
}
|
|
5
|
+
function x(n, t) {
|
|
6
|
+
return typeof n == "function" ? p(n, t) : (r) => p(r, n);
|
|
7
|
+
}
|
|
8
|
+
async function p(n, { count: t = 1 / 0, delay: r } = {}) {
|
|
9
|
+
let o = 0, s;
|
|
10
|
+
do
|
|
11
|
+
try {
|
|
12
|
+
return await n();
|
|
13
|
+
} catch (c) {
|
|
14
|
+
switch (s = c, o++, typeof r) {
|
|
15
|
+
case "number":
|
|
16
|
+
await h(r);
|
|
17
|
+
break;
|
|
18
|
+
case "function":
|
|
19
|
+
await h(r(o, c));
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
while (o <= t);
|
|
24
|
+
throw s;
|
|
25
|
+
}
|
|
26
|
+
function I(n, t, r) {
|
|
27
|
+
if (typeof n == "function" && typeof t != "function") {
|
|
28
|
+
const o = n, s = t;
|
|
29
|
+
return (c) => w(c, o, s);
|
|
30
|
+
} else {
|
|
31
|
+
if (Array.isArray(n) && typeof t == "function")
|
|
32
|
+
return w(n, t, r);
|
|
33
|
+
throw new Error("Invalid arguments passed to asyncMap");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function w(n, t, { concurrent: r = 5 } = {}) {
|
|
37
|
+
return new Promise((o, s) => {
|
|
38
|
+
const c = [], u = [];
|
|
39
|
+
let i = 0;
|
|
40
|
+
function y(e) {
|
|
41
|
+
if (c.length == n.length && u.length == 0)
|
|
42
|
+
return m();
|
|
43
|
+
if (e >= n.length)
|
|
44
|
+
return;
|
|
45
|
+
const f = n[e];
|
|
46
|
+
u.push([
|
|
47
|
+
e,
|
|
48
|
+
t(f, e, n).then((a) => {
|
|
49
|
+
c.push([e, a]), g(u, ([l]) => e == l).map((l) => u.splice(l, 1)), y(i);
|
|
50
|
+
}, s)
|
|
51
|
+
]), i++;
|
|
52
|
+
}
|
|
53
|
+
function m() {
|
|
54
|
+
const e = c.sort(([f], [a]) => f - a);
|
|
55
|
+
o(e.map(([, f]) => f));
|
|
56
|
+
}
|
|
57
|
+
for (let e = 0; e < r; e++)
|
|
58
|
+
y(i);
|
|
59
|
+
});
|
|
3
60
|
}
|
|
4
61
|
export {
|
|
5
|
-
|
|
62
|
+
I as asyncMap,
|
|
63
|
+
h as pause,
|
|
64
|
+
x as retry
|
|
6
65
|
};
|
package/dist/functional.js
CHANGED
|
@@ -1,202 +1,260 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var S = (r, e, t) => e in r ? d(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var u = (r, e, t) => (S(r, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
const a = "@terrygonguet/utils/functional/result", l = "@terrygonguet/utils/functional/result/Success", w = "@terrygonguet/utils/functional/result/Failure";
|
|
5
|
+
class f {
|
|
6
|
+
constructor(e) {
|
|
7
|
+
u(this, "value");
|
|
8
|
+
Object.defineProperties(this, {
|
|
9
|
+
$_kind: { value: a, enumerable: !1, writable: !1 },
|
|
10
|
+
$_variant: { value: l, enumerable: !1, writable: !1 },
|
|
11
|
+
value: { value: e, writable: !1 }
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
isSuccess() {
|
|
15
|
+
return !0;
|
|
16
|
+
}
|
|
17
|
+
isFailure() {
|
|
18
|
+
return !1;
|
|
19
|
+
}
|
|
20
|
+
merge(e) {
|
|
21
|
+
return e(this.value);
|
|
22
|
+
}
|
|
23
|
+
match(e) {
|
|
24
|
+
return e(this.value);
|
|
25
|
+
}
|
|
26
|
+
map(e) {
|
|
27
|
+
return new f(e(this.value));
|
|
28
|
+
}
|
|
29
|
+
flatMap(e) {
|
|
30
|
+
return e(this.value);
|
|
31
|
+
}
|
|
32
|
+
toJSON() {
|
|
33
|
+
return { $_kind: a, $_variant: l, value: this.value };
|
|
34
|
+
}
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
class g {
|
|
37
|
+
constructor(e) {
|
|
38
|
+
u(this, "reason");
|
|
39
|
+
Object.defineProperties(this, {
|
|
40
|
+
$_kind: { value: a, enumerable: !1, writable: !1 },
|
|
41
|
+
$_variant: { value: l, enumerable: !1, writable: !1 },
|
|
42
|
+
reason: { value: e, writable: !1 }
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
isSuccess() {
|
|
46
|
+
return !1;
|
|
47
|
+
}
|
|
48
|
+
isFailure() {
|
|
49
|
+
return !0;
|
|
50
|
+
}
|
|
51
|
+
merge(e, t) {
|
|
52
|
+
return t(this.reason);
|
|
53
|
+
}
|
|
54
|
+
match(e, t) {
|
|
55
|
+
return t(this.reason);
|
|
56
|
+
}
|
|
57
|
+
map() {
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
flatMap() {
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
toJSON() {
|
|
64
|
+
return { $_kind: a, $_variant: w, reason: this.reason };
|
|
65
|
+
}
|
|
54
66
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
67
|
+
function v(r, e) {
|
|
68
|
+
return e ? r.toResult(e) : r.toResult();
|
|
69
|
+
}
|
|
70
|
+
const c = {
|
|
71
|
+
Success(r) {
|
|
72
|
+
return new f(r);
|
|
73
|
+
},
|
|
74
|
+
Failure(r) {
|
|
75
|
+
return new g(r);
|
|
60
76
|
},
|
|
61
|
-
|
|
62
|
-
return
|
|
77
|
+
try(r) {
|
|
78
|
+
return new y(r);
|
|
63
79
|
},
|
|
64
|
-
|
|
65
|
-
return
|
|
80
|
+
fromPromise(r, e, t) {
|
|
81
|
+
return r.then(
|
|
82
|
+
b(e, this.Success),
|
|
83
|
+
b(t, this.Failure)
|
|
84
|
+
);
|
|
66
85
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
86
|
+
fromMaybe: v,
|
|
87
|
+
JSONReviver(r, e) {
|
|
88
|
+
if ((e == null ? void 0 : e.$_kind) == a) {
|
|
89
|
+
const t = e == null ? void 0 : e.$_variant;
|
|
90
|
+
return t == l ? new f(e == null ? void 0 : e.value) : t == w ? new g(e == null ? void 0 : e.reason) : e;
|
|
71
91
|
} else
|
|
72
92
|
return e;
|
|
73
93
|
}
|
|
74
94
|
};
|
|
75
|
-
class
|
|
95
|
+
class y {
|
|
76
96
|
constructor(e) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.tryFn = e, this.catchFn =
|
|
97
|
+
u(this, "tryFn");
|
|
98
|
+
u(this, "catchFn");
|
|
99
|
+
this.tryFn = e, this.catchFn = p;
|
|
80
100
|
}
|
|
81
101
|
catch(e) {
|
|
82
102
|
return this.catchFn = e, this;
|
|
83
103
|
}
|
|
84
104
|
exec(e) {
|
|
85
105
|
try {
|
|
86
|
-
const
|
|
87
|
-
return e == null || e(
|
|
88
|
-
} catch (
|
|
89
|
-
const n = c(this.catchFn(
|
|
106
|
+
const t = c.Success(this.tryFn());
|
|
107
|
+
return e == null || e(t), t;
|
|
108
|
+
} catch (t) {
|
|
109
|
+
const n = c.Failure(this.catchFn(t));
|
|
90
110
|
return e == null || e(n), n;
|
|
91
111
|
}
|
|
92
112
|
}
|
|
93
113
|
}
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
orDefault() {
|
|
101
|
-
return this.value;
|
|
102
|
-
},
|
|
103
|
-
map(e) {
|
|
104
|
-
return l(e(this.value));
|
|
105
|
-
},
|
|
106
|
-
flatMap(e) {
|
|
107
|
-
return e(this.value);
|
|
108
|
-
},
|
|
109
|
-
toResult() {
|
|
110
|
-
return g.Success(this.value);
|
|
111
|
-
},
|
|
112
|
-
toJSON() {
|
|
113
|
-
return { $_kind: u, $_variant: _, value: this.value };
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
$_kind: { value: u, enumerable: !1, writable: !1 },
|
|
114
|
+
const h = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", $ = "@terrygonguet/utils/functional/maybe/None";
|
|
115
|
+
class o {
|
|
116
|
+
constructor(e) {
|
|
117
|
+
u(this, "value");
|
|
118
|
+
Object.defineProperties(this, {
|
|
119
|
+
$_kind: { value: h, enumerable: !1, writable: !1 },
|
|
118
120
|
$_variant: { value: _, enumerable: !1, writable: !1 },
|
|
119
|
-
value: { value:
|
|
120
|
-
}
|
|
121
|
-
|
|
121
|
+
value: { value: e, writable: !1 }
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
isSome() {
|
|
125
|
+
return !0;
|
|
126
|
+
}
|
|
127
|
+
isNone() {
|
|
128
|
+
return !1;
|
|
129
|
+
}
|
|
130
|
+
orDefault() {
|
|
131
|
+
return this.value;
|
|
132
|
+
}
|
|
133
|
+
map(e) {
|
|
134
|
+
return new o(e(this.value));
|
|
135
|
+
}
|
|
136
|
+
flatMap(e) {
|
|
137
|
+
return e(this.value);
|
|
138
|
+
}
|
|
139
|
+
toResult() {
|
|
140
|
+
return c.Success(this.value);
|
|
141
|
+
}
|
|
142
|
+
toJSON() {
|
|
143
|
+
return { $_kind: h, $_variant: _, value: this.value };
|
|
144
|
+
}
|
|
122
145
|
}
|
|
123
|
-
|
|
124
|
-
{
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
146
|
+
class k {
|
|
147
|
+
constructor() {
|
|
148
|
+
}
|
|
149
|
+
isSome() {
|
|
150
|
+
return !1;
|
|
151
|
+
}
|
|
152
|
+
isNone() {
|
|
153
|
+
return !0;
|
|
154
|
+
}
|
|
155
|
+
orDefault(e) {
|
|
156
|
+
return e;
|
|
157
|
+
}
|
|
158
|
+
map() {
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
flatMap() {
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
toResult(e) {
|
|
165
|
+
return e ? c.Failure(e == null ? void 0 : e()) : c.Failure(void 0);
|
|
166
|
+
}
|
|
167
|
+
toJSON() {
|
|
168
|
+
return { $_kind: h, $_variant: $ };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const N = new k(), s = {
|
|
172
|
+
Some(r) {
|
|
173
|
+
return new o(r);
|
|
132
174
|
},
|
|
133
|
-
{
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
)
|
|
138
|
-
Some: l,
|
|
139
|
-
None: s,
|
|
140
|
-
from(t) {
|
|
141
|
-
switch (t) {
|
|
175
|
+
None() {
|
|
176
|
+
return N;
|
|
177
|
+
},
|
|
178
|
+
from(r) {
|
|
179
|
+
switch (r) {
|
|
142
180
|
case null:
|
|
143
181
|
case void 0:
|
|
144
|
-
return
|
|
182
|
+
return this.None();
|
|
145
183
|
default:
|
|
146
|
-
return
|
|
184
|
+
return new o(r);
|
|
147
185
|
}
|
|
148
186
|
},
|
|
149
187
|
/**
|
|
150
188
|
* CAUTION: this method swallows errors and simply returns None!
|
|
151
189
|
* Use `Result.fromPromise()` if you need error details.
|
|
152
190
|
*/
|
|
153
|
-
fromPromise(
|
|
154
|
-
return
|
|
191
|
+
fromPromise(r, e = p) {
|
|
192
|
+
return r.then(b(e, s.from), M(this.None()));
|
|
155
193
|
},
|
|
156
|
-
JSONReviver(
|
|
157
|
-
if ((e == null ? void 0 : e.$_kind) ==
|
|
158
|
-
const
|
|
159
|
-
return
|
|
194
|
+
JSONReviver(r, e) {
|
|
195
|
+
if ((e == null ? void 0 : e.$_kind) == h) {
|
|
196
|
+
const t = e == null ? void 0 : e.$_variant;
|
|
197
|
+
return t == _ ? new o(e == null ? void 0 : e.value) : t == $ ? s.None() : e;
|
|
160
198
|
} else
|
|
161
199
|
return e;
|
|
162
200
|
}
|
|
163
201
|
};
|
|
164
|
-
var
|
|
165
|
-
function
|
|
202
|
+
var b = F;
|
|
203
|
+
function F(r, e) {
|
|
166
204
|
if (!arguments.length)
|
|
167
205
|
throw new Error(
|
|
168
206
|
"expected at least one (and probably more) function arguments"
|
|
169
207
|
);
|
|
170
|
-
var
|
|
208
|
+
var t = arguments;
|
|
171
209
|
return function() {
|
|
172
|
-
for (var n =
|
|
173
|
-
n =
|
|
210
|
+
for (var n = t[0].apply(this, arguments), i = t.length, m = 1; m < i; m++)
|
|
211
|
+
n = t[m].call(this, n);
|
|
174
212
|
return n;
|
|
175
213
|
};
|
|
176
214
|
}
|
|
177
|
-
|
|
215
|
+
var J = x;
|
|
216
|
+
function x(r, ...e) {
|
|
217
|
+
if (!arguments.length)
|
|
218
|
+
throw new Error("expected one value argument and least one function argument");
|
|
219
|
+
if (!e.length)
|
|
220
|
+
throw new Error(
|
|
221
|
+
"expected at least one (and probably more) function arguments"
|
|
222
|
+
);
|
|
223
|
+
for (var t = e[0](r), n = e.length, i = 1; i < n; i++)
|
|
224
|
+
t = e[i](t);
|
|
178
225
|
return t;
|
|
179
226
|
}
|
|
180
|
-
function
|
|
181
|
-
return
|
|
227
|
+
function p(r) {
|
|
228
|
+
return r;
|
|
182
229
|
}
|
|
183
|
-
function
|
|
184
|
-
return
|
|
230
|
+
function M(r) {
|
|
231
|
+
return () => r;
|
|
185
232
|
}
|
|
186
|
-
function
|
|
187
|
-
return
|
|
233
|
+
function j(r, e) {
|
|
234
|
+
return typeof r == "number" ? (t) => s.from(t.at(r)) : s.from(r.at(e));
|
|
188
235
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
191
|
-
|
|
236
|
+
function E(r, e) {
|
|
237
|
+
return typeof r == "function" ? (t) => s.from(t.find(r)) : s.from(r.find(e));
|
|
238
|
+
}
|
|
239
|
+
function O(r, e) {
|
|
240
|
+
if (typeof r == "function")
|
|
241
|
+
return (t) => {
|
|
242
|
+
const n = t.findIndex(r);
|
|
243
|
+
return n == -1 ? s.None() : s.Some(n);
|
|
244
|
+
};
|
|
245
|
+
{
|
|
246
|
+
const t = r.findIndex(e);
|
|
247
|
+
return t == -1 ? s.None() : s.Some(t);
|
|
248
|
+
}
|
|
192
249
|
}
|
|
193
250
|
export {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
251
|
+
s as Maybe,
|
|
252
|
+
c as Result,
|
|
253
|
+
j as at,
|
|
254
|
+
b as compose,
|
|
255
|
+
M as constant,
|
|
256
|
+
E as find,
|
|
257
|
+
O as findIndex,
|
|
258
|
+
p as identity,
|
|
259
|
+
J as pipe
|
|
202
260
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"module": "./dist/
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
6
|
"author": {
|
|
7
7
|
"email": "terry@gonguet.com",
|
|
8
8
|
"name": "Terry Gonguet",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
],
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
-
"import": "./dist/
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
33
|
},
|
|
34
34
|
"./async": {
|
|
35
35
|
"import": "./dist/async.js"
|
|
@@ -40,13 +40,18 @@
|
|
|
40
40
|
},
|
|
41
41
|
"typesVersions": {
|
|
42
42
|
"*": {
|
|
43
|
-
"async": [
|
|
44
|
-
|
|
43
|
+
"async": [
|
|
44
|
+
"./types/async.d.ts"
|
|
45
|
+
],
|
|
46
|
+
"functional": [
|
|
47
|
+
"./types/functional/index.d.ts"
|
|
48
|
+
]
|
|
45
49
|
}
|
|
46
50
|
},
|
|
47
|
-
"types": "./types/
|
|
51
|
+
"types": "./types/index.d.ts",
|
|
48
52
|
"dependencies": {
|
|
49
|
-
"just-compose": "^2.3.0"
|
|
53
|
+
"just-compose": "^2.3.0",
|
|
54
|
+
"just-pipe": "^1.0.0"
|
|
50
55
|
},
|
|
51
56
|
"repository": {
|
|
52
57
|
"type": "git",
|
package/src/async.ts
CHANGED
|
@@ -1,3 +1,112 @@
|
|
|
1
|
-
|
|
1
|
+
import { findIndex } from "./functional/index.ts"
|
|
2
|
+
|
|
3
|
+
export function pause(ms: number): Promise<void> {
|
|
2
4
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
3
|
-
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface RetryOptions {
|
|
8
|
+
count?: number
|
|
9
|
+
delay?: number | ((retryCount: number, error: any) => number)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>
|
|
13
|
+
export function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>
|
|
14
|
+
export function retry<T>(
|
|
15
|
+
providerOrOptions: RetryOptions | (() => Promise<T>),
|
|
16
|
+
options?: RetryOptions,
|
|
17
|
+
) {
|
|
18
|
+
if (typeof providerOrOptions == "function") return _retry(providerOrOptions, options)
|
|
19
|
+
else return (provider: () => Promise<T>) => _retry(provider, providerOrOptions)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function _retry<T>(
|
|
23
|
+
provider: () => Promise<T>,
|
|
24
|
+
{ count = Infinity, delay }: RetryOptions = {},
|
|
25
|
+
): Promise<T> {
|
|
26
|
+
let retryCount = 0
|
|
27
|
+
let lastError: any
|
|
28
|
+
do {
|
|
29
|
+
try {
|
|
30
|
+
return await provider()
|
|
31
|
+
} catch (error) {
|
|
32
|
+
lastError = error
|
|
33
|
+
retryCount++
|
|
34
|
+
switch (typeof delay) {
|
|
35
|
+
case "number":
|
|
36
|
+
await pause(delay)
|
|
37
|
+
break
|
|
38
|
+
case "function":
|
|
39
|
+
await pause(delay(retryCount, error))
|
|
40
|
+
break
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} while (retryCount <= count)
|
|
44
|
+
throw lastError
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface AsyncMapOptions {
|
|
48
|
+
concurrent?: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function asyncMap<T, U>(
|
|
52
|
+
f: AsyncMapFn<T, U>,
|
|
53
|
+
options?: AsyncMapOptions,
|
|
54
|
+
): (data: T[]) => Promise<U[]>
|
|
55
|
+
export function asyncMap<T, U>(
|
|
56
|
+
data: T[],
|
|
57
|
+
f: AsyncMapFn<T, U>,
|
|
58
|
+
options?: AsyncMapOptions,
|
|
59
|
+
): Promise<U[]>
|
|
60
|
+
export function asyncMap<T, U>(
|
|
61
|
+
dataOrF: T[] | AsyncMapFn<T, U>,
|
|
62
|
+
ForOptions: AsyncMapFn<T, U> | AsyncMapOptions | undefined,
|
|
63
|
+
options?: AsyncMapOptions,
|
|
64
|
+
): Promise<U[]> | ((data: T[]) => Promise<U[]>) {
|
|
65
|
+
if (typeof dataOrF == "function" && typeof ForOptions != "function") {
|
|
66
|
+
const f = dataOrF
|
|
67
|
+
const options = ForOptions
|
|
68
|
+
return (data: T[]) => asyncMap_(data, f, options)
|
|
69
|
+
} else if (Array.isArray(dataOrF) && typeof ForOptions == "function") {
|
|
70
|
+
const data = dataOrF
|
|
71
|
+
const f = ForOptions
|
|
72
|
+
return asyncMap_(data, f, options)
|
|
73
|
+
} else throw new Error("Invalid arguments passed to asyncMap")
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>
|
|
77
|
+
|
|
78
|
+
async function asyncMap_<T, U>(
|
|
79
|
+
data: T[],
|
|
80
|
+
f: AsyncMapFn<T, U>,
|
|
81
|
+
{ concurrent = 5 }: AsyncMapOptions = {},
|
|
82
|
+
): Promise<U[]> {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
const resolved: [i: number, result: U][] = []
|
|
85
|
+
const inFlight: [i: number, promise: Promise<void>][] = []
|
|
86
|
+
|
|
87
|
+
let next = 0
|
|
88
|
+
function queue(i: number) {
|
|
89
|
+
if (resolved.length == data.length && inFlight.length == 0) return finish()
|
|
90
|
+
if (i >= data.length) return
|
|
91
|
+
const el = data[i]
|
|
92
|
+
inFlight.push([
|
|
93
|
+
i,
|
|
94
|
+
f(el, i, data).then(result => {
|
|
95
|
+
resolved.push([i, result])
|
|
96
|
+
findIndex(inFlight, ([j]) => i == j).map(j => inFlight.splice(j, 1))
|
|
97
|
+
queue(next)
|
|
98
|
+
}, reject),
|
|
99
|
+
])
|
|
100
|
+
next++
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function finish() {
|
|
104
|
+
const sorted = resolved.sort(([a], [b]) => a - b)
|
|
105
|
+
resolve(sorted.map(([, value]) => value))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
for (let i = 0; i < concurrent; i++) {
|
|
109
|
+
queue(next)
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
}
|
package/src/functional/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Maybe } from "./maybe.ts"
|
|
|
3
3
|
export * from "./maybe.ts"
|
|
4
4
|
export * from "./result.ts"
|
|
5
5
|
export { default as compose } from "just-compose"
|
|
6
|
+
export { default as pipe } from "just-pipe"
|
|
6
7
|
|
|
7
8
|
export function identity<T>(value: T) {
|
|
8
9
|
return value
|
|
@@ -12,15 +13,33 @@ export function constant<T>(value: T) {
|
|
|
12
13
|
return () => value
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export function at<T>(
|
|
16
|
-
|
|
16
|
+
export function at(idx: number): <T>(data: T[]) => Maybe<T>
|
|
17
|
+
export function at<T>(data: T[], idx: number): Maybe<T>
|
|
18
|
+
export function at<T>(dataOrIdx: T[] | number, idx?: number) {
|
|
19
|
+
if (typeof dataOrIdx == "number") return (data: T[]) => Maybe.from(data.at(dataOrIdx))
|
|
20
|
+
else return Maybe.from(dataOrIdx.at(idx!))
|
|
17
21
|
}
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
type Predicate<T> = (value: T, idx: number, arr: T[]) => boolean
|
|
24
|
+
|
|
25
|
+
export function find<T>(predicate: Predicate<T>): (data: T[]) => Maybe<T>
|
|
26
|
+
export function find<T>(data: T[], predicate: Predicate<T>): Maybe<T>
|
|
27
|
+
export function find<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Predicate<T>) {
|
|
28
|
+
if (typeof dataOrPredicate == "function")
|
|
29
|
+
return (data: T[]) => Maybe.from(data.find(dataOrPredicate))
|
|
30
|
+
else return Maybe.from(dataOrPredicate.find(predicate!))
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
export function findIndex<T>(
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
export function findIndex<T>(predicate: Predicate<T>): (data: T[]) => Maybe<number>
|
|
34
|
+
export function findIndex<T>(data: T[], predicate: Predicate<T>): Maybe<number>
|
|
35
|
+
export function findIndex<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Predicate<T>) {
|
|
36
|
+
if (typeof dataOrPredicate == "function") {
|
|
37
|
+
return (data: T[]) => {
|
|
38
|
+
const idx = data.findIndex(dataOrPredicate)
|
|
39
|
+
return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
const idx = dataOrPredicate.findIndex(predicate!)
|
|
43
|
+
return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
|
|
44
|
+
}
|
|
26
45
|
}
|
package/src/functional/maybe.ts
CHANGED
|
@@ -1,79 +1,98 @@
|
|
|
1
1
|
import { compose, constant, identity } from "./index.ts"
|
|
2
2
|
import { Result } from "./result.ts"
|
|
3
3
|
|
|
4
|
-
interface API<T> {
|
|
5
|
-
isSome(this: Maybe<T>): this is Some<T>
|
|
6
|
-
isNone(this: Maybe<T>): this is None<T>
|
|
7
|
-
orDefault(this: Maybe<T>, defaultValue: T): T
|
|
8
|
-
map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>
|
|
9
|
-
flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>
|
|
10
|
-
toResult<U>(this: Maybe<T>, mapNone?: () => U): Result<T, U>
|
|
11
|
-
toJSON(this: Maybe<T>): Object
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type Some<T> = { value: T } & API<T>
|
|
15
|
-
export type None<T> = API<T>
|
|
16
|
-
export type Maybe<T> = Some<T> | None<T>
|
|
17
|
-
|
|
18
4
|
const $_kind = "@terrygonguet/utils/functional/maybe"
|
|
19
5
|
const $_variant_Some = "@terrygonguet/utils/functional/maybe/Some"
|
|
20
6
|
const $_variant_None = "@terrygonguet/utils/functional/maybe/None"
|
|
21
7
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
toJSON(this: Some<T>) {
|
|
40
|
-
return { $_kind, $_variant: $_variant_Some, value: this.value }
|
|
41
|
-
},
|
|
42
|
-
} as API<T>,
|
|
43
|
-
{
|
|
8
|
+
export interface Maybe<T> {
|
|
9
|
+
isSome(): this is Some<T>
|
|
10
|
+
isNone(): this is None<T>
|
|
11
|
+
orDefault(defaultValue: T): T
|
|
12
|
+
map<U>(f: (value: T) => U): Maybe<U>
|
|
13
|
+
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>
|
|
14
|
+
toResult(): Result<T, undefined>
|
|
15
|
+
toResult<U>(mapNone: () => U): Result<T, U>
|
|
16
|
+
toJSON(): Object
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Some<T> implements Maybe<T> {
|
|
20
|
+
value!: T
|
|
21
|
+
|
|
22
|
+
constructor(value: T) {
|
|
23
|
+
Object.defineProperties(this, {
|
|
44
24
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
45
25
|
$_variant: { value: $_variant_Some, enumerable: false, writable: false },
|
|
46
26
|
value: { value, writable: false },
|
|
47
|
-
}
|
|
48
|
-
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
isSome(): true {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
isNone(): false {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
orDefault() {
|
|
37
|
+
return this.value
|
|
38
|
+
}
|
|
39
|
+
map<U>(f: (value: T) => U) {
|
|
40
|
+
return new Some(f(this.value))
|
|
41
|
+
}
|
|
42
|
+
flatMap<U>(f: (value: T) => Maybe<U>) {
|
|
43
|
+
return f(this.value)
|
|
44
|
+
}
|
|
45
|
+
toResult<U>(): Result<T, U> {
|
|
46
|
+
return Result.Success(this.value)
|
|
47
|
+
}
|
|
48
|
+
toJSON(): Object {
|
|
49
|
+
return { $_kind, $_variant: $_variant_Some, value: this.value }
|
|
50
|
+
}
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
class None<T> implements Maybe<T> {
|
|
54
|
+
constructor() {}
|
|
55
|
+
|
|
56
|
+
isSome(): false {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
isNone(): true {
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
orDefault(defaultValue: T) {
|
|
63
|
+
return defaultValue
|
|
64
|
+
}
|
|
65
|
+
map<U>() {
|
|
66
|
+
return this as unknown as Maybe<U>
|
|
67
|
+
}
|
|
68
|
+
flatMap<U>() {
|
|
69
|
+
return this as unknown as Maybe<U>
|
|
70
|
+
}
|
|
71
|
+
toResult(): Result<T, undefined>
|
|
72
|
+
toResult<U>(mapNone: () => U): Result<T, U>
|
|
73
|
+
toResult<U>(mapNone?: () => U) {
|
|
74
|
+
return mapNone ? Result.Failure<T, U>(mapNone?.()) : Result.Failure<T, undefined>(undefined)
|
|
75
|
+
}
|
|
76
|
+
toJSON(): Object {
|
|
77
|
+
return { $_kind, $_variant: $_variant_None }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
66
80
|
|
|
81
|
+
const none = new None()
|
|
67
82
|
export const Maybe = {
|
|
68
|
-
Some
|
|
69
|
-
|
|
83
|
+
Some<T>(value: T): Maybe<T> {
|
|
84
|
+
return new Some(value)
|
|
85
|
+
},
|
|
86
|
+
None<T>() {
|
|
87
|
+
return none as Maybe<T>
|
|
88
|
+
},
|
|
70
89
|
from<T>(value: T | undefined | null): Maybe<T> {
|
|
71
90
|
switch (value) {
|
|
72
91
|
case null:
|
|
73
92
|
case undefined:
|
|
74
|
-
return None
|
|
93
|
+
return this.None()
|
|
75
94
|
default:
|
|
76
|
-
return Some(value!)
|
|
95
|
+
return new Some(value!)
|
|
77
96
|
}
|
|
78
97
|
},
|
|
79
98
|
/**
|
|
@@ -81,14 +100,16 @@ export const Maybe = {
|
|
|
81
100
|
* Use `Result.fromPromise()` if you need error details.
|
|
82
101
|
*/
|
|
83
102
|
fromPromise<T>(promise: Promise<T>, onResolve: (value: T) => T = identity): Promise<Maybe<T>> {
|
|
84
|
-
return promise.then(compose(onResolve, Maybe.from), constant(None))
|
|
103
|
+
return promise.then(compose(onResolve, Maybe.from), constant(this.None()))
|
|
85
104
|
},
|
|
86
105
|
JSONReviver(_key: string, value: any) {
|
|
87
106
|
if (value?.$_kind == $_kind) {
|
|
88
107
|
const $_variant = value?.$_variant
|
|
89
|
-
if ($_variant == $_variant_Some) return Some<unknown>(value?.value)
|
|
90
|
-
else if ($_variant == $_variant_None) return None
|
|
108
|
+
if ($_variant == $_variant_Some) return new Some<unknown>(value?.value)
|
|
109
|
+
else if ($_variant == $_variant_None) return Maybe.None()
|
|
91
110
|
else return value
|
|
92
111
|
} else return value
|
|
93
112
|
},
|
|
94
113
|
}
|
|
114
|
+
|
|
115
|
+
export type { Some, None }
|
package/src/functional/result.ts
CHANGED
|
@@ -1,78 +1,101 @@
|
|
|
1
1
|
import { compose, identity } from "./index.ts"
|
|
2
2
|
import { Maybe } from "./maybe.ts"
|
|
3
3
|
|
|
4
|
-
interface API<S, F> {
|
|
5
|
-
isSuccess(this: Result<S, F>): this is Success<S, F>
|
|
6
|
-
isFailure(this: Result<S, F>): this is Failure<S, F>
|
|
7
|
-
merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T
|
|
8
|
-
map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>
|
|
9
|
-
flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
|
|
10
|
-
toJSON(this: Result<S, F>): Object
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type Success<S, F> = { value: S } & API<S, F>
|
|
14
|
-
export type Failure<S, F> = { reason: F } & API<S, F>
|
|
15
|
-
export type Result<S, F> = Success<S, F> | Failure<S, F>
|
|
16
|
-
|
|
17
4
|
const $_kind = "@terrygonguet/utils/functional/result"
|
|
18
5
|
const $_variant_Success = "@terrygonguet/utils/functional/result/Success"
|
|
19
6
|
const $_variant_Failure = "@terrygonguet/utils/functional/result/Failure"
|
|
20
7
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return { $_kind, $_variant: $_variant_Success, value: this.value }
|
|
37
|
-
},
|
|
38
|
-
} as API<S, F>,
|
|
39
|
-
{
|
|
8
|
+
export interface Result<S, F> {
|
|
9
|
+
isSuccess(): this is Success<S, F>
|
|
10
|
+
isFailure(): this is Failure<S, F>
|
|
11
|
+
merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T
|
|
12
|
+
match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void
|
|
13
|
+
map<S2>(f: (value: S) => S2): Result<S2, F>
|
|
14
|
+
flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
|
|
15
|
+
toJSON(): Object
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class Success<S, F> implements Result<S, F> {
|
|
19
|
+
value!: S
|
|
20
|
+
|
|
21
|
+
constructor(value: S) {
|
|
22
|
+
Object.defineProperties(this, {
|
|
40
23
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
41
24
|
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
42
25
|
value: { value, writable: false },
|
|
43
|
-
}
|
|
44
|
-
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isSuccess(): true {
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
isFailure(): false {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
merge<T>(whenSuccess: (value: S) => T) {
|
|
36
|
+
return whenSuccess(this.value)
|
|
37
|
+
}
|
|
38
|
+
match(onSuccess: (value: S) => void) {
|
|
39
|
+
return onSuccess(this.value)
|
|
40
|
+
}
|
|
41
|
+
map<S2>(f: (value: S) => S2) {
|
|
42
|
+
return new Success<S2, F>(f(this.value))
|
|
43
|
+
}
|
|
44
|
+
flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2> {
|
|
45
|
+
return f(this.value)
|
|
46
|
+
}
|
|
47
|
+
toJSON(this: Success<S, F>) {
|
|
48
|
+
return { $_kind, $_variant: $_variant_Success, value: this.value }
|
|
49
|
+
}
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
merge<T>(this: Failure<S, F>, _: (value: S) => T, g: (reason: F) => T) {
|
|
53
|
-
return g(this.reason)
|
|
54
|
-
},
|
|
55
|
-
map<S2>(this: Failure<S, F>) {
|
|
56
|
-
return this as unknown as Result<S2, F>
|
|
57
|
-
},
|
|
58
|
-
flatMap<S2, F2>(this: Failure<S, F>) {
|
|
59
|
-
return this as unknown as Result<S2, F | F2>
|
|
60
|
-
},
|
|
61
|
-
toJSON(this: Failure<S, F>) {
|
|
62
|
-
return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
|
|
63
|
-
},
|
|
64
|
-
} as API<S, F>,
|
|
65
|
-
{
|
|
52
|
+
class Failure<S, F> implements Result<S, F> {
|
|
53
|
+
reason!: F
|
|
54
|
+
|
|
55
|
+
constructor(reason: F) {
|
|
56
|
+
Object.defineProperties(this, {
|
|
66
57
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
67
58
|
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
68
59
|
reason: { value: reason, writable: false },
|
|
69
|
-
}
|
|
70
|
-
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
isSuccess(): false {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
isFailure(): true {
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T) {
|
|
70
|
+
return whenFailure(this.reason)
|
|
71
|
+
}
|
|
72
|
+
match(_: (value: S) => void, onFailure: (reason: F) => void) {
|
|
73
|
+
return onFailure(this.reason)
|
|
74
|
+
}
|
|
75
|
+
map<S2>() {
|
|
76
|
+
return this as unknown as Result<S2, F>
|
|
77
|
+
}
|
|
78
|
+
flatMap<S2, F2>() {
|
|
79
|
+
return this as unknown as Result<S2, F | F2>
|
|
80
|
+
}
|
|
81
|
+
toJSON() {
|
|
82
|
+
return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>
|
|
87
|
+
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>
|
|
88
|
+
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone?: () => F) {
|
|
89
|
+
return mapNone ? maybe.toResult(mapNone) : maybe.toResult()
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
export const Result = {
|
|
74
|
-
Success,
|
|
75
|
-
|
|
93
|
+
Success<S, F>(value: S): Result<S, F> {
|
|
94
|
+
return new Success<S, F>(value)
|
|
95
|
+
},
|
|
96
|
+
Failure<S, F>(reason: F): Result<S, F> {
|
|
97
|
+
return new Failure<S, F>(reason)
|
|
98
|
+
},
|
|
76
99
|
try<S, F>(tryFn: () => S) {
|
|
77
100
|
return new TryCatch<S, F>(tryFn)
|
|
78
101
|
},
|
|
@@ -81,16 +104,18 @@ export const Result = {
|
|
|
81
104
|
onResolve: (value: S) => S,
|
|
82
105
|
onReject: (reason: unknown) => F,
|
|
83
106
|
): Promise<Result<S, F>> {
|
|
84
|
-
return promise.then(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
107
|
+
return promise.then(
|
|
108
|
+
compose(onResolve, this.Success<S, F>),
|
|
109
|
+
compose(onReject, this.Failure<S, F>),
|
|
110
|
+
)
|
|
88
111
|
},
|
|
112
|
+
fromMaybe: resultFromMaybe,
|
|
89
113
|
JSONReviver(_key: string, value: any) {
|
|
90
114
|
if (value?.$_kind == $_kind) {
|
|
91
115
|
const $_variant = value?.$_variant
|
|
92
|
-
if ($_variant == $_variant_Success) return Success<unknown, unknown>(value?.value)
|
|
93
|
-
else if ($_variant == $_variant_Failure)
|
|
116
|
+
if ($_variant == $_variant_Success) return new Success<unknown, unknown>(value?.value)
|
|
117
|
+
else if ($_variant == $_variant_Failure)
|
|
118
|
+
return new Failure<unknown, unknown>(value?.reason)
|
|
94
119
|
else return value
|
|
95
120
|
} else return value
|
|
96
121
|
},
|
|
@@ -112,13 +137,15 @@ class TryCatch<S, F> {
|
|
|
112
137
|
|
|
113
138
|
exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F> {
|
|
114
139
|
try {
|
|
115
|
-
const result = Success<S, F>(this.tryFn())
|
|
140
|
+
const result = Result.Success<S, F>(this.tryFn())
|
|
116
141
|
finallyFn?.(result)
|
|
117
142
|
return result
|
|
118
143
|
} catch (error) {
|
|
119
|
-
const result = Failure<S, F>(this.catchFn(error))
|
|
144
|
+
const result = Result.Failure<S, F>(this.catchFn(error))
|
|
120
145
|
finallyFn?.(result)
|
|
121
146
|
return result
|
|
122
147
|
}
|
|
123
148
|
}
|
|
124
149
|
}
|
|
150
|
+
|
|
151
|
+
export type { Success, Failure }
|
package/types/async.d.ts
CHANGED
|
@@ -1 +1,14 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function pause(ms: number): Promise<void>;
|
|
2
|
+
interface RetryOptions {
|
|
3
|
+
count?: number;
|
|
4
|
+
delay?: number | ((retryCount: number, error: any) => number);
|
|
5
|
+
}
|
|
6
|
+
export declare function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>;
|
|
7
|
+
export declare function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
8
|
+
interface AsyncMapOptions {
|
|
9
|
+
concurrent?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function asyncMap<T, U>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions): (data: T[]) => Promise<U[]>;
|
|
12
|
+
export declare function asyncMap<T, U>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions): Promise<U[]>;
|
|
13
|
+
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>;
|
|
14
|
+
export {};
|
|
@@ -2,8 +2,13 @@ import { Maybe } from "./maybe.ts";
|
|
|
2
2
|
export * from "./maybe.ts";
|
|
3
3
|
export * from "./result.ts";
|
|
4
4
|
export { default as compose } from "just-compose";
|
|
5
|
+
export { default as pipe } from "just-pipe";
|
|
5
6
|
export declare function identity<T>(value: T): T;
|
|
6
7
|
export declare function constant<T>(value: T): () => T;
|
|
7
|
-
export declare function at<T>(
|
|
8
|
-
export declare function
|
|
9
|
-
|
|
8
|
+
export declare function at(idx: number): <T>(data: T[]) => Maybe<T>;
|
|
9
|
+
export declare function at<T>(data: T[], idx: number): Maybe<T>;
|
|
10
|
+
type Predicate<T> = (value: T, idx: number, arr: T[]) => boolean;
|
|
11
|
+
export declare function find<T>(predicate: Predicate<T>): (data: T[]) => Maybe<T>;
|
|
12
|
+
export declare function find<T>(data: T[], predicate: Predicate<T>): Maybe<T>;
|
|
13
|
+
export declare function findIndex<T>(predicate: Predicate<T>): (data: T[]) => Maybe<number>;
|
|
14
|
+
export declare function findIndex<T>(data: T[], predicate: Predicate<T>): Maybe<number>;
|
|
@@ -1,28 +1,45 @@
|
|
|
1
1
|
import { Result } from "./result.ts";
|
|
2
|
-
interface
|
|
3
|
-
isSome(
|
|
4
|
-
isNone(
|
|
5
|
-
orDefault(
|
|
6
|
-
map<U>(
|
|
7
|
-
flatMap<U>(
|
|
8
|
-
toResult
|
|
9
|
-
|
|
2
|
+
export interface Maybe<T> {
|
|
3
|
+
isSome(): this is Some<T>;
|
|
4
|
+
isNone(): this is None<T>;
|
|
5
|
+
orDefault(defaultValue: T): T;
|
|
6
|
+
map<U>(f: (value: T) => U): Maybe<U>;
|
|
7
|
+
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
|
|
8
|
+
toResult(): Result<T, undefined>;
|
|
9
|
+
toResult<U>(mapNone: () => U): Result<T, U>;
|
|
10
|
+
toJSON(): Object;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
+
declare class Some<T> implements Maybe<T> {
|
|
12
13
|
value: T;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
constructor(value: T);
|
|
15
|
+
isSome(): true;
|
|
16
|
+
isNone(): false;
|
|
17
|
+
orDefault(): T;
|
|
18
|
+
map<U>(f: (value: T) => U): Some<U>;
|
|
19
|
+
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
|
|
20
|
+
toResult<U>(): Result<T, U>;
|
|
21
|
+
toJSON(): Object;
|
|
22
|
+
}
|
|
23
|
+
declare class None<T> implements Maybe<T> {
|
|
24
|
+
constructor();
|
|
25
|
+
isSome(): false;
|
|
26
|
+
isNone(): true;
|
|
27
|
+
orDefault(defaultValue: T): T;
|
|
28
|
+
map<U>(): Maybe<U>;
|
|
29
|
+
flatMap<U>(): Maybe<U>;
|
|
30
|
+
toResult(): Result<T, undefined>;
|
|
31
|
+
toResult<U>(mapNone: () => U): Result<T, U>;
|
|
32
|
+
toJSON(): Object;
|
|
33
|
+
}
|
|
17
34
|
export declare const Maybe: {
|
|
18
|
-
Some:
|
|
19
|
-
None:
|
|
20
|
-
from<
|
|
35
|
+
Some<T>(value: T): Maybe<T>;
|
|
36
|
+
None<T_1>(): Maybe<T_1>;
|
|
37
|
+
from<T_2>(value: T_2 | null | undefined): Maybe<T_2>;
|
|
21
38
|
/**
|
|
22
39
|
* CAUTION: this method swallows errors and simply returns None!
|
|
23
40
|
* Use `Result.fromPromise()` if you need error details.
|
|
24
41
|
*/
|
|
25
|
-
fromPromise<
|
|
42
|
+
fromPromise<T_3>(promise: Promise<T_3>, onResolve?: (value: T_3) => T_3): Promise<Maybe<T_3>>;
|
|
26
43
|
JSONReviver(_key: string, value: any): any;
|
|
27
44
|
};
|
|
28
|
-
export {};
|
|
45
|
+
export type { Some, None };
|
|
@@ -1,27 +1,51 @@
|
|
|
1
1
|
import { Maybe } from "./maybe.ts";
|
|
2
|
-
interface
|
|
3
|
-
isSuccess(
|
|
4
|
-
isFailure(
|
|
5
|
-
merge<T>(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
export interface Result<S, F> {
|
|
3
|
+
isSuccess(): this is Success<S, F>;
|
|
4
|
+
isFailure(): this is Failure<S, F>;
|
|
5
|
+
merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T;
|
|
6
|
+
match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void;
|
|
7
|
+
map<S2>(f: (value: S) => S2): Result<S2, F>;
|
|
8
|
+
flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
|
|
9
|
+
toJSON(): Object;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
+
declare class Success<S, F> implements Result<S, F> {
|
|
11
12
|
value: S;
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
constructor(value: S);
|
|
14
|
+
isSuccess(): true;
|
|
15
|
+
isFailure(): false;
|
|
16
|
+
merge<T>(whenSuccess: (value: S) => T): T;
|
|
17
|
+
match(onSuccess: (value: S) => void): void;
|
|
18
|
+
map<S2>(f: (value: S) => S2): Success<S2, F>;
|
|
19
|
+
flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2>;
|
|
20
|
+
toJSON(this: Success<S, F>): {
|
|
21
|
+
$_kind: string;
|
|
22
|
+
$_variant: string;
|
|
23
|
+
value: S;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
declare class Failure<S, F> implements Result<S, F> {
|
|
14
27
|
reason: F;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
constructor(reason: F);
|
|
29
|
+
isSuccess(): false;
|
|
30
|
+
isFailure(): true;
|
|
31
|
+
merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T): T;
|
|
32
|
+
match(_: (value: S) => void, onFailure: (reason: F) => void): void;
|
|
33
|
+
map<S2>(): Result<S2, F>;
|
|
34
|
+
flatMap<S2, F2>(): Result<S2, F | F2>;
|
|
35
|
+
toJSON(): {
|
|
36
|
+
$_kind: string;
|
|
37
|
+
$_variant: string;
|
|
38
|
+
reason: F;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
declare function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>;
|
|
42
|
+
declare function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>;
|
|
19
43
|
export declare const Result: {
|
|
20
|
-
Success:
|
|
21
|
-
Failure:
|
|
22
|
-
try<
|
|
23
|
-
fromPromise<
|
|
24
|
-
fromMaybe
|
|
44
|
+
Success<S, F>(value: S): Result<S, F>;
|
|
45
|
+
Failure<S_1, F_1>(reason: F_1): Result<S_1, F_1>;
|
|
46
|
+
try<S_2, F_2>(tryFn: () => S_2): TryCatch<S_2, F_2>;
|
|
47
|
+
fromPromise<S_3, F_3>(promise: Promise<S_3>, onResolve: (value: S_3) => S_3, onReject: (reason: unknown) => F_3): Promise<Result<S_3, F_3>>;
|
|
48
|
+
fromMaybe: typeof resultFromMaybe;
|
|
25
49
|
JSONReviver(_key: string, value: any): any;
|
|
26
50
|
};
|
|
27
51
|
declare class TryCatch<S, F> {
|
|
@@ -31,4 +55,4 @@ declare class TryCatch<S, F> {
|
|
|
31
55
|
catch(catchFn: (err: unknown) => F): this;
|
|
32
56
|
exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F>;
|
|
33
57
|
}
|
|
34
|
-
export {};
|
|
58
|
+
export type { Success, Failure };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|