feat: add string template filters "default" and "ternary" (#3677) (#5878)

This commit is contained in:
Kolja Markwardt
2021-10-11 17:11:06 +02:00
committed by GitHub
parent c2c7d8309e
commit c791158dd5
3 changed files with 31 additions and 1 deletions

View File

@ -130,6 +130,28 @@ describe('stringTemplate', () => {
),
).toBe('BACKENDSLUG-title-01-02-2020');
});
it('return apply filter for default value', () => {
expect(
compileStringTemplate(
"{{slug | upper}}-{{title | default('none')}}-{{subtitle | default('none')}}",
date,
'backendSlug',
fromJS({ slug: 'entrySlug', title: 'title', subtitle: null, published: date, date }),
),
).toBe('BACKENDSLUG-title-none');
});
it('return apply filter for ternary', () => {
expect(
compileStringTemplate(
"{{slug | upper}}-{{starred | ternary('star','nostar')}}-{{done | ternary('done', 'open')}}",
date,
'backendSlug',
fromJS({ slug: 'entrySlug', starred: true, done: false }),
),
).toBe('BACKENDSLUG-star-open');
});
});
describe('expandPath', () => {

View File

@ -13,6 +13,14 @@ const filters = [
pattern: /^date\('(.+)'\)$/,
transform: (str: string, match: RegExpMatchArray) => moment(str).format(match[1]),
},
{
pattern: /^default\('(.+)'\)$/,
transform: (str: string, match: RegExpMatchArray) => (str ? str : match[1]),
},
{
pattern: /^ternary\('(.*)',\s*'(.*)'\)$/,
transform: (str: string, match: RegExpMatchArray) => (str ? match[1] : match[2]),
},
];
const FIELD_PREFIX = 'fields.';