Skip to content

Commit

Permalink
Merge pull request #16 from yajamon/fix/signature
Browse files Browse the repository at this point in the history
シグネチャの更新
  • Loading branch information
yajamon authored Apr 28, 2022
2 parents a885442 + e3b184d commit 4c68e05
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export type Result<T, E> = ResultOk<T> | ResultErr<E>;
## Usage

```ts
import { Result, Ok, Err } from "@yajamon/result.ts"
import { Result, ok, err } from "@yajamon/result.ts"

class Foo {
static create(input:string): Result<Foo, Error> {
if (validation(input)) {
const foo = new Foo();
// ...
return Ok(foo);
return ok(foo);
} else {
return Err(new Error("error message"));
return err(new Error("error message"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yajamon/result.ts",
"version": "0.2.1",
"version": "0.3.0",
"description": "Type definitions and simple functions inspired by Rust's Result.",
"main": "lib/main.js",
"scripts": {
Expand Down
26 changes: 25 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Err, Ok, Result, unwrap } from "./main";
import { err, Err, Ok, ok, Result, unwrap } from "./main";

describe("Ok", () => {
it("is return ResultOk", () => {
Expand All @@ -24,6 +24,30 @@ describe("Err", () => {
});
});

describe("ok", () => {
it("is return ResultOk", () => {
const success = ok("success") as Result<string, never>;
if (success.isError === false) {
// isError を確認することで、スコープ内の型を ResultからResultOkに推定できる
expect(success.value).toEqual("success");
} else {
throw "Wrong!";
}
});
});

describe("err", () => {
it("is return ResultErr", () => {
const error = err("error") as Result<never, string>;
if (error.isError) {
// isError を確認することで、スコープ内の型を ResultからResultErrに推定できる
expect(error.error).toEqual("error");
} else {
throw "Wrong!";
}
});
});

describe("unwrap()", () => {
it("throw when ResultErr", () => {
expect(() => {
Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ export type ResultErr<E> = {

export type Result<T, E> = ResultOk<T> | ResultErr<E>;

export const Ok: <T>(value: T) => ResultOk<T> = (v) => ({
export const ok: <T>(value: T) => ResultOk<T> = (v) => ({
isError: false,
value: v,
});
export const Err: <E>(error: E) => ResultErr<E> = (e) => ({
export const err: <E>(error: E) => ResultErr<E> = (e) => ({
isError: true,
error: e,
});

/** @deprecated Use `ok(result: T)` instead */
export const Ok = ok;
/** @deprecated Use `err(error: E)` instead */
export const Err = err;

export const unwrap: <T, E>(result: Result<T, E>) => T = (result) => {
if (result.isError) {
throw result.error;
Expand Down

0 comments on commit 4c68e05

Please sign in to comment.