ts-data-forge
    Preparing search index...
    • Maps an Optional to Optional by applying a function to a contained value. If the Optional is Optional.None, it returns Optional.none. Otherwise, it applies the mapFn to the value in Optional.Some and returns a new Optional.Some with the result.

      Type Parameters

      • O extends Optional.Base

        The input Optional.Base type.

      • S2

        The type of the value returned by the mapping function.

      Parameters

      • optional: O

        The Optional to map.

      • mapFn: (value: Unwrap<O>) => S2

        The function to apply to the value if it exists.

      Returns Optional<S2>

      A new Optional<S2> resulting from the mapping, or Optional.None if the input was Optional.None.

      const numberOptional = Optional.some(21);
      const mapped = Optional.map(numberOptional, (value) => value * 2);

      assert.deepStrictEqual(mapped, Optional.some(42));

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

      assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));
      assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);
    • Maps an Optional to Optional by applying a function to a contained value. If the Optional is Optional.None, it returns Optional.none. Otherwise, it applies the mapFn to the value in Optional.Some and returns a new Optional.Some with the result.

      Type Parameters

      • S
      • S2

        The type of the value returned by the mapping function.

      Parameters

      • mapFn: (value: S) => S2

        The function to apply to the value if it exists.

      Returns (optional: Optional<S>) => Optional<S2>

      A new Optional<S2> resulting from the mapping, or Optional.None if the input was Optional.None.

      const numberOptional = Optional.some(21);
      const mapped = Optional.map(numberOptional, (value) => value * 2);

      assert.deepStrictEqual(mapped, Optional.some(42));

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

      assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));
      assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);