Skip to content

Commit

Permalink
feat: adds unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Jul 15, 2019
1 parent 8c505c7 commit e0ca437
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ class Result<R, E> {

return this;
}

/**
* Returns wrapped value or throws an `Error`
* @returns {(R | never)}
* @memberof Result
*/
public unwrap(): R | never {
if (instanceOfError<R, E>(this.value)) {
throw this.value;
}

return this.value;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/Result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ describe(`Result`, () => {
expect(Ok("a").orElse(() => Ok("b"))).toStrictEqual(Ok("a"));
expect(Err("a").orElse(() => Ok("b"))).toStrictEqual(Ok("b"));
});

it("Returns wrapped value or throws an `Error`", () => {
expect(Ok("a").unwrap()).toBe("a");
expect(() => Err("a").unwrap()).toThrowError();
});
});

0 comments on commit e0ca437

Please sign in to comment.