Skip to content

Commit

Permalink
feat(templating): adds encodeBase64 handlebars helper (renovatebot#26197
Browse files Browse the repository at this point in the history
)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
3 people authored and zT-1337 committed Jan 24, 2024
1 parent 65a5594 commit 0d065a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/usage/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ In the example above `depName` is the string you want to decode.

Read the [MDN Web Docs, decodeURIComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) to learn more.

### encodeBase64

If you want to convert a string to Base64, use the built-in function `encodeBase64` like this:

`{{{encodeBase64 body}}}`

In the example above `body` is the string you want to transform into a Base64-encoded value.

### replace

The `replace` helper replaces _all_ found strings matching the given regex with the replacement string.
Expand Down
24 changes: 24 additions & 0 deletions lib/util/template/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ describe('util/template/index', () => {
});
});

describe('base64 encoding', () => {
it('encodes values', () => {
const output = template.compile(
'{{{encodeBase64 "@fsouza/prettierd"}}}',
undefined as never,
);
expect(output).toBe('QGZzb3V6YS9wcmV0dGllcmQ=');
});

it('handles null values gracefully', () => {
const output = template.compile('{{{encodeBase64 packageName}}}', {
packageName: null,
});
expect(output).toBe('');
});

it('handles undefined values gracefully', () => {
const output = template.compile('{{{encodeBase64 packageName}}}', {
packageName: undefined,
});
expect(output).toBe('');
});
});

describe('equals', () => {
it('equals', () => {
const output = template.compile(
Expand Down
4 changes: 4 additions & 0 deletions lib/util/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { regEx } from '../regex';
handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('decodeURIComponent', decodeURIComponent);

handlebars.registerHelper('encodeBase64', (str: string) =>
Buffer.from(str ?? '').toString('base64'),
);

handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
JSON.stringify(JSON.parse(input), null, 2),
);
Expand Down

0 comments on commit 0d065a4

Please sign in to comment.