mirror of
https://github.com/zulip/zulip.git
synced 2025-11-17 20:41:46 +00:00
search: Extract phrase_match to common.js.
Extracts phrase_match to common.js so it can be used by other components like integrations search.
This commit is contained in:
@@ -6,3 +6,13 @@ run_test('basics', () => {
|
|||||||
common.autofocus('#home');
|
common.autofocus('#home');
|
||||||
assert($('#home').is_focused());
|
assert($('#home').is_focused());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
run_test('phrase_match', () => {
|
||||||
|
assert(common.phrase_match('tes', 'test'));
|
||||||
|
assert(common.phrase_match('Tes', 'test'));
|
||||||
|
assert(common.phrase_match('Tes', 'Test'));
|
||||||
|
assert(common.phrase_match('tes', 'Stream Test'));
|
||||||
|
|
||||||
|
assert(!common.phrase_match('tests', 'test'));
|
||||||
|
assert(!common.phrase_match('tes', 'hostess'));
|
||||||
|
});
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ zrequire('stream_data');
|
|||||||
zrequire('topic_data');
|
zrequire('topic_data');
|
||||||
zrequire('people');
|
zrequire('people');
|
||||||
zrequire('unread');
|
zrequire('unread');
|
||||||
|
zrequire('common');
|
||||||
var search = zrequire('search_suggestion');
|
var search = zrequire('search_suggestion');
|
||||||
|
|
||||||
var bob = {
|
var bob = {
|
||||||
|
|||||||
@@ -69,6 +69,25 @@ exports.password_warning = function (password, password_field) {
|
|||||||
return zxcvbn(password).feedback.warning || i18n.t("Password is too weak");
|
return zxcvbn(password).feedback.warning || i18n.t("Password is too weak");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.phrase_match = function (query, phrase) {
|
||||||
|
// match "tes" to "test" and "stream test" but not "hostess"
|
||||||
|
var i;
|
||||||
|
query = query.toLowerCase();
|
||||||
|
|
||||||
|
phrase = phrase.toLowerCase();
|
||||||
|
if (phrase.indexOf(query) === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parts = phrase.split(' ');
|
||||||
|
for (i = 0; i < parts.length; i += 1) {
|
||||||
|
if (parts[i].indexOf(query) === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|||||||
@@ -2,27 +2,8 @@ var search_suggestion = (function () {
|
|||||||
|
|
||||||
var exports = {};
|
var exports = {};
|
||||||
|
|
||||||
function phrase_match(phrase, q) {
|
|
||||||
// match "tes" to "test" and "stream test" but not "hostess"
|
|
||||||
var i;
|
|
||||||
q = q.toLowerCase();
|
|
||||||
|
|
||||||
phrase = phrase.toLowerCase();
|
|
||||||
if (phrase.indexOf(q) === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var parts = phrase.split(' ');
|
|
||||||
for (i = 0; i < parts.length; i += 1) {
|
|
||||||
if (parts[i].indexOf(q) === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function stream_matches_query(stream_name, q) {
|
function stream_matches_query(stream_name, q) {
|
||||||
return phrase_match(stream_name, q);
|
return common.phrase_match(q, stream_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
function highlight_person(query, person) {
|
function highlight_person(query, person) {
|
||||||
@@ -325,7 +306,7 @@ function get_topic_suggestions(last, operators) {
|
|||||||
|
|
||||||
if (guess !== '') {
|
if (guess !== '') {
|
||||||
topics = _.filter(topics, function (topic) {
|
topics = _.filter(topics, function (topic) {
|
||||||
return phrase_match(topic, guess);
|
return common.phrase_match(guess, topic);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,7 +512,7 @@ function get_operator_suggestions(last) {
|
|||||||
|
|
||||||
var choices = ['stream', 'topic', 'pm-with', 'sender', 'near', 'from', 'group-pm-with'];
|
var choices = ['stream', 'topic', 'pm-with', 'sender', 'near', 'from', 'group-pm-with'];
|
||||||
choices = _.filter(choices, function (choice) {
|
choices = _.filter(choices, function (choice) {
|
||||||
return phrase_match(choice, last_operand);
|
return common.phrase_match(last_operand, choice);
|
||||||
});
|
});
|
||||||
|
|
||||||
return _.map(choices, function (choice) {
|
return _.map(choices, function (choice) {
|
||||||
|
|||||||
Reference in New Issue
Block a user