Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Always rank exact string match first.

Wesley Aptekar-Cassels673af19a4d0e
diff --git a/frontend_tests/node_tests/typeahead.js b/frontend_tests/node_tests/typeahead.js
index 371cbb0678b..911bc5d9d43 100644
--- a/frontend_tests/node_tests/typeahead.js
+++ b/frontend_tests/node_tests/typeahead.js
@@ -57,16 +57,18 @@ run_test("get_emoji_matcher", () => {
run_test("triage", () => {
const alice = {name: "alice"};
const alicia = {name: "Alicia"};
+ const joan = {name: "Joan"};
+ const jo = {name: "Jo"};
const steve = {name: "steve"};
const stephanie = {name: "Stephanie"};
- const names = [alice, alicia, steve, stephanie];
+ const names = [alice, alicia, joan, jo, steve, stephanie];
assert.deepEqual(
typeahead.triage("a", names, (r) => r.name),
{
matches: [alice, alicia],
- rest: [steve, stephanie],
+ rest: [joan, jo, steve, stephanie],
},
);
@@ -74,7 +76,7 @@ run_test("triage", () => {
typeahead.triage("A", names, (r) => r.name),
{
matches: [alicia, alice],
- rest: [steve, stephanie],
+ rest: [joan, jo, steve, stephanie],
},
);
@@ -82,7 +84,7 @@ run_test("triage", () => {
typeahead.triage("S", names, (r) => r.name),
{
matches: [stephanie, steve],
- rest: [alice, alicia],
+ rest: [alice, alicia, joan, jo],
},
);
@@ -90,6 +92,22 @@ run_test("triage", () => {
typeahead.triage("fred", names, (r) => r.name),
{
matches: [],
+ rest: [alice, alicia, joan, jo, steve, stephanie],
+ },
+ );
+
+ assert.deepEqual(
+ typeahead.triage("Jo", names, (r) => r.name),
+ {
+ matches: [jo, joan],
+ rest: [alice, alicia, steve, stephanie],
+ },
+ );
+
+ assert.deepEqual(
+ typeahead.triage("jo", names, (r) => r.name),
+ {
+ matches: [jo, joan],
rest: [alice, alicia, steve, stephanie],
},
);
diff --git a/static/shared/js/typeahead.js b/static/shared/js/typeahead.js
index 6c607c7a71a..cf04671cfe1 100644
--- a/static/shared/js/typeahead.js
+++ b/static/shared/js/typeahead.js
@@ -110,13 +110,14 @@ export function get_emoji_matcher(query) {
export function triage(query, objs, get_item) {
/*
- We split objs into three groups:
+ We split objs into four groups:
+ - entire string exact match
- match prefix exactly with `query`
- match prefix case-insensitively
- other
- Then we concat the first two groups into
+ Then we concat the first three groups into
`matches` and then call the rest `rest`.
*/
@@ -124,6 +125,7 @@ export function triage(query, objs, get_item) {
get_item = (x) => x;
}
+ const exactMatch = [];
const beginswithCaseSensitive = [];
const beginswithCaseInsensitive = [];
const noMatch = [];
@@ -131,17 +133,20 @@ export function triage(query, objs, get_item) {
for (const obj of objs) {
const item = get_item(obj);
+ const lowerItem = item.toLowerCase();
- if (item.startsWith(query)) {
+ if (lowerItem === lowerQuery) {
+ exactMatch.push(obj);
+ } else if (item.startsWith(query)) {
beginswithCaseSensitive.push(obj);
- } else if (item.toLowerCase().startsWith(lowerQuery)) {
+ } else if (lowerItem.startsWith(lowerQuery)) {
beginswithCaseInsensitive.push(obj);
} else {
noMatch.push(obj);
}
}
return {
- matches: beginswithCaseSensitive.concat(beginswithCaseInsensitive),
+ matches: exactMatch.concat(beginswithCaseSensitive.concat(beginswithCaseInsensitive)),
rest: noMatch,
};
}