Skip to content

Commit 8461239

Browse files
Regex utils improvement (#17047)
* use string[] based keys for configuration * use prop_${count} based configuration
1 parent b668e70 commit 8461239

File tree

2 files changed

+72
-57
lines changed

2 files changed

+72
-57
lines changed

components/pipedream_utils/actions/extract-by-regular-expressions-list/extract-by-regular-expressions-list.mjs

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,92 @@ export default {
44
name: "Formatting - [Text] Extract by Regular Expressions List (Regex)",
55
description: "Find matches for regular expressions. Returns all matched groups with start and end position.",
66
key: "pipedream_utils-extract-by-regular-expressions-list",
7-
version: "0.0.1",
7+
version: "0.1.0",
88
type: "action",
99
props: {
1010
pipedream_utils,
11-
input: {
11+
key_0: {
12+
type: "string",
13+
label: "Key",
14+
description: "The key where the extraction result for a regex will be stored",
15+
reloadProps: true,
16+
},
17+
input_0: {
1218
type: "string",
1319
label: "Input",
14-
description: "Text you would like to find a pattern from",
20+
description: "The text you would like to find a pattern from",
1521
},
16-
regExpStrings: {
17-
type: "string[]",
18-
label: "Regular Expressions",
19-
description: "An array of [regex strings](https://www.w3schools.com/js/js_regexp.asp) (e.g. `/foo/g`, `/bar/i`)",
22+
regex_0: {
23+
type: "string",
24+
label: "Regular Expression",
25+
description: "[Regular expression](https://www.w3schools.com/js/js_regexp.asp)",
2026
},
2127
},
22-
methods: {
23-
getAllResults(input) {
24-
const resultMap = {};
25-
const resultList = [];
26-
27-
for (const rStr of this.regExpStrings) {
28-
if (typeof rStr !== "string" || !rStr.length) {
29-
// still push an empty array to preserve order
30-
resultMap[rStr] = [];
31-
resultList.push([]);
32-
continue;
33-
}
28+
additionalProps() {
29+
const props = {};
30+
let count = 1;
3431

35-
const re = rStr.startsWith("/")
36-
? buildRegExp(rStr, [
37-
"g",
38-
])
39-
: new RegExp(rStr, "g");
40-
41-
const matches = [
42-
...input.matchAll(re),
43-
].map((m) => ({
44-
match: m[0],
45-
groups: m.groups ?? {},
46-
startPosition: m.index,
47-
endPosition: m.index + m[0].length,
48-
}));
32+
while (this[`key_${count}`]) {
33+
props[`key_${count}`] = {
34+
type: "string",
35+
label: "Another Key",
36+
description: "The key where the extraction result for a regex will be stored",
37+
};
38+
props[`input_${count}`] = {
39+
type: "string",
40+
label: "Input",
41+
description: "The text you would like to find a pattern from",
42+
};
43+
props[`regex_${count}`] = {
44+
type: "string",
45+
label: "Regular Expression",
46+
description: "[Regular expression](https://www.w3schools.com/js/js_regexp.asp)",
47+
};
48+
count++;
49+
}
4950

50-
resultMap[rStr] = matches;
51-
resultList.push(matches);
52-
}
51+
props[`key_${count}`] = {
52+
type: "string",
53+
label: "Another Key",
54+
description: "The key where the extraction result for a regex will be stored",
55+
optional: true,
56+
reloadProps: true,
57+
};
5358

54-
return {
55-
resultMap,
56-
resultList,
57-
};
59+
return props;
60+
},
61+
methods: {
62+
getRegExp(regExpStr) {
63+
return regExpStr.startsWith("/")
64+
? buildRegExp(regExpStr, [
65+
"g",
66+
])
67+
: regExpStr;
68+
},
69+
getResults(input, regExpStr) {
70+
return [
71+
...input.matchAll(this.getRegExp(regExpStr)),
72+
].map((match) => ({
73+
match: match[0],
74+
startPosition: match.index,
75+
endPosition: match.index + match[0].length,
76+
}));
5877
},
5978
},
60-
async run({ $ }) {
61-
const {
62-
resultMap,
63-
resultList,
64-
} = this.getAllResults(this.input);
79+
async run() {
80+
let count = 0;
81+
const resultMap = {};
6582

66-
const totalMatches = resultList.reduce((sum, arr) => sum + arr.length, 0);
83+
while (this[`key_${count}`]) {
84+
const input = this[`input_${count}`];
85+
const regExpStr = this[`regex_${count}`];
6786

68-
$.export(
69-
"$summary",
70-
totalMatches
71-
? `Found ${totalMatches} matches across ${Object.keys(resultMap).length} patterns`
72-
: "No matches found",
73-
);
87+
const result = this.getResults(input, regExpStr);
88+
resultMap[this[`key_${count}`]] = result;
7489

75-
return {
76-
map: resultMap,
77-
list: resultList,
78-
};
90+
count++;
91+
}
92+
93+
return resultMap;
7994
},
8095
};

components/pipedream_utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/pipedream_utils",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Pipedream Utils Components",
55
"main": "pipedream_utils.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)