✨ Added >, <, >=, and <= operators to match helper (#14215)
refs https://github.com/TryGhost/Team/issues/1386 - The current match handler supports normal (in)equality operators, but no numeric comparisons (<, >, <=, >=) - A use case for these new operators is to show the latest post in a separate way from other posts Includes unit tests to check the new behaviour. Run via `yarn test test/unit/frontend/helpers/match.test.js`
This commit is contained in:
parent
5220083470
commit
e97abeceb5
@ -45,6 +45,18 @@ const handleMatch = (data, operator, value) => {
|
||||
case '!=':
|
||||
result = data !== value;
|
||||
break;
|
||||
case '<':
|
||||
result = data < value;
|
||||
break;
|
||||
case '>':
|
||||
result = data > value;
|
||||
break;
|
||||
case '>=':
|
||||
result = data >= value;
|
||||
break;
|
||||
case '<=':
|
||||
result = data <= value;
|
||||
break;
|
||||
default:
|
||||
result = data === value;
|
||||
}
|
||||
|
@ -204,6 +204,57 @@ describe('Match helper', function () {
|
||||
}, hash);
|
||||
});
|
||||
|
||||
describe('Explicit Greater Than', function () {
|
||||
runTests({
|
||||
// Number to Number comparisons
|
||||
'{{match zero ">" 1}}': 'false',
|
||||
'{{match one ">" 0}}': 'true',
|
||||
'{{match zero ">" 0}}': 'false'
|
||||
}, hash);
|
||||
});
|
||||
|
||||
describe('Explicit Less Than', function () {
|
||||
runTests({
|
||||
// Number to Number comparisons
|
||||
'{{match zero "<" 1}}': 'true',
|
||||
'{{match one "<" 0}}': 'false',
|
||||
'{{match zero "<" 0}}': 'false'
|
||||
}, hash);
|
||||
});
|
||||
|
||||
describe('Explicit Greater Than Or Equal To', function () {
|
||||
runTests({
|
||||
// Number to Number comparisons
|
||||
'{{match zero ">=" 1}}': 'false',
|
||||
'{{match one ">=" 0}}': 'true',
|
||||
'{{match zero ">=" 0}}': 'true',
|
||||
|
||||
// String to String comparisons
|
||||
'{{match string ">=" "Hello world"}}': 'true',
|
||||
'{{match "b" ">=" "a"}}': 'true',
|
||||
'{{match "b" ">=" "b"}}': 'true',
|
||||
'{{match "a" ">=" "b"}}': 'false',
|
||||
'{{match "a" ">=" "3"}}': 'true',
|
||||
|
||||
// String to Number comparisons
|
||||
'{{match "5" ">=" 3}}': 'true',
|
||||
'{{match "3" ">=" 3}}': 'true',
|
||||
'{{match "3" ">=" 5}}': 'false',
|
||||
|
||||
'{{match "hello" ">=" 5}}': 'false',
|
||||
'{{match 5 ">=" "hello"}}': 'false'
|
||||
}, hash);
|
||||
});
|
||||
|
||||
describe('Explicit Less Than Or Equal To', function () {
|
||||
runTests({
|
||||
// Number to Number comparisons
|
||||
'{{match zero "<=" 1}}': 'true',
|
||||
'{{match one "<=" 0}}': 'false',
|
||||
'{{match zero "<=" 0}}': 'true'
|
||||
}, 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
|
||||
|
Loading…
Reference in New Issue
Block a user