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.
This commit is contained in:
Steve Howell 2018-07-29 14:11:48 +00:00 committed by Tim Abbott
parent 064d0f3c89
commit 95d136ca5e
6 changed files with 48 additions and 11 deletions

View File

@ -102,6 +102,7 @@
"Filter": false,
"flatpickr": false,
"pointer": false,
"search_util": false,
"util": false,
"rtl": false,
"MessageListData": false,

View File

@ -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",

View File

@ -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";

37
static/js/search_util.js Normal file
View File

@ -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;

View File

@ -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;

View File

@ -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',