-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,897 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changesets | ||
|
||
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works | ||
with multi-package repos, or single-package repos to help you version and publish your code. You can | ||
find the full documentation for it [in our repository](https://github.com/changesets/changesets) | ||
|
||
We have a quick list of common questions to get you started engaging with this project in | ||
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json", | ||
"changelog": "@changesets/cli/changelog", | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "public", | ||
"baseBranch": "main", | ||
"updateInternalDependencies": "patch", | ||
"ignore": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Publish Package | ||
|
||
on: | ||
push: | ||
branches: | ||
- dev | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
cache: "pnpm" | ||
|
||
- uses: pnpm/action-setup@v2 | ||
name: Setup pnpm | ||
with: | ||
version: 8.6.0 | ||
run_install: false | ||
|
||
- name: Install Dependencies | ||
run: pnpm install --frozen-lockfile | ||
|
||
- name: CI | ||
run: pnpm run ci | ||
|
||
- name: Publish | ||
id: changesets | ||
uses: changesets/action@v2 | ||
with: | ||
publish: pnpm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,133 @@ | ||
# @miralce/result | ||
# @justmiracle/result | ||
|
||
A minimal implementation of the Result type in TypeScript. Allowing you to handle success and error states in a more explicit and structured way. | ||
|
||
A simple result type wrapper for TypeScript. | ||
|
||
### Installation | ||
|
||
```bash | ||
npm install @miralce/result | ||
# or | ||
yarn add @miralce/result | ||
# or | ||
pnpm add @miralce/result | ||
# or | ||
bun add @miralce/result | ||
npm install @justmiracle/result | ||
yarn add @justmiracle/result | ||
pnpm add @justmiracle/result | ||
bun add @justmiracle/result | ||
``` | ||
|
||
|
||
### Docs | ||
|
||
- [@justmiracle/result](#justmiracleresult) | ||
- [Installation](#installation) | ||
- [Docs](#docs) | ||
- [API](#api) | ||
- [Creating a Result](#creating-a-result) | ||
- [Checking Result Type](#checking-result-type) | ||
- [Unwrapping Result](#unwrapping-result) | ||
- [Transforming Result](#transforming-result) | ||
- [Roadmap](#roadmap) | ||
- [Contributing](#contributing) | ||
|
||
|
||
### API | ||
|
||
> [!FUTURE] | ||
> This might change a little in the future, but the core concepts will remain the same. | ||
> I will try to make it so Err can be typed as `Error | AnyErrorType`, this will make it easier to use with custom error types. | ||
```typescript | ||
// A result is either successful or an error | ||
type Result<T> = Ok<T> | Err; | ||
|
||
// A successful result contains a value and null for error | ||
type Ok<T> = { value: T; error: null }; | ||
|
||
// An error result contains null for value and an error | ||
type Err = { value: null; error: Error }; | ||
``` | ||
|
||
### Getting Started | ||
|
||
```ts | ||
import { Result, ok, err } from '@miralce/result' | ||
### Creating a Result | ||
|
||
You can create a result using the `ok` or `err` function: | ||
|
||
```typescript | ||
import { ok, err } from '@justmiracle/result'; | ||
|
||
const successResult: Result<number> = ok(42); | ||
// { value: 42, error: null } | ||
|
||
const errorResult: Result<number> = err(new Error("Something went wrong")); | ||
// { value: null, error: Error } | ||
|
||
// error can be anything, if the function detect that it's not an error it will be converted to an error | ||
const errorResult: Result<number> = err("Something went wrong"); | ||
// { value: null, error: Error } the same as above | ||
``` | ||
|
||
|
||
### Checking Result Type | ||
|
||
You can check if a result is successful using the `isOk` function: | ||
|
||
type User = { | ||
name: string | ||
age: number | ||
```typescript | ||
if (isOk(successResult)) { | ||
console.log("Success:", successResult.value); | ||
} else { | ||
console.error("Error:", successResult.error.message); | ||
} | ||
``` | ||
|
||
const data: Result<User> = await getData().then(ok).catch(err) | ||
You can check if a result is an error using the `isErr` function: | ||
|
||
if (data.error) { | ||
console.error(data.error) | ||
```typescript | ||
if (isErr(errorResult)) { | ||
console.error("Error:", errorResult.error.message); | ||
} else { | ||
console.log(data.value, data.value.name) | ||
console.log("Success:", errorResult.value); | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
> More documentation coming soon. | ||
### Unwrapping Result | ||
|
||
You can unwrap the value from a result type using `unwrapOr`, this will return the value if the result is successful or a default value if it's an error: | ||
|
||
```typescript | ||
const resultOne = ok(42); | ||
const value = unwrapOr(resultOne, 12); // 42 | ||
|
||
const resultTwo = err("Something went wrong"); | ||
const value = unwrapOr(resultTwo, 12); // 12 default value | ||
``` | ||
|
||
You can also unwrap the value from a successful result using `unwrap`, which throws an error for an error result: | ||
|
||
> [!WARNING] | ||
> This will throw an error if the result is an error, so make sure to handle it properly. | ||
```typescript | ||
const resultOne = ok(42); | ||
const value = unwrap(resultOne); // 42 | ||
|
||
const resultTwo = err("Something went wrong"); | ||
const value = unwrap(resultTwo); // throws an error | ||
``` | ||
|
||
### Transforming Result | ||
|
||
You can apply a function to the value inside a successful result using `map`: | ||
|
||
```typescript | ||
const successResult = ok(42); | ||
const mappedResult = map(successResult, (value) => value * 2); | ||
// { value: 84, error: null } | ||
``` | ||
|
||
|
||
### Roadmap | ||
|
||
- [ ] Typesafe error handling | ||
- [ ] `pipe` function | ||
|
||
|
||
### Contributing | ||
|
||
Contributions are welcome, feel free to open an issue or a pull request.🍀 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.