[polari] cleanup: Use find/findIndex over loops



commit c11760560ca1b69b0cbfcdb2fb8d2e2e04353f26
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Apr 4 02:30:44 2019 +0200

    cleanup: Use find/findIndex over loops
    
    Now that gjs was updated to a SpiderMonkey version that supports the
    more idiomatic methods, use those instead of implementing lookups as
    loops.
    
    https://gitlab.gnome.org/GNOME/polari/merge_requests/109

 src/networksManager.js |  9 ++++-----
 src/roomManager.js     | 18 +++++++-----------
 src/userTracker.js     |  7 +------
 3 files changed, 12 insertions(+), 22 deletions(-)
---
diff --git a/src/networksManager.js b/src/networksManager.js
index 113c325..b288acb 100644
--- a/src/networksManager.js
+++ b/src/networksManager.js
@@ -104,11 +104,10 @@ var NetworksManager = class {
     }
 
     findByServer(server) {
-        for (let n of this._networks) {
-            if (n.servers.some(s => s.address == server))
-                return n.id;
-        }
-        return null;
+        let network = this._networks.find(n => {
+            return n.servers.some(s => s.address == server);
+        });
+        return network ? network.id : null;
     }
 };
 Signals.addSignalMethods(NetworksManager.prototype);
diff --git a/src/roomManager.js b/src/roomManager.js
index 1e35409..283268d 100644
--- a/src/roomManager.js
+++ b/src/roomManager.js
@@ -55,11 +55,9 @@ var RoomManager = class {
     }
 
     lookupRoomByName(name, account) {
-        for (let room of this._rooms.values()) {
-            if (room.channel_name == name && room.account == account)
-                return room;
-        }
-        return null;
+        return [...this._rooms.values()].find(room => {
+            return room.channel_name == name && room.account == account;
+        });
     }
 
     lookupRoomByChannel(channel) {
@@ -122,12 +120,10 @@ var RoomManager = class {
 
     _findChannelIndex(channels, accountPath, channelName) {
         let matchName = channelName.toLowerCase();
-        for (let i = 0; i < channels.length; i++) {
-            if (channels[i].account.deep_unpack() == accountPath &&
-                channels[i].channel.deep_unpack().toLowerCase() == matchName)
-                return i;
-        }
-        return -1;
+        return channels.findIndex(c => {
+            return c.account.deep_unpack() == accountPath &&
+                   c.channel.deep_unpack().toLowerCase() == matchName;
+        });
     }
 
     _addSavedChannel(accountPath, channelName) {
diff --git a/src/userTracker.js b/src/userTracker.js
index 3c6e9ed..f5cbf08 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -287,12 +287,7 @@ const UserTracker = GObject.registerClass({
         if (!contacts.length)
             return null;
 
-        for (let i = 0; i < contacts.length; i++) {
-            if (contacts[i].alias == nickName)
-                return contacts[i];
-        }
-
-        return contacts[0];
+        return contacts.find(c => c.alias == nickName) || contacts[0];
     }
 
     watchRoomStatus(room, baseNick, callback) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]