Browse Source

Member list editor: Light query refactoring

Explicit short-circuit logic.
Danilo Bargen 6 years ago
parent
commit
1489a6530e
1 changed files with 22 additions and 14 deletions
  1. 22 14
      src/directives/member_list_editor.ts

+ 22 - 14
src/directives/member_list_editor.ts

@@ -51,24 +51,32 @@ export default [
                 };
 
                 this.querySearch = (query: string): threema.ContactReceiver[] => {
-
                     if (query !== undefined && query.length <= 0) {
-                        // do not show a result on empty entry
+                        // Do not show a result on empty entry
                         return [];
                     } else {
-                        // search for contacts, do not show selected contacts
+                        // Search for contacts, do not show selected contacts
                         const lowercaseQuery = query.toLowerCase();
-                        const result = this.allContacts.filter((contactReceiver: threema.ContactReceiver) => {
-                            return this.members.filter((identity: string) => {
-                                    return identity === contactReceiver.id;
-                                }).length === 0
-                                && (
-                                    // find in display name
-                                    contactReceiver.displayName.toLowerCase().indexOf(lowercaseQuery) >= 0
-                                    // or threema identity
-                                    || contactReceiver.id.toLowerCase().indexOf(lowercaseQuery) >= 0
-                                );
-                        });
+                        const result = this.allContacts
+                            .filter((contactReceiver: threema.ContactReceiver) => {
+                                // Ignore already selected contacts
+                                if (this.members.filter((id: string) => id === contactReceiver.id).length !== 0) {
+                                    return false;
+                                }
+
+                                // Search in display name
+                                if (contactReceiver.displayName.toLowerCase().indexOf(lowercaseQuery) >= 0) {
+                                    return true;
+                                }
+
+                                // Search in identity
+                                if (contactReceiver.id.toLowerCase().indexOf(lowercaseQuery) >= 0) {
+                                    return true;
+                                }
+
+                                // Not found
+                                return false;
+                            });
 
                         return result.length <= AUTOCOMPLETE_MAX_RESULTS ? result
                             : result.slice(0, AUTOCOMPLETE_MAX_RESULTS);