Created
November 4, 2022 13:56
-
-
Save ezzabuzaid/618574465a25e4fdf95cbe5cbd499f14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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