Skip to content

Commit 877ee2a

Browse files
add utils regexes list
1 parent 7918c48 commit 877ee2a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import buildRegExp from "../../common/text/buildRegExp.mjs";
2+
import pipedream_utils from "../../pipedream_utils.app.mjs";
3+
export default {
4+
name: "Formatting - [Text] Extract by Regular Expressions List (Regex)",
5+
description: "Find matches for regular expressions. Returns all matched groups with start and end position.",
6+
key: "pipedream_utils-extract-by-regular-expressions-list",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
pipedream_utils,
11+
input: {
12+
type: "string",
13+
label: "Input",
14+
description: "Text you would like to find a pattern from",
15+
},
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`)",
20+
},
21+
},
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+
}
34+
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+
}));
49+
50+
resultMap[rStr] = matches;
51+
resultList.push(matches);
52+
}
53+
54+
return {
55+
resultMap,
56+
resultList,
57+
};
58+
},
59+
},
60+
async run({ $ }) {
61+
const {
62+
resultMap,
63+
resultList,
64+
} = this.getAllResults(this.input);
65+
66+
const totalMatches = resultList.reduce((sum, arr) => sum + arr.length, 0);
67+
68+
$.export(
69+
"$summary",
70+
totalMatches
71+
? `Found ${totalMatches} matches across ${Object.keys(resultMap).length} patterns`
72+
: "No matches found",
73+
);
74+
75+
return {
76+
map: resultMap,
77+
list: resultList,
78+
};
79+
},
80+
};

0 commit comments

Comments
 (0)