Skip to content

Commit f55b480

Browse files
tenstadviceice
andauthored
feat: add decodeBase64 handlebars helper (renovatebot#36660)
Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
1 parent 7f6256e commit f55b480

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

docs/usage/templates.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ Returns `true` if a given string is a substring.
4545

4646
`{{#if (containsString depName 'python')}}Python{{else}}Other{{/if}}`
4747

48+
### decodeBase64
49+
50+
If you want to convert a Base64 value to a string, use the built-in function `decodeBase64` like this:
51+
52+
`{{{decodeBase64 body}}}`
53+
54+
In the example above `body` is the base64 encoded value you want to decode.
55+
4856
### decodeURIComponent
4957

5058
If you want to decode a percent-encoded string, use the built-in function `decodeURIComponent` like this:

lib/util/template/index.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,30 @@ describe('util/template/index', () => {
294294
});
295295
});
296296

297+
describe('base64 decoding', () => {
298+
it('decode values', () => {
299+
const output = template.compile(
300+
'{{{decodeBase64 "QGZzb3V6YS9wcmV0dGllcmQ="}}}',
301+
undefined as never,
302+
);
303+
expect(output).toBe('@fsouza/prettierd');
304+
});
305+
306+
it('handles null values gracefully', () => {
307+
const output = template.compile('{{{decodeBase64 packageName}}}', {
308+
packageName: null,
309+
});
310+
expect(output).toBe('');
311+
});
312+
313+
it('handles undefined values gracefully', () => {
314+
const output = template.compile('{{{decodeBase64 packageName}}}', {
315+
packageName: undefined,
316+
});
317+
expect(output).toBe('');
318+
});
319+
});
320+
297321
describe('equals', () => {
298322
it('equals', () => {
299323
const output = template.compile(

lib/util/template/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ handlebars.registerHelper('encodeBase64', (str: string) =>
1818
Buffer.from(str ?? '').toString('base64'),
1919
);
2020

21+
handlebars.registerHelper('decodeBase64', (str: string) =>
22+
Buffer.from(str ?? '', 'base64').toString(),
23+
);
24+
2125
handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
2226
JSON.stringify(JSON.parse(input), null, 2),
2327
);

0 commit comments

Comments
 (0)