diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index 7ace2bc7fd..3a748d09ba 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -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]; diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.js index 8b16904d2b..a658669d18 100644 --- a/frontend_tests/node_tests/typeahead_helper.js +++ b/frontend_tests/node_tests/typeahead_helper.js @@ -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' }, diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js index 754116d8e8..179cd0aa1f 100644 --- a/static/js/composebox_typeahead.js +++ b/static/js/composebox_typeahead.js @@ -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') { diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.js index 415b896cb9..59c2a33cce 100644 --- a/static/js/typeahead_helper.js +++ b/static/js/typeahead_helper.js @@ -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; });