Skip to content

Function: map()

map<T, R>(transform): Operator<T, R>

Defined in: operators/map.ts:19

Creates a stream operator that applies a transformation function to each value emitted by the source stream.

This operator is a fundamental part of stream processing. It consumes each value from the source, passes it to the transform function, and then emits the result of that function. This is a one-to-one mapping, meaning the output stream will have the same number of values as the source stream, but with potentially different content and/or type.

Type Parameters

T

T = any

The type of the values in the source stream.

R

R = any

The type of the values in the output stream.

Parameters

transform

(value, index) => CallbackReturnType<R>

The transformation function to apply to each value. It receives the value and its index. This function can be synchronous or asynchronous.

Returns

Operator<T, R>

An Operator instance that can be used in a stream's pipe method.

Examples

From map.spec.ts:4

typescript
const testStream = from([1, 2, 3]);
const transform = (value: number) => value * 2;
const mappedStream = testStream.pipe(map(transform));
let results: any[] = [];
mappedStream.subscribe({
  next: (value) => results.push(value),
  complete: () => {
    expect(results).toEqual([2, 4, 6]);
    done();
  },
});

Released under the MIT License.