From 561c9d7368431061cc1c1ebdf2260c1e6c4c3f79 Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Fri, 1 Jun 2018 19:29:17 +0530 Subject: [PATCH] search: Add format_as_suggestion to search_suggestions.js. `format_as_suggestion` formats a list of operators into a suggestion using the Filter.describe and Filter.unparse methods. This change aims to increase readability. --- static/js/search_suggestion.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js index 93766a263c..7a0f63642a 100644 --- a/static/js/search_suggestion.js +++ b/static/js/search_suggestion.js @@ -52,6 +52,13 @@ function check_validity(last, operators, valid, invalid) { return true; } +function format_as_suggestion(terms) { + return { + description: Filter.describe(terms), + search_string: Filter.unparse(terms), + }; +} + function compare_by_huddle(huddle) { huddle = _.map(huddle.slice(0, -1), function (person) { person = people.get_by_email(person); @@ -242,9 +249,7 @@ function get_default_suggestion(operators) { if (operators.length === 0) { return {description: '', search_string: ''}; } - var search_string = Filter.unparse(operators); - var description = Filter.describe(operators); - return {description: description, search_string: search_string}; + return format_as_suggestion(operators); } function get_topic_suggestions(last, operators) { @@ -335,9 +340,7 @@ function get_topic_suggestions(last, operators) { return _.map(topics, function (topic) { var topic_term = {operator: 'topic', operand: topic, negated: negated}; var operators = suggest_operators.concat([topic_term]); - var search_string = Filter.unparse(operators); - var description = Filter.describe(operators); - return {description: description, search_string: search_string}; + return format_as_suggestion(operators); }); } @@ -354,10 +357,7 @@ function get_operator_subset_suggestions(operators) { for (i = operators.length - 1; i >= 1; i -= 1) { var subset = operators.slice(0, i); - var search_string = Filter.unparse(subset); - var description = Filter.describe(subset); - var suggestion = {description: description, search_string: search_string}; - suggestions.push(suggestion); + suggestions.push(format_as_suggestion(subset)); } return suggestions; @@ -536,9 +536,7 @@ function get_operator_suggestions(last) { return _.map(choices, function (choice) { var op = [{operator: choice, operand: '', negated: negated}]; - var search_string = Filter.unparse(op); - var description = Filter.describe(op); - return {description: description, search_string: search_string}; + return format_as_suggestion(op); }); }