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

      Type Parameters

      • R extends Result.Base

        The input Result.Base type.

      • S2

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

      Parameters

      • result: R

        The Result to map.

      • mapFn: (value: UnwrapOk<R>) => S2

        The function to apply to the success value if present.

      Returns Result<S2, UnwrapErr<R>>

      A new Result<S2, UnwrapErr<R>>.

      const okNumber = Result.ok(5);
      const errMessage = Result.err('error');

      const doubled = Result.map(okNumber, (value) => value * 2);
      const untouchedError = Result.map(errMessage, (value: number) => value * 2);

      assert.deepStrictEqual(doubled, Result.ok(10));
      assert.deepStrictEqual(untouchedError, errMessage);

      const mapToLength = Result.map((text: string) => text.length);

      assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));
      assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));
    • Maps a Result<S, E> to Result<S2, E> by applying a function to the success value. If the Result is Result.Err, returns the original Err.

      Type Parameters

      • S
      • S2

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

      Parameters

      • mapFn: (value: S) => S2

        The function to apply to the success value if present.

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

      A new Result<S2, UnwrapErr<R>>.

      const okNumber = Result.ok(5);
      const errMessage = Result.err('error');

      const doubled = Result.map(okNumber, (value) => value * 2);
      const untouchedError = Result.map(errMessage, (value: number) => value * 2);

      assert.deepStrictEqual(doubled, Result.ok(10));
      assert.deepStrictEqual(untouchedError, errMessage);

      const mapToLength = Result.map((text: string) => text.length);

      assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));
      assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));