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:
Shubham Padia
2018-06-25 20:44:45 +05:30
committed by showell
parent a2a695dfa7
commit 1364971a17
4 changed files with 33 additions and 22 deletions

View File

@@ -6,3 +6,13 @@ run_test('basics', () => {
common.autofocus('#home');
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'));
});

View File

@@ -7,6 +7,7 @@ zrequire('stream_data');
zrequire('topic_data');
zrequire('people');
zrequire('unread');
zrequire('common');
var search = zrequire('search_suggestion');
var bob = {

View File

@@ -69,6 +69,25 @@ exports.password_warning = function (password, password_field) {
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;
}());

View File

@@ -2,27 +2,8 @@ var search_suggestion = (function () {
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) {
return phrase_match(stream_name, q);
return common.phrase_match(q, stream_name);
}
function highlight_person(query, person) {
@@ -325,7 +306,7 @@ function get_topic_suggestions(last, operators) {
if (guess !== '') {
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'];
choices = _.filter(choices, function (choice) {
return phrase_match(choice, last_operand);
return common.phrase_match(last_operand, choice);
});
return _.map(choices, function (choice) {