Skip to content

Commit

Permalink
Adds json flag
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
frabert committed Jun 16, 2021
1 parent a8b449d commit 110e444
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 63 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ jobs:
pattern: '[\w\d/]*v(\d+(?:\.\d+)*)'
replace-with: '$1'
- name: Check equality
run: '[[ "${{ steps.test.outputs.replaced }}" == "1.0.1" ]]'
run: '[[ "${{ steps.test.outputs.replaced }}" == "1.0.1" ]]'
test-json:
runs-on: ubuntu-latest
name: Test
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Test
uses: ./
id: test
with:
string: '_replace_underscores_'
pattern: '_'
replace-with: '" "'
flags: 'g'
json: true
- name: Check equality
run: '[[ "${{ steps.test.outputs.replaced }}" == " replace underscores " ]]'
78 changes: 41 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
# `replace-string` GitHub Action

Replaces strings with regular expressions.

## Inputs

### `pattern`

**Required** Regular expression to match.

### `string`

**Required** Input string.

### `replace-with`

**Required** String to use for replacement.

### `flags`

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.

## Outputs

### `replaced`

The replaced string.

## Example usage

```yaml
uses: frabert/replace-string-action@v1.2
with:
pattern: 'Hello, (\w+)!'
string: 'Hello, world!'
replace-with: 'I greet you, $1!'
```
# `replace-string` GitHub Action

Replaces strings with regular expressions.

## Inputs

### `pattern`

**Required** Regular expression to match.

### `string`

**Required** Input string.

### `replace-with`

**Required** String to use for replacement.

### `flags`

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.

### `json`

Boolean. Interprets `replace-with` as JSON data. Useful for when `replace-with` contains leading or trailing whitespace that would be trimmed away by GitHub.

## Outputs

### `replaced`

The replaced string.

## Example usage

```yaml
uses: frabert/replace-string-action@v1.2
with:
pattern: 'Hello, (\w+)!'
string: 'Hello, world!'
replace-with: 'I greet you, $1!'
```
51 changes: 27 additions & 24 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
name: 'Replace string'
author: 'Francesco Bertolaccini'
description: 'Use regular expressions to manipulate strings'
inputs:
pattern:
description: 'Regular expression pattern'
required: true
string:
description: 'The input string'
required: true
replace-with:
description: 'What to replace with'
required: true
flags:
description: 'Flags to use when matching'
required: false
outputs:
replaced:
description: 'The output string'
runs:
using: 'node12'
main: 'index.js'
branding:
icon: 'search'
name: 'Replace string'
author: 'Francesco Bertolaccini'
description: 'Use regular expressions to manipulate strings'
inputs:
pattern:
description: 'Regular expression pattern'
required: true
string:
description: 'The input string'
required: true
replace-with:
description: 'What to replace with'
required: true
flags:
description: 'Flags to use when matching'
required: false
json:
description: 'Interprets `replace-with` as JSON data. Useful for when `replace-with` contains leading or trailing whitespace that would be trimmed away by GitHub'
required: false
outputs:
replaced:
description: 'The output string'
runs:
using: 'node12'
main: 'index.js'
branding:
icon: 'search'
color: gray-dark
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ const core = require('@actions/core');
try {
const pattern = core.getInput('pattern');
const string = core.getInput('string');
const replaceWith = core.getInput('replace-with');
let replaceWith = core.getInput('replace-with');
const flags = core.getInput('flags');
const json = core.getInput('json');

if(json) {
replaceWith = JSON.parse(replaceWith);
}

const regex = new RegExp(pattern, flags);

Expand Down

0 comments on commit 110e444

Please sign in to comment.