From 635d2aea06e6548b80b679411fa128135327602c Mon Sep 17 00:00:00 2001 From: Anton Nesterov Date: Thu, 17 Oct 2024 10:03:07 +0200 Subject: [PATCH] add aliases for capitalized methods --- README.md | 8 ++++---- dist/grip.js | 30 ++++++++++++++++++++++++++++++ grip.test.ts | 2 +- grip.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2def9e5..ea6b374 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ Instead of returning a nullish error, Grip always returns a consistent status ob ```javascript const [value, status] = grip(callable) -if (status.Of(MySpecificError)) { +if (status.of(MySpecificError)) { // handle specific error } -if (status.Fail()) { +if (status.fail()) { // handle any error } ``` @@ -24,10 +24,10 @@ The call result is better than tuple: ```javascript const result = grip(callable) -if (result.Fail()) { +if (result.fail()) { // handle any error } -if (result.Of(MySpecificError)) { +if (result.of(MySpecificError)) { // handle specific error } diff --git a/dist/grip.js b/dist/grip.js index 0b4cdcb..d9aa77f 100644 --- a/dist/grip.js +++ b/dist/grip.js @@ -18,12 +18,21 @@ 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; @@ -36,12 +45,21 @@ 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"; } @@ -64,12 +82,21 @@ class Result extends Array { 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; @@ -92,6 +119,9 @@ class Result extends Array { } }; } + iter() { + return this.Iter(); + } } var promise = (result) => { return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err))); diff --git a/grip.test.ts b/grip.test.ts index 18abf17..04524ef 100644 --- a/grip.test.ts +++ b/grip.test.ts @@ -49,7 +49,7 @@ test("fetch err", async () => { const [result, status] = await grip(fetch("https://localhost:30012")); expect(status.Ok()).toBe(false); expect(result === null).toBe(true); - expect(status.Of(Error)).toBe(true); + expect(status.of(Error)).toBe(true); }); test("fetch json", async () => { diff --git a/grip.ts b/grip.ts index 4c93e60..28d5f5a 100644 --- a/grip.ts +++ b/grip.ts @@ -2,20 +2,38 @@ interface Status { message?: string; cause?: any; Ok(): boolean; + /* alias Ok */ + ok(): boolean; Fail(): boolean; + /* alias Fail */ + fail(): boolean; Of(cls: any): boolean; + /* alias Of */ + of(cls: any): boolean; } export class Err extends Error { Ok() { return false; } + /* alias Ok */ + ok() { + return this.Ok(); + } Fail() { return true; } + /* alias Fail */ + fail() { + return this.Fail(); + } Of(cls: any) { return this.cause instanceof cls || this instanceof cls; } + /* alias Of */ + of(cls: any) { + return this.Of(cls); + } static fromCatch(error: any) { const e = new Err(typeof error === "string" ? error : error.message); e.cause = error; @@ -28,12 +46,24 @@ export class Ok { Ok() { return true; } + /* alias Ok */ + ok() { + return this.Ok(); + } Fail() { return false; } + /* alias Fail */ + fail() { + return this.Fail(); + } Of(cls: any) { return this instanceof cls; } + /* alias Of */ + of(cls: any) { + return this.Of(cls); + } toString() { return "Ok"; } @@ -66,12 +96,24 @@ class Result extends Array implements IResult { Ok() { return (this[1] as Status).Ok(); } + /* alias Ok */ + ok() { + return this.Ok(); + } Fail() { return (this[1] as Status).Fail(); } + /* alias Fail */ + fail() { + return this.Fail(); + } Of(cls: any) { return (this[1] as Status).Of(cls); } + /* alias Of */ + of(cls: any) { + return this.Of(cls); + } Iter() { const value = this.value; const that = this; @@ -100,6 +142,10 @@ class Result extends Array implements IResult { }, }; } + /* alias Iter */ + iter() { + return this.Iter(); + } } type Unwrap =