[polari/wip/fmuellner/roomlist-filtering: 1/8] utils: Add updateTerms() helper



commit fb7337629a0092aa0381c6bf4de0500d9ba5d394
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Mar 12 02:33:25 2017 +0100

    utils: Add updateTerms() helper
    
    As we will soon add filtering to the server room list, it makes
    sense to generalize how we normalize and split filter text into
    terms. Add a utility method based on the code in ConnectionsList
    for that purpose. As a bonus, the method's return value indicates
    whether the terms actually changed, so callers can avoid unneeded
    work (for example when the amount of stripped whitespace changed).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779960

 src/utils.js |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
---
diff --git a/src/utils.js b/src/utils.js
index 93dc483..afa86b2 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -188,6 +188,20 @@ function openURL(url, timestamp) {
     }
 }
 
+function updateTerms(terms, str) {
+    let normalized = str.trim().toLowerCase().replace(/\s+/g, ' ');
+    let newTerms = normalized ? normalized.split(' ') : [];
+
+    let changed = newTerms.length != terms.length;
+    for (let i = 0; i < terms.length && !changed; i++)
+        changed = terms[i] != newTerms[i];
+
+    if (changed)
+        terms.splice.apply(terms, [0, terms.length, ...newTerms]);
+
+    return changed;
+}
+
 function gpaste(text, title, callback) {
     if (title.length > MAX_PASTE_TITLE_LENGTH)
         title = title.substr(0, MAX_PASTE_TITLE_LENGTH - 1) + '…';


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