Added startsWith check to the match helper

no issue
This commit is contained in:
Mohamed Ramrami 2023-09-30 00:11:44 +01:00 committed by Ramrami Mohamed
parent a837cf0247
commit 3d08d00bdd
2 changed files with 35 additions and 0 deletions

View File

@ -57,6 +57,9 @@ const handleMatch = (data, operator, value) => {
case '<=': case '<=':
result = data <= value; result = data <= value;
break; break;
case 'startsWith':
result = _.isString(data) && _.isString(value) && data.startsWith(value);
break;
default: default:
result = data === value; result = data === value;
} }

View File

@ -246,6 +246,38 @@ describe('Match helper', function () {
}, hash); }, 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: // SafeStrings represent the original value as an object for example:
// SafeString { string: true } vs SafeString { string: 'true' } // SafeString { string: true } vs SafeString { string: 'true' }
// allows us to know if the original value was a boolean or a string // allows us to know if the original value was a boolean or a string