Skip to content

Commit

Permalink
Merge pull request #901 from mouhamadalmounayar/regex-multiple-matches
Browse files Browse the repository at this point in the history
Regex multiple matches
  • Loading branch information
jmcook1186 authored Jul 12, 2024
2 parents 789cbf0 + 03275e3 commit ad1c825
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/if-run/builtins/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ describe('builtins/regex: ', () => {
expect(result).toStrictEqual(expectedResult);
});

it('successfully applies regex strategy with multiple matches', async () => {
const globalConfig = {
parameter: 'cloud/instance-type',
match: '/(?<=_)[^_]+?(?=_|$)/g',
output: 'cloud/instance-type',
};
const regex = Regex(globalConfig);

const expectedResult = [
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cloud/instance-type': 'DS1 v2',
},
];

const result = await regex.execute([
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cloud/instance-type': 'Standard_DS1_v2',
},
]);

expect(result).toStrictEqual(expectedResult);
});

it('returns a result when regex is not started and ended with ``.', async () => {
const physicalProcessor =
'Intel® Xeon® Platinum 8272CL,Intel® Xeon® 8171M 2.1 GHz,Intel® Xeon® E5-2673 v4 2.3 GHz,Intel® Xeon® E5-2673 v3 2.4 GHz';
Expand Down
3 changes: 2 additions & 1 deletion src/if-run/builtins/regex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Intel® Xeon® Platinum 8272CL,Intel® Xeon® 8171M 2.1 GHz,Intel® Xeon® E5-26

## Returns

- `output`: the first match of `parameter` with the parameter name with `match` defined in global config.
- `output`: The match of the `parameter` value using the `match` regex defined in the global config. If the `match` regex includes the global flag (`g`), a string containing all matches separated by spaces.


## Implementation

Expand Down
6 changes: 3 additions & 3 deletions src/if-run/builtins/regex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ export const Regex = (globalConfig: ConfigParams): ExecutePlugin => {
}

const regex = eval(match);
const matchedItem = input[parameter].match(regex);
const matchedItems = input[parameter].match(regex);

if (!matchedItem || !matchedItem[0]) {
if (!matchedItems || matchedItems.length === 0) {
throw new RegexMismatchError(REGEX_MISMATCH(input[parameter], match));
}

return matchedItem[0];
return matchedItems.join(' ');
};

return {
Expand Down

0 comments on commit ad1c825

Please sign in to comment.