Compare commits
No commits in common. "9051b3549182dd777ea0dd5390047c25748c4ecd" and "69d4788f6462a576d8feea30d13e4e06e93e93f3" have entirely different histories.
9051b35491
...
69d4788f64
2
.github/workflows/publish.yml
vendored
2
.github/workflows/publish.yml
vendored
|
@ -16,4 +16,4 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Publish package
|
||||
run: npx jsr publish
|
||||
run: npx jsr publish --allow-slow-types
|
||||
|
|
63
grip.ts
63
grip.ts
|
@ -1,6 +1,6 @@
|
|||
interface Status {
|
||||
message?: string;
|
||||
cause?: unknown;
|
||||
cause?: any;
|
||||
Ok(): boolean;
|
||||
ok(): boolean;
|
||||
Fail(): boolean;
|
||||
|
@ -13,25 +13,25 @@ interface Status {
|
|||
* Error result
|
||||
*/
|
||||
export class Err extends Error {
|
||||
Ok(): boolean {
|
||||
Ok() {
|
||||
return false;
|
||||
}
|
||||
ok(): boolean {
|
||||
ok() {
|
||||
return this.Ok();
|
||||
}
|
||||
Fail(): boolean {
|
||||
Fail() {
|
||||
return true;
|
||||
}
|
||||
fail(): boolean {
|
||||
fail() {
|
||||
return this.Fail();
|
||||
}
|
||||
Of(cls: any): boolean {
|
||||
Of(cls: any) {
|
||||
return this.cause instanceof cls || this instanceof cls;
|
||||
}
|
||||
of(cls: any): boolean {
|
||||
of(cls: any) {
|
||||
return this.Of(cls);
|
||||
}
|
||||
static fromCatch(error: any): Status {
|
||||
static fromCatch(error: any) {
|
||||
const e = new Err(typeof error === "string" ? error : error.message);
|
||||
e.cause = error;
|
||||
e.stack = error.stack;
|
||||
|
@ -43,25 +43,25 @@ export class Err extends Error {
|
|||
* Successful result
|
||||
*/
|
||||
export class Ok {
|
||||
Ok(): boolean {
|
||||
Ok() {
|
||||
return true;
|
||||
}
|
||||
ok(): boolean {
|
||||
ok() {
|
||||
return this.Ok();
|
||||
}
|
||||
Fail(): boolean {
|
||||
Fail() {
|
||||
return false;
|
||||
}
|
||||
fail(): boolean {
|
||||
fail() {
|
||||
return this.Fail();
|
||||
}
|
||||
Of(cls: any): boolean {
|
||||
Of(cls: any) {
|
||||
return this instanceof cls;
|
||||
}
|
||||
of(cls: any): boolean {
|
||||
of(cls: any) {
|
||||
return this.Of(cls);
|
||||
}
|
||||
toString(): string {
|
||||
toString() {
|
||||
return "Ok";
|
||||
}
|
||||
}
|
||||
|
@ -76,15 +76,6 @@ interface IResult<T> {
|
|||
Fail(): boolean;
|
||||
}
|
||||
|
||||
type IterResult<T> = {
|
||||
[Symbol.asyncIterator](): AsyncGenerator<
|
||||
Awaited<SafeResult<T>>,
|
||||
void,
|
||||
unknown
|
||||
>;
|
||||
[Symbol.iterator](): Generator<SafeResult<T>, void, unknown>;
|
||||
};
|
||||
|
||||
class Result<T> extends Array<T | Status> implements IResult<T> {
|
||||
0: T;
|
||||
1: Status;
|
||||
|
@ -93,31 +84,31 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
|
|||
this[0] = result;
|
||||
this[1] = status;
|
||||
}
|
||||
get value(): T {
|
||||
get value() {
|
||||
return this[0];
|
||||
}
|
||||
get status(): Status {
|
||||
get status() {
|
||||
return this[1];
|
||||
}
|
||||
Ok(): boolean {
|
||||
Ok() {
|
||||
return (this[1] as Status).Ok();
|
||||
}
|
||||
ok(): boolean {
|
||||
ok() {
|
||||
return this.Ok();
|
||||
}
|
||||
Fail(): boolean {
|
||||
Fail() {
|
||||
return (this[1] as Status).Fail();
|
||||
}
|
||||
fail(): boolean {
|
||||
fail() {
|
||||
return this.Fail();
|
||||
}
|
||||
Of(cls: any): boolean {
|
||||
Of(cls: any) {
|
||||
return (this[1] as Status).Of(cls);
|
||||
}
|
||||
of(cls: any): boolean {
|
||||
of(cls: any) {
|
||||
return this.Of(cls);
|
||||
}
|
||||
Iter(): IterResult<T> {
|
||||
Iter() {
|
||||
const value = this.value;
|
||||
const that = this;
|
||||
if (
|
||||
|
@ -145,7 +136,7 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
|
|||
},
|
||||
};
|
||||
}
|
||||
iter(): IterResult<T> {
|
||||
iter() {
|
||||
return this.Iter();
|
||||
}
|
||||
}
|
||||
|
@ -192,8 +183,8 @@ export function grip<T>(action: T): SafeResult<T> {
|
|||
if (result instanceof Promise) {
|
||||
return promise<T>(result) as SafeResult<T>;
|
||||
}
|
||||
return new Result<T>(result as T, new Ok()) as SafeResult<T>;
|
||||
} catch (err) {
|
||||
return new Result<T>(result, new Ok()) as SafeResult<T>;
|
||||
} catch (err: any) {
|
||||
return new Result<T>(
|
||||
null as never,
|
||||
Err.fromCatch(err),
|
||||
|
|
29
index.js
29
index.js
|
@ -1,4 +1,19 @@
|
|||
// 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;
|
||||
|
@ -108,20 +123,6 @@ class Result extends Array {
|
|||
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);
|
||||
|
|
2
jsr.json
2
jsr.json
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "@nesterow/grip",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.2",
|
||||
"exports": "./mod.ts"
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.2",
|
||||
"author": {
|
||||
"name": "Anton Nesterov",
|
||||
"url": "https://github.com/nesterow"
|
||||
|
|
Loading…
Reference in a new issue