Skip to content

Commit

Permalink
Replace all occurrences
Browse files Browse the repository at this point in the history
Use replaceAll and require Node16 (which supports replaceAll)
  • Loading branch information
LarsenLP committed Jun 30, 2022
1 parent 4e9819d commit 3d36424
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run find-and-replace to remove refs/heads/
uses: mad9000/actions-find-and-replace-string@1
- name: Run find-and-replace to replace / with - (ref/heads/main -> ref-heads-main)
uses: LarsenLP/actions-find-and-replace-string@3
id: findandreplace
with:
source: ${{ github.ref }}
find: 'refs/heads/'
replace: ''
- name: Get the above output
run: echo "The replaced value is ${{ steps.findandreplace.outputs.value }}"
- name: Run find-and-replace to remove slashes
uses: mad9000/actions-find-and-replace-string@1
id: findandreplace2
with:
source: ${{ steps.findandreplace.outputs.value }}
find: '/'
replace: '-'
- name: Get the final output
run: echo "The replaced value is ${{ steps.findandreplace2.outputs.value }}"
- name: Get the above output
run: echo "The replaced value is ${{ steps.findandreplace.outputs.value }}"
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Find and replace strings

This action executes find-and-replace on a given string (hint: use `${{ github.ref }}` to get your branch name and apply this on it for use in another action).
This action executes find-and-replace all occurences on a given string.

## Inputs

Expand All @@ -10,29 +10,29 @@ This action executes find-and-replace on a given string (hint: use `${{ github.r

### `find`

**Required** The text you want to search for within the branch name (eg. `ref/heads/`)
**Required** The text you want to search for

### `replace`

**Required** The text you want to replace (eg. `head-`, ``, `root_`)
**Required** The text you want to replace with

## Outputs

### `value`

The new value containing the found-and-replaced string.
The new value containing the found-and-replaced string

### Example usage

```yaml
uses: mad9000/actions-find-and-replace-string@2
uses: LarsenLP/actions-find-and-replace-string@3
with:
source: ${{ github.ref }} # this translates to ref/heads/main on the main branch, but can be any arbitrary string
find: 'ref/heads/' # we want to remove ref/heads/ from source
replace: '' # and replace it with a blank string (ie. removing it)
find: '/' # we want to find /
replace: '-' # and replace it with -
```
This will output `main`.
This will output `ref-heads-main`.

Check out `.github/workflows/main.yml` for more examples

8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Find-and-replace strings'
description: 'Finds and replaces text on a string'
author: "Kishyr Ramdial"
name: 'Find and replace in strings'
description: 'Improves action by mad9000 by replacing all occurrences, and fixes andac-ozcan issue with replaceAll not supported in Node'
author: "Lars Peter Larsen"
branding:
color: purple
icon: copy
Expand All @@ -18,5 +18,5 @@ outputs:
value:
description: 'The new value after find-and-replace has been run'
runs:
using: 'node12'
using: 'node16'
main: 'index.js'
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ try {
const source = core.getInput('source')
const find = core.getInput('find')
const replace = core.getInput('replace')
const branchName = source.replace(find, replace)
core.setOutput('value', branchName)
const value = source.replaceAll(find, replace)
core.setOutput('value', value)
} catch (error) {
core.setFailed(error.message)
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "find-and-replace-string",
"version": "1.0.0",
"description": "Find-and-replace on a string",
"description": "Find and replace in strings",
"main": "index.js",
"author": "Kishyr Ramdial <[email protected]>",
"author": "Lars Peter Larsen",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
Expand Down

0 comments on commit 3d36424

Please sign in to comment.