Skip to content

Commit

Permalink
feat(core): add anyOf operator (#2516)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 26, 2023
1 parent 53c6bdc commit a61dc02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions libs/core/src/operators/any-of.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
cold,
hot,
} from 'jasmine-marbles';

import { anyOf } from './any-of.pipe';

describe('@daffodil/core | anyOf', () => {
it('should return true when any of the input streams emit a truthy value', () => {
expect(anyOf([
hot('ab', { a: true, b: true }),
hot('ab', { a: false, b: true }),
])).toBeObservable(cold('a(bc)', { a: true, b: true, c: true }));
});

it('should return false when all of the input streams emit a falsy value', () => {
expect(anyOf([
hot('ab', { a: true, b: false }),
hot('ab', { a: false, b: false }),
])).toBeObservable(cold('a(bc)', { a: true, b: false, c: false }));
});
});
13 changes: 13 additions & 0 deletions libs/core/src/operators/any-of.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
Observable,
combineLatest,
map,
} from 'rxjs';

/**
* Accepts a list of input observables and emits true when any of the inputs emit a truthy value.
* Does not emit until all of the input streams have emitted at least once.
*/
export const anyOf = (items: Observable<boolean>[]) => combineLatest(items).pipe(map((els) => els.reduce(
(agg, el) => agg || el, false)),
);
1 change: 1 addition & 0 deletions libs/core/src/operators/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './backoff.pipe';
export * from './any-of.pipe';
export * from './all-of.pipe';

0 comments on commit a61dc02

Please sign in to comment.