Skip to content

Commit

Permalink
replace functions optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalcek committed Dec 8, 2021
1 parent 478788d commit 5dca06d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ Asside of integated functions bafi contains additional common functions
- **b64dec** - decode from base64
- **b32enc** - oncode to base32
- **b32dec** - decode from base32
- **replaceAll** - {{replaceAll .Value "oldValue" "newValue"}} - replace all occurences of "oldValue" with "newValue" e.g. {{replaceAll "aaxbb" "x" "Z"}} -> "aaZbb"
- **replaceAllRegex** - {{replaceAllRegex .Value "regex" "newValue"}} - replace all occurences of "regex" with "newValue" e.g. {{replaceAllRegex "aaxbb" "[a-d]", "Z"}} -> "ZZxZZ"
- **replaceAll** - {{replaceAll "oldValue" "newValue" .Value}} - replace all occurences of "oldValue" with "newValue" e.g. {{replaceAll "x" "Z" "aaxbb"}} -> "aaZbb"
- **replaceAllRegex** - {{replaceAllRegex "regex" "newValue" .Value}} - replace all occurences of "regex" with "newValue" e.g. {{replaceAllRegex "[a-d]", "Z" "aaxbb"}} -> "ZZxZZ"
- **uuid** - generate UUID
- **regexMatch** - {{regexMatch pattern .Value1}} more about go [regex](https://gobyexample.com/regular-expressions)
- **contains** - check if string contains substring e.g. {{contains "aaxbb" "xb"}}
Expand Down
4 changes: 2 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ func base32decode(v string) string {
// newUUID returns UUID
func newUUID() string { return uuid.New().String() }

func replaceAll(src, old, new string) string {
func replaceAll(old, new, src string) string {
return strings.Replace(src, old, new, -1)
}

func replaceAllRegex(src string, regex string, new string) string {
func replaceAllRegex(regex, new, src string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllString(src, new)
}
Expand Down
4 changes: 2 additions & 2 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ func TestContains(t *testing.T) {
}

func TestReplaceAll(t *testing.T) {
if replaceAll("aaxbb", "x", "Z") != "aaZbb" {
if replaceAll("x", "Z", "aaxbb") != "aaZbb" {
t.Errorf("result: %v", replaceAll("aaxbb", "x", "Z"))
}
}

func TestReplaceAllRegex(t *testing.T) {
if replaceAllRegex("aaxbb", `[a-d]`, "Z") != "ZZxZZ" {
if replaceAllRegex("[a-d]", "Z", "aaxbb") != "ZZxZZ" {
t.Errorf("result: %v", replaceAllRegex("aaxbb", `[a-d]`, "Z"))
}
}
Expand Down

0 comments on commit 5dca06d

Please sign in to comment.