1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-18 07:25:15 +02:00
bitwarden-browser/libs/angular/src/pipes/search.pipe.ts
renovate[bot] 28de9439be
[deps] Autofill: Update prettier to v3 (#7014)
* [deps] Autofill: Update prettier to v3

* prettier formatting updates

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com>
2023-11-29 16:15:20 -05:00

68 lines
1.6 KiB
TypeScript

import { Pipe, PipeTransform } from "@angular/core";
type PropertyValueFunction<T> = (item: T) => { toString: () => string };
@Pipe({
name: "search",
})
export class SearchPipe implements PipeTransform {
transform<T>(
items: T[],
searchText: string,
prop1?: keyof T,
prop2?: keyof T,
prop3?: keyof T,
): T[];
transform<T>(
items: T[],
searchText: string,
prop1?: PropertyValueFunction<T>,
prop2?: PropertyValueFunction<T>,
prop3?: PropertyValueFunction<T>,
): T[];
transform<T>(
items: T[],
searchText: string,
prop1?: keyof T | PropertyValueFunction<T>,
prop2?: keyof T | PropertyValueFunction<T>,
prop3?: keyof T | PropertyValueFunction<T>,
): T[] {
if (items == null || items.length === 0) {
return [];
}
if (searchText == null || searchText.length < 2) {
return items;
}
searchText = searchText.trim().toLowerCase();
return items.filter((i) => {
if (prop1 != null) {
const propValue = typeof prop1 === "function" ? prop1(i) : i[prop1];
if (propValue?.toString().toLowerCase().indexOf(searchText) > -1) {
return true;
}
}
if (prop2 != null) {
const propValue = typeof prop2 === "function" ? prop2(i) : i[prop2];
if (propValue?.toString().toLowerCase().indexOf(searchText) > -1) {
return true;
}
}
if (prop3 != null) {
const propValue = typeof prop3 === "function" ? prop3(i) : i[prop3];
if (propValue?.toString().toLowerCase().indexOf(searchText) > -1) {
return true;
}
}
return false;
});
}
}