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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

49 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-07-06 18:40:08 +02:00
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "search",
})
export class SearchPipe implements PipeTransform {
2018-07-06 21:03:00 +02:00
transform(
items: any[],
searchText: string,
prop1?: string,
prop2?: string,
prop3?: string
): any[] {
2018-07-06 18:40:08 +02:00
if (items == null || items.length === 0) {
return [];
}
if (searchText == null || searchText.length < 2) {
2018-07-06 21:03:00 +02:00
return items;
2018-07-06 18:40:08 +02:00
}
2021-12-16 13:36:21 +01:00
2018-07-06 20:11:47 +02:00
searchText = searchText.trim().toLowerCase();
return items.filter((i) => {
2021-12-16 13:36:21 +01:00
if (
2018-07-06 18:40:08 +02:00
prop1 != null &&
i[prop1] != null &&
2018-07-06 21:03:00 +02:00
i[prop1].toString().toLowerCase().indexOf(searchText) > -1
2021-12-16 13:36:21 +01:00
) {
2018-07-06 18:40:08 +02:00
return true;
2021-12-16 13:36:21 +01:00
}
if (
2018-07-06 18:40:08 +02:00
prop2 != null &&
i[prop2] != null &&
i[prop2].toString().toLowerCase().indexOf(searchText) > -1
2021-12-16 13:36:21 +01:00
) {
2018-07-06 18:40:08 +02:00
return true;
2021-12-16 13:36:21 +01:00
}
if (
2018-07-06 21:03:00 +02:00
prop3 != null &&
i[prop3] != null &&
i[prop3].toString().toLowerCase().indexOf(searchText) > -1
2021-12-16 13:36:21 +01:00
) {
2018-07-06 18:40:08 +02:00
return true;
2021-12-16 13:36:21 +01:00
}
2018-07-06 18:40:08 +02:00
return false;
2021-12-16 13:36:21 +01:00
});
}
2018-07-06 18:40:08 +02:00
}