From 95d136ca5e76d1c2e5a0a7dc61a6b4a93725988e Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 29 Jul 2018 14:11:48 +0000 Subject: [PATCH] Extract search_util.js module. We probably should have done this a while ago, even though these functions are pretty tiny. The goal here is to make it easier to have more consistent search semantics. Our first use case is subs.js. In this case we are able to decouple a bit of generic string matching from the subs-specific code. --- .eslintrc.json | 1 + frontend_tests/node_tests/subs.js | 2 ++ static/js/bundles/app.js | 1 + static/js/search_util.js | 37 +++++++++++++++++++++++++++++++ static/js/subs.js | 17 +++++--------- tools/test-js-with-node | 1 + 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 static/js/search_util.js diff --git a/.eslintrc.json b/.eslintrc.json index abbf2be9d3..386ef35f4d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -102,6 +102,7 @@ "Filter": false, "flatpickr": false, "pointer": false, + "search_util": false, "util": false, "rtl": false, "MessageListData": false, diff --git a/frontend_tests/node_tests/subs.js b/frontend_tests/node_tests/subs.js index 82c5f02367..d4de5e9b86 100644 --- a/frontend_tests/node_tests/subs.js +++ b/frontend_tests/node_tests/subs.js @@ -2,6 +2,8 @@ global.stub_out_jquery(); set_global('ui', {}); zrequire('stream_data'); +zrequire('search_util'); + global.patch_builtin('window', { location: { hash: "#streams/1/announce", diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 42b31dc1a7..88c85c3051 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -33,6 +33,7 @@ import "js/feature_flags.js"; import "js/loading.js"; import 'js/schema.js'; import "js/util.js"; +import "js/search_util.js"; import "js/keydown_util.js"; import "js/lightbox_canvas.js"; import "js/rtl.js"; diff --git a/static/js/search_util.js b/static/js/search_util.js new file mode 100644 index 0000000000..8d65875af3 --- /dev/null +++ b/static/js/search_util.js @@ -0,0 +1,37 @@ +var search_util = (function () { + +var exports = {}; + +exports.get_search_terms = function (input) { + var search_terms = input.toLowerCase().split(",").map(function (s) { + return s.trim(); + }); + return search_terms; +}; + +exports.vanilla_match = function (opts) { + /* + This is a pretty vanilla search criteria + where we see if any of our search terms + is in our value. When in doubt we should use + this for all Zulip filters, but we may + have more complicated use cases in some + places. + + This is case insensitive. + */ + var val = opts.val.toLowerCase(); + return _.any(opts.search_terms, function (term) { + if (val.indexOf(term) !== -1) { + return true; + } + }); +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = search_util; +} +window.search_util = search_util; diff --git a/static/js/subs.js b/static/js/subs.js index 21c1d7bb46..ffd46337ab 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -318,19 +318,14 @@ function remove_temporarily_miscategorized_streams() { exports.remove_miscategorized_streams = remove_temporarily_miscategorized_streams; function stream_matches_query(query, sub, attr) { - var search_terms = query.input.toLowerCase().split(",").map(function (s) { - return s.trim(); + var search_terms = search_util.get_search_terms(query.input); + var val = sub[attr]; + + var flag = search_util.vanilla_match({ + val: val, + search_terms: search_terms, }); - var flag = true; - flag = flag && (function () { - var sub_attr = sub[attr].toLowerCase(); - return _.any(search_terms, function (o) { - if (sub_attr.indexOf(o) !== -1) { - return true; - } - }); - }()); flag = flag && (sub.subscribed || !query.subscribed_only || sub.data_temp_view === "true"); return flag; diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 199e235e28..3fece35da4 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -60,6 +60,7 @@ enforce_fully_covered = { 'static/js/scroll_util.js', 'static/js/search.js', 'static/js/search_suggestion.js', + 'static/js/search_util.js', # Removed because we're migrating code from uncovered other settings pages to here. # 'static/js/settings_ui.js', 'static/js/settings_muting.js',