From b8ecf08f3421ae9cda4027cfbf96eba78383a923 Mon Sep 17 00:00:00 2001 From: Anton Nesterov Date: Wed, 16 Oct 2024 07:21:20 +0200 Subject: [PATCH] add dist --- .gitignore | 2 +- LICENSE | 21 ++++++++++++++ README.md | 14 +++++----- dist/grip.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 LICENSE create mode 100644 dist/grip.js diff --git a/.gitignore b/.gitignore index 9b1ee42..9b3e627 100644 --- a/.gitignore +++ b/.gitignore @@ -118,7 +118,7 @@ out # Nuxt.js build / generate output .nuxt -dist + # Gatsby files diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..79c949c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Anton Nesterov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 2e9a195..c0c1f7a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ It makes the control flow similar to that of Golang, but doesn't force you to ma ## Install ```bash -bun add github:nesterow/grip +bun add github:nesterow/grip # or pnpm ``` ## Usage @@ -24,20 +24,20 @@ import { grip } from '@nesterow/grip'; The result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}` ```javascript -const fetchResult = await grip( +const res = await grip( fetch('https://api.example.com') ); -if (fetchResult.Fail()) { +if (res.Fail()) { handleErrorProperly(); return; } -const jsonResult = await grip( +const json = await grip( res.value.json() ); -if (jsonResult.Of(SyntaxError)) { +if (json.Of(SyntaxError)) { handleJsonParseError(); return; } @@ -49,7 +49,7 @@ if (jsonResult.Of(SyntaxError)) { The result can also be received as a tuple if you want to handle errors in Go'ish style: ```javascript -const [response, fetchStatus] = await grip( +const [res, fetchStatus] = await grip( fetch('https://api.example.com') ); if (fetchStatus.Fail()) { @@ -58,7 +58,7 @@ if (fetchStatus.Fail()) { } const [json, parseStatus] = await grip( - response.json() + res.json() ); if (parseStatus.Of(SyntaxError)) { handleJsonParseError(); diff --git a/dist/grip.js b/dist/grip.js new file mode 100644 index 0000000..fe42fe5 --- /dev/null +++ b/dist/grip.js @@ -0,0 +1,78 @@ +// 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; + } + Fail() { + return true; + } + Of(cls) { + return this.cause instanceof cls || this instanceof cls; + } + static fromCatch(error) { + const e = new Err(typeof error === "string" ? error : error.message); + e.cause = error; + e.stack = error.stack; + return e; + } +} + +class Ok extends Error { + Ok() { + return true; + } + Fail() { + return false; + } + Of(cls) { + return this instanceof cls; + } +} + +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(); + } + Fail() { + return this[1].Fail(); + } + Of(impl) { + return this[1].Of(impl); + } +} +var promise = (result) => { + return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err))); +}; +export { + grip, + Ok, + Err +};