Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/usage/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ Syntax: `{repeat "string" {numtimes}}`

Repeats the "string" the specified number of times

#### Replace

Syntax: `{replace {0} old new}`

Replaces each occurrence of `old` string with `new` in the first argument.

#### Humanize Number (Add Commas)

Syntax: `{hf val}`, `{hi val}`
Expand Down
19 changes: 10 additions & 9 deletions pkg/expressions/stdlib/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ var StandardFunctions = map[string]KeyBuilderFunction{
"or": KeyBuilderFunction(kfOr),

// Strings
"len": KeyBuilderFunction(kfLen),
"like": KeyBuilderFunction(kfLike),
"prefix": KeyBuilderFunction(kfPrefix),
"suffix": KeyBuilderFunction(kfSuffix),
"format": KeyBuilderFunction(kfFormat),
"substr": KeyBuilderFunction(kfSubstr),
"select": KeyBuilderFunction(kfSelect),
"upper": KeyBuilderFunction(kfUpper),
"lower": KeyBuilderFunction(kfLower),
"len": KeyBuilderFunction(kfLen),
"like": KeyBuilderFunction(kfLike),
"prefix": KeyBuilderFunction(kfPrefix),
"suffix": KeyBuilderFunction(kfSuffix),
"format": KeyBuilderFunction(kfFormat),
"substr": KeyBuilderFunction(kfSubstr),
"select": KeyBuilderFunction(kfSelect),
"upper": KeyBuilderFunction(kfUpper),
"lower": KeyBuilderFunction(kfLower),
"replace": KeyBuilderFunction(kfReplace),

// Separation (Join)
"tab": kfJoin('\t'),
Expand Down
2 changes: 1 addition & 1 deletion pkg/expressions/stdlib/funcsLookups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestLoadFile(t *testing.T) {
testExpression(t, mockContext(), "{load ../../../cmd/testdata/graph.txt}", "bob 22\njack 93\njill 3\nmaria 19")
testExpression(t, mockContext(), "{replace {load ../../../cmd/testdata/graph.txt} \"\\r\\n\" \"\\n\"}", "bob 22\njack 93\njill 3\nmaria 19")
testExpressionErr(t, mockContext(), "{load {0}}", "<CONST>", ErrConst)
testExpressionErr(t, mockContext(), "{load a b}", "<ARGN>", ErrArgCount)
testExpressionErr(t, mockContext(), "{load notarealfile.txt}", "<FILE>", ErrFile)
Expand Down
13 changes: 13 additions & 0 deletions pkg/expressions/stdlib/funcsStrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,16 @@ func kfJoin(delim rune) KeyBuilderFunction {
}), nil
}
}

func kfReplace(args []KeyBuilderStage) (KeyBuilderStage, error) {
if len(args) != 3 {
return stageErrArgCount(args, 3)
}

return func(context KeyBuilderContext) string {
s := args[0](context)
old := args[1](context)
new := args[2](context)
return strings.ReplaceAll(s, old, new)
}, nil
}
6 changes: 6 additions & 0 deletions pkg/expressions/stdlib/funcsStrings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestUpperLower(t *testing.T) {
testExpressionErr(t, mockContext("aBc"), "{lower {0}} {lower a b}", "abc <ARGN>", ErrArgCount)
}

func TestReplace(t *testing.T) {
testExpression(t, mockContext(), "{replace abc b q}", "aqc")
testExpression(t, mockContext("abc\r\nefg"), "{replace {0} \"\\r\\n\" \"\\n\"}", "abc\nefg")
testExpressionErr(t, mockContext(), "{replace a} {replace a b} {replace a b c d}", "<ARGN> <ARGN> <ARGN>", ErrArgCount)
}

func TestSubstring(t *testing.T) {
testExpression(t,
mockContext("abcd"),
Expand Down
Loading