-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (156 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// grip.ts
class Err extends Error {
Ok() {
return false;
}
ok() {
return this.Ok();
}
Fail() {
return true;
}
fail() {
return this.Fail();
}
Of(cls) {
return this.cause instanceof cls || this instanceof cls;
}
of(cls) {
return this.Of(cls);
}
static fromCatch(error) {
const e = new Err(typeof error === "string" ? error : error.message);
e.cause = error;
e.stack = error.stack;
return e;
}
}
class Ok {
Ok() {
return true;
}
ok() {
return this.Ok();
}
Fail() {
return false;
}
fail() {
return this.Fail();
}
Of(cls) {
return this instanceof cls;
}
of(cls) {
return this.Of(cls);
}
toString() {
return "Ok";
}
}
class Result extends Array {
0;
1;
constructor(result, status) {
super(2);
this[0] = result;
this[1] = status;
}
get value() {
return this[0];
}
get status() {
return this[1];
}
Ok() {
return this[1].Ok();
}
ok() {
return this.Ok();
}
Fail() {
return this[1].Fail();
}
fail() {
return this.Fail();
}
Of(cls) {
return this[1].Of(cls);
}
of(cls) {
return this.Of(cls);
}
Iter() {
const value = this.value;
const that = this;
if (typeof value !== "object" && !(typeof value[Symbol.iterator] === "function" || typeof value[Symbol.asyncIterator] === "function")) {
return {
async* [Symbol.asyncIterator]() {
yield new Result(that.value, that.status);
},
*[Symbol.iterator]() {
yield new Result(that.value, that.status);
}
};
}
return {
async* [Symbol.asyncIterator]() {
yield* asyncIterator(value);
},
*[Symbol.iterator]() {
yield* iterator(value);
}
};
}
iter() {
return this.Iter();
}
}
function grip(action) {
if (action instanceof Promise) {
return promise(action);
}
try {
const result = action();
if (result instanceof Promise) {
return promise(result);
}
return new Result(result, new Ok);
} catch (err) {
return new Result(null, Err.fromCatch(err));
}
}
var promise = async (result) => {
try {
return new Result(await result, new Ok);
} catch (err) {
return new Result(null, Err.fromCatch(err));
}
};
var iterator = function* (iter) {
try {
let data = iter.next();
while (!data.done) {
yield new Result(data.value, new Ok);
data = iter.next();
}
} catch (e) {
yield new Result(null, Err.fromCatch(e));
}
};
var asyncIterator = async function* (iter) {
try {
let data = await iter.next();
while (!data.done) {
yield new Result(data.value, new Ok);
data = await iter.next();
}
} catch (e) {
yield new Result(null, Err.fromCatch(e));
}
};
export {
grip,
Ok,
Err
};