Skip to content

Commit

Permalink
ci: added test ci
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-yato committed Dec 13, 2023
1 parent 3e7cedb commit b3c6911
Show file tree
Hide file tree
Showing 8 changed files with 1,897 additions and 38 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
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)
11 changes: 11 additions & 0 deletions .changeset/config.json
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": []
}
35 changes: 35 additions & 0 deletions .github/workflows/publish.yaml
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
135 changes: 114 additions & 21 deletions README.md
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.🍀
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
"test:run": "vitest --run",
"test:watch": "vitest --watch"
"test:watch": "vitest --watch",
"typecheck": "tsc --noEmit",
"ci": "pnpm run test:run && pnpm run typecheck && pnpm run build"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"tsup": "^8.0.1",
"vitest": "^1.0.4",
"typescript": "^5.0.2"
"typescript": "^5.0.2",
"vitest": "^1.0.4"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
Loading

0 comments on commit b3c6911

Please sign in to comment.