Skip to content

Commit

Permalink
feat(operators): adds match
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 10, 2020
1 parent 9c50989 commit 8491c61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/operators/compounds/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './match';
30 changes: 30 additions & 0 deletions src/operators/compounds/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Observable, OperatorFunction } from 'rxjs';
import { map, debounceTime } from 'rxjs/operators';
import { changes, pick } from '../basics';
import { getCompare } from '../../helpers/get-compare';
import { CompareOptions } from '../../types';

export interface MatchOptions extends CompareOptions {
pick?: boolean | null;
debounce?: number | null;
}

export function match<T, U>(
value: T,
options?: MatchOptions
): OperatorFunction<U, boolean> {
const equal = getCompare(options);

return (observable: Observable<U>): Observable<boolean> => {
return observable.pipe(
map((item) => equal(value, item)),
changes(),
options && typeof options.debounce === 'number'
? debounceTime(Math.max(0, options.debounce))
: (x) => x,
options && typeof options.pick === 'boolean'
? pick(options.pick)
: (x) => x
);
};
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './basics';
export * from './compounds';

0 comments on commit 8491c61

Please sign in to comment.