Skip to content

Commit

Permalink
fix(bruno-common): add ability to reference the same variable repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
leoferreiralima committed May 4, 2024
1 parent e2aa786 commit 9141bc9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
39 changes: 39 additions & 0 deletions packages/bruno-common/src/interpolate/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,43 @@ describe('interpolate - recursive', () => {

expect(result).toBe('{{recursion2}}');
});

it('should replace repetead placeholders with 1 level of recursion with values from the object', () => {
const inputString = '{{repetead}}';
const inputObject = {
repetead: '{{repetead2}} {{repetead2}}',
repetead2: 'repetead2'
};

const result = interpolate(inputString, inputObject);

expect(result).toBe(new Array(2).fill('repetead2').join(' '));
});

it('should replace repetead placeholders with 2 level of recursion with values from the object', () => {
const inputString = '{{repetead}}';
const inputObject = {
repetead: '{{repetead2}} {{repetead2}}',
repetead2: '{{repetead3}} {{repetead3}} {{repetead3}}',
repetead3: 'repetead3'
};

const result = interpolate(inputString, inputObject);

expect(result).toBe(new Array(6).fill('repetead3').join(' '));
});

it('should replace repetead placeholders with 3 level of recursion with values from the object', () => {
const inputString = '{{repetead}}';
const inputObject = {
repetead: '{{repetead2}} {{repetead2}}',
repetead2: '{{repetead3}} {{repetead3}} {{repetead3}}',
repetead3: '{{repetead4}} {{repetead4}} {{repetead4}} {{repetead4}}',
repetead4: 'repetead4'
};

const result = interpolate(inputString, inputObject);

expect(result).toBe(new Array(24).fill('repetead4').join(' '));
});
});
27 changes: 21 additions & 6 deletions packages/bruno-common/src/interpolate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,34 @@ const interpolate = (str: string, obj: Record<string, any>): string => {
return replace(str, flattenedObj);
};

const replace = (str: string, flattenedObj: Record<string, any>, matches: Set<string> = new Set<string>()): string => {
const replace = (
str: string,
flattenedObj: Record<string, any>,
visited = new Set<String>(),
results = new Map<string, string>()
): string => {
const patternRegex = /\{\{([^}]+)\}\}/g;

return str.replace(patternRegex, (match, placeholder) => {
const replacement = flattenedObj[placeholder];

if (patternRegex.test(replacement) && !matches.has(match)) {
matches.add(match);
return replace(replacement, flattenedObj, matches);
if (results.has(match)) {
return results.get(match);
}

matches.add(match);
return replacement !== undefined ? replacement : match;
if (patternRegex.test(replacement) && !visited.has(match)) {
visited.add(match);
const result = replace(replacement, flattenedObj, visited, results);
results.set(match, result);

return result;
}

visited.add(match);
const result = replacement !== undefined ? replacement : match;
results.set(match, result);

return result;
});
};

Expand Down

0 comments on commit 9141bc9

Please sign in to comment.