Skip to content

Commit

Permalink
feat(every): add higher-order lettable version of every
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 9, 2017
1 parent b8e956b commit 13f3503
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 56 deletions.
60 changes: 4 additions & 56 deletions src/operator/every.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Operator } from '../Operator';
import { Observer } from '../Observer';

import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { every as higherOrder } from '../operators';

/**
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
Expand All @@ -19,56 +18,5 @@ import { Subscriber } from '../Subscriber';
*/
export function every<T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): Observable<boolean> {
return this.lift(new EveryOperator(predicate, thisArg, this));
}

class EveryOperator<T> implements Operator<T, boolean> {
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg?: any,
private source?: Observable<T>) {
}

call(observer: Subscriber<boolean>, source: any): any {
return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class EverySubscriber<T> extends Subscriber<T> {
private index: number = 0;

constructor(destination: Observer<boolean>,
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg: any,
private source?: Observable<T>) {
super(destination);
this.thisArg = thisArg || this;
}

private notifyComplete(everyValueMatch: boolean): void {
this.destination.next(everyValueMatch);
this.destination.complete();
}

protected _next(value: T): void {
let result = false;
try {
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
} catch (err) {
this.destination.error(err);
return;
}

if (!result) {
this.notifyComplete(false);
}
}

protected _complete(): void {
this.notifyComplete(true);
}
}
return higherOrder(predicate, thisArg)(this);
}
75 changes: 75 additions & 0 deletions src/operators/every.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Operator } from '../Operator';
import { Observer } from '../Observer';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { OperatorFunction } from '../interfaces';

/**
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
*
* @example <caption>A simple example emitting true if all elements are less than 5, false otherwise</caption>
* Observable.of(1, 2, 3, 4, 5, 6)
* .every(x => x < 5)
* .subscribe(x => console.log(x)); // -> false
*
* @param {function} predicate A function for determining if an item meets a specified condition.
* @param {any} [thisArg] Optional object to use for `this` in the callback.
* @return {Observable} An Observable of booleans that determines if all items of the source Observable meet the condition specified.
* @method every
* @owner Observable
*/
export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): OperatorFunction<T, boolean> {
return (source: Observable<T>) => source.lift(new EveryOperator(predicate, thisArg, source));
}

class EveryOperator<T> implements Operator<T, boolean> {
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg?: any,
private source?: Observable<T>) {
}

call(observer: Subscriber<boolean>, source: any): any {
return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class EverySubscriber<T> extends Subscriber<T> {
private index: number = 0;

constructor(destination: Observer<boolean>,
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg: any,
private source?: Observable<T>) {
super(destination);
this.thisArg = thisArg || this;
}

private notifyComplete(everyValueMatch: boolean): void {
this.destination.next(everyValueMatch);
this.destination.complete();
}

protected _next(value: T): void {
let result = false;
try {
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
} catch (err) {
this.destination.error(err);
return;
}

if (!result) {
this.notifyComplete(false);
}
}

protected _complete(): void {
this.notifyComplete(true);
}
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { dematerialize } from './dematerialize';
export { distinctUntilChanged } from './distinctUntilChanged';
export { distinctUntilKeyChanged } from './distinctUntilKeyChanged';
export { elementAt } from './elementAt';
export { every } from './every';
export { filter } from './filter';
export { ignoreElements } from './ignoreElements';
export { map } from './map';
Expand Down

0 comments on commit 13f3503

Please sign in to comment.