Skip to content

Commit

Permalink
replace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Oct 16, 2020
1 parent 62ad2a1 commit 4213b66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,55 @@ describe('formatNumber helper', () => {
});
});

describe('replace helper', () => {
test('replaces all occurrences', () => {
const url = 'https://elastic.co/{{replace value "replace-me" "with-me"}}';

expect(compile(url, { value: 'replace-me test replace-me' })).toMatchInlineSnapshot(
`"https://elastic.co/with-me%20test%20with-me"`
);
});

test('can be used to remove a substring', () => {
const url = 'https://elastic.co/{{replace value "Label:" ""}}';

expect(compile(url, { value: 'Label:Feature:Something' })).toMatchInlineSnapshot(
`"https://elastic.co/Feature:Something"`
);
});

test('works if no matches', () => {
const url = 'https://elastic.co/{{replace value "Label:" ""}}';

expect(compile(url, { value: 'No matches' })).toMatchInlineSnapshot(
`"https://elastic.co/No%20matches"`
);
});

test('throws on incorrect args', () => {
expect(() =>
compile('https://elastic.co/{{replace value "Label:"}}', { value: 'No matches' })
).toThrowErrorMatchingInlineSnapshot(
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
);
expect(() =>
compile('https://elastic.co/{{replace value "Label:" 4}}', { value: 'No matches' })
).toThrowErrorMatchingInlineSnapshot(
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
);
expect(() =>
compile('https://elastic.co/{{replace value 4 ""}}', { value: 'No matches' })
).toThrowErrorMatchingInlineSnapshot(
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
);
expect(() =>
compile('https://elastic.co/{{replace value}}', { value: 'No matches' })
).toThrowErrorMatchingInlineSnapshot(
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
);
});
});

describe('basic string formatting helpers', () => {
test('lowercase', () => {
const compileUrl = (value: unknown) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ handlebars.registerHelper('mid', (rawValue: unknown, start: number, length: numb
if (typeof length !== 'number') throw new Error('[left]: expected "length" to be a number');
return String(rawValue).substr(start, length);
});
handlebars.registerHelper('replace', (...args) => {
const [str, searchString, valueString] = args.slice(0, -1) as [string, string, string];
if (typeof searchString !== 'string' || typeof valueString !== 'string')
throw new Error(
'[replace]: "searchString" and "valueString" parameters expected to be strings, but not a string or missing'
);
return String(str).split(searchString).join(valueString);
});

export function compile(url: string, context: object): string {
const template = handlebars.compile(url, { strict: true, noEscape: true });
Expand Down

0 comments on commit 4213b66

Please sign in to comment.