https://github.com/just-do-halee/rusultts
Rust Result Implementation for Typescript, simply. i.e. Modern error handling library.
https://github.com/just-do-halee/rusultts
debug error error-handling exception handling library modern result rust typescript
Last synced: 4 months ago
JSON representation
Rust Result Implementation for Typescript, simply. i.e. Modern error handling library.
- Host: GitHub
- URL: https://github.com/just-do-halee/rusultts
- Owner: just-do-halee
- License: mit
- Created: 2021-10-04T09:36:26.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-06T11:50:41.000Z (over 4 years ago)
- Last Synced: 2025-09-29T05:20:56.011Z (9 months ago)
- Topics: debug, error, error-handling, exception, handling, library, modern, result, rust, typescript
- Language: TypeScript
- Homepage:
- Size: 86.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `rusultTs`
Rust **_Result Implementation for Typescript_**, simply. i.e. Modern error handling library. (no dependencies, pure Typescript code about 200 lines) 100% [[coverage]][ci-url]
[][ci-url]
[][ci-url]
[][ci-url]
[][ci-url]
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[][ci-url]
[![License][license-image]][license-url]
[](https://replit.com/@justdohalee/rusultts-playground#index.ts)
[[changelog]](CHANGELOG.md)
---
## **Installation**
```js
npm install rusultts
```
or
```js
yarn add rusultts
```
## **Examples**
```ts
import { Result, Ok, Err } from 'rusultts';
// Result: any type can be into it,
// This is just the generic type.
// I chose as object.
type SomeType = { foo: string, bar: number };
// Also these are just some example functions.
function tryParse(token: string): Result {
// ... doing heavy stuffs ...
if (somethingWrong) { // happended
// so returns an Error-Object implementing Result
return Err.new(`something wrong...`, null);
}
// Or returns an Ok Object containing value for
return Ok.new({ foo: 'any', bar: 999 });
}
// tryParse() wrapping function
function verify(token: string): Result {
// ↓ automatic throwing new Error(), or retuns the directly.
const someType = tryParse(token).unwrap();
// ... doing more stuffs ...
// another unwrap
const isItGood = tryGetBool(...).unwrap();
// ...
return Ok.new(isItGood);
}
try {
// if unwrap is possible, you get a sign that you can use an obvious try catch statement.
const bool = verify(someToken).unwrap();
} catch(e) {
// ↓ can get [`string`, 'E | null'] type value.
const [msg, value] = Err.eSplit(e);
// message of error & contained value
// this value is `null` because of Result = ResultBox
console.log(msg, value);
}
```
### ResultBox
```ts
type Result = ResultBox;
```
```ts
import { ResultBox, Ok, Err } from 'rusultts';
// simple example
// ResultBox: equals containing user value for Error statement. it can be any type.
function divide(a: number, b: number): ResultBox {
if (b === 0) {
return Err.new(`b cannot be `, b);
}
return Ok.new(a / b);
}
const val = divide(4, 2).unwrap(); // 4 / 2 = 2
const err = divide(4, 0); // 4 / 0, so error statement.
console.log(err.isErr); // true
// returns contained value = 0
const getValueE = err.unwrap_err();
// if state is error, returns input value = 10
const getDefault = err.unwrap_or(10);
// like .map((x) => y) for value
// ↓ will return 1
const getMapped = err.unwrap_or_else((eV: number) => eV + 1);
try {
err.unwrap();
} catch (e) {
const [errMessage, valueE] = Err.eSplit(e);
// print `b cannot be :--> -1` out.
console.log(errMessage, (-1 + valueE) as number);
}
```
## **Advanced**
#### **./errors.ts**
```ts
import { createErrorSet } from 'rusultts';
// you can easily set all errors.
export default createErrorSet({
notFound: 'not found',
somethingWrong: 'something wrong...',
wrongHeader: 'please fix your header.',
undefinedValue: 'this value is undefined:',
dividedByZero: 'do not divide by Zero.',
dividedByNegative: 'well, you did divide as Negative value.',
});
```
```ts
import { ResultBox, Ok, Err } from 'rusultts';
import err from './errors'; // import errors
function divide(a: number, b: number): ResultBox {
if (b === 0) {
return err.new('dividedByZero', b); // autocompleted string argument
} else if (b < 0) {
return err.new('dividedByNegative', b);
}
return Ok.new(a / b);
}
try {
divide(4, -2).unwrap(); // dividedByNegative error occurs.
} catch (e) {
// you can do error type matching.
const val1 = err.match(e, 'dividedByZero').unwrap(); // this will return undefined.
const val2 = err.match(e, 'dividedByNegative').unwrap(); // this will return value of number type, `-2`
const val3 = err.match({ is: 'not errorType' }, 'dividedByNegative').unwrap(); // throw new Error
}
```
## **License**
[MIT](LICENSE)
[npm-image]: https://img.shields.io/npm/v/rusultts.svg
[npm-url]: https://npmjs.org/package/rusultts
[downloads-image]: https://img.shields.io/npm/dm/rusultts.svg
[downloads-url]: https://npmcharts.com/compare/rusultts?minimal=true
[license-url]: https://opensource.org/licenses/MIT
[license-image]: https://img.shields.io/npm/l/rusultts
[ci-url]: https://github.com/just-do-halee/rusultts/actions/workflows/main.yml