mapSkipUndefined
Created by Daniele Morosinotto
Import
import { mapSkipUndefined } from 'ngxtension/map-skip-undefined';
Usage
You can use it as a normal map operator, but with the ability to skip some values: returning undefined (explicit or implicit).
import { from } from 'rxjs';import { mapSkipUndefined } from 'ngxtension/map-skip-undefined';
const in$ = from([1, 42, 3]);const out$ = in$.pipe( mapSkipUndefined((n) => { if (n % 2) return String(n * 2); //else return undefined // <-- this is the same as not returning anything! //In either case the even value is filtered out }),); //infer Observable<string>
out$.subscribe(console.log); // logs: 2, 6
Bonus: filterUndefined
If you need to filter out undefined
value from and Observable stream you can use the filterUndefined
operator.
Example
import { filterUndefined } from 'ngxtension/map-skip-undefined';
const source$ = of(null, undefined, 42);const filtered$ = source$.pipe(filterUndefined()); //emit only null and 42