Skip to content

Instantly share code, notes, and snippets.

@ezzabuzaid
Created November 4, 2022 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezzabuzaid/618574465a25e4fdf95cbe5cbd499f14 to your computer and use it in GitHub Desktop.
Save ezzabuzaid/618574465a25e4fdf95cbe5cbd499f14 to your computer and use it in GitHub Desktop.
import { MonoTypeOperatorFunction, Observable } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
filter,
map
} from 'rxjs/operators';
interface ITypeaheadOperatorOptions {
minLength: number;
debounceTime: number;
allowEmptyString?: boolean;
}
export function typeaheadOperator(
options: ITypeaheadOperatorOptions
): MonoTypeOperatorFunction<string> {
return (source): Observable<string> =>
source.pipe(
debounceTime(options.debounceTime),
filter((value) => value !== null || value !== undefined),
filter((value) => typeof value === 'string'),
filter((value) => {
if (value === '') {
return options.allowEmptyString;
}
return value.length >= options.minLength;
}),
map((value) => value.toLowerCase()),
distinctUntilChanged()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment