Skip to content

Commit

Permalink
feat(result): add Result.unwrapRaw (#475)
Browse files Browse the repository at this point in the history
Co-authored-by: A. Román <kyradiscord@gmail.com>
  • Loading branch information
favna and kyranet committed Oct 1, 2022
1 parent dc08606 commit 82fe79e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/result/src/lib/Result/Err.ts
Expand Up @@ -115,6 +115,11 @@ export class Err<E> implements IResult<any, E> {
return op(this.error);
}

public unwrapRaw(): never {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw this.error;
}

public and(result?: Result<any, E>): this;
public and(): this {
return this;
Expand Down
42 changes: 42 additions & 0 deletions packages/result/src/lib/Result/IResult.ts
Expand Up @@ -449,6 +449,8 @@ export interface IResult<T, E> {
* If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`.
* @seealso {@link unwrapOr}
* @seealso {@link unwrapOrElse}
* @seealso {@link unwrapErr}
* @seealso {@link unwrapRaw}
*
* @example
* ```typescript
Expand All @@ -473,6 +475,10 @@ export interface IResult<T, E> {
* Returns the contained `Err` value.
*
* If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`.
* @seealso {@link unwrap}
* @seealso {@link unwrapOr}
* @seealso {@link unwrapOrElse}
* @seealso {@link unwrapRaw}
*
* @example
* ```typescript
Expand All @@ -498,6 +504,11 @@ export interface IResult<T, E> {
*
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
* recommended to use {@link unwrapOrElse}, which is lazily evaluated.
* @seealso {@link unwrap}
* @seealso {@link unwrapOrElse}
* @seealso {@link unwrapErr}
* @seealso {@link unwrapRaw}
*
* @param defaultValue The default value.
*
* @example
Expand All @@ -517,6 +528,11 @@ export interface IResult<T, E> {

/**
* Returns the contained `Ok` value or computes it from a closure.
* @seealso {@link unwrap}
* @seealso {@link unwrapOr}
* @seealso {@link unwrapErr}
* @seealso {@link unwrapRaw}
*
* @param op The predicate.
*
* @example
Expand All @@ -531,6 +547,32 @@ export interface IResult<T, E> {
*/
unwrapOrElse<V>(op: (error: E) => V): T | V;

/**
* Returns the contained `Ok` value.
*
* If the value is an `Err`, it throws the contained error.
* @seealso {@link unwrap}
* @seealso {@link unwrapOr}
* @seealso {@link unwrapOrElse}
* @seealso {@link unwrapErr}
*
* @example
* ```typescript
* const x = ok(2);
* assert.equal(x.unwrapRaw(), 2);
* ```
* @example
* ```typescript
* const x = err('Emergency failure');
* assert.throws(() => x.unwrapRaw(), {
* name: 'Error',
* message: 'Unwrap failed',
* value: 'Emergency failure'
* });
* ```
*/
unwrapRaw(): T | never;

/**
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
* @param result The result to check.
Expand Down
4 changes: 4 additions & 0 deletions packages/result/src/lib/Result/Ok.ts
Expand Up @@ -116,6 +116,10 @@ export class Ok<T> implements IResult<T, any> {
return this.value;
}

public unwrapRaw(): T {
return this.value;
}

public and<R extends Result<any, any>>(result: R): R {
return result;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/result/tests/Result.test.ts
Expand Up @@ -438,6 +438,21 @@ describe('Result', () => {
});
});

describe('unwrapRaw', () => {
test('GIVEN ok THEN returns value', () => {
const x = ok(2);

expect<number>(x.unwrapRaw()).toBe(2);
});

test('GIVEN err THEN throws Error', () => {
const error = new Error('Some error message');
const x = err(error);

expect(() => x.unwrapRaw()).toThrowError(error);
});
});

describe('and', () => {
test('GIVEN x=ok and y=ok THEN returns y', () => {
const x = ok(2);
Expand Down

0 comments on commit 82fe79e

Please sign in to comment.