Skip to content

Commit 502f06f

Browse files
authored
Add regex switch expression tool (#7314)
* Initial commit * Change test input name * Fix linting * Push * Fix linting * Fix linting * Fix linting * Fix tests * Update repository URLs to main branch * Refactor regex matching logic and add link for regex syntax to help * Test
1 parent c63b1bf commit 502f06f

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

tools/regex_switch/.shed.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
categories:
2+
- Text Manipulation
3+
description: Regex switch
4+
long_description: |
5+
Regex switch outputs a boolean from regex on element identifier.
6+
name: regex_switch
7+
owner: iuc
8+
remote_repository_url: https://github.com/galaxyproject/tools-iuc/tree/main/tools/regex_switch
9+
homepage_url: https://github.com/galaxyproject/tools-iuc/tree/main/tools/regex_switch
10+
type: unrestricted
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<tool name="Regex switch" id="regex_switch" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" tool_type="expression">
2+
<description>boolean from regex on element identifier</description>
3+
<macros>
4+
<token name="@TOOL_VERSION@">1.0.0</token>
5+
<token name="@VERSION_SUFFIX@">0</token>
6+
</macros>
7+
<expression type="ecma5.1"><![CDATA[{
8+
var name = $job.input.name;
9+
var pat = $job.pattern;
10+
var matched = false;
11+
12+
try {
13+
var re = new RegExp(pat);
14+
matched = re.test(name);
15+
} catch (e) {
16+
return { "__error_message": "Invalid regular expression: " + (e && e.message ? e.message : String(e)) };
17+
}
18+
19+
return { "matched": matched, "unmatched": !matched };
20+
}]]></expression>
21+
<inputs>
22+
<param name="input" type="data" format="data"
23+
label="Dataset" />
24+
<param name="pattern" sanitize="false" type="text" label="Regex pattern"
25+
help="Example: \.csv$">
26+
<sanitizer>
27+
<valid>
28+
<add preset="string.printable"/>
29+
</valid>
30+
</sanitizer>
31+
<validator type="empty_field" message="Please enter a regular expression."/>
32+
</param>
33+
</inputs>
34+
<outputs>
35+
<output type="boolean" name="matched" from="matched" label="Match result"/>
36+
<output type="boolean" name="unmatched" from="unmatched" label="Unmatched result"/>
37+
</outputs>
38+
<tests>
39+
<test expect_num_outputs="2">
40+
<param name="input" value="sample.csv" ftype="txt"/>
41+
<param name="pattern" value="\.csv$"/>
42+
<output name="matched" value_json="true"/>
43+
<output name="unmatched" value_json="false"/>
44+
</test>
45+
46+
<test expect_num_outputs="2">
47+
<param name="input" value="table.tsv" ftype="txt"/>
48+
<param name="pattern" value="\.csv$"/>
49+
<output name="matched" value_json="false"/>
50+
<output name="unmatched" value_json="true"/>
51+
</test>
52+
</tests>
53+
<help><![CDATA[
54+
**Regex switch**
55+
56+
Matches a regular expression against the name of the input file. It emits two booleans:
57+
`matched` (the match result) and `unmatched` (its negation). Connect either to a step's **Conditionally skip step?** input.
58+
59+
**Regex syntax reference:** https://262.ecma-international.org/16.0/index.html#sec-patterns
60+
]]></help>
61+
</tool>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
col1,col2
2+
1,2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
col1 col2
2+
1 2

0 commit comments

Comments
 (0)