1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-30 13:03:53 +01:00

move search functions to utils helper

This commit is contained in:
Kyle Spearrin 2018-05-16 11:17:40 -04:00
parent c82ded58eb
commit df7ccaea9d
2 changed files with 46 additions and 27 deletions

View File

@ -10,6 +10,8 @@ import { CollectionResponse } from '../models/response/collectionResponse';
import { FolderResponse } from '../models/response/folderResponse'; import { FolderResponse } from '../models/response/folderResponse';
import { ListResponse } from '../models/response/listResponse'; import { ListResponse } from '../models/response/listResponse';
import { CliUtils } from '../utils';
export class ListCommand { export class ListCommand {
constructor(private cipherService: CipherService, private folderService: FolderService, constructor(private cipherService: CipherService, private folderService: FolderService,
private collectionService: CollectionService) { } private collectionService: CollectionService) { }
@ -69,19 +71,7 @@ export class ListCommand {
} }
if (cmd.search != null && cmd.search.trim() !== '') { if (cmd.search != null && cmd.search.trim() !== '') {
const search = cmd.search.toLowerCase(); ciphers = CliUtils.searchCiphers(ciphers, cmd.search);
ciphers = ciphers.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
return true;
}
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(search) > -1) {
return true;
}
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
} }
const res = new ListResponse(ciphers.map((o) => new CipherResponse(o))); const res = new ListResponse(ciphers.map((o) => new CipherResponse(o)));
@ -92,13 +82,7 @@ export class ListCommand {
let folders = await this.folderService.getAllDecrypted(); let folders = await this.folderService.getAllDecrypted();
if (cmd.search != null && cmd.search.trim() !== '') { if (cmd.search != null && cmd.search.trim() !== '') {
const search = cmd.search.toLowerCase(); folders = CliUtils.searchFolders(folders, cmd.search);
folders = folders.filter((f) => {
if (f.name != null && f.name.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
} }
const res = new ListResponse(folders.map((o) => new FolderResponse(o))); const res = new ListResponse(folders.map((o) => new FolderResponse(o)));
@ -118,13 +102,7 @@ export class ListCommand {
} }
if (cmd.search != null && cmd.search.trim() !== '') { if (cmd.search != null && cmd.search.trim() !== '') {
const search = cmd.search.toLowerCase(); collections = CliUtils.searchCollections(collections, cmd.search);
collections = collections.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
} }
const res = new ListResponse(collections.map((o) => new CollectionResponse(o))); const res = new ListResponse(collections.map((o) => new CollectionResponse(o)));

41
src/utils.ts Normal file
View File

@ -0,0 +1,41 @@
import { CipherView } from 'jslib/models/view/cipherView';
import { CollectionView } from 'jslib/models/view/collectionView';
import { FolderView } from 'jslib/models/view/folderView';
export class CliUtils {
static searchCiphers(ciphers: CipherView[], search: string) {
search = search.toLowerCase();
return ciphers.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
return true;
}
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(search) > -1) {
return true;
}
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
}
static searchFolders(folders: FolderView[], search: string) {
search = search.toLowerCase();
return folders.filter((f) => {
if (f.name != null && f.name.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
}
static searchCollections(collections: CollectionView[], search: string) {
search = search.toLowerCase();
return collections.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
return true;
}
return false;
});
}
}