Skip to main content

Transform operators

map

Applies a converter function to converts the observed value into a new value.

output.svg
import { from, map } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs
.pipe(map(v => v * v))
.subscribe(v => console.log(v))

scan

Applies an accumulator (or "reducer function") to each value from the source.

output.svg
import { from, scan } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
scan((p, c) => p + c, 0)
).subscribe(v => console.log(v))

timestamp

Wraps value in an object with a timestamp property and lastTimestamp property.

output.svg
import { from, timestamp } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
timestamp()
).subscribe(v => console.log(v))

switchMap

Takes a source Observable and calls transform(T) for each emitted value. It will immediately subscribe to any Observable coming from transform(T), b ut in addition to this, will unsubscribe() from any prior Observables - so that there is only ever one Observable subscribed at any one time.

output.svg
import { from, switchMap } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
switchMap(v => from([v + 1, v + 1, v + 1]))
).subscribe(v => console.log(v))