Skip to content

Commit

Permalink
Add upper and lowercase expression functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Sep 15, 2022
1 parent 7f4de82 commit 4035fa1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/usage/expressions.md
Expand Up @@ -184,6 +184,12 @@ Syntax: `{substr {0} pos length}`

Takes the substring of the first argument starting at `pos` for `length`

### Upper, Lower

Syntax: `{upper val}`, `{lower val}`

Converts a string to all-upper or all-lower case

### Repeat

Syntax: `{repeat "string" {numtimes}}`
Expand Down
6 changes: 5 additions & 1 deletion pkg/expressions/stdlib/funcs.go
@@ -1,6 +1,8 @@
package stdlib

import . "rare/pkg/expressions" //lint:ignore ST1001 Legacy
import (
. "rare/pkg/expressions" //lint:ignore ST1001 Legacy
)

var StandardFunctions = map[string]KeyBuilderFunction{
"coalesce": KeyBuilderFunction(kfCoalesce),
Expand Down Expand Up @@ -52,6 +54,8 @@ var StandardFunctions = map[string]KeyBuilderFunction{
"format": KeyBuilderFunction(kfFormat),
"substr": KeyBuilderFunction(kfSubstr),
"select": KeyBuilderFunction(kfSelect),
"upper": KeyBuilderFunction(kfUpper),
"lower": KeyBuilderFunction(kfLower),

// Separation (Join)
"tab": kfJoin('\t'),
Expand Down
18 changes: 18 additions & 0 deletions pkg/expressions/stdlib/funcsStrings.go
Expand Up @@ -41,6 +41,24 @@ func kfSuffix(args []KeyBuilderStage) KeyBuilderStage {
})
}

func kfUpper(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return func(context KeyBuilderContext) string {
return strings.ToUpper(args[0](context))
}
}

func kfLower(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return func(context KeyBuilderContext) string {
return strings.ToLower(args[0](context))
}
}

// {substr {0} }
func kfSubstr(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 3 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/expressions/stdlib/funcsStrings_test.go
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUpperLower(t *testing.T) {
testExpression(t, mockContext("aBc"), "{upper {0}} {upper a b}", "ABC <ARGN>")
testExpression(t, mockContext("aBc"), "{lower {0}} {lower a b}", "abc <ARGN>")
}

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

0 comments on commit 4035fa1

Please sign in to comment.