Skip to content

Commit

Permalink
5. [release:1.2.0]
Browse files Browse the repository at this point in the history
  • Loading branch information
just-do-halee committed Oct 5, 2021
1 parent e5e226f commit d636801
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ npm-debug.log
.github

/test
/badges
/badges
/coverage
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.2.0 (October 6, 2021)

### Release 1.2.0

- defining Result interface: IResult
- new feature: **_Error Set_** -> `createErrorSet`, `ErrSet`

---

## 1.1.3 (October 5, 2021)

### Release 1.1.3
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 just-do-halee(=Hwakyeom Kim)
Copyright 2021 just-do-halee(=Hwakyeom Kim)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
50 changes: 44 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# `rusultTs`

Rust **_Result Implementation for Typescript_**, simply. i.e. Modern error handling library. (no dependencies, pure Typescript code about 50 lines) 100% [[coverage]](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml)
Rust **_Result Implementation for Typescript_**, simply. i.e. Modern error handling library. (no dependencies, pure Typescript code about 50 lines) 100% [[coverage]][ci-url]
<br>
<br>

![Coverage lines](./badges/badge-lines.svg)
![Coverage functions](./badges/badge-functions.svg)
![Coverage branches](./badges/badge-branches.svg)
![Coverage statements](./badges/badge-statements.svg)
[![Coverage lines](./badges/badge-lines.svg)][ci-url]
[![Coverage functions](./badges/badge-functions.svg)][ci-url]
[![Coverage branches](./badges/badge-branches.svg)][ci-url]
[![Coverage statements](./badges/badge-statements.svg)][ci-url]

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![CI](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml/badge.svg)](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml)
[![CI](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml/badge.svg)][ci-url]
[![License][license-image]][license-url]
[[changelog]](CHANGELOG.md)

Expand Down Expand Up @@ -79,6 +79,43 @@ try {
}
```

## **Advanced**<br>

```ts
import { createErrorSet } from 'rusultts';

const err = 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';
// and also import our **const `err`**

function divide(a: number, b: number): ResultBox<number, number> => {
if (b === 0) {
return err.new('dividedByZero', b); // autocompleted string
} else if (b < 0) {
return err.new('dividedByNegative', b);
}
return Ok.new(a / b);
};

try {
divide(4, -2).unwrap();
} catch (e) {
const val1 = err.match(e, 'dividedByZero').unwrap(); // val1 === undefined
const val2 = err.match(e, 'dividedByNegative').unwrap(); // val2 === '-2'
const val3 = err.match({ is: 'not errorType' }, 'dividedByNegative').unwrap(); // throw new Error()
}
```

## **License**<br>

[MIT](LICENSE)
Expand All @@ -89,3 +126,4 @@ try {
[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
85 changes: 84 additions & 1 deletion dist/rusultts.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
/**
* kind of internal subject(ok or err)
*/
export declare type ResultObject<T> = {
readonly error?: Error;
readonly value: T;
};
/**
* international interface
*/
export interface IResult<T, E> {
readonly isOk: boolean;
readonly isErr: boolean;
unwrap(): T | never;
}
/**
* ## Examples
*```ts
Expand All @@ -25,7 +36,7 @@ export declare type ResultObject<T> = {
* }
*```
*/
export declare abstract class ResultBox<T, E> {
export declare abstract class ResultBox<T, E> implements IResult<T, E> {
protected readonly val: ResultObject<T | E>;
readonly isOk: boolean;
readonly isErr: boolean;
Expand Down Expand Up @@ -96,3 +107,75 @@ export declare class Err<T, E> extends ResultBox<T, E> {
* easy one, has value of Error as `null`
*/
export declare type Result<T> = ResultBox<T, null>;
/**
* error's message pair object
* ## Example
* ```ts
* const mp: MessagePair = {
* notFound: 'not found',
* somethingWrong: 'something wrong...',
* wrongHeader: 'please fix your header.'
* }
* ```
*/
export declare type MessagePair = {
[key: string]: string;
};
export declare type TOrUndefinedToNull<T> = T extends undefined ? null : T;
/**
* creates errors that have already been set.
* ## Example
* ```ts
* const err = createErrorSet({
* notFound: 'not found',
* somethingWrong: 'something wrong...',
* wrongHeader: 'please fix your header.'
* });
*
* err.new('wrongHeader'); // === Err.new('please fix your header.', null)
* ```
*/
export declare class ErrSet<M extends MessagePair> {
readonly messagePair: M;
constructor(messagePair: M);
/**
* creates and return the error that have already been set.
*/
new<T, E>(errorMessageType: keyof M, val: TOrUndefinedToNull<E>): Err<T, TOrUndefinedToNull<E>>;
/**
*
* @param {Error} e the error in the scope of try~catch.
* @param {MessagePair} errorMessageType in the MessagePair.
* @returns if `e` is not Error type, return Err<, Type>, or returns Ok<string | undefined,> which means `e` === the error of errorMessageType then returns `error value<E>` or `undefined`.
*
* ## Example
*```ts
* const test = divide(4, 0);
* try {
* test.unwrap();
* } catch (e) {
* const val = err.match(e, 'dividedByZero').unwrap();
* if(val) {
* return val;
* } else {
* return 'unexpected error.';
* }
* }
* ```
*/
match(e: Error | unknown, errorMessageType: keyof M): ResultBox<string | undefined, unknown>;
}
/**
* creates errors that have already been set.
* ## Example
* ```ts
* const err = createErrorSet({
* notFound: 'not found',
* somethingWrong: 'something wrong...',
* wrongHeader: 'please fix your header.'
* });
*
* err.new('wrongHeader'); // === Err.new('please fix your header.', null)
* ```
*/
export declare const createErrorSet: <M extends MessagePair>(messagePair: M) => ErrSet<M>;
75 changes: 73 additions & 2 deletions dist/rusultts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
// rusultTs
// (c) 2021 just-do-halee(=Hwakyeom Kim)
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
Expand All @@ -16,7 +16,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Err = exports.Ok = exports.ResultBox = void 0;
exports.createErrorSet = exports.ErrSet = exports.Err = exports.Ok = exports.ResultBox = void 0;
/**
* ## Examples
*```ts
Expand Down Expand Up @@ -141,3 +141,74 @@ var Err = /** @class */ (function (_super) {
return Err;
}(ResultBox));
exports.Err = Err;
/**
* creates errors that have already been set.
* ## Example
* ```ts
* const err = createErrorSet({
* notFound: 'not found',
* somethingWrong: 'something wrong...',
* wrongHeader: 'please fix your header.'
* });
*
* err.new('wrongHeader'); // === Err.new('please fix your header.', null)
* ```
*/
var ErrSet = /** @class */ (function () {
function ErrSet(messagePair) {
this.messagePair = messagePair;
}
/**
* creates and return the error that have already been set.
*/
ErrSet.prototype.new = function (errorMessageType, val) {
return Err.new(this.messagePair[errorMessageType], val);
};
/**
*
* @param {Error} e the error in the scope of try~catch.
* @param {MessagePair} errorMessageType in the MessagePair.
* @returns if `e` is not Error type, return Err<, Type>, or returns Ok<string | undefined,> which means `e` === the error of errorMessageType then returns `error value<E>` or `undefined`.
*
* ## Example
*```ts
* const test = divide(4, 0);
* try {
* test.unwrap();
* } catch (e) {
* const val = err.match(e, 'dividedByZero').unwrap();
* if(val) {
* return val;
* } else {
* return 'unexpected error.';
* }
* }
* ```
*/
ErrSet.prototype.match = function (e, errorMessageType) {
if (!(e instanceof Error)) {
return Err.new("e is unknown type:", e);
}
var _a = Err.eSplit(e), message = _a[0], value = _a[1];
return Ok.new(message === this.messagePair[errorMessageType] ? value : undefined);
};
return ErrSet;
}());
exports.ErrSet = ErrSet;
/**
* creates errors that have already been set.
* ## Example
* ```ts
* const err = createErrorSet({
* notFound: 'not found',
* somethingWrong: 'something wrong...',
* wrongHeader: 'please fix your header.'
* });
*
* err.new('wrongHeader'); // === Err.new('please fix your header.', null)
* ```
*/
var createErrorSet = function (messagePair) {
return new ErrSet(messagePair);
};
exports.createErrorSet = createErrorSet;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "rusultts",
"main": "dist/rusultts.js",
"types": "dist/rusultts.d.ts",
"version": "1.1.3",
"version": "1.2.0",
"description": "Rust Result Implementation for Typescript, simply. i.e. Modern error handling library.",
"author": "just-do-halee <just.do.halee@gmail.com>",
"license": "MIT",
Expand Down
Loading

0 comments on commit d636801

Please sign in to comment.