From 2398cac572448289c274f3fc8b1660d704c8383a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 4 May 2023 02:19:55 +0200 Subject: [PATCH] A more efficient and clean logic for `selectedEntities` calculation. --- .../components/panels/Entities/Selector.vue | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/Selector.vue b/platypush/backend/http/webapp/src/components/panels/Entities/Selector.vue index d6899ba70e..3081324ca3 100644 --- a/platypush/backend/http/webapp/src/components/panels/Entities/Selector.vue +++ b/platypush/backend/http/webapp/src/components/panels/Entities/Selector.vue @@ -89,21 +89,27 @@ export default { }, selectedEntities() { - const searchTerm = (this.searchTerm || '').toLowerCase() + if (!this.searchTerm?.length) + return this.entityGroups.id + + const searchTerm = this.searchTerm.toLowerCase().trim() return Object.values(this.entityGroups.id).filter((entity) => { if (!this.selectedGroups[entity[this.value?.grouping]]) - return false + return false - if (searchTerm?.length) { - return ( - ((entity.name || '').toLowerCase()).indexOf(searchTerm) >= 0 || - ((entity.plugin || '').toLowerCase()).indexOf(searchTerm) >= 0 || - ((entity.external_id || '').toLowerCase()).indexOf(searchTerm) >= 0 || - (entity.id || 0).toString() == searchTerm - ) + if (!searchTerm?.length) + return true + + for (const attr of ['id', 'external_id', 'name', 'plugin']) { + if (!entity[attr]) + continue + + const entityValue = entity[attr].toString().toLowerCase() + if (entityValue.indexOf(searchTerm) >= 0) + return true } - return true + return false }).reduce((obj, entity) => { obj[entity.id] = entity return obj