Skip to content

Commit 4213b66

Browse files
committed
replace helper
1 parent 62ad2a1 commit 4213b66

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_template.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,55 @@ describe('formatNumber helper', () => {
175175
});
176176
});
177177

178+
describe('replace helper', () => {
179+
test('replaces all occurrences', () => {
180+
const url = 'https://elastic.co/{{replace value "replace-me" "with-me"}}';
181+
182+
expect(compile(url, { value: 'replace-me test replace-me' })).toMatchInlineSnapshot(
183+
`"https://elastic.co/with-me%20test%20with-me"`
184+
);
185+
});
186+
187+
test('can be used to remove a substring', () => {
188+
const url = 'https://elastic.co/{{replace value "Label:" ""}}';
189+
190+
expect(compile(url, { value: 'Label:Feature:Something' })).toMatchInlineSnapshot(
191+
`"https://elastic.co/Feature:Something"`
192+
);
193+
});
194+
195+
test('works if no matches', () => {
196+
const url = 'https://elastic.co/{{replace value "Label:" ""}}';
197+
198+
expect(compile(url, { value: 'No matches' })).toMatchInlineSnapshot(
199+
`"https://elastic.co/No%20matches"`
200+
);
201+
});
202+
203+
test('throws on incorrect args', () => {
204+
expect(() =>
205+
compile('https://elastic.co/{{replace value "Label:"}}', { value: 'No matches' })
206+
).toThrowErrorMatchingInlineSnapshot(
207+
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
208+
);
209+
expect(() =>
210+
compile('https://elastic.co/{{replace value "Label:" 4}}', { value: 'No matches' })
211+
).toThrowErrorMatchingInlineSnapshot(
212+
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
213+
);
214+
expect(() =>
215+
compile('https://elastic.co/{{replace value 4 ""}}', { value: 'No matches' })
216+
).toThrowErrorMatchingInlineSnapshot(
217+
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
218+
);
219+
expect(() =>
220+
compile('https://elastic.co/{{replace value}}', { value: 'No matches' })
221+
).toThrowErrorMatchingInlineSnapshot(
222+
`"[replace]: \\"searchString\\" and \\"valueString\\" parameters expected to be strings, but not a string or missing"`
223+
);
224+
});
225+
});
226+
178227
describe('basic string formatting helpers', () => {
179228
test('lowercase', () => {
180229
const compileUrl = (value: unknown) =>

x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_template.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ handlebars.registerHelper('mid', (rawValue: unknown, start: number, length: numb
103103
if (typeof length !== 'number') throw new Error('[left]: expected "length" to be a number');
104104
return String(rawValue).substr(start, length);
105105
});
106+
handlebars.registerHelper('replace', (...args) => {
107+
const [str, searchString, valueString] = args.slice(0, -1) as [string, string, string];
108+
if (typeof searchString !== 'string' || typeof valueString !== 'string')
109+
throw new Error(
110+
'[replace]: "searchString" and "valueString" parameters expected to be strings, but not a string or missing'
111+
);
112+
return String(str).split(searchString).join(valueString);
113+
});
106114

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

0 commit comments

Comments
 (0)