typeahead_helper: Add sorter function for slash commands.

This adds a `sort_slash_commands` function in typeahead_helper to sort
slash commands.
This commit is contained in:
Pragati Agrawal 2019-03-19 09:35:45 +05:30 committed by Tim Abbott
parent 76a35bf1d9
commit f617008924
4 changed files with 46 additions and 1 deletions

View File

@ -122,6 +122,11 @@ const me_slash = {
text: "translated: /me is excited (Display action text)",
};
const my_slash = {
name: "my",
text: "translated: /my (Test)",
};
var sweden_stream = {
name: 'Sweden',
description: 'Cold, mountains and home decor.',
@ -822,6 +827,11 @@ run_test('initialize', () => {
expected_value = [make_emoji(emoji_heart), make_emoji(emoji_headphones)];
assert.deepEqual(actual_value, expected_value);
fake_this = { completing: 'slash', token: 'm' };
actual_value = options.sorter.call(fake_this, [my_slash, me_slash]);
expected_value = [me_slash, my_slash];
assert.deepEqual(actual_value, expected_value);
fake_this = { completing: 'mention', token: 'co' };
actual_value = options.sorter.call(fake_this, [othello, cordelia]);
expected_value = [cordelia, othello];

View File

@ -544,6 +544,25 @@ run_test('render_emoji', () => {
assert(rendered);
});
run_test('sort_slash_commands', () => {
var slash_commands = [
{ name: 'my' },
{ name: 'poll' },
{ name: 'me' },
{ name: 'mine' },
{ name: 'test' },
{ name: 'ping' },
];
assert.deepEqual(th.sort_slash_commands(slash_commands, 'm'), [
{ name: 'me' },
{ name: 'mine' },
{ name: 'my' },
{ name: 'ping' },
{ name: 'poll' },
{ name: 'test' },
]);
});
run_test('sort_emojis', () => {
var emoji_list = [
{ emoji_name: '+1' },

View File

@ -625,7 +625,7 @@ exports.compose_matches_sorter = function (matches) {
} else if (this.completing === 'mention' || this.completing === 'silent_mention') {
return typeahead_helper.sort_people_and_user_groups(this.token, matches);
} else if (this.completing === 'slash') {
return matches;
return typeahead_helper.sort_slash_commands(matches, this.token);
} else if (this.completing === 'stream') {
return typeahead_helper.sort_streams(matches, this.token);
} else if (this.completing === 'syntax') {

View File

@ -331,6 +331,22 @@ exports.sort_recipients = function (users, query, current_stream, current_topic,
return result.concat(rest_sorted);
};
function slash_command_comparator(slash_command_a, slash_command_b) {
if (slash_command_a.name < slash_command_b.name) {
return -1;
} else if (slash_command_a.name > slash_command_b.name) {
return 1;
}
}
exports.sort_slash_commands = function (matches, query) {
// We will likely want to in the future make this sort the
// just-`/` commands by something approximating usefulness.
var results = util.prefix_sort(query, matches, function (x) { return x.name; });
results.matches = results.matches.sort(slash_command_comparator);
results.rest = results.rest.sort(slash_command_comparator);
return results.matches.concat(results.rest);
};
exports.sort_emojis = function (matches, query) {
// TODO: sort by category in v2
var results = emoji_prefix_sort(query, matches, function (x) { return x.emoji_name; });