Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added "startsWith" check to the match helper #18411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ghost/core/core/frontend/helpers/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 32 additions & 0 deletions ghost/core/test/unit/frontend/helpers/match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down