Skip to content

Commit

Permalink
feat(templates): add decodeURIComponent helper (#19616)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman committed Dec 31, 2022
1 parent f2a8699 commit aecfcdb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/usage/templates.md
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions lib/util/template/index.spec.ts
Expand Up @@ -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');
});
});
});
1 change: 1 addition & 0 deletions lib/util/template/index.ts
Expand Up @@ -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)
Expand Down

0 comments on commit aecfcdb

Please sign in to comment.