From 947ce2b79de22e0434e72234eed51a32d0f70cf3 Mon Sep 17 00:00:00 2001 From: Ganesh Pawar Date: Wed, 10 Mar 2021 18:54:50 +0530 Subject: [PATCH] minor: Check if typeahead query exists. When a user entered an invalid character (whitespace or characters not present in a name), the cleaned-up array, and hence the query, would be empty which resulted in an error. Fixes #17542 --- frontend_tests/node_tests/typeahead.js | 16 ++++++++++++++++ static/shared/js/typeahead.js | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/frontend_tests/node_tests/typeahead.js b/frontend_tests/node_tests/typeahead.js index 6cca3f9ce7..aa2163279f 100644 --- a/frontend_tests/node_tests/typeahead.js +++ b/frontend_tests/node_tests/typeahead.js @@ -111,6 +111,22 @@ run_test("triage", () => { rest: [alice, alicia, steve, stephanie], }, ); + + assert.deepEqual( + typeahead.triage(" ", names, (r) => r.name), + { + matches: [], + rest: [alice, alicia, joan, jo, steve, stephanie], + }, + ); + + assert.deepEqual( + typeahead.triage(";", names, (r) => r.name), + { + matches: [], + rest: [alice, alicia, joan, jo, steve, stephanie], + }, + ); }); run_test("sort_emojis th", () => { diff --git a/static/shared/js/typeahead.js b/static/shared/js/typeahead.js index cf04671cfe..8dc05f87f0 100644 --- a/static/shared/js/typeahead.js +++ b/static/shared/js/typeahead.js @@ -129,7 +129,7 @@ export function triage(query, objs, get_item) { const beginswithCaseSensitive = []; const beginswithCaseInsensitive = []; const noMatch = []; - const lowerQuery = query.toLowerCase(); + const lowerQuery = query ? query.toLowerCase() : ""; for (const obj of objs) { const item = get_item(obj);