feat(templates): add decodeURIComponent helper (#19616)

This commit is contained in:
William Boman 2022-12-31 10:25:06 +01:00 committed by GitHub
parent f2a8699277
commit aecfcdbb3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -49,6 +49,16 @@ In the example above `baseDir` is the string you want to transform into a valid
Read the [MDN Web Docs, encodeURIComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) to learn more.
### decodeURIComponent
If you want to decode a percent-encoded string, use the built-in function `decodeURIComponent` like this:
`{{{decodeURIComponent depName}}}`
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.
### replace
The `replace` helper replaces _all_ found strings with the replacement string.

View file

@ -158,4 +158,22 @@ describe('util/template/index', () => {
expect(template.containsTemplates('{{body}}', ['logJSON'])).toBeFalse();
});
});
describe('percent encoding', () => {
it('encodes values', () => {
const output = template.compile(
'{{{encodeURIComponent "@fsouza/prettierd"}}}',
undefined as never
);
expect(output).toBe('%40fsouza%2Fprettierd');
});
it('decodes values', () => {
const output = template.compile(
'{{{decodeURIComponent "%40fsouza/prettierd"}}}',
undefined as never
);
expect(output).toBe('@fsouza/prettierd');
});
});
});

View file

@ -4,6 +4,7 @@ import { GlobalConfig } from '../../config/global';
import { logger } from '../../logger';
handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('decodeURIComponent', decodeURIComponent);
handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
JSON.stringify(JSON.parse(input), null, 2)