This commit is contained in:
Anton Nesterov 2024-10-16 07:21:20 +02:00
parent 849b917d50
commit b8ecf08f34
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
4 changed files with 107 additions and 8 deletions

2
.gitignore vendored
View file

@ -118,7 +118,7 @@ out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files

21
LICENSE Normal file
View file

@ -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.

View file

@ -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();

78
dist/grip.js vendored Normal file
View file

@ -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
};