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:
Simon Backx 2022-03-03 15:43:47 +01:00 committed by GitHub
parent 5220083470
commit e97abeceb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -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;
}

View File

@ -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