From 849b917d507e0bf2f75c85544d141cf2965ac05a Mon Sep 17 00:00:00 2001 From: Anton Nesterov Date: Wed, 16 Oct 2024 07:02:31 +0200 Subject: [PATCH] init --- .gitignore | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 70 ++++++++++++++++++++ bun.lockb | Bin 0 -> 3144 bytes grip.test.ts | 65 +++++++++++++++++++ grip.ts | 107 ++++++++++++++++++++++++++++++ mod.ts | 1 + package.json | 15 +++++ tsconfig.json | 27 ++++++++ 8 files changed, 460 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 bun.lockb create mode 100644 grip.test.ts create mode 100644 grip.ts create mode 100644 mod.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e9a195 --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# Grip + +Simplified result/error handling for JavaScript. +Grip always returns a consistent call result ready to be handled. +It makes the control flow similar to that of Golang, but doesn't force you to make additional null checks or create transitional variables to hold error results. + +## Install + +```bash +bun add github:nesterow/grip +``` + +## Usage + +The `grip` function accepts a function or a promise and returns a result with return value and status. +The result can be hadled as either an object or a tuple. + +```javascript +import { grip } from '@nesterow/grip'; +``` + +## Handle result as an object: + +The result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}` + +```javascript +const fetchResult = await grip( + fetch('https://api.example.com') +); + +if (fetchResult.Fail()) { + handleErrorProperly(); + return; +} + +const jsonResult = await grip( + res.value.json() +); + +if (jsonResult.Of(SyntaxError)) { + handleJsonParseError(); + return; +} + +``` + +## Handle result as a tuple: + +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( + fetch('https://api.example.com') +); +if (fetchStatus.Fail()) { + handleErrorProperly(); + return; +} + +const [json, parseStatus] = await grip( + response.json() +); +if (parseStatus.Of(SyntaxError)) { + handleJsonParseError(); + return; +} +``` + +## License +MIT diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..c9d59f2f9ffaf6af2208e39a6b7d17d7006b19e5 GIT binary patch literal 3144 zcmd5;YfKbZ6rROJaIG(j{h@&_O3?y4`+{YK?Sfi=8Bx#>rD9oNmK|MYmzmi`z!Yey z&_=?m4`NL$uhyUvF+nNDR*Mp-Xnhc(RY*x%NwvOG2^a-??l2doO+`YR(39Lf_s*Q} zn{&^(d(T-OpmuSrI)!G`6ysK=xEKWtmEDoL%SziRmDRy!Sa_9NqwpXI!vBl*?ylQq zOS4sVpAXG7-%cowIkElF$nw;Nb5H7g3i4X!f)R+% zCxM;=x&bthC?eK^m>42W@(|(FLQ3=)V-;lM!RQQfm3- zzP4w1&zeq-1a>Z2TAnprddsWu*TTAAqOOFNtt)-xy!)tr`@H+|3-Yfw7p>?#(AwNE z=x%%*-oK%uRvy1nRV3j>d&MHJvgL-g99fkc=ox!rfzQ>aPaal%vnJV1l!f-z=HD0^ z_@l9MxM!hX>sy<>`VP2<>-Rn$?Qq__VSHX==#=nE^CWNTY#ZpNlcQbjyF*rc zw3lscjc@9Xj`*0eS@!wvzs1*mZ@A3mnGf@}fH=*0S$P|0F0G=&W|=?p%*sp**)QRh zV`o9jgEsAp8mQ*iC>r82D{Ed)$oEaK>F=G7>Wc0kbn}NJ4mAbb-t=kg#X$Ax!m4fA zp4-*MdyZEG4E2AvezEmVPMw5TdamVt*RE8IynQ0(w@X`fA^?K^wZ%s(7iA27+p}ZqS=ZX&;+&cqd-}@m_1&k&FW^2BtFj`q7}q>dcZ4{Cpln_(#Y79{*nm}BBybWYR?6UU~KG{f;0yPd=pm)ycqlv%0M zC^dgPG=lesE>k-J|7dbKwL0tS>XbU&c~GRiyp zj~GSlmML=J`xW24m<#D*k;4py9Erm8j?R$eGQzG5<34Hsx+`4Qah+Z$+YwG`NDV>Y z&Hz_>Zx6eLr#PM(plR5(#_79j~VPCLQafcn_JC&xCqcYqSORn3^Q!Hb#Z{}H=NjEFC6G1SJ z2Z85O16)@2-nxF+2}wkKQK2!sOSXId z=&- zl}>X!n@uv#jGY|mVAItTMrsu@RiWT(wIrXOgH{5?B=B(?5iQsY9C{7n5b3ixal$y# zs7dG~RzQf1RbzS8h9Nc=lSZe~FLg?41~k&8N;nMRi0tKVWO0Db;#9(+(+MFF@;R2n XN$?Xn0D_4HeB40l5s!HA{&(t65$aZ0 literal 0 HcmV?d00001 diff --git a/grip.test.ts b/grip.test.ts new file mode 100644 index 0000000..8c49ee3 --- /dev/null +++ b/grip.test.ts @@ -0,0 +1,65 @@ +import { test, expect } from "bun:test"; +import { grip } from "./grip"; + +test("Promise", async () => { + const [result, status] = await grip(Promise.resolve("ok")); + expect(result).toBe("ok"); + expect(status.Ok()).toBe(true); +}); + +test("Promise.reject", async () => { + const [result, status] = await grip(() => + Promise.reject(new Error("not ok")), + ); + expect(status.Ok()).toBe(false); + expect(status.message).toBe("not ok"); + expect(result === null).toBe(true); +}); + +test("() => Promise", async () => { + const [result, status] = await grip(() => Promise.resolve("ok")); + expect(result).toBe("ok"); + expect(status.Ok()).toBe(true); +}); + +test("() => value", () => { + const [result, status] = grip(() => "ok"); + expect(result).toBe("ok"); + expect(status.Ok()).toBe(true); +}); + +test("() => throw", async () => { + const [_, status] = grip(() => { + if (1) throw "not ok"; + }); + expect(status.Ok()).toBe(false); + expect(status.message).toBe("not ok"); +}); + +test("Result { 0, 1, value, Ok(), Fail() }", async () => { + const result = grip(() => { + if (1) throw "not ok"; + }); + expect(result.Ok()).toBe(false); + expect(result.value === null).toBe(true); + expect(result.status.message).toBe("not ok"); +}); + +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); +}); + +test("fetch json", async () => { + const [result, fetchStatus] = await grip(() => + fetch("https://google.com/404"), + ); + expect(fetchStatus.Ok()).toBe(true); + expect(result.ok).toBe(false); + const [json, jsonStatus] = await grip(result.json()); + expect(jsonStatus.Ok()).toBe(false); + expect(jsonStatus.Of(SyntaxError)).toBe(true); + expect(json === null).toBe(true); +}); diff --git a/grip.ts b/grip.ts new file mode 100644 index 0000000..24fbd1a --- /dev/null +++ b/grip.ts @@ -0,0 +1,107 @@ +interface Status extends Error { + Ok(): boolean; + Fail(): boolean; + Of(cls: any): boolean; +} + +export class Err extends Error { + Ok() { + return false; + } + Fail() { + return true; + } + Of(cls: any) { + return this.cause instanceof cls || this instanceof cls; + } + static fromCatch(error: any) { + const e = new Err(typeof error === "string" ? error : error.message); + e.cause = error; + e.stack = error.stack; + return e; + } +} + +export class Ok extends Error { + Ok() { + return true; + } + Fail() { + return false; + } + Of(cls: any) { + return this instanceof cls; + } +} + +interface IResult { + 0: T; + 1: Status; + value: T; + status: Status; + Of(cls: any): boolean; + Ok(): boolean; + Fail(): boolean; +} + +class Result extends Array implements IResult { + 0: T; + 1: Status; + constructor(result: T, status: Status) { + super(2); + this[0] = result; + this[1] = status; + } + get value() { + return this[0]; + } + get status() { + return this[1]; + } + Ok() { + return (this[1] as Status).Ok(); + } + Fail() { + return (this[1] as Status).Fail(); + } + Of(impl: any) { + return (this[1] as Status).Of(impl); + } +} + +type Unwrap = + T extends Promise + ? U + : T extends (...args: any) => Promise + ? U + : T extends (...args: any) => infer U + ? U + : T; + +export type SafeResult = + T extends Promise + ? Promise>> + : T extends () => Promise + ? Promise>> + : Result>; + +export function grip(action: T): SafeResult { + if (action instanceof Promise) { + return promise(action) as SafeResult; + } + try { + const result = (action as any)(); + if (result instanceof Promise) { + return promise(result) as SafeResult; + } + return new Result(result, new Ok()) as SafeResult; + } catch (err: any) { + return new Result(null as any, Err.fromCatch(err)) as SafeResult; + } +} + +const promise = (result: Promise) => { + return result + .then((res) => new Result(res, new Ok())) + .catch((err) => new Result(null, Err.fromCatch(err))) as Promise>; +}; diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..a5dc390 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export * from "./grip.ts"; diff --git a/package.json b/package.json new file mode 100644 index 0000000..e820ca1 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "@nesterow/follow", + "module": "grip.ts", + "type": "module", + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "scripts": { + "bundle": "bun build ./grip.ts --outdir ./dist", + "test": "bun test" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}