From 877ee2ad77de6a44d66ca99a684af430cf477d68 Mon Sep 17 00:00:00 2001 From: Andrew Chuang Date: Wed, 21 May 2025 10:44:59 -0300 Subject: [PATCH 1/2] add utils regexes list --- .../extract-by-regular-expressions-list.mjs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 components/pipedream_utils/actions/extract-by-regular-expressions-list/extract-by-regular-expressions-list.mjs diff --git a/components/pipedream_utils/actions/extract-by-regular-expressions-list/extract-by-regular-expressions-list.mjs b/components/pipedream_utils/actions/extract-by-regular-expressions-list/extract-by-regular-expressions-list.mjs new file mode 100644 index 0000000000000..84fcf8dbe2319 --- /dev/null +++ b/components/pipedream_utils/actions/extract-by-regular-expressions-list/extract-by-regular-expressions-list.mjs @@ -0,0 +1,80 @@ +import buildRegExp from "../../common/text/buildRegExp.mjs"; +import pipedream_utils from "../../pipedream_utils.app.mjs"; +export default { + name: "Formatting - [Text] Extract by Regular Expressions List (Regex)", + description: "Find matches for regular expressions. Returns all matched groups with start and end position.", + key: "pipedream_utils-extract-by-regular-expressions-list", + version: "0.0.1", + type: "action", + props: { + pipedream_utils, + input: { + type: "string", + label: "Input", + description: "Text you would like to find a pattern from", + }, + regExpStrings: { + type: "string[]", + label: "Regular Expressions", + description: "An array of [regex strings](https://www.w3schools.com/js/js_regexp.asp) (e.g. `/foo/g`, `/bar/i`)", + }, + }, + methods: { + getAllResults(input) { + const resultMap = {}; + const resultList = []; + + for (const rStr of this.regExpStrings) { + if (typeof rStr !== "string" || !rStr.length) { + // still push an empty array to preserve order + resultMap[rStr] = []; + resultList.push([]); + continue; + } + + const re = rStr.startsWith("/") + ? buildRegExp(rStr, [ + "g", + ]) + : new RegExp(rStr, "g"); + + const matches = [ + ...input.matchAll(re), + ].map((m) => ({ + match: m[0], + groups: m.groups ?? {}, + startPosition: m.index, + endPosition: m.index + m[0].length, + })); + + resultMap[rStr] = matches; + resultList.push(matches); + } + + return { + resultMap, + resultList, + }; + }, + }, + async run({ $ }) { + const { + resultMap, + resultList, + } = this.getAllResults(this.input); + + const totalMatches = resultList.reduce((sum, arr) => sum + arr.length, 0); + + $.export( + "$summary", + totalMatches + ? `Found ${totalMatches} matches across ${Object.keys(resultMap).length} patterns` + : "No matches found", + ); + + return { + map: resultMap, + list: resultList, + }; + }, +}; From 4c389cca0251fa3c3674fbbb4ed12dbdb8c74c4d Mon Sep 17 00:00:00 2001 From: Andrew Chuang Date: Wed, 21 May 2025 10:49:05 -0300 Subject: [PATCH 2/2] bump package.json --- components/pipedream_utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/pipedream_utils/package.json b/components/pipedream_utils/package.json index 531223cfc1927..a18914bc07f7b 100644 --- a/components/pipedream_utils/package.json +++ b/components/pipedream_utils/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/pipedream_utils", - "version": "0.0.3", + "version": "0.0.4", "description": "Pipedream Utils Components", "main": "pipedream_utils.app.mjs", "keywords": [