@terrygonguet/utils 0.0.10 → 0.0.11
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 +92 -65
- package/package.json +12 -7
- package/src/async.ts +111 -2
- package/src/functional/index.ts +26 -7
- package/src/functional/maybe.ts +6 -5
- package/src/functional/result.ts +16 -5
- package/types/async.d.ts +14 -1
- package/types/functional/index.d.ts +8 -3
- package/types/functional/maybe.d.ts +2 -1
- package/types/functional/result.d.ts +4 -1
- /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,8 +1,8 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
function
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var v = (t, e, r) => e in t ? d(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var _ = (t, e, r) => (v(t, typeof e != "symbol" ? e + "" : e, r), r);
|
|
4
|
+
const o = "@terrygonguet/utils/functional/result", m = "@terrygonguet/utils/functional/result/Success", p = "@terrygonguet/utils/functional/result/Failure";
|
|
5
|
+
function i(t) {
|
|
6
6
|
return Object.create(
|
|
7
7
|
{
|
|
8
8
|
isSuccess: () => !0,
|
|
@@ -10,24 +10,27 @@ function a(t) {
|
|
|
10
10
|
merge(e) {
|
|
11
11
|
return e(this.value);
|
|
12
12
|
},
|
|
13
|
+
match(e) {
|
|
14
|
+
return e(this.value);
|
|
15
|
+
},
|
|
13
16
|
map(e) {
|
|
14
|
-
return
|
|
17
|
+
return i(e(this.value));
|
|
15
18
|
},
|
|
16
19
|
flatMap(e) {
|
|
17
20
|
return e(this.value);
|
|
18
21
|
},
|
|
19
22
|
toJSON() {
|
|
20
|
-
return { $_kind:
|
|
23
|
+
return { $_kind: o, $_variant: m, value: this.value };
|
|
21
24
|
}
|
|
22
25
|
},
|
|
23
26
|
{
|
|
24
|
-
$_kind: { value:
|
|
25
|
-
$_variant: { value:
|
|
27
|
+
$_kind: { value: o, enumerable: !1, writable: !1 },
|
|
28
|
+
$_variant: { value: m, enumerable: !1, writable: !1 },
|
|
26
29
|
value: { value: t, writable: !1 }
|
|
27
30
|
}
|
|
28
31
|
);
|
|
29
32
|
}
|
|
30
|
-
function
|
|
33
|
+
function f(t) {
|
|
31
34
|
return Object.create(
|
|
32
35
|
{
|
|
33
36
|
isSuccess: () => !1,
|
|
@@ -35,6 +38,9 @@ function c(t) {
|
|
|
35
38
|
merge(e, r) {
|
|
36
39
|
return r(this.reason);
|
|
37
40
|
},
|
|
41
|
+
match(e, r) {
|
|
42
|
+
return r(this.reason);
|
|
43
|
+
},
|
|
38
44
|
map() {
|
|
39
45
|
return this;
|
|
40
46
|
},
|
|
@@ -42,40 +48,41 @@ function c(t) {
|
|
|
42
48
|
return this;
|
|
43
49
|
},
|
|
44
50
|
toJSON() {
|
|
45
|
-
return { $_kind:
|
|
51
|
+
return { $_kind: o, $_variant: p, reason: this.reason };
|
|
46
52
|
}
|
|
47
53
|
},
|
|
48
54
|
{
|
|
49
|
-
$_kind: { value:
|
|
50
|
-
$_variant: { value:
|
|
55
|
+
$_kind: { value: o, enumerable: !1, writable: !1 },
|
|
56
|
+
$_variant: { value: m, enumerable: !1, writable: !1 },
|
|
51
57
|
reason: { value: t, writable: !1 }
|
|
52
58
|
}
|
|
53
59
|
);
|
|
54
60
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
function y(t, e) {
|
|
62
|
+
return e ? t.toResult(e) : t.toResult();
|
|
63
|
+
}
|
|
64
|
+
const w = {
|
|
65
|
+
Success: i,
|
|
66
|
+
Failure: f,
|
|
58
67
|
try(t) {
|
|
59
|
-
return new
|
|
68
|
+
return new k(t);
|
|
60
69
|
},
|
|
61
70
|
fromPromise(t, e, r) {
|
|
62
|
-
return t.then($(e,
|
|
63
|
-
},
|
|
64
|
-
fromMaybe(t, e) {
|
|
65
|
-
return t.toResult(e);
|
|
71
|
+
return t.then($(e, i), $(r, f));
|
|
66
72
|
},
|
|
73
|
+
fromMaybe: y,
|
|
67
74
|
JSONReviver(t, e) {
|
|
68
|
-
if ((e == null ? void 0 : e.$_kind) ==
|
|
75
|
+
if ((e == null ? void 0 : e.$_kind) == o) {
|
|
69
76
|
const r = e == null ? void 0 : e.$_variant;
|
|
70
|
-
return r ==
|
|
77
|
+
return r == m ? i(e == null ? void 0 : e.value) : r == p ? f(e == null ? void 0 : e.reason) : e;
|
|
71
78
|
} else
|
|
72
79
|
return e;
|
|
73
80
|
}
|
|
74
81
|
};
|
|
75
|
-
class
|
|
82
|
+
class k {
|
|
76
83
|
constructor(e) {
|
|
77
|
-
|
|
78
|
-
|
|
84
|
+
_(this, "tryFn");
|
|
85
|
+
_(this, "catchFn");
|
|
79
86
|
this.tryFn = e, this.catchFn = S;
|
|
80
87
|
}
|
|
81
88
|
catch(e) {
|
|
@@ -83,15 +90,15 @@ class p {
|
|
|
83
90
|
}
|
|
84
91
|
exec(e) {
|
|
85
92
|
try {
|
|
86
|
-
const r =
|
|
93
|
+
const r = i(this.tryFn());
|
|
87
94
|
return e == null || e(r), r;
|
|
88
95
|
} catch (r) {
|
|
89
|
-
const n =
|
|
96
|
+
const n = f(this.catchFn(r));
|
|
90
97
|
return e == null || e(n), n;
|
|
91
98
|
}
|
|
92
99
|
}
|
|
93
100
|
}
|
|
94
|
-
const
|
|
101
|
+
const c = "@terrygonguet/utils/functional/maybe", b = "@terrygonguet/utils/functional/maybe/Some", g = "@terrygonguet/utils/functional/maybe/None";
|
|
95
102
|
function l(t) {
|
|
96
103
|
return Object.create(
|
|
97
104
|
{
|
|
@@ -107,41 +114,41 @@ function l(t) {
|
|
|
107
114
|
return e(this.value);
|
|
108
115
|
},
|
|
109
116
|
toResult() {
|
|
110
|
-
return
|
|
117
|
+
return w.Success(this.value);
|
|
111
118
|
},
|
|
112
119
|
toJSON() {
|
|
113
|
-
return { $_kind:
|
|
120
|
+
return { $_kind: c, $_variant: b, value: this.value };
|
|
114
121
|
}
|
|
115
122
|
},
|
|
116
123
|
{
|
|
117
|
-
$_kind: { value:
|
|
118
|
-
$_variant: { value:
|
|
124
|
+
$_kind: { value: c, enumerable: !1, writable: !1 },
|
|
125
|
+
$_variant: { value: b, enumerable: !1, writable: !1 },
|
|
119
126
|
value: { value: t, writable: !1 }
|
|
120
127
|
}
|
|
121
128
|
);
|
|
122
129
|
}
|
|
123
|
-
const
|
|
130
|
+
const u = Object.create(
|
|
124
131
|
{
|
|
125
132
|
isSome: () => !1,
|
|
126
133
|
isNone: () => !0,
|
|
127
134
|
orDefault: (t) => t,
|
|
128
|
-
map: () =>
|
|
129
|
-
flatMap: () =>
|
|
130
|
-
toResult: (t) =>
|
|
131
|
-
toJSON: () => ({ $_kind:
|
|
135
|
+
map: () => u,
|
|
136
|
+
flatMap: () => u,
|
|
137
|
+
toResult: (t) => w.Failure(t == null ? void 0 : t()),
|
|
138
|
+
toJSON: () => ({ $_kind: c, $_variant: g })
|
|
132
139
|
},
|
|
133
140
|
{
|
|
134
|
-
$_kind: { value:
|
|
135
|
-
$_variant: { value:
|
|
141
|
+
$_kind: { value: c, enumerable: !1, writable: !1 },
|
|
142
|
+
$_variant: { value: g, enumerable: !1, writable: !1 }
|
|
136
143
|
}
|
|
137
|
-
),
|
|
144
|
+
), s = {
|
|
138
145
|
Some: l,
|
|
139
|
-
None:
|
|
146
|
+
None: u,
|
|
140
147
|
from(t) {
|
|
141
148
|
switch (t) {
|
|
142
149
|
case null:
|
|
143
150
|
case void 0:
|
|
144
|
-
return
|
|
151
|
+
return u;
|
|
145
152
|
default:
|
|
146
153
|
return l(t);
|
|
147
154
|
}
|
|
@@ -151,52 +158,72 @@ const s = Object.create(
|
|
|
151
158
|
* Use `Result.fromPromise()` if you need error details.
|
|
152
159
|
*/
|
|
153
160
|
fromPromise(t, e = S) {
|
|
154
|
-
return t.then($(e,
|
|
161
|
+
return t.then($(e, s.from), M(u));
|
|
155
162
|
},
|
|
156
163
|
JSONReviver(t, e) {
|
|
157
|
-
if ((e == null ? void 0 : e.$_kind) ==
|
|
164
|
+
if ((e == null ? void 0 : e.$_kind) == c) {
|
|
158
165
|
const r = e == null ? void 0 : e.$_variant;
|
|
159
|
-
return r ==
|
|
166
|
+
return r == b ? l(e == null ? void 0 : e.value) : r == g ? u : e;
|
|
160
167
|
} else
|
|
161
168
|
return e;
|
|
162
169
|
}
|
|
163
170
|
};
|
|
164
|
-
var $ =
|
|
165
|
-
function
|
|
171
|
+
var $ = x;
|
|
172
|
+
function x(t, e) {
|
|
166
173
|
if (!arguments.length)
|
|
167
174
|
throw new Error(
|
|
168
175
|
"expected at least one (and probably more) function arguments"
|
|
169
176
|
);
|
|
170
177
|
var r = arguments;
|
|
171
178
|
return function() {
|
|
172
|
-
for (var n = r[0].apply(this, arguments),
|
|
173
|
-
n = r[
|
|
179
|
+
for (var n = r[0].apply(this, arguments), a = r.length, h = 1; h < a; h++)
|
|
180
|
+
n = r[h].call(this, n);
|
|
174
181
|
return n;
|
|
175
182
|
};
|
|
176
183
|
}
|
|
184
|
+
var J = N;
|
|
185
|
+
function N(t, ...e) {
|
|
186
|
+
if (!arguments.length)
|
|
187
|
+
throw new Error("expected one value argument and least one function argument");
|
|
188
|
+
if (!e.length)
|
|
189
|
+
throw new Error(
|
|
190
|
+
"expected at least one (and probably more) function arguments"
|
|
191
|
+
);
|
|
192
|
+
for (var r = e[0](t), n = e.length, a = 1; a < n; a++)
|
|
193
|
+
r = e[a](r);
|
|
194
|
+
return r;
|
|
195
|
+
}
|
|
177
196
|
function S(t) {
|
|
178
197
|
return t;
|
|
179
198
|
}
|
|
180
|
-
function
|
|
199
|
+
function M(t) {
|
|
181
200
|
return () => t;
|
|
182
201
|
}
|
|
183
|
-
function
|
|
184
|
-
return
|
|
202
|
+
function F(t, e) {
|
|
203
|
+
return typeof t == "number" ? (r) => s.from(r.at(t)) : s.from(t.at(e));
|
|
185
204
|
}
|
|
186
|
-
function
|
|
187
|
-
return
|
|
205
|
+
function j(t, e) {
|
|
206
|
+
return typeof t == "function" ? (r) => s.from(r.find(t)) : s.from(t.find(e));
|
|
188
207
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
191
|
-
|
|
208
|
+
function O(t, e) {
|
|
209
|
+
if (typeof t == "function")
|
|
210
|
+
return (r) => {
|
|
211
|
+
const n = r.findIndex(t);
|
|
212
|
+
return n == -1 ? s.None : s.Some(n);
|
|
213
|
+
};
|
|
214
|
+
{
|
|
215
|
+
const r = t.findIndex(e);
|
|
216
|
+
return r == -1 ? s.None : s.Some(r);
|
|
217
|
+
}
|
|
192
218
|
}
|
|
193
219
|
export {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
220
|
+
s as Maybe,
|
|
221
|
+
w as Result,
|
|
222
|
+
F as at,
|
|
197
223
|
$ as compose,
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
S as identity
|
|
224
|
+
M as constant,
|
|
225
|
+
j as find,
|
|
226
|
+
O as findIndex,
|
|
227
|
+
S as identity,
|
|
228
|
+
J as pipe
|
|
202
229
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
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 : Maybe.Some(idx)
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
const idx = dataOrPredicate.findIndex(predicate!)
|
|
43
|
+
return idx == -1 ? Maybe.None : Maybe.Some(idx)
|
|
44
|
+
}
|
|
26
45
|
}
|
package/src/functional/maybe.ts
CHANGED
|
@@ -7,7 +7,8 @@ interface API<T> {
|
|
|
7
7
|
orDefault(this: Maybe<T>, defaultValue: T): T
|
|
8
8
|
map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>
|
|
9
9
|
flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>
|
|
10
|
-
toResult
|
|
10
|
+
toResult(this: Maybe<T>): Result<T, undefined>
|
|
11
|
+
toResult<U>(this: Maybe<T>, mapNone: () => U): Result<T, U>
|
|
11
12
|
toJSON(this: Maybe<T>): Object
|
|
12
13
|
}
|
|
13
14
|
|
|
@@ -34,12 +35,12 @@ function Some<T>(value: NonNullable<T>): Some<T> {
|
|
|
34
35
|
return f(this.value)
|
|
35
36
|
},
|
|
36
37
|
toResult(this: Some<T>) {
|
|
37
|
-
return Result.Success(this.value)
|
|
38
|
+
return Result.Success<T, any>(this.value)
|
|
38
39
|
},
|
|
39
40
|
toJSON(this: Some<T>) {
|
|
40
41
|
return { $_kind, $_variant: $_variant_Some, value: this.value }
|
|
41
42
|
},
|
|
42
|
-
}
|
|
43
|
+
} satisfies API<T>,
|
|
43
44
|
{
|
|
44
45
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
45
46
|
$_variant: { value: $_variant_Some, enumerable: false, writable: false },
|
|
@@ -55,9 +56,9 @@ const None: None<any> = Object.create(
|
|
|
55
56
|
orDefault: defaultValue => defaultValue,
|
|
56
57
|
map: () => None,
|
|
57
58
|
flatMap: () => None,
|
|
58
|
-
toResult: mapNone => Result.Failure(mapNone?.()),
|
|
59
|
+
toResult: <U>(mapNone?: () => U) => Result.Failure(mapNone?.()),
|
|
59
60
|
toJSON: () => ({ $_kind, $_variant: $_variant_None }),
|
|
60
|
-
}
|
|
61
|
+
} satisfies API<any>,
|
|
61
62
|
{
|
|
62
63
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
63
64
|
$_variant: { value: $_variant_None, enumerable: false, writable: false },
|
package/src/functional/result.ts
CHANGED
|
@@ -5,6 +5,7 @@ interface API<S, F> {
|
|
|
5
5
|
isSuccess(this: Result<S, F>): this is Success<S, F>
|
|
6
6
|
isFailure(this: Result<S, F>): this is Failure<S, F>
|
|
7
7
|
merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T
|
|
8
|
+
match(this: Result<S, F>, successFn: (value: S) => void, failureFn: (reason: F) => void): void
|
|
8
9
|
map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>
|
|
9
10
|
flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
|
|
10
11
|
toJSON(this: Result<S, F>): Object
|
|
@@ -26,6 +27,9 @@ function Success<S, F>(value: S): Success<S, F> {
|
|
|
26
27
|
merge<T>(this: Success<S, F>, f: (value: S) => T) {
|
|
27
28
|
return f(this.value)
|
|
28
29
|
},
|
|
30
|
+
match(this: Success<S, F>, successFn) {
|
|
31
|
+
return successFn(this.value)
|
|
32
|
+
},
|
|
29
33
|
map<S2>(this: Success<S, F>, f: (value: S) => S2) {
|
|
30
34
|
return Success(f(this.value))
|
|
31
35
|
},
|
|
@@ -35,7 +39,7 @@ function Success<S, F>(value: S): Success<S, F> {
|
|
|
35
39
|
toJSON(this: Success<S, F>) {
|
|
36
40
|
return { $_kind, $_variant: $_variant_Success, value: this.value }
|
|
37
41
|
},
|
|
38
|
-
}
|
|
42
|
+
} satisfies API<S, F>,
|
|
39
43
|
{
|
|
40
44
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
41
45
|
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
@@ -52,6 +56,9 @@ function Failure<S, F>(reason: F): Failure<S, F> {
|
|
|
52
56
|
merge<T>(this: Failure<S, F>, _: (value: S) => T, g: (reason: F) => T) {
|
|
53
57
|
return g(this.reason)
|
|
54
58
|
},
|
|
59
|
+
match(this: Failure<S, F>, _, failureFn) {
|
|
60
|
+
return failureFn(this.reason)
|
|
61
|
+
},
|
|
55
62
|
map<S2>(this: Failure<S, F>) {
|
|
56
63
|
return this as unknown as Result<S2, F>
|
|
57
64
|
},
|
|
@@ -61,7 +68,7 @@ function Failure<S, F>(reason: F): Failure<S, F> {
|
|
|
61
68
|
toJSON(this: Failure<S, F>) {
|
|
62
69
|
return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
|
|
63
70
|
},
|
|
64
|
-
}
|
|
71
|
+
} satisfies API<S, F>,
|
|
65
72
|
{
|
|
66
73
|
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
67
74
|
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
@@ -70,6 +77,12 @@ function Failure<S, F>(reason: F): Failure<S, F> {
|
|
|
70
77
|
)
|
|
71
78
|
}
|
|
72
79
|
|
|
80
|
+
function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>
|
|
81
|
+
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>
|
|
82
|
+
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone?: () => F) {
|
|
83
|
+
return mapNone ? maybe.toResult(mapNone) : maybe.toResult()
|
|
84
|
+
}
|
|
85
|
+
|
|
73
86
|
export const Result = {
|
|
74
87
|
Success,
|
|
75
88
|
Failure,
|
|
@@ -83,9 +96,7 @@ export const Result = {
|
|
|
83
96
|
): Promise<Result<S, F>> {
|
|
84
97
|
return promise.then(compose(onResolve, Success<S, F>), compose(onReject, Failure<S, F>))
|
|
85
98
|
},
|
|
86
|
-
fromMaybe
|
|
87
|
-
return maybe.toResult(mapNone)
|
|
88
|
-
},
|
|
99
|
+
fromMaybe: resultFromMaybe,
|
|
89
100
|
JSONReviver(_key: string, value: any) {
|
|
90
101
|
if (value?.$_kind == $_kind) {
|
|
91
102
|
const $_variant = value?.$_variant
|
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>;
|
|
@@ -5,7 +5,8 @@ interface API<T> {
|
|
|
5
5
|
orDefault(this: Maybe<T>, defaultValue: T): T;
|
|
6
6
|
map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>;
|
|
7
7
|
flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>;
|
|
8
|
-
toResult
|
|
8
|
+
toResult(this: Maybe<T>): Result<T, undefined>;
|
|
9
|
+
toResult<U>(this: Maybe<T>, mapNone: () => U): Result<T, U>;
|
|
9
10
|
toJSON(this: Maybe<T>): Object;
|
|
10
11
|
}
|
|
11
12
|
export type Some<T> = {
|
|
@@ -3,6 +3,7 @@ interface API<S, F> {
|
|
|
3
3
|
isSuccess(this: Result<S, F>): this is Success<S, F>;
|
|
4
4
|
isFailure(this: Result<S, F>): this is Failure<S, F>;
|
|
5
5
|
merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T;
|
|
6
|
+
match(this: Result<S, F>, successFn: (value: S) => void, failureFn: (reason: F) => void): void;
|
|
6
7
|
map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>;
|
|
7
8
|
flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
|
|
8
9
|
toJSON(this: Result<S, F>): Object;
|
|
@@ -16,12 +17,14 @@ export type Failure<S, F> = {
|
|
|
16
17
|
export type Result<S, F> = Success<S, F> | Failure<S, F>;
|
|
17
18
|
declare function Success<S, F>(value: S): Success<S, F>;
|
|
18
19
|
declare function Failure<S, F>(reason: F): Failure<S, F>;
|
|
20
|
+
declare function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>;
|
|
21
|
+
declare function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>;
|
|
19
22
|
export declare const Result: {
|
|
20
23
|
Success: typeof Success;
|
|
21
24
|
Failure: typeof Failure;
|
|
22
25
|
try<S, F>(tryFn: () => S): TryCatch<S, F>;
|
|
23
26
|
fromPromise<S_1, F_1>(promise: Promise<S_1>, onResolve: (value: S_1) => S_1, onReject: (reason: unknown) => F_1): Promise<Result<S_1, F_1>>;
|
|
24
|
-
fromMaybe
|
|
27
|
+
fromMaybe: typeof resultFromMaybe;
|
|
25
28
|
JSONReviver(_key: string, value: any): any;
|
|
26
29
|
};
|
|
27
30
|
declare class TryCatch<S, F> {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|