2024-10-16 05:21:20 +00:00
|
|
|
// grip.ts
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Err extends Error {
|
|
|
|
Ok() {
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
ok() {
|
|
|
|
return this.Ok();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
Fail() {
|
|
|
|
return true;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
fail() {
|
|
|
|
return this.Fail();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
Of(cls) {
|
|
|
|
return this.cause instanceof cls || this instanceof cls;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
of(cls) {
|
|
|
|
return this.Of(cls);
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
static fromCatch(error) {
|
|
|
|
const e = new Err(typeof error === "string" ? error : error.message);
|
|
|
|
e.cause = error;
|
|
|
|
e.stack = error.stack;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 06:18:40 +00:00
|
|
|
class Ok {
|
2024-10-16 05:21:20 +00:00
|
|
|
Ok() {
|
|
|
|
return true;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
ok() {
|
|
|
|
return this.Ok();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
Fail() {
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
fail() {
|
|
|
|
return this.Fail();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
Of(cls) {
|
|
|
|
return this instanceof cls;
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
of(cls) {
|
|
|
|
return this.Of(cls);
|
|
|
|
}
|
2024-10-16 06:18:40 +00:00
|
|
|
toString() {
|
|
|
|
return "Ok";
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
ok() {
|
|
|
|
return this.Ok();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
Fail() {
|
|
|
|
return this[1].Fail();
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
fail() {
|
|
|
|
return this.Fail();
|
|
|
|
}
|
2024-10-17 03:24:06 +00:00
|
|
|
Of(cls) {
|
|
|
|
return this[1].Of(cls);
|
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
of(cls) {
|
|
|
|
return this.Of(cls);
|
|
|
|
}
|
2024-10-17 03:24:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 05:21:20 +00:00
|
|
|
}
|
2024-10-17 08:03:07 +00:00
|
|
|
iter() {
|
|
|
|
return this.Iter();
|
|
|
|
}
|
2024-10-16 05:21:20 +00:00
|
|
|
}
|
|
|
|
var promise = (result) => {
|
|
|
|
return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err)));
|
|
|
|
};
|
2024-10-17 03:24:06 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 05:21:20 +00:00
|
|
|
export {
|
|
|
|
grip,
|
|
|
|
Ok,
|
|
|
|
Err
|
|
|
|
};
|