Skip to content

Commit 110e444

Browse files
committed
Adds json flag
This is useful in case the string to use as replacement contains leading or trailing whitespace. This should function as a workaround for #6. Thanks to @Simran-B for the suggestion!
1 parent a8b449d commit 110e444

4 files changed

Lines changed: 92 additions & 63 deletions

File tree

.github/workflows/test.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,21 @@ jobs:
1515
pattern: '[\w\d/]*v(\d+(?:\.\d+)*)'
1616
replace-with: '$1'
1717
- name: Check equality
18-
run: '[[ "${{ steps.test.outputs.replaced }}" == "1.0.1" ]]'
18+
run: '[[ "${{ steps.test.outputs.replaced }}" == "1.0.1" ]]'
19+
test-json:
20+
runs-on: ubuntu-latest
21+
name: Test
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v1
25+
- name: Test
26+
uses: ./
27+
id: test
28+
with:
29+
string: '_replace_underscores_'
30+
pattern: '_'
31+
replace-with: '" "'
32+
flags: 'g'
33+
json: true
34+
- name: Check equality
35+
run: '[[ "${{ steps.test.outputs.replaced }}" == " replace underscores " ]]'

README.md

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
# `replace-string` GitHub Action
2-
3-
Replaces strings with regular expressions.
4-
5-
## Inputs
6-
7-
### `pattern`
8-
9-
**Required** Regular expression to match.
10-
11-
### `string`
12-
13-
**Required** Input string.
14-
15-
### `replace-with`
16-
17-
**Required** String to use for replacement.
18-
19-
### `flags`
20-
21-
Flags to use when matching. Please refer to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) for more information.
22-
23-
## Outputs
24-
25-
### `replaced`
26-
27-
The replaced string.
28-
29-
## Example usage
30-
31-
```yaml
32-
uses: frabert/replace-string-action@v1.2
33-
with:
34-
pattern: 'Hello, (\w+)!'
35-
string: 'Hello, world!'
36-
replace-with: 'I greet you, $1!'
37-
```
1+
# `replace-string` GitHub Action
2+
3+
Replaces strings with regular expressions.
4+
5+
## Inputs
6+
7+
### `pattern`
8+
9+
**Required** Regular expression to match.
10+
11+
### `string`
12+
13+
**Required** Input string.
14+
15+
### `replace-with`
16+
17+
**Required** String to use for replacement.
18+
19+
### `flags`
20+
21+
Flags to use when matching. Please refer to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) for more information.
22+
23+
### `json`
24+
25+
Boolean. Interprets `replace-with` as JSON data. Useful for when `replace-with` contains leading or trailing whitespace that would be trimmed away by GitHub.
26+
27+
## Outputs
28+
29+
### `replaced`
30+
31+
The replaced string.
32+
33+
## Example usage
34+
35+
```yaml
36+
uses: frabert/replace-string-action@v1.2
37+
with:
38+
pattern: 'Hello, (\w+)!'
39+
string: 'Hello, world!'
40+
replace-with: 'I greet you, $1!'
41+
```

action.yml

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
name: 'Replace string'
2-
author: 'Francesco Bertolaccini'
3-
description: 'Use regular expressions to manipulate strings'
4-
inputs:
5-
pattern:
6-
description: 'Regular expression pattern'
7-
required: true
8-
string:
9-
description: 'The input string'
10-
required: true
11-
replace-with:
12-
description: 'What to replace with'
13-
required: true
14-
flags:
15-
description: 'Flags to use when matching'
16-
required: false
17-
outputs:
18-
replaced:
19-
description: 'The output string'
20-
runs:
21-
using: 'node12'
22-
main: 'index.js'
23-
branding:
24-
icon: 'search'
1+
name: 'Replace string'
2+
author: 'Francesco Bertolaccini'
3+
description: 'Use regular expressions to manipulate strings'
4+
inputs:
5+
pattern:
6+
description: 'Regular expression pattern'
7+
required: true
8+
string:
9+
description: 'The input string'
10+
required: true
11+
replace-with:
12+
description: 'What to replace with'
13+
required: true
14+
flags:
15+
description: 'Flags to use when matching'
16+
required: false
17+
json:
18+
description: 'Interprets `replace-with` as JSON data. Useful for when `replace-with` contains leading or trailing whitespace that would be trimmed away by GitHub'
19+
required: false
20+
outputs:
21+
replaced:
22+
description: 'The output string'
23+
runs:
24+
using: 'node12'
25+
main: 'index.js'
26+
branding:
27+
icon: 'search'
2528
color: gray-dark

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ const core = require('@actions/core');
33
try {
44
const pattern = core.getInput('pattern');
55
const string = core.getInput('string');
6-
const replaceWith = core.getInput('replace-with');
6+
let replaceWith = core.getInput('replace-with');
77
const flags = core.getInput('flags');
8+
const json = core.getInput('json');
9+
10+
if(json) {
11+
replaceWith = JSON.parse(replaceWith);
12+
}
813

914
const regex = new RegExp(pattern, flags);
1015

0 commit comments

Comments
 (0)