From f4db89c7e90bd410104b7186aa87575ef2c8a148 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 11 Feb 2014 14:45:54 -0500 Subject: [PATCH] Supported negated terms in filter predicates (JS). (imported from commit b50527620b5b451b4d6e0dc073de4036fe2b7e1f) --- static/js/filter.js | 8 +++++++- zerver/tests/frontend/node/filter.js | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/static/js/filter.js b/static/js/filter.js index f8bbb7a397..80c924d3fa 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -131,6 +131,7 @@ Filter.canonicalize_operator = function (operator) { }; Filter.canonicalize_term = function (opts) { + var negated = opts.negated; var operator = opts.operator; var operand = opts.operand; @@ -169,6 +170,7 @@ Filter.canonicalize_term = function (opts) { // We may want to consider allowing mixed-case operators at some point return { + negated: negated, operator: operator, operand: operand }; @@ -327,7 +329,11 @@ Filter.prototype = { return function (message) { return _.all(operators, function (term) { - return message_matches_search_term(message, term.operator, term.operand); + var ok = message_matches_search_term(message, term.operator, term.operand); + if (term.negated) { + ok = !ok; + } + return ok; }); }; } diff --git a/zerver/tests/frontend/node/filter.js b/zerver/tests/frontend/node/filter.js index 04eda182ad..d7aad0b5d8 100644 --- a/zerver/tests/frontend/node/filter.js +++ b/zerver/tests/frontend/node/filter.js @@ -184,6 +184,17 @@ function get_predicate(operators) { assert(!predicate({type: 'private', reply_to: 'steve@foo.com'})); }()); +(function test_negated_predicates() { + var predicate; + var narrow; + + narrow = [ + {operator: 'stream', operand: 'social', negated: true} + ]; + predicate = new Filter(narrow).predicate(); + assert(predicate({type: 'stream', stream: 'devel'})); + assert(!predicate({type: 'stream', stream: 'social'})); +}()); (function test_mit_exceptions() { global.page_params.domain = 'mit.edu';