From 3d08d00bddddef10f7e55057e7e7e7ae5e8f22aa Mon Sep 17 00:00:00 2001 From: Mohamed Ramrami Date: Sat, 30 Sep 2023 00:11:44 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20startsWith=20check=20to=20t?= =?UTF-8?q?he=20match=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue --- ghost/core/core/frontend/helpers/match.js | 3 ++ .../test/unit/frontend/helpers/match.test.js | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/ghost/core/core/frontend/helpers/match.js b/ghost/core/core/frontend/helpers/match.js index 37d2c45fc7..f99f4f1ea8 100644 --- a/ghost/core/core/frontend/helpers/match.js +++ b/ghost/core/core/frontend/helpers/match.js @@ -57,6 +57,9 @@ const handleMatch = (data, operator, value) => { case '<=': result = data <= value; break; + case 'startsWith': + result = _.isString(data) && _.isString(value) && data.startsWith(value); + break; default: result = data === value; } diff --git a/ghost/core/test/unit/frontend/helpers/match.test.js b/ghost/core/test/unit/frontend/helpers/match.test.js index 320d9e7df7..9cfb4a4155 100644 --- a/ghost/core/test/unit/frontend/helpers/match.test.js +++ b/ghost/core/test/unit/frontend/helpers/match.test.js @@ -246,6 +246,38 @@ describe('Match helper', function () { }, hash); }); + describe('Explicit Starts With', function () { + runTests({ + // Using string values + '{{match empty "startsWith" ""}}': 'true', + '{{match empty "startsWith" " "}}': 'false', + '{{match string "startsWith" "Hello"}}': 'true', + '{{match string "startsWith" "World"}}': 'false', + '{{match string_true "startsWith" "tr"}}': 'true', + '{{match string_false "startsWith" "tr"}}': 'false', + '{{match safestring_string_false "startsWith" "fa"}}': 'true', + '{{match safestring_string_true "startsWith" "fa"}}': 'false', + '{{match string_five "startsWith" "5"}}': 'true', + '{{match string_five "startsWith" "6"}}': 'false', + '{{match object.foo "startsWith" "fo"}}': 'true', + '{{match object.foo "startsWith" "ba"}}': 'false', + '{{match array.[0] "startsWith" "fo"}}': 'true', + '{{match array.[0] "startsWith" "ba"}}': 'false', + + // Using non-string values + '{{match zero "startsWith" 0}}': 'false', + '{{match zero "startsWith" "0"}}': 'false', + '{{match "1" "startsWith" one}}': 'false', + '{{match null "startsWith" "null"}}': 'false', + '{{match truthy_bool "startsWith" "tr"}}': 'false', + '{{match safestring_bool_false "startsWith" "fa"}}': 'false', + '{{match undefined "startsWith" "undefined"}}': 'false', + '{{match unknown "startsWith" "unknown" }}': 'false', + '{{match object "startsWith" "object" }}': 'false', + '{{match array "startsWith" "array" }}': 'false' + }, hash); + }); + // SafeStrings represent the original value as an object for example: // SafeString { string: true } vs SafeString { string: 'true' } // allows us to know if the original value was a boolean or a string