Skip to content

Commit

Permalink
feat(core): add allOf operator (#2517)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 25, 2023
1 parent 4761b9e commit 53c6bdc
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/all-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 { allOf } from './all-of.pipe';

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

it('should return false when any of the input streams emit a falsy value', () => {
expect(allOf([
hot('ab', { a: true, b: false }),
hot('ab', { a: false, b: false }),
])).toBeObservable(cold('a(bc)', { a: false, b: false, c: false }));
});
});
13 changes: 13 additions & 0 deletions libs/core/src/operators/all-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 all of the inputs emit a truthy value.
* Does not emit until all of the input streams have emitted at least once.
*/
export const allOf = (items: Observable<boolean>[]) => combineLatest(items).pipe(map((els) => els.reduce(
(agg, el) => agg && el, true)),
);
1 change: 1 addition & 0 deletions libs/core/src/operators/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './backoff.pipe';
export * from './all-of.pipe';

0 comments on commit 53c6bdc

Please sign in to comment.