Skip to content

Commit 0ea7a66

Browse files
use string[] based keys for configuration
1 parent b668e70 commit 0ea7a66

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

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

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,74 @@ 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: {
12-
type: "string",
13-
label: "Input",
14-
description: "Text you would like to find a pattern from",
11+
alert: {
12+
type: "alert",
13+
alertType: "info",
14+
content: "Start by defining the keys you want to extract. Each key will have its own input and regex configuration.\nMake sure to Refresh the fields at the bottom after changing a configuration.",
1515
},
16-
regExpStrings: {
16+
keys: {
1717
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`)",
18+
label: "Keys",
19+
description: "The list of keys to use for the result map",
20+
reloadProps: true,
2021
},
2122
},
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);
23+
additionalProps() {
24+
const props = {};
25+
for (const key of this.keys) {
26+
if (this.isKeyValid(key)) {
27+
props[`${key}Input`] = {
28+
type: "string",
29+
label: `Input for: ${key}`,
30+
description: `The text you would like to find a pattern from for **${key}**`,
31+
};
32+
props[`${key}RegExp`] = {
33+
type: "string",
34+
label: `Regular Expression for: ${key}`,
35+
description: `[Regular expression](https://www.w3schools.com/js/js_regexp.asp) to use for **${key}**`,
36+
};
5237
}
53-
54-
return {
55-
resultMap,
56-
resultList,
57-
};
38+
}
39+
return props;
40+
},
41+
methods: {
42+
isKeyValid(key) {
43+
return key?.trim().length;
44+
},
45+
getRegExp(regExpStr) {
46+
return regExpStr.startsWith("/")
47+
? buildRegExp(regExpStr, [
48+
"g",
49+
])
50+
: regExpStr;
51+
},
52+
getResults(input, regExpStr) {
53+
return [
54+
...input.matchAll(this.getRegExp(regExpStr)),
55+
].map((match) => ({
56+
match: match[0],
57+
startPosition: match.index,
58+
endPosition: match.index + match[0].length,
59+
}));
5860
},
5961
},
60-
async run({ $ }) {
61-
const {
62-
resultMap,
63-
resultList,
64-
} = this.getAllResults(this.input);
62+
async run() {
63+
const resultMap = {};
64+
65+
for (const key of this.keys) {
66+
if (!this.isKeyValid(key)) continue;
6567

66-
const totalMatches = resultList.reduce((sum, arr) => sum + arr.length, 0);
68+
const input = this[`${key}Input`];
69+
const regExpStr = this[`${key}RegExp`];
6770

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

75-
return {
76-
map: resultMap,
77-
list: resultList,
78-
};
75+
return resultMap;
7976
},
8077
};

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)