Skip to content

Function: every()

every<T>(predicate): Operator<T, boolean>

Defined in: operators/every.ts:21

Creates a stream operator that tests if all values from the source stream satisfy a predicate.

This operator consumes the source stream and applies the provided predicate function to each value.

  • If the predicate returns a truthy value for every element until the source stream completes, the operator emits true.
  • If the predicate returns a falsy value for any element, the operator immediately emits false and then completes, effectively "short-circuiting" the evaluation.

This is a "pull-based" equivalent of Array.prototype.every and is useful for validating data streams. The operator will emit only a single boolean value before it completes.

Type Parameters

T

T = any

The type of the values in the source stream.

Parameters

predicate

(value, index) => CallbackReturnType<boolean>

The function to test each value. It receives the value and its index. It can be synchronous or asynchronous.

Returns

Operator<T, boolean>

An Operator instance that can be used in a stream's pipe method.

Examples

From every.spec.ts:12

typescript
let subject: ReturnType<typeof createSubject<number>> = createSubject<number>();

let source: Stream<number> = subject;

const predicate = (value: number) => value > 0;
const everyStream = source.pipe(every(predicate));
const results: boolean[] = [];
(async () => {
  for await (const value of eachValueFrom(everyStream)) {
    results.push(value);
  }
})();
subject.next(1);
subject.next(2);
subject.next(3);
subject.complete();
await new Promise((resolve) => setTimeout(resolve, 100));
expect(results).toEqual([true]);

Released under the MIT License.