Skip to content
Snippets Groups Projects
Unverified Commit 24386169 authored by Roman's avatar Roman Committed by GitHub
Browse files

feat(templating): adds encodeBase64 handlebars helper (#26197)

parent c1b47395
No related branches found
Tags 37.90.0
No related merge requests found
...@@ -59,6 +59,14 @@ In the example above `depName` is the string you want to decode. ...@@ -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. 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 ### replace
The `replace` helper replaces _all_ found strings matching the given regex with the replacement string. The `replace` helper replaces _all_ found strings matching the given regex with the replacement string.
......
...@@ -219,6 +219,30 @@ describe('util/template/index', () => { ...@@ -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', () => { describe('equals', () => {
it('equals', () => { it('equals', () => {
const output = template.compile( const output = template.compile(
......
...@@ -8,6 +8,10 @@ import { regEx } from '../regex'; ...@@ -8,6 +8,10 @@ import { regEx } from '../regex';
handlebars.registerHelper('encodeURIComponent', encodeURIComponent); handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('decodeURIComponent', decodeURIComponent); handlebars.registerHelper('decodeURIComponent', decodeURIComponent);
handlebars.registerHelper('encodeBase64', (str: string) =>
Buffer.from(str ?? '').toString('base64'),
);
handlebars.registerHelper('stringToPrettyJSON', (input: string): string => handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
JSON.stringify(JSON.parse(input), null, 2), JSON.stringify(JSON.parse(input), null, 2),
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment