From bdb2efd77054d28a686dcf50a849ffbf62a996d6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 13 Aug 2018 14:28:10 -0400 Subject: [PATCH] searchCiphersBasic --- src/abstractions/search.service.ts | 1 + src/services/cipher.service.ts | 10 +++++--- src/services/search.service.ts | 41 +++++++++++++++++------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/abstractions/search.service.ts b/src/abstractions/search.service.ts index 4050a27230..d9f87d50bd 100644 --- a/src/abstractions/search.service.ts +++ b/src/abstractions/search.service.ts @@ -5,4 +5,5 @@ export abstract class SearchService { isSearchable: (query: string) => boolean; indexCiphers: () => Promise; searchCiphers: (query: string, filter?: (cipher: CipherView) => boolean) => Promise; + searchCiphersBasic: (ciphers: CipherView[], query: string) => CipherView[]; } diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index b5f1c046e9..3903495af8 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -67,10 +67,12 @@ export class CipherService implements CipherServiceAbstraction { } set decryptedCipherCache(value: CipherView[]) { this._decryptedCipherCache = value; - if (value == null) { - this.searchService().clearIndex(); - } else { - this.searchService().indexCiphers(); + if (this.searchService != null) { + if (value == null) { + this.searchService().clearIndex(); + } else { + this.searchService().indexCiphers(); + } } } diff --git a/src/services/search.service.ts b/src/services/search.service.ts index 1cb1afda13..852da8eb81 100644 --- a/src/services/search.service.ts +++ b/src/services/search.service.ts @@ -103,24 +103,7 @@ export class SearchService implements SearchServiceAbstraction { if (this.index == null) { // Fall back to basic search if index is not available - return ciphers.filter((c) => { - if (c.name != null && c.name.toLowerCase().indexOf(query) > -1) { - return true; - } - if (this.onlySearchName) { - return false; - } - if (query.length >= 8 && c.id.startsWith(query)) { - return true; - } - if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(query) > -1) { - return true; - } - if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(query) > -1) { - return true; - } - return false; - }); + return this.searchCiphersBasic(ciphers, query); } const ciphersMap = new Map(); @@ -157,4 +140,26 @@ export class SearchService implements SearchServiceAbstraction { } return results; } + + searchCiphersBasic(ciphers: CipherView[], query: string) { + query = query.trim().toLowerCase(); + return ciphers.filter((c) => { + if (c.name != null && c.name.toLowerCase().indexOf(query) > -1) { + return true; + } + if (this.onlySearchName) { + return false; + } + if (query.length >= 8 && c.id.startsWith(query)) { + return true; + } + if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(query) > -1) { + return true; + } + if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(query) > -1) { + return true; + } + return false; + }); + } }