Skip to content

Commit

Permalink
fix: edge cases with ambiguous errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Dec 27, 2023
1 parent 3d0b3d4 commit 529a04f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import TupleConstructor from "./tupleConstructor";
* A `Result` can either contain a result value or an error value.
*/
class Result<TResult, TError> extends TupleConstructor<TResult, TError> {
private readonly containsAmbiguousError!: boolean;

/**
* Get the result value.
*
Expand All @@ -24,6 +26,33 @@ class Result<TResult, TError> extends TupleConstructor<TResult, TError> {
return this[1];
}

/**
* Creates an `Ok` `Result`.
*
* @param result - The result value.
*
* @returns A `Result` object containing the given result value.
*/
static makeOk = <R, E>(result: R): Result<R, E> => {
return new Result<R, E>({ result });
};

/**
* Creates an `Err` `Result`.
*
* @param error - The error value.
*
* @returns A `Result` object containing the given error value.
*/
static makeErr = <R, E>(error: E): Result<R, E> => {
const self = new Result<R, E>({ error });
if (!error) {
// @ts-expect-error this field is supposed to be set by makeErr function
self.containsAmbiguousError = true;
}
return self;
};

/**
* Create a Result.
*
Expand All @@ -33,7 +62,7 @@ class Result<TResult, TError> extends TupleConstructor<TResult, TError> {
*
* @internal
*/
constructor({
private constructor({
result = null,
error = null,
}: {
Expand Down Expand Up @@ -63,7 +92,8 @@ class Result<TResult, TError> extends TupleConstructor<TResult, TError> {
*
* @returns `true` if the result is `Err`.
*/
isErr = (): boolean => this.err !== null;
isErr = (): boolean =>
this.err !== null || this.containsAmbiguousError || this.isOk === null;

/**
* Get the result value.
Expand Down Expand Up @@ -200,27 +230,8 @@ class Result<TResult, TError> extends TupleConstructor<TResult, TError> {
};
}

/**
* Creates an `Ok` `Result`.
*
* @param result - The result value.
*
* @returns A `Result` object containing the given result value.
*/
function Ok<TResult, TError>(result: TResult): Result<TResult, TError> {
return new Result({ result });
}

/**
* Creates an `Err` `Result`.
*
* @param error - The error value.
*
* @returns A `Result` object containing the given error value.
*/
function Err<TResult, TError>(error: TError): Result<TResult, TError> {
return new Result({ error });
}
const Ok = Result.makeOk;
const Err = Result.makeErr;

export type { Result };
export { Ok, Err };
12 changes: 12 additions & 0 deletions tests/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Ok, Err } from "../src/result";
import when from "../src/when";

describe("Ok Result Tests", () => {
test("should return a Result reporting isOk status", () => {
expect(Ok(undefined).isOk()).toBe(true);
});

test("should return a Result reporting isOk status", () => {
expect(Ok(null).isOk()).toBe(true);
});

test("should return a Result reporting isOk status", () => {
expect(Ok("").isOk()).toBe(true);
});
Expand Down Expand Up @@ -90,6 +98,10 @@ describe("Result 'and' function Tests", () => {
});

describe("Err Result Tests", () => {
test("should return a Result reporting isErr status", () => {
expect(Err(undefined).isErr()).toBe(true);
});

test("should return a Result not reporting isOk status", () => {
expect(Err("").isOk()).toBe(false);
});
Expand Down

0 comments on commit 529a04f

Please sign in to comment.