@terrygonguet/utils 0.0.13 → 0.1.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/LICENSE +21 -21
- package/README.md +2 -2
- package/dist/async.d.ts +15 -0
- package/dist/async.js +60 -48
- package/dist/index.d.ts +18 -0
- package/dist/index.js +46 -39
- package/package.json +45 -63
- package/dist/functional.js +0 -266
- package/src/async.ts +0 -112
- package/src/functional/index.ts +0 -80
- package/src/functional/maybe.ts +0 -115
- package/src/functional/result.ts +0 -151
- package/src/index.ts +0 -63
- package/src/vite-env.d.ts +0 -1
- package/types/async.d.ts +0 -14
- package/types/functional/index.d.ts +0 -19
- package/types/functional/maybe.d.ts +0 -45
- package/types/functional/result.d.ts +0 -58
- package/types/index.d.ts +0 -13
package/dist/functional.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
var S = Object.defineProperty;
|
|
2
|
-
var v = (r, e, t) => e in r ? S(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
-
var u = (r, e, t) => (v(r, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
-
const a = "@terrygonguet/utils/functional/result", f = "@terrygonguet/utils/functional/result/Success", w = "@terrygonguet/utils/functional/result/Failure";
|
|
5
|
-
class l {
|
|
6
|
-
constructor(e) {
|
|
7
|
-
u(this, "value");
|
|
8
|
-
Object.defineProperties(this, {
|
|
9
|
-
$_kind: { value: a, enumerable: !1, writable: !1 },
|
|
10
|
-
$_variant: { value: f, 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 l(e(this.value));
|
|
28
|
-
}
|
|
29
|
-
flatMap(e) {
|
|
30
|
-
return e(this.value);
|
|
31
|
-
}
|
|
32
|
-
toJSON() {
|
|
33
|
-
return { $_kind: a, $_variant: f, value: this.value };
|
|
34
|
-
}
|
|
35
|
-
}
|
|
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: f, 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
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function y(r, e) {
|
|
68
|
-
return e ? r.toResult(e) : r.toResult();
|
|
69
|
-
}
|
|
70
|
-
const c = {
|
|
71
|
-
Success(r) {
|
|
72
|
-
return new l(r);
|
|
73
|
-
},
|
|
74
|
-
Failure(r) {
|
|
75
|
-
return new g(r);
|
|
76
|
-
},
|
|
77
|
-
try(r) {
|
|
78
|
-
return new d(r);
|
|
79
|
-
},
|
|
80
|
-
fromPromise(r, e, t) {
|
|
81
|
-
return r.then(
|
|
82
|
-
b(e, this.Success),
|
|
83
|
-
b(t, this.Failure)
|
|
84
|
-
);
|
|
85
|
-
},
|
|
86
|
-
fromMaybe: y,
|
|
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 == f ? new l(e == null ? void 0 : e.value) : t == w ? new g(e == null ? void 0 : e.reason) : e;
|
|
91
|
-
} else
|
|
92
|
-
return e;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
class d {
|
|
96
|
-
constructor(e) {
|
|
97
|
-
u(this, "tryFn");
|
|
98
|
-
u(this, "catchFn");
|
|
99
|
-
this.tryFn = e, this.catchFn = $;
|
|
100
|
-
}
|
|
101
|
-
catch(e) {
|
|
102
|
-
return this.catchFn = e, this;
|
|
103
|
-
}
|
|
104
|
-
exec(e) {
|
|
105
|
-
try {
|
|
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));
|
|
110
|
-
return e == null || e(n), n;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const h = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", p = "@terrygonguet/utils/functional/maybe/None";
|
|
115
|
-
class i {
|
|
116
|
-
constructor(e) {
|
|
117
|
-
u(this, "value");
|
|
118
|
-
Object.defineProperties(this, {
|
|
119
|
-
$_kind: { value: h, enumerable: !1, writable: !1 },
|
|
120
|
-
$_variant: { value: _, enumerable: !1, writable: !1 },
|
|
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 i(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
|
-
}
|
|
145
|
-
}
|
|
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: p };
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
const N = new k(), s = {
|
|
172
|
-
Some(r) {
|
|
173
|
-
return new i(r);
|
|
174
|
-
},
|
|
175
|
-
None() {
|
|
176
|
-
return N;
|
|
177
|
-
},
|
|
178
|
-
from(r) {
|
|
179
|
-
switch (r) {
|
|
180
|
-
case null:
|
|
181
|
-
case void 0:
|
|
182
|
-
return this.None();
|
|
183
|
-
default:
|
|
184
|
-
return new i(r);
|
|
185
|
-
}
|
|
186
|
-
},
|
|
187
|
-
/**
|
|
188
|
-
* CAUTION: this method swallows errors and simply returns None!
|
|
189
|
-
* Use `Result.fromPromise()` if you need error details.
|
|
190
|
-
*/
|
|
191
|
-
fromPromise(r, e = $) {
|
|
192
|
-
return r.then(b(e, s.from), M(this.None()));
|
|
193
|
-
},
|
|
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 i(e == null ? void 0 : e.value) : t == p ? s.None() : e;
|
|
198
|
-
} else
|
|
199
|
-
return e;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
var b = F;
|
|
203
|
-
function F(r, e) {
|
|
204
|
-
if (!arguments.length)
|
|
205
|
-
throw new Error(
|
|
206
|
-
"expected at least one (and probably more) function arguments"
|
|
207
|
-
);
|
|
208
|
-
var t = arguments;
|
|
209
|
-
return function() {
|
|
210
|
-
for (var n = t[0].apply(this, arguments), o = t.length, m = 1; m < o; m++)
|
|
211
|
-
n = t[m].call(this, n);
|
|
212
|
-
return n;
|
|
213
|
-
};
|
|
214
|
-
}
|
|
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, o = 1; o < n; o++)
|
|
224
|
-
t = e[o](t);
|
|
225
|
-
return t;
|
|
226
|
-
}
|
|
227
|
-
function $(r) {
|
|
228
|
-
return r;
|
|
229
|
-
}
|
|
230
|
-
function M(r) {
|
|
231
|
-
return () => r;
|
|
232
|
-
}
|
|
233
|
-
function j(r, e) {
|
|
234
|
-
return typeof r == "number" ? (t) => s.from(t.at(r)) : s.from(r.at(e));
|
|
235
|
-
}
|
|
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
|
-
}
|
|
249
|
-
}
|
|
250
|
-
function C(r, ...e) {
|
|
251
|
-
for (const t of e)
|
|
252
|
-
r = r == null ? void 0 : r[t];
|
|
253
|
-
return s.from(r);
|
|
254
|
-
}
|
|
255
|
-
export {
|
|
256
|
-
s as Maybe,
|
|
257
|
-
c as Result,
|
|
258
|
-
j as at,
|
|
259
|
-
b as compose,
|
|
260
|
-
M as constant,
|
|
261
|
-
E as find,
|
|
262
|
-
O as findIndex,
|
|
263
|
-
$ as identity,
|
|
264
|
-
J as pipe,
|
|
265
|
-
C as prop
|
|
266
|
-
};
|
package/src/async.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { findIndex } from "./functional/index.ts"
|
|
2
|
-
|
|
3
|
-
export function pause(ms: number): Promise<void> {
|
|
4
|
-
return new Promise(resolve => setTimeout(resolve, ms))
|
|
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
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Maybe } from "./maybe.ts"
|
|
2
|
-
|
|
3
|
-
export * from "./maybe.ts"
|
|
4
|
-
export * from "./result.ts"
|
|
5
|
-
export { default as compose } from "just-compose"
|
|
6
|
-
export { default as pipe } from "just-pipe"
|
|
7
|
-
|
|
8
|
-
export function identity<T>(value: T) {
|
|
9
|
-
return value
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function constant<T>(value: T) {
|
|
13
|
-
return () => value
|
|
14
|
-
}
|
|
15
|
-
|
|
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!))
|
|
21
|
-
}
|
|
22
|
-
|
|
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!))
|
|
31
|
-
}
|
|
32
|
-
|
|
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
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function prop<T extends {}, K1 extends keyof T = keyof T>(data: T, key1: K1): Maybe<T[K1]>
|
|
48
|
-
export function prop<
|
|
49
|
-
T extends {},
|
|
50
|
-
K1 extends keyof T = keyof T,
|
|
51
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
52
|
-
>(data: T, key1: K1, key2: K2): Maybe<T[K1][K2]>
|
|
53
|
-
export function prop<
|
|
54
|
-
T extends {},
|
|
55
|
-
K1 extends keyof T = keyof T,
|
|
56
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
57
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
58
|
-
>(data: T, key1: K1, key2: K2, key: K3): Maybe<T[K1][K2][K3]>
|
|
59
|
-
export function prop<
|
|
60
|
-
T extends {},
|
|
61
|
-
K1 extends keyof T = keyof T,
|
|
62
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
63
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
64
|
-
K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3],
|
|
65
|
-
>(data: T, key1: K1, key2: K2, key: K3, key4: K4): Maybe<T[K1][K2][K3][K4]>
|
|
66
|
-
export function prop<
|
|
67
|
-
T extends {},
|
|
68
|
-
K1 extends keyof T = keyof T,
|
|
69
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
70
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
71
|
-
K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3],
|
|
72
|
-
K5 extends keyof T[K1][K2][K3][K4] = keyof T[K1][K2][K3][K4],
|
|
73
|
-
>(data: T, key1: K1, key2: K2, key: K3, key4: K4, key5: K5): Maybe<T[K1][K2][K3][K4][K5]>
|
|
74
|
-
|
|
75
|
-
export function prop(data: any, ...path: string[]) {
|
|
76
|
-
for (const key of path) {
|
|
77
|
-
data = data?.[key]
|
|
78
|
-
}
|
|
79
|
-
return Maybe.from(data)
|
|
80
|
-
}
|
package/src/functional/maybe.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { compose, constant, identity } from "./index.ts"
|
|
2
|
-
import { Result } from "./result.ts"
|
|
3
|
-
|
|
4
|
-
const $_kind = "@terrygonguet/utils/functional/maybe"
|
|
5
|
-
const $_variant_Some = "@terrygonguet/utils/functional/maybe/Some"
|
|
6
|
-
const $_variant_None = "@terrygonguet/utils/functional/maybe/None"
|
|
7
|
-
|
|
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, {
|
|
24
|
-
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
25
|
-
$_variant: { value: $_variant_Some, enumerable: false, writable: false },
|
|
26
|
-
value: { value, writable: false },
|
|
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
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
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
|
-
}
|
|
80
|
-
|
|
81
|
-
const none = new None()
|
|
82
|
-
export const Maybe = {
|
|
83
|
-
Some<T>(value: T): Maybe<T> {
|
|
84
|
-
return new Some(value)
|
|
85
|
-
},
|
|
86
|
-
None<T>() {
|
|
87
|
-
return none as Maybe<T>
|
|
88
|
-
},
|
|
89
|
-
from<T>(value: T | undefined | null): Maybe<T> {
|
|
90
|
-
switch (value) {
|
|
91
|
-
case null:
|
|
92
|
-
case undefined:
|
|
93
|
-
return this.None()
|
|
94
|
-
default:
|
|
95
|
-
return new Some(value!)
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
/**
|
|
99
|
-
* CAUTION: this method swallows errors and simply returns None!
|
|
100
|
-
* Use `Result.fromPromise()` if you need error details.
|
|
101
|
-
*/
|
|
102
|
-
fromPromise<T>(promise: Promise<T>, onResolve: (value: T) => T = identity): Promise<Maybe<T>> {
|
|
103
|
-
return promise.then(compose(onResolve, Maybe.from), constant(this.None()))
|
|
104
|
-
},
|
|
105
|
-
JSONReviver(_key: string, value: any) {
|
|
106
|
-
if (value?.$_kind == $_kind) {
|
|
107
|
-
const $_variant = value?.$_variant
|
|
108
|
-
if ($_variant == $_variant_Some) return new Some<unknown>(value?.value)
|
|
109
|
-
else if ($_variant == $_variant_None) return Maybe.None()
|
|
110
|
-
else return value
|
|
111
|
-
} else return value
|
|
112
|
-
},
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export type { Some, None }
|