From cd765e2393842879f2004e95809331119a863a51 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 14 Oct 2021 12:02:50 +0100 Subject: [PATCH] Added implicit equals support to match helper refs: https://github.com/TryGhost/Team/issues/759 - This allows for {{match x y}} to work without having to supply an "=" sign explicitly --- core/frontend/helpers/match.js | 6 +++- test/unit/frontend/helpers/match.test.js | 35 ++++++++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/core/frontend/helpers/match.js b/core/frontend/helpers/match.js index c1ccddff8d..4585fbd56d 100644 --- a/core/frontend/helpers/match.js +++ b/core/frontend/helpers/match.js @@ -77,9 +77,13 @@ function match(...attrs) { }); if (attrs.length === 1) { - // If we only have one attribute, treat it as simple true/false (like the if helper) + // CASE: single attribute, treat it as simple true/false (like the if helper) result = handleConditional(attrs[0], options); + } else if (attrs.length === 2) { + // CASE: two attributes, assume the operator is "=" + result = handleMatch(attrs[0], '=', attrs[1]); } else if (attrs.length === 3) { + // CASE: three attributes, handle the match exactly result = handleMatch(attrs[0], attrs[1], attrs[2]); } else { logging.warn(tpl(messages.invalidAttribute)); diff --git a/test/unit/frontend/helpers/match.test.js b/test/unit/frontend/helpers/match.test.js index 98daa6b9fa..20a5ccb7fe 100644 --- a/test/unit/frontend/helpers/match.test.js +++ b/test/unit/frontend/helpers/match.test.js @@ -100,12 +100,35 @@ describe('Match helper', function () { }); // @TODO: Implement Implicit Equals - // describe('Implicit Equals', function () { - // runTests({ - // '{{match string "Hello world"}}': 'true', - // '{{match string "Hello world!"}}': 'false', - // }, hash); - // }); + describe('Implicit Equals', function () { + runTests({ + '{{match string "Hello world"}}': 'true', + '{{match string "Hello world!"}}': 'false', + '{{match string_true "true"}}': 'true', + '{{match string_true true}}': 'false', + '{{match string_false "false"}}': 'true', + '{{match string_false false}}': 'false', + '{{match safestring_string_true "true"}}': 'true', + '{{match safestring_string_true true}}': 'false', + '{{match safestring_string_false "false"}}': 'true', + '{{match safestring_string_false false}}': 'false', + '{{match safestring_bool_true "true"}}': 'false', + '{{match safestring_bool_true true}}': 'true', + '{{match safestring_bool_false "false"}}': 'false', + '{{match safestring_bool_false false}}': 'true', + '{{match truthy_bool true}}': 'true', + '{{match truthy_bool false}}': 'false', + '{{match falsy_bool false}}': 'true', + '{{match falsy_bool true}}': 'false', + '{{match one 1}}': 'true', + '{{match one "1"}}': 'false', + '{{match zero 0}}': 'true', + '{{match zero "0"}}': 'false', + + '{{match (title) "=" "The Title"}}': 'true', + '{{match (title) "=" "The Title!"}}': 'false' + }, hash); + }); describe('Explicit Equals', function () { runTests({