Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/pipeline/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ describe('evalExpr', () => {
it('applies join filter', () => {
expect(evalExpr('item.tags | join(,)', { item: { tags: ['a', 'b', 'c'] } })).toBe('a,b,c');
});
it('applies replace filter', () => {
expect(evalExpr("item.x | replace('foo','bar')", { item: { x: 'foo baz' } })).toBe('bar baz');
});
it('preserves commas in the replace filter replacement value', () => {
expect(evalExpr("item.x | replace('a','x,y')", { item: { x: 'a' } })).toBe('x,y');
});
it('replaces all occurrences with the replace filter', () => {
expect(evalExpr("item.x | replace(' ','_')", { item: { x: 'a b c' } })).toBe('a_b_c');
});
it('passes non-string values through the replace filter unchanged', () => {
expect(evalExpr('item.x | replace(1,2)', { item: { x: 42 } })).toBe(42);
});
it('returns the value unchanged when replace has a single arg', () => {
expect(evalExpr("item.x | replace('a')", { item: { x: 'abc' } })).toBe('abc');
});
it('applies upper filter', () => {
expect(evalExpr('item.name | upper', { item: { name: 'hello' } })).toBe('HELLO');
});
Expand Down
12 changes: 10 additions & 2 deletions src/pipeline/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,16 @@ function applyFilter(filterExpr: string, value: unknown): unknown {
}
case 'replace': {
if (typeof value !== 'string') return value;
const parts = rawArgs?.split(',').map(s => s.trim().replace(/^['"]|['"]$/g, '')) ?? [];
return parts.length >= 2 ? value.replaceAll(parts[0], parts[1]) : value;
if (rawArgs == null) return value;
// Split on the FIRST comma only so the replacement value may contain commas,
// e.g. replace('a', 'x,y') -> 'x,y'. (A literal comma needle is unsupported by
// design: the comma is the argument separator.)
const ci = rawArgs.indexOf(',');
if (ci === -1) return value;
const strip = (s: string) => s.trim().replace(/^['"]|['"]$/g, '');
const oldVal = strip(rawArgs.slice(0, ci));
const newVal = strip(rawArgs.slice(ci + 1));
return value.replaceAll(oldVal, newVal);
}
case 'keys':
return value && typeof value === 'object' ? Object.keys(value) : value;
Expand Down
Loading