Skip to content

Commit

Permalink
feat: adding includes helper for templating (#28148)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosjr97 authored Mar 27, 2024
1 parent 87bba9d commit e4020c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/usage/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ Returns `true` if at least one expression is `true`.

`{{#if (or isPatch isSingleVersion}}Small update, safer to merge and release.{{else}}Check out the changelog for all versions before merging!{{/if}}`

### includes

Returns `true` if the value is included on the list given.

`{{#if (includes labels 'dependencies')}}Production Dependencies{{else}}Not Production Dependencies{{/if}}`

## Environment variables

By default, you can only access a handful of basic environment variables like `HOME` or `PATH`.
Expand Down
46 changes: 46 additions & 0 deletions lib/util/template/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,50 @@ describe('util/template/index', () => {
expect(output).toBe('not equals');
});
});

describe('includes', () => {
it('includes is true', () => {
const output = template.compile(
'{{#if (includes labels "dependencies")}}production{{else}}notProduction{{/if}}',
{
labels: ['dependencies'],
},
);

expect(output).toBe('production');
});

it('includes is false', () => {
const output = template.compile(
'{{#if (includes labels "dependencies")}}production{{else}}notProduction{{/if}}',
{
labels: ['devDependencies'],
},
);

expect(output).toBe('notProduction');
});

it('includes with incorrect type first argument', () => {
const output = template.compile(
'{{#if (includes labels "dependencies")}}production{{else}}notProduction{{/if}}',
{
labels: 'devDependencies',
},
);

expect(output).toBe('notProduction');
});

it('includes with incorrect type second argument', () => {
const output = template.compile(
'{{#if (includes labels 555)}}production{{else}}notProduction{{/if}}',
{
labels: ['devDependencies'],
},
);

expect(output).toBe('notProduction');
});
});
});
8 changes: 8 additions & 0 deletions lib/util/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ handlebars.registerHelper('containsString', (str, subStr) =>

handlebars.registerHelper('equals', (arg1, arg2) => arg1 === arg2);

handlebars.registerHelper('includes', (arg1: string[], arg2: string) => {
if (is.array(arg1, is.string) && is.string(arg2)) {
return arg1.includes(arg2);
}

return false;
});

handlebars.registerHelper({
and(...args) {
// Need to remove the 'options', as last parameter
Expand Down

0 comments on commit e4020c1

Please sign in to comment.