1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-04 13:44:00 +01:00
bitwarden-browser/src/angular/pipes/search-ciphers.pipe.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import {
Pipe,
PipeTransform,
} from '@angular/core';
import { CipherView } from '../../models/view/cipherView';
2018-04-12 21:42:22 +02:00
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
2018-07-11 21:47:54 +02:00
import { DeviceType } from '../../enums';
@Pipe({
name: 'searchCiphers',
})
export class SearchCiphersPipe implements PipeTransform {
2018-04-12 21:42:22 +02:00
private onlySearchName = false;
2018-07-11 21:47:54 +02:00
constructor(platformUtilsService: PlatformUtilsService) {
this.onlySearchName = platformUtilsService.getDevice() === DeviceType.EdgeExtension;
2018-04-12 21:42:22 +02:00
}
transform(ciphers: CipherView[], searchText: string): CipherView[] {
if (ciphers == null || ciphers.length === 0) {
return [];
}
if (searchText == null || searchText.length < 2) {
return ciphers;
}
2018-07-06 20:11:47 +02:00
searchText = searchText.trim().toLowerCase();
return ciphers.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(searchText) > -1) {
return true;
}
2018-04-12 21:42:22 +02:00
if (this.onlySearchName) {
return false;
}
2018-07-11 21:47:54 +02:00
if (c.id.substr(0, 8) === searchText) {
return true;
}
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(searchText) > -1) {
return true;
}
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(searchText) > -1) {
return true;
}
return false;
});
}
}