Compare commits

...

4 commits

Author SHA1 Message Date
Anton Nesterov 9051b35491
fix wrong type
Some checks failed
Publish / publish (push) Has been cancelled
2024-10-19 16:20:05 +02:00
Anton Nesterov 70373a6dc9
satisfy no-slow-types 2024-10-19 16:13:24 +02:00
Anton Nesterov e5dde2dedb
publish 2024-10-19 15:57:34 +02:00
Anton Nesterov a17ffc8204
publish 2024-10-19 15:56:23 +02:00
5 changed files with 53 additions and 45 deletions

View file

@ -16,4 +16,4 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Publish package - name: Publish package
run: npx jsr publish --allow-slow-types run: npx jsr publish

63
grip.ts
View file

@ -1,6 +1,6 @@
interface Status { interface Status {
message?: string; message?: string;
cause?: any; cause?: unknown;
Ok(): boolean; Ok(): boolean;
ok(): boolean; ok(): boolean;
Fail(): boolean; Fail(): boolean;
@ -13,25 +13,25 @@ interface Status {
* Error result * Error result
*/ */
export class Err extends Error { export class Err extends Error {
Ok() { Ok(): boolean {
return false; return false;
} }
ok() { ok(): boolean {
return this.Ok(); return this.Ok();
} }
Fail() { Fail(): boolean {
return true; return true;
} }
fail() { fail(): boolean {
return this.Fail(); return this.Fail();
} }
Of(cls: any) { Of(cls: any): boolean {
return this.cause instanceof cls || this instanceof cls; return this.cause instanceof cls || this instanceof cls;
} }
of(cls: any) { of(cls: any): boolean {
return this.Of(cls); return this.Of(cls);
} }
static fromCatch(error: any) { static fromCatch(error: any): Status {
const e = new Err(typeof error === "string" ? error : error.message); const e = new Err(typeof error === "string" ? error : error.message);
e.cause = error; e.cause = error;
e.stack = error.stack; e.stack = error.stack;
@ -43,25 +43,25 @@ export class Err extends Error {
* Successful result * Successful result
*/ */
export class Ok { export class Ok {
Ok() { Ok(): boolean {
return true; return true;
} }
ok() { ok(): boolean {
return this.Ok(); return this.Ok();
} }
Fail() { Fail(): boolean {
return false; return false;
} }
fail() { fail(): boolean {
return this.Fail(); return this.Fail();
} }
Of(cls: any) { Of(cls: any): boolean {
return this instanceof cls; return this instanceof cls;
} }
of(cls: any) { of(cls: any): boolean {
return this.Of(cls); return this.Of(cls);
} }
toString() { toString(): string {
return "Ok"; return "Ok";
} }
} }
@ -76,6 +76,15 @@ interface IResult<T> {
Fail(): boolean; 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> { class Result<T> extends Array<T | Status> implements IResult<T> {
0: T; 0: T;
1: Status; 1: Status;
@ -84,31 +93,31 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
this[0] = result; this[0] = result;
this[1] = status; this[1] = status;
} }
get value() { get value(): T {
return this[0]; return this[0];
} }
get status() { get status(): Status {
return this[1]; return this[1];
} }
Ok() { Ok(): boolean {
return (this[1] as Status).Ok(); return (this[1] as Status).Ok();
} }
ok() { ok(): boolean {
return this.Ok(); return this.Ok();
} }
Fail() { Fail(): boolean {
return (this[1] as Status).Fail(); return (this[1] as Status).Fail();
} }
fail() { fail(): boolean {
return this.Fail(); return this.Fail();
} }
Of(cls: any) { Of(cls: any): boolean {
return (this[1] as Status).Of(cls); return (this[1] as Status).Of(cls);
} }
of(cls: any) { of(cls: any): boolean {
return this.Of(cls); return this.Of(cls);
} }
Iter() { Iter(): IterResult<T> {
const value = this.value; const value = this.value;
const that = this; const that = this;
if ( if (
@ -136,7 +145,7 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
}, },
}; };
} }
iter() { iter(): IterResult<T> {
return this.Iter(); return this.Iter();
} }
} }
@ -183,8 +192,8 @@ export function grip<T>(action: T): SafeResult<T> {
if (result instanceof Promise) { if (result instanceof Promise) {
return promise<T>(result) as SafeResult<T>; return promise<T>(result) as SafeResult<T>;
} }
return new Result<T>(result, new Ok()) as SafeResult<T>; return new Result<T>(result as T, new Ok()) as SafeResult<T>;
} catch (err: any) { } catch (err) {
return new Result<T>( return new Result<T>(
null as never, null as never,
Err.fromCatch(err), Err.fromCatch(err),

View file

@ -1,19 +1,4 @@
// grip.ts // 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 { class Err extends Error {
Ok() { Ok() {
return false; return false;
@ -123,6 +108,20 @@ class Result extends Array {
return this.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) => { var promise = async (result) => {
try { try {
return new Result(await result, new Ok); return new Result(await result, new Ok);

View file

@ -1,5 +1,5 @@
{ {
"name": "@nesterow/grip", "name": "@nesterow/grip",
"version": "0.0.2", "version": "0.0.3",
"exports": "./mod.ts" "exports": "./mod.ts"
} }

View file

@ -1,5 +1,5 @@
{ {
"version": "0.0.2", "version": "0.0.3",
"author": { "author": {
"name": "Anton Nesterov", "name": "Anton Nesterov",
"url": "https://github.com/nesterow" "url": "https://github.com/nesterow"