ts-data-forge
    Preparing search index...
    • Maps a Result<S, E> to Result<S, E2> by applying a function to the error value. If the Result is Result.Ok, returns the original Ok.

      Type Parameters

      • R extends Result.Base

        The input Result.Base type.

      • E2

        The type of the error value returned by the mapping function.

      Parameters

      • result: R

        The Result to map.

      • mapFn: (error: UnwrapErr<R>) => E2

        The function to apply to the error value if present.

      Returns Result<UnwrapOk<R>, E2>

      A new Result<UnwrapOk<R>, E2>.

      const okValue = Result.ok(3) as Result<number, string>;
      const errValue = Result.err('missing');

      const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());
      const uppercasedErr = Result.mapErr(errValue, (error) => error.toUpperCase());

      assert.deepStrictEqual(untouchedOk, Result.ok(3));
      assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));

      const mapError = Result.mapErr((error: Readonly<Error>) => error.message);

      const wrapped = mapError(Result.err(new Error('boom')));

      assert.deepStrictEqual(wrapped, Result.err('boom'));
    • Maps a Result<S, E> to Result<S, E2> by applying a function to the error value. If the Result is Result.Ok, returns the original Ok.

      Type Parameters

      • E
      • E2

        The type of the error value returned by the mapping function.

      Parameters

      • mapFn: (error: E) => E2

        The function to apply to the error value if present.

      Returns <S>(result: Result<S, E>) => Result<S, E2>

      A new Result<UnwrapOk<R>, E2>.

      const okValue = Result.ok(3) as Result<number, string>;
      const errValue = Result.err('missing');

      const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());
      const uppercasedErr = Result.mapErr(errValue, (error) => error.toUpperCase());

      assert.deepStrictEqual(untouchedOk, Result.ok(3));
      assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));

      const mapError = Result.mapErr((error: Readonly<Error>) => error.message);

      const wrapped = mapError(Result.err(new Error('boom')));

      assert.deepStrictEqual(wrapped, Result.err('boom'));