import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators'; import { MonoTypeOperatorFunction, Observable, OperatorFunction, pipe, UnaryFunction } from 'rxjs'; export function get(predicate: (value: T, index: number) => boolean): OperatorFunction { return map(entities => entities.find(predicate)); } export function any(predicate: (value: T, index: number) => boolean): OperatorFunction { return map(entities => entities.some(predicate)); } export function mapEach(predicate: (value: T, index: number) => R): OperatorFunction { return map(entities => entities.map(predicate)); } export function filterEach(predicate: (value: T, index: number) => boolean): OperatorFunction { return map(entities => entities.filter(predicate)); } export function shareLast(values = 1): MonoTypeOperatorFunction { return shareReplay({ bufferSize: values, refCount: true }); } export function shareDistinctLast(values = 1): UnaryFunction, Observable> { return pipe(distinctUntilChanged(), shareLast(values)); } export const toLengthValue = (entities: unknown[]): number => entities?.length ?? 0; export const getLength = pipe(map(toLengthValue), distinctUntilChanged()); export function boolFactory(obs: Observable, project: (value: T) => boolean = value => !!value): Observable[] { const result = obs.pipe(map(project), shareDistinctLast()); const inverse = result.pipe( map(value => !value), shareDistinctLast(), ); return [result, inverse]; }