@terrygonguet/utils 0.0.8 → 0.0.10
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/functional.js +124 -89
- package/dist/main.js +45 -12
- package/package.json +7 -4
- package/src/functional/index.ts +19 -0
- package/src/functional/maybe.ts +15 -2
- package/src/functional/result.ts +12 -1
- package/src/main.ts +38 -0
- package/types/functional/index.d.ts +5 -0
- package/types/functional/maybe.d.ts +7 -0
- package/types/functional/result.d.ts +3 -0
- package/types/main.d.ts +5 -0
package/dist/functional.js
CHANGED
|
@@ -1,66 +1,8 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
return Object.create(
|
|
7
|
-
{
|
|
8
|
-
isSome: () => !0,
|
|
9
|
-
isNone: () => !1,
|
|
10
|
-
orDefault() {
|
|
11
|
-
return this.value;
|
|
12
|
-
},
|
|
13
|
-
map(e) {
|
|
14
|
-
return u(e(this.value));
|
|
15
|
-
},
|
|
16
|
-
flatMap(e) {
|
|
17
|
-
return e(this.value);
|
|
18
|
-
},
|
|
19
|
-
toJSON() {
|
|
20
|
-
return { $_kind: s, $_variant: _, value: this.value };
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
$_kind: { value: s, enumerable: !1, writable: !1 },
|
|
25
|
-
$_variant: { value: _, enumerable: !1, writable: !1 },
|
|
26
|
-
value: { value: t, writable: !1 }
|
|
27
|
-
}
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
const a = Object.create(
|
|
31
|
-
{
|
|
32
|
-
isSome: () => !1,
|
|
33
|
-
isNone: () => !0,
|
|
34
|
-
orDefault: $,
|
|
35
|
-
map: () => a,
|
|
36
|
-
flatMap: () => a,
|
|
37
|
-
toJSON: () => ({ $_kind: s, $_variant: h })
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
$_kind: { value: s, enumerable: !1, writable: !1 },
|
|
41
|
-
$_variant: { value: h, enumerable: !1, writable: !1 }
|
|
42
|
-
}
|
|
43
|
-
), y = {
|
|
44
|
-
Some: u,
|
|
45
|
-
None: a,
|
|
46
|
-
from(t) {
|
|
47
|
-
switch (t) {
|
|
48
|
-
case null:
|
|
49
|
-
case void 0:
|
|
50
|
-
return a;
|
|
51
|
-
default:
|
|
52
|
-
return u(t);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
JSONReviver(t, e) {
|
|
56
|
-
if ((e == null ? void 0 : e.$_kind) == s) {
|
|
57
|
-
const r = e == null ? void 0 : e.$_variant;
|
|
58
|
-
return r == _ ? u(e == null ? void 0 : e.value) : r == h ? a : e;
|
|
59
|
-
} else
|
|
60
|
-
return e;
|
|
61
|
-
}
|
|
62
|
-
}, i = "@terrygonguet/utils/functional/result", o = "@terrygonguet/utils/functional/result/Success", b = "@terrygonguet/utils/functional/result/Failure";
|
|
63
|
-
function c(t) {
|
|
1
|
+
var y = Object.defineProperty;
|
|
2
|
+
var k = (t, e, r) => e in t ? y(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var h = (t, e, r) => (k(t, typeof e != "symbol" ? e + "" : e, r), r);
|
|
4
|
+
const i = "@terrygonguet/utils/functional/result", f = "@terrygonguet/utils/functional/result/Success", d = "@terrygonguet/utils/functional/result/Failure";
|
|
5
|
+
function a(t) {
|
|
64
6
|
return Object.create(
|
|
65
7
|
{
|
|
66
8
|
isSuccess: () => !0,
|
|
@@ -69,23 +11,23 @@ function c(t) {
|
|
|
69
11
|
return e(this.value);
|
|
70
12
|
},
|
|
71
13
|
map(e) {
|
|
72
|
-
return
|
|
14
|
+
return a(e(this.value));
|
|
73
15
|
},
|
|
74
16
|
flatMap(e) {
|
|
75
17
|
return e(this.value);
|
|
76
18
|
},
|
|
77
19
|
toJSON() {
|
|
78
|
-
return { $_kind: i, $_variant:
|
|
20
|
+
return { $_kind: i, $_variant: f, value: this.value };
|
|
79
21
|
}
|
|
80
22
|
},
|
|
81
23
|
{
|
|
82
24
|
$_kind: { value: i, enumerable: !1, writable: !1 },
|
|
83
|
-
$_variant: { value:
|
|
25
|
+
$_variant: { value: f, enumerable: !1, writable: !1 },
|
|
84
26
|
value: { value: t, writable: !1 }
|
|
85
27
|
}
|
|
86
28
|
);
|
|
87
29
|
}
|
|
88
|
-
function
|
|
30
|
+
function c(t) {
|
|
89
31
|
return Object.create(
|
|
90
32
|
{
|
|
91
33
|
isSuccess: () => !1,
|
|
@@ -100,68 +42,161 @@ function m(t) {
|
|
|
100
42
|
return this;
|
|
101
43
|
},
|
|
102
44
|
toJSON() {
|
|
103
|
-
return { $_kind: i, $_variant:
|
|
45
|
+
return { $_kind: i, $_variant: d, reason: this.reason };
|
|
104
46
|
}
|
|
105
47
|
},
|
|
106
48
|
{
|
|
107
49
|
$_kind: { value: i, enumerable: !1, writable: !1 },
|
|
108
|
-
$_variant: { value:
|
|
50
|
+
$_variant: { value: f, enumerable: !1, writable: !1 },
|
|
109
51
|
reason: { value: t, writable: !1 }
|
|
110
52
|
}
|
|
111
53
|
);
|
|
112
54
|
}
|
|
113
|
-
const
|
|
114
|
-
Success:
|
|
115
|
-
Failure:
|
|
55
|
+
const g = {
|
|
56
|
+
Success: a,
|
|
57
|
+
Failure: c,
|
|
116
58
|
try(t) {
|
|
117
|
-
return new
|
|
59
|
+
return new p(t);
|
|
60
|
+
},
|
|
61
|
+
fromPromise(t, e, r) {
|
|
62
|
+
return t.then($(e, a), $(r, c));
|
|
63
|
+
},
|
|
64
|
+
fromMaybe(t, e) {
|
|
65
|
+
return t.toResult(e);
|
|
118
66
|
},
|
|
119
67
|
JSONReviver(t, e) {
|
|
120
68
|
if ((e == null ? void 0 : e.$_kind) == i) {
|
|
121
69
|
const r = e == null ? void 0 : e.$_variant;
|
|
122
|
-
return r ==
|
|
70
|
+
return r == f ? a(e == null ? void 0 : e.value) : r == d ? c(e == null ? void 0 : e.reason) : e;
|
|
123
71
|
} else
|
|
124
72
|
return e;
|
|
125
73
|
}
|
|
126
74
|
};
|
|
127
|
-
class
|
|
75
|
+
class p {
|
|
128
76
|
constructor(e) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.tryFn = e, this.catchFn =
|
|
77
|
+
h(this, "tryFn");
|
|
78
|
+
h(this, "catchFn");
|
|
79
|
+
this.tryFn = e, this.catchFn = S;
|
|
132
80
|
}
|
|
133
81
|
catch(e) {
|
|
134
82
|
return this.catchFn = e, this;
|
|
135
83
|
}
|
|
136
84
|
exec(e) {
|
|
137
85
|
try {
|
|
138
|
-
const r =
|
|
86
|
+
const r = a(this.tryFn());
|
|
139
87
|
return e == null || e(r), r;
|
|
140
88
|
} catch (r) {
|
|
141
|
-
const n =
|
|
89
|
+
const n = c(this.catchFn(r));
|
|
142
90
|
return e == null || e(n), n;
|
|
143
91
|
}
|
|
144
92
|
}
|
|
145
93
|
}
|
|
146
|
-
|
|
147
|
-
function
|
|
94
|
+
const u = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", b = "@terrygonguet/utils/functional/maybe/None";
|
|
95
|
+
function l(t) {
|
|
96
|
+
return Object.create(
|
|
97
|
+
{
|
|
98
|
+
isSome: () => !0,
|
|
99
|
+
isNone: () => !1,
|
|
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 },
|
|
118
|
+
$_variant: { value: _, enumerable: !1, writable: !1 },
|
|
119
|
+
value: { value: t, writable: !1 }
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const s = Object.create(
|
|
124
|
+
{
|
|
125
|
+
isSome: () => !1,
|
|
126
|
+
isNone: () => !0,
|
|
127
|
+
orDefault: (t) => t,
|
|
128
|
+
map: () => s,
|
|
129
|
+
flatMap: () => s,
|
|
130
|
+
toResult: (t) => g.Failure(t == null ? void 0 : t()),
|
|
131
|
+
toJSON: () => ({ $_kind: u, $_variant: b })
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
$_kind: { value: u, enumerable: !1, writable: !1 },
|
|
135
|
+
$_variant: { value: b, enumerable: !1, writable: !1 }
|
|
136
|
+
}
|
|
137
|
+
), o = {
|
|
138
|
+
Some: l,
|
|
139
|
+
None: s,
|
|
140
|
+
from(t) {
|
|
141
|
+
switch (t) {
|
|
142
|
+
case null:
|
|
143
|
+
case void 0:
|
|
144
|
+
return s;
|
|
145
|
+
default:
|
|
146
|
+
return l(t);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* CAUTION: this method swallows errors and simply returns None!
|
|
151
|
+
* Use `Result.fromPromise()` if you need error details.
|
|
152
|
+
*/
|
|
153
|
+
fromPromise(t, e = S) {
|
|
154
|
+
return t.then($(e, o.from), O(s));
|
|
155
|
+
},
|
|
156
|
+
JSONReviver(t, e) {
|
|
157
|
+
if ((e == null ? void 0 : e.$_kind) == u) {
|
|
158
|
+
const r = e == null ? void 0 : e.$_variant;
|
|
159
|
+
return r == _ ? l(e == null ? void 0 : e.value) : r == b ? s : e;
|
|
160
|
+
} else
|
|
161
|
+
return e;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var $ = v;
|
|
165
|
+
function v(t, e) {
|
|
148
166
|
if (!arguments.length)
|
|
149
167
|
throw new Error(
|
|
150
168
|
"expected at least one (and probably more) function arguments"
|
|
151
169
|
);
|
|
152
170
|
var r = arguments;
|
|
153
171
|
return function() {
|
|
154
|
-
for (var n = r[0].apply(this, arguments),
|
|
155
|
-
n = r[
|
|
172
|
+
for (var n = r[0].apply(this, arguments), w = r.length, m = 1; m < w; m++)
|
|
173
|
+
n = r[m].call(this, n);
|
|
156
174
|
return n;
|
|
157
175
|
};
|
|
158
176
|
}
|
|
159
|
-
function
|
|
177
|
+
function S(t) {
|
|
160
178
|
return t;
|
|
161
179
|
}
|
|
180
|
+
function O(t) {
|
|
181
|
+
return () => t;
|
|
182
|
+
}
|
|
183
|
+
function x(t, e) {
|
|
184
|
+
return o.from(t.at(e));
|
|
185
|
+
}
|
|
186
|
+
function J(t, e) {
|
|
187
|
+
return o.from(t.find(e));
|
|
188
|
+
}
|
|
189
|
+
function M(t, e) {
|
|
190
|
+
const r = t.findIndex(e);
|
|
191
|
+
return r == -1 ? o.None : o.Some(r);
|
|
192
|
+
}
|
|
162
193
|
export {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
$ as
|
|
194
|
+
o as Maybe,
|
|
195
|
+
g as Result,
|
|
196
|
+
x as at,
|
|
197
|
+
$ as compose,
|
|
198
|
+
O as constant,
|
|
199
|
+
J as find,
|
|
200
|
+
M as findIndex,
|
|
201
|
+
S as identity
|
|
167
202
|
};
|
package/dist/main.js
CHANGED
|
@@ -1,22 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { constant as r } from "./functional.js";
|
|
2
|
+
function s(n, t, e) {
|
|
3
|
+
return Math.min(Math.max(n, t), e);
|
|
3
4
|
}
|
|
4
|
-
function
|
|
5
|
+
function a(n, t, e) {
|
|
5
6
|
try {
|
|
6
|
-
return JSON.parse(n,
|
|
7
|
+
return JSON.parse(n, e);
|
|
7
8
|
} catch {
|
|
8
9
|
return t;
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
|
-
function
|
|
12
|
-
return function(t,
|
|
13
|
-
for (const
|
|
14
|
-
|
|
15
|
-
return
|
|
12
|
+
function i(...n) {
|
|
13
|
+
return function(t, e) {
|
|
14
|
+
for (const o of n)
|
|
15
|
+
e = o(t, e);
|
|
16
|
+
return e;
|
|
16
17
|
};
|
|
17
18
|
}
|
|
19
|
+
function u() {
|
|
20
|
+
const n = () => o, t = r(!1), e = r(!0), o = new Proxy(() => {
|
|
21
|
+
}, {
|
|
22
|
+
get: n,
|
|
23
|
+
set: n,
|
|
24
|
+
apply: n,
|
|
25
|
+
construct: n,
|
|
26
|
+
deleteProperty: e,
|
|
27
|
+
has: e,
|
|
28
|
+
preventExtensions: t,
|
|
29
|
+
defineProperty: t
|
|
30
|
+
});
|
|
31
|
+
return o;
|
|
32
|
+
}
|
|
33
|
+
function f() {
|
|
34
|
+
}
|
|
35
|
+
function p(n) {
|
|
36
|
+
throw new Error("This should never be called");
|
|
37
|
+
}
|
|
38
|
+
async function h(n) {
|
|
39
|
+
const e = new TextEncoder().encode(n);
|
|
40
|
+
return await crypto.subtle.digest("SHA-1", e);
|
|
41
|
+
}
|
|
42
|
+
function* d(n, t, e = 1) {
|
|
43
|
+
for (let o = n; o < t; o += e)
|
|
44
|
+
yield o;
|
|
45
|
+
}
|
|
18
46
|
export {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
47
|
+
s as clamp,
|
|
48
|
+
i as composeJSONRevivers,
|
|
49
|
+
u as createNoopProxy,
|
|
50
|
+
p as exhaustive,
|
|
51
|
+
h as hash,
|
|
52
|
+
f as noop,
|
|
53
|
+
d as range,
|
|
54
|
+
a as safeParse
|
|
22
55
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "./dist/main.js",
|
|
6
6
|
"author": {
|
|
@@ -29,18 +29,21 @@
|
|
|
29
29
|
],
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
-
"types": "./types/main.d.ts",
|
|
33
32
|
"import": "./dist/main.js"
|
|
34
33
|
},
|
|
35
34
|
"./async": {
|
|
36
|
-
"types": "./types/async.d.ts",
|
|
37
35
|
"import": "./dist/async.js"
|
|
38
36
|
},
|
|
39
37
|
"./functional": {
|
|
40
|
-
"types": "./types/functional/index.d.ts",
|
|
41
38
|
"import": "./dist/functional.js"
|
|
42
39
|
}
|
|
43
40
|
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"*": {
|
|
43
|
+
"async": ["./types/async.d.ts"],
|
|
44
|
+
"functional": ["./types/functional/index.d.ts"]
|
|
45
|
+
}
|
|
46
|
+
},
|
|
44
47
|
"types": "./types/main.d.ts",
|
|
45
48
|
"dependencies": {
|
|
46
49
|
"just-compose": "^2.3.0"
|
package/src/functional/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Maybe } from "./maybe.ts"
|
|
2
|
+
|
|
1
3
|
export * from "./maybe.ts"
|
|
2
4
|
export * from "./result.ts"
|
|
3
5
|
export { default as compose } from "just-compose"
|
|
@@ -5,3 +7,20 @@ export { default as compose } from "just-compose"
|
|
|
5
7
|
export function identity<T>(value: T) {
|
|
6
8
|
return value
|
|
7
9
|
}
|
|
10
|
+
|
|
11
|
+
export function constant<T>(value: T) {
|
|
12
|
+
return () => value
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function at<T>(arr: T[], idx: number) {
|
|
16
|
+
return Maybe.from(arr.at(idx))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function find<T>(arr: T[], predicate: (value: T, idx: number, arr: T[]) => boolean) {
|
|
20
|
+
return Maybe.from(arr.find(predicate))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function findIndex<T>(arr: T[], predicate: (value: T, idx: number, arr: T[]) => boolean): Maybe<number> {
|
|
24
|
+
const idx = arr.findIndex(predicate)
|
|
25
|
+
return idx == -1 ? Maybe.None : Maybe.Some(idx)
|
|
26
|
+
}
|
package/src/functional/maybe.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { identity } from "./index.ts"
|
|
1
|
+
import { compose, constant, identity } from "./index.ts"
|
|
2
|
+
import { Result } from "./result.ts"
|
|
2
3
|
|
|
3
4
|
interface API<T> {
|
|
4
5
|
isSome(this: Maybe<T>): this is Some<T>
|
|
@@ -6,6 +7,7 @@ interface API<T> {
|
|
|
6
7
|
orDefault(this: Maybe<T>, defaultValue: T): T
|
|
7
8
|
map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>
|
|
8
9
|
flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>
|
|
10
|
+
toResult<U>(this: Maybe<T>, mapNone?: () => U): Result<T, U>
|
|
9
11
|
toJSON(this: Maybe<T>): Object
|
|
10
12
|
}
|
|
11
13
|
|
|
@@ -31,6 +33,9 @@ function Some<T>(value: NonNullable<T>): Some<T> {
|
|
|
31
33
|
flatMap(this: Some<T>, f) {
|
|
32
34
|
return f(this.value)
|
|
33
35
|
},
|
|
36
|
+
toResult(this: Some<T>) {
|
|
37
|
+
return Result.Success(this.value)
|
|
38
|
+
},
|
|
34
39
|
toJSON(this: Some<T>) {
|
|
35
40
|
return { $_kind, $_variant: $_variant_Some, value: this.value }
|
|
36
41
|
},
|
|
@@ -47,9 +52,10 @@ const None: None<any> = Object.create(
|
|
|
47
52
|
{
|
|
48
53
|
isSome: () => false,
|
|
49
54
|
isNone: () => true,
|
|
50
|
-
orDefault:
|
|
55
|
+
orDefault: defaultValue => defaultValue,
|
|
51
56
|
map: () => None,
|
|
52
57
|
flatMap: () => None,
|
|
58
|
+
toResult: mapNone => Result.Failure(mapNone?.()),
|
|
53
59
|
toJSON: () => ({ $_kind, $_variant: $_variant_None }),
|
|
54
60
|
} as API<any>,
|
|
55
61
|
{
|
|
@@ -70,6 +76,13 @@ export const Maybe = {
|
|
|
70
76
|
return Some(value!)
|
|
71
77
|
}
|
|
72
78
|
},
|
|
79
|
+
/**
|
|
80
|
+
* CAUTION: this method swallows errors and simply returns None!
|
|
81
|
+
* Use `Result.fromPromise()` if you need error details.
|
|
82
|
+
*/
|
|
83
|
+
fromPromise<T>(promise: Promise<T>, onResolve: (value: T) => T = identity): Promise<Maybe<T>> {
|
|
84
|
+
return promise.then(compose(onResolve, Maybe.from), constant(None))
|
|
85
|
+
},
|
|
73
86
|
JSONReviver(_key: string, value: any) {
|
|
74
87
|
if (value?.$_kind == $_kind) {
|
|
75
88
|
const $_variant = value?.$_variant
|
package/src/functional/result.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { identity } from "./index.ts"
|
|
1
|
+
import { compose, identity } from "./index.ts"
|
|
2
|
+
import { Maybe } from "./maybe.ts"
|
|
2
3
|
|
|
3
4
|
interface API<S, F> {
|
|
4
5
|
isSuccess(this: Result<S, F>): this is Success<S, F>
|
|
@@ -75,6 +76,16 @@ export const Result = {
|
|
|
75
76
|
try<S, F>(tryFn: () => S) {
|
|
76
77
|
return new TryCatch<S, F>(tryFn)
|
|
77
78
|
},
|
|
79
|
+
fromPromise<S, F>(
|
|
80
|
+
promise: Promise<S>,
|
|
81
|
+
onResolve: (value: S) => S,
|
|
82
|
+
onReject: (reason: unknown) => F,
|
|
83
|
+
): Promise<Result<S, F>> {
|
|
84
|
+
return promise.then(compose(onResolve, Success<S, F>), compose(onReject, Failure<S, F>))
|
|
85
|
+
},
|
|
86
|
+
fromMaybe<S, F>(maybe: Maybe<S>, mapNone?: () => F) {
|
|
87
|
+
return maybe.toResult(mapNone)
|
|
88
|
+
},
|
|
78
89
|
JSONReviver(_key: string, value: any) {
|
|
79
90
|
if (value?.$_kind == $_kind) {
|
|
80
91
|
const $_variant = value?.$_variant
|
package/src/main.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { constant } from "./functional/index.ts"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Behaviour is undefined when max < min
|
|
3
5
|
*/
|
|
@@ -23,3 +25,39 @@ export function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver {
|
|
|
23
25
|
return value
|
|
24
26
|
}
|
|
25
27
|
}
|
|
28
|
+
|
|
29
|
+
export function createNoopProxy<T>() {
|
|
30
|
+
const noop = () => proxy
|
|
31
|
+
const no = constant(false)
|
|
32
|
+
const yes = constant(true)
|
|
33
|
+
const proxy: any = new Proxy(() => {}, {
|
|
34
|
+
get: noop,
|
|
35
|
+
set: noop,
|
|
36
|
+
apply: noop,
|
|
37
|
+
construct: noop,
|
|
38
|
+
deleteProperty: yes,
|
|
39
|
+
has: yes,
|
|
40
|
+
preventExtensions: no,
|
|
41
|
+
defineProperty: no,
|
|
42
|
+
})
|
|
43
|
+
return proxy as T
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function noop() {}
|
|
47
|
+
|
|
48
|
+
export function exhaustive(_: never): never {
|
|
49
|
+
throw new Error("This should never be called")
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function hash(message: string) {
|
|
53
|
+
const encoder = new TextEncoder()
|
|
54
|
+
const data = encoder.encode(message)
|
|
55
|
+
const hash = await crypto.subtle.digest("SHA-1", data)
|
|
56
|
+
return hash
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function* range(start: number, end: number, step = 1) {
|
|
60
|
+
for (let i = start; i < end; i += step) {
|
|
61
|
+
yield i
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { Maybe } from "./maybe.ts";
|
|
1
2
|
export * from "./maybe.ts";
|
|
2
3
|
export * from "./result.ts";
|
|
3
4
|
export { default as compose } from "just-compose";
|
|
4
5
|
export declare function identity<T>(value: T): T;
|
|
6
|
+
export declare function constant<T>(value: T): () => T;
|
|
7
|
+
export declare function at<T>(arr: T[], idx: number): Maybe<T>;
|
|
8
|
+
export declare function find<T>(arr: T[], predicate: (value: T, idx: number, arr: T[]) => boolean): Maybe<T>;
|
|
9
|
+
export declare function findIndex<T>(arr: T[], predicate: (value: T, idx: number, arr: T[]) => boolean): Maybe<number>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { Result } from "./result.ts";
|
|
1
2
|
interface API<T> {
|
|
2
3
|
isSome(this: Maybe<T>): this is Some<T>;
|
|
3
4
|
isNone(this: Maybe<T>): this is None<T>;
|
|
4
5
|
orDefault(this: Maybe<T>, defaultValue: T): T;
|
|
5
6
|
map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>;
|
|
6
7
|
flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>;
|
|
8
|
+
toResult<U>(this: Maybe<T>, mapNone?: () => U): Result<T, U>;
|
|
7
9
|
toJSON(this: Maybe<T>): Object;
|
|
8
10
|
}
|
|
9
11
|
export type Some<T> = {
|
|
@@ -16,6 +18,11 @@ export declare const Maybe: {
|
|
|
16
18
|
Some: typeof Some;
|
|
17
19
|
None: None<any>;
|
|
18
20
|
from<T>(value: T | null | undefined): Maybe<T>;
|
|
21
|
+
/**
|
|
22
|
+
* CAUTION: this method swallows errors and simply returns None!
|
|
23
|
+
* Use `Result.fromPromise()` if you need error details.
|
|
24
|
+
*/
|
|
25
|
+
fromPromise<T_1>(promise: Promise<T_1>, onResolve?: (value: T_1) => T_1): Promise<Maybe<T_1>>;
|
|
19
26
|
JSONReviver(_key: string, value: any): any;
|
|
20
27
|
};
|
|
21
28
|
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Maybe } from "./maybe.ts";
|
|
1
2
|
interface API<S, F> {
|
|
2
3
|
isSuccess(this: Result<S, F>): this is Success<S, F>;
|
|
3
4
|
isFailure(this: Result<S, F>): this is Failure<S, F>;
|
|
@@ -19,6 +20,8 @@ export declare const Result: {
|
|
|
19
20
|
Success: typeof Success;
|
|
20
21
|
Failure: typeof Failure;
|
|
21
22
|
try<S, F>(tryFn: () => S): TryCatch<S, F>;
|
|
23
|
+
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<S_2, F_2>(maybe: Maybe<S_2>, mapNone?: (() => F_2) | undefined): Result<S_2, F_2>;
|
|
22
25
|
JSONReviver(_key: string, value: any): any;
|
|
23
26
|
};
|
|
24
27
|
declare class TryCatch<S, F> {
|
package/types/main.d.ts
CHANGED
|
@@ -5,4 +5,9 @@ export declare function clamp(value: number, min: number, max: number): number;
|
|
|
5
5
|
type JSONReviver = (key: string, value: any) => any;
|
|
6
6
|
export declare function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver): any;
|
|
7
7
|
export declare function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver;
|
|
8
|
+
export declare function createNoopProxy<T>(): T;
|
|
9
|
+
export declare function noop(): void;
|
|
10
|
+
export declare function exhaustive(_: never): never;
|
|
11
|
+
export declare function hash(message: string): Promise<ArrayBuffer>;
|
|
12
|
+
export declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
8
13
|
export {};
|