diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c534381..7fbd945 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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" ]]' \ No newline at end of file + 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 " ]]' \ No newline at end of file diff --git a/README.md b/README.md index 56a3c97..65dcd6f 100644 --- a/README.md +++ b/README.md @@ -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!' +``` diff --git a/action.yml b/action.yml index 8318dcb..ab3e424 100644 --- a/action.yml +++ b/action.yml @@ -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 \ No newline at end of file diff --git a/index.js b/index.js index 6610aa2..23969ad 100644 --- a/index.js +++ b/index.js @@ -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);