I created an rxjs search for a hiring company, but I got rejected for the following reason: -performance issue on a bigger data set. -bugs
My question is how can I make this code better for the next time if I have similar task?
brands = These should be populated from the brand property from the collection of products.
The search should match using the name, description and brand properties.
- The search should not fire unless a minimum of three characters have been typed.
- The search should only fire a maximum of once per 400 milliseconds.
If searching for “xxx” with the brand selected as “xxx” and In Stock, then only products matching all three filters
const stackoverflowFilter = (filter: ISearch) => {
return (product: IProducts): boolean => {
return ((
(
(product.name.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1) ||
product.description.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1 ||
product.brand.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1) &&
product.brand.toLowerCase().indexOf(filter.select.toLowerCase()) !== -1 &&
(filter.radio === 'all' ? true : filter.radio === 'in-stock' ? product.quantity > 0 : product.quantity === 0)
)
)
};
};
export interface ISearch { input: string; select: string; radio: string }
> public getStackoverflow(): Observable<Stackoverflow[]> {
> return of([
> {
> id: "1",
> name: "This is just test data",
> price: "89.9",
> description: "test data",
> brand: "stackoverflow",
> quantity: 4
> }....])}
this.stackoverflow$ = this.service.getStackoverflow();
this.stackoverflow$.pipe(
tap((prod) => {
this.stackoverflow = prod;
this.brands = this.stackoverflow.map(({ brand }) => brand).reduce((prev, cur) => {
if (prev.indexOf(cur) < 0) prev.push(cur);
return prev;
}, []);
}),
concatMap(() =>
this.search$.pipe(
debounceTime(400),
distinctUntilChanged(),
switchMap((search: string) =>
iif(() => search.length >= 3,
this.stackoverflow$.pipe(
map(stackoverflow=> stackoverflow.filter(stackoverflowFilter(this.filter))),
distinctUntilChanged((curr, prev) => curr === prev),
),
this.stackoverflow$.pipe(
filter(() => search.length === 0),
)
)
)
))
).subscribe(
stackoverflow=> { this.stackoverflow= stackoverflow; },
(err) => throwError(err)
);