https://github.com/arnavk-09/unwrap-go
๐ซง Go-like Error Handling for TypeScript/JavaScript...
https://github.com/arnavk-09/unwrap-go
bun cross error handler jsr lib node npm package pkg typescript workflows
Last synced: 4 months ago
JSON representation
๐ซง Go-like Error Handling for TypeScript/JavaScript...
- Host: GitHub
- URL: https://github.com/arnavk-09/unwrap-go
- Owner: ArnavK-09
- License: unlicense
- Created: 2025-02-07T07:04:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-08T20:42:13.000Z (over 1 year ago)
- Last Synced: 2025-02-17T06:59:42.882Z (over 1 year ago)
- Topics: bun, cross, error, handler, jsr, lib, node, npm, package, pkg, typescript, workflows
- Language: TypeScript
- Homepage: https://jsr.io/@arnavk-09/unwrap-go/
- Size: 115 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
๐ซง unwrap-go ๐ซง
Go-like Error Handling for TypeScript/JavaScript
> [!NOTE]
>
> unwrap-go brings Go-style error handling to TypeScript and JavaScript, offering a clean and predictable way to handle errors using Result tuples. This approach eliminates the need for try-catch blocks and provides a more functional approach to error management.
## ๐ Features
> **unwrap-go** features intro:
- **Go-Style Error Handling** โ Uses tuple returns (`[data, error]`) similar to Go's approach, making error handling more explicit and predictable
- **Promise Support** โ Built-in support for handling asynchronous operations with the same elegant syntax
- **Type Safety** โ Full TypeScript support with generic types for both success and error cases
- **Zero Dependencies** โ Lightweight implementation with no external dependencies
- **Synchronous Support** โ Handle both async and sync operations with consistent patterns
## ๐งจ Registries
- **[jsr.io/@arnavk-09/unwrap-go](https://jsr.io/@arnavk-09/unwrap-go)**
- **[npmjs.com/package/@arnavk-09/unwrap-go](https://www.npmjs.com/package/@arnavk-09/unwrap-go)**
## ๐ป Installation
> You can add **unwrap-go** with one command via:
###### terminal
```bash
npm install @arnavk-09/unwrap-go
deno add jsr:@arnavk-09/unwrap-go
bun add @arnavk-09/unwrap-go
pnpm add @arnavk-09/unwrap-go
yarn add @arnavk-09/unwrap-go
```
OR
###### Deno
```bash
deno add jsr:@arnavk-09/unwrap-go
```
###### Npx
```bash
npm install @arnavk-09/unwrap-go
```
###### Bun
```bash
bun add @arnavk-09/unwrap-go
```
###### Pnpm
```bash
pnpm add @arnavk-09/unwrap-go
```
###### Yarn
```bash
yarn add @arnavk-09/unwrap-go
```
## ๐ Usage
```typescript
import { unwrapPromise, unwrapSync } from "@arnavk-09/unwrap-go";
// Async example
async function fetchData() {
const [data, err] = await unwrapPromise(
fetch("https://api.example.com/data"),
);
if (err) {
console.error("Failed to fetch:", err);
return;
}
console.log("Data:", data);
}
// Sync example
function divide(a: number, b: number) {
const [result, err] = unwrapSync(() => {
if (b === 0) throw new Error("Division by zero");
return a / b;
});
if (err) {
console.error("Division failed:", err);
return;
}
console.log("Result:", result);
}
```
## ๐ API Reference
| Function | Description | Parameters | Return Type |
| --------------------- | ---------------------------------------------------------- | ------------------------------------ | ------------------------ |
| `unwrapPromise` | Wraps a promise to return a Result tuple | `promise: Promise` | `Promise>` |
| `unwrapSync` | Executes a synchronous function and returns a Result tuple | `fn: () => T` | `Result` |
| `Result` | Type representing either success (T) or error (E) | `T`: Success type
`E`: Error type | `[T, null] \| [null, E]` |
## ๐ฃ๏ธ Roadmap
| Task | Status |
| :---------------: | :----: |
| ๐งช Add tests | ๐ด |
| ๐งช Publish To JSR | ๐ข |
| ๐งช Publish To NPM | ๐ข |
## ๐ป Contributing
> [!TIP]
> We welcome contributions to improve **unwrap-go**! If you have suggestions, bug fixes, or new feature ideas, follow these steps:
1. **Fork the Repository** you
Click the **Fork** button at the top-right of the repo page.
2. **Clone Your Fork**
Clone the repo locally: YOU
```bash
git clone https://github.com/ArnavK-09/unwrap-go.git
```
3. **Create a Branch**
Create a new branch for your changes:
```bash
git checkout -b your-feature-branch
```
4. **Make Changes**
Implement your changes (bug fixes, features, etc.).
5. **Commit and Push**
Commit your changes and push the branch:
```bash
git commit -m "feat(scope): description"
git push origin your-feature-branch
```
6. **Open a Pull Request**
Open a PR with a detailed description of your changes.
7. **Collaborate and Merge**
The maintainers will review your PR, request changes if needed, and merge it once approved.
## ๐โโ๏ธ Issues
Found a bug or need help? Please create an issue on the [GitHub repository](https://github.com/ArnavK-09/unwrap-go/issues) with a detailed description.
## ๐ค Author
---
๐ License
unwrap-go is licensed under the Unlicense License. See the LICENSE file for more details.
---
๐ If you find this project helpful, please give it a star on GitHub! ๐
