diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index cea4156..ad5781d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -16,4 +16,4 @@ jobs: - uses: actions/checkout@v4 - name: Publish package - run: npx jsr publish --allow-slow-types + run: npx jsr publish diff --git a/grip.ts b/grip.ts index ca37ed8..9f0b5b1 100644 --- a/grip.ts +++ b/grip.ts @@ -1,6 +1,6 @@ interface Status { message?: string; - cause?: any; + cause?: unknown; Ok(): boolean; ok(): boolean; Fail(): boolean; @@ -13,25 +13,25 @@ interface Status { * Error result */ export class Err extends Error { - Ok() { + Ok(): boolean { return false; } - ok() { + ok(): boolean { return this.Ok(); } - Fail() { + Fail(): boolean { return true; } - fail() { + fail(): boolean { return this.Fail(); } - Of(cls: any) { + Of(cls: any): boolean { return this.cause instanceof cls || this instanceof cls; } - of(cls: any) { + of(cls: any): boolean { return this.Of(cls); } - static fromCatch(error: any) { + static fromCatch(error: any): Error { 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() { + Ok(): boolean { return true; } - ok() { + ok(): boolean { return this.Ok(); } - Fail() { + Fail(): boolean { return false; } - fail() { + fail(): boolean { return this.Fail(); } - Of(cls: any) { + Of(cls: any): boolean { return this instanceof cls; } - of(cls: any) { + of(cls: any): boolean { return this.Of(cls); } - toString() { + toString(): string { return "Ok"; } } @@ -76,6 +76,15 @@ interface IResult { Fail(): boolean; } +type IterResult = { + [Symbol.asyncIterator](): AsyncGenerator< + Awaited>, + void, + unknown + >; + [Symbol.iterator](): Generator, void, unknown>; +}; + class Result extends Array implements IResult { 0: T; 1: Status; @@ -84,31 +93,31 @@ class Result extends Array implements IResult { this[0] = result; this[1] = status; } - get value() { + get value(): T { return this[0]; } - get status() { + get status(): Status { return this[1]; } - Ok() { + Ok(): boolean { return (this[1] as Status).Ok(); } - ok() { + ok(): boolean { return this.Ok(); } - Fail() { + Fail(): boolean { return (this[1] as Status).Fail(); } - fail() { + fail(): boolean { return this.Fail(); } - Of(cls: any) { + Of(cls: any): boolean { return (this[1] as Status).Of(cls); } - of(cls: any) { + of(cls: any): boolean { return this.Of(cls); } - Iter() { + Iter(): IterResult { const value = this.value; const that = this; if ( @@ -136,7 +145,7 @@ class Result extends Array implements IResult { }, }; } - iter() { + iter(): IterResult { return this.Iter(); } } @@ -183,8 +192,8 @@ export function grip(action: T): SafeResult { if (result instanceof Promise) { return promise(result) as SafeResult; } - return new Result(result, new Ok()) as SafeResult; - } catch (err: any) { + return new Result(result as T, new Ok()) as SafeResult; + } catch (err) { return new Result( null as never, Err.fromCatch(err), diff --git a/index.js b/index.js index 293ec04..56fa561 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,4 @@ // 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; @@ -123,6 +108,20 @@ 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); diff --git a/jsr.json b/jsr.json index 0575027..a8ae250 100644 --- a/jsr.json +++ b/jsr.json @@ -1,5 +1,5 @@ { "name": "@nesterow/grip", - "version": "0.0.2", + "version": "0.0.3", "exports": "./mod.ts" } diff --git a/package.json b/package.json index 8aa39e3..49fcda3 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.0.2", + "version": "0.0.3", "author": { "name": "Anton Nesterov", "url": "https://github.com/nesterow"