31 lines
817 B
TypeScript
31 lines
817 B
TypeScript
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject } from 'rxjs';
|
|
import { IListable } from '../listing';
|
|
|
|
@Injectable()
|
|
export class SearchService<T extends IListable> {
|
|
skip = false;
|
|
private readonly _query$ = new BehaviorSubject('');
|
|
readonly valueChanges$ = this._query$.asObservable();
|
|
|
|
get searchValue(): string {
|
|
return this._query$.getValue();
|
|
}
|
|
|
|
set searchValue(value: string) {
|
|
this._query$.next(value);
|
|
}
|
|
|
|
searchIn(entities: T[]): T[] {
|
|
const searchValue = this.searchValue.toLowerCase();
|
|
if (!searchValue || this.skip) {
|
|
return entities;
|
|
}
|
|
return entities.filter(entity => entity.searchKey?.toLowerCase().includes(searchValue));
|
|
}
|
|
|
|
reset(): void {
|
|
this._query$.next('');
|
|
}
|
|
}
|