| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Falsy, OperatorFunction } from '../types'; |
| 3 |
import { operate } from '../util/lift'; |
| 4 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 5 |
|
| 6 |
export function every<T>(predicate: BooleanConstructor): OperatorFunction<T, Exclude<T, Falsy> extends never ? false : boolean>; |
| 7 |
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */ |
| 8 |
export function every<T>( |
| 9 |
predicate: BooleanConstructor, |
| 10 |
thisArg: any |
| 11 |
): OperatorFunction<T, Exclude<T, Falsy> extends never ? false : boolean>; |
| 12 |
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */ |
| 13 |
export function every<T, A>( |
| 14 |
predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean, |
| 15 |
thisArg: A |
| 16 |
): OperatorFunction<T, boolean>; |
| 17 |
export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, boolean>; |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified. |
| 21 |
* |
| 22 |
* <span class="informal">If all values pass predicate before the source completes, emits true before completion, |
| 23 |
* otherwise emit false, then complete.</span> |
| 24 |
* |
| 25 |
*  |
| 26 |
* |
| 27 |
* ## Example |
| 28 |
* |
| 29 |
* A simple example emitting true if all elements are less than 5, false otherwise |
| 30 |
* |
| 31 |
* ```ts |
| 32 |
* import { of, every } from 'rxjs'; |
| 33 |
* |
| 34 |
* of(1, 2, 3, 4, 5, 6) |
| 35 |
* .pipe(every(x => x < 5)) |
| 36 |
* .subscribe(x => console.log(x)); // -> false |
| 37 |
* ``` |
| 38 |
* |
| 39 |
* @param predicate A function for determining if an item meets a specified condition. |
| 40 |
* @param thisArg Optional object to use for `this` in the callback. |
| 41 |
* @return A function that returns an Observable of booleans that determines if |
| 42 |
* all items of the source Observable meet the condition specified. |
| 43 |
*/ |
| 44 |
export function every<T>( |
| 45 |
predicate: (value: T, index: number, source: Observable<T>) => boolean, |
| 46 |
thisArg?: any |
| 47 |
): OperatorFunction<T, boolean> { |
| 48 |
return operate((source, subscriber) => { |
| 49 |
let index = 0; |
| 50 |
source.subscribe( |
| 51 |
createOperatorSubscriber( |
| 52 |
subscriber, |
| 53 |
(value) => { |
| 54 |
if (!predicate.call(thisArg, value, index++, source)) { |
| 55 |
subscriber.next(false); |
| 56 |
subscriber.complete(); |
| 57 |
} |
| 58 |
}, |
| 59 |
() => { |
| 60 |
subscriber.next(true); |
| 61 |
subscriber.complete(); |
| 62 |
} |
| 63 |
) |
| 64 |
); |
| 65 |
}); |
| 66 |
} |