I am currently working on developing a custom rxjs operator. My previous custom operators, such as MonoTypeOperatorFunction or the regular Observable that accepts input like strings or numbers, have been successful. However, I am facing a challenge with creating an operator that can take in an anonymous function or predicate.
For instance, I am interested in creating an operator that is able to flatten elements within an object.
interface B {
bid: number;
}
interface A {
aid: number;
bs: B[];
}
const b1: B = { bid: 1 };
const b2: B = { bid: 2 };
const a1: A = { aid: 1, bs: [b1, b2] };
const a1$ = of(a1);
// My goal is to merge map and concatMap into a single operator
const result = a1$.pipe(
map(x => x.bs),
concatMap(x => x)
).subscribe(x => console.log(x))
// OUTPUT: {bid:1}, {bid:2}
// Desired syntax
// a1$.pipe(many(x => x.bs))
// Attempt at creating the operator
// function many<T>(predicate: (input: T) => boolean) {
// return function<T1>(source: Observable<T1>) {
// return source.pipe(map(predicate),concatMap(x => x));
// };
// }