Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toLower and toUpper functions #306

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
* *`trim $string`*: Removes whitespace from both sides of `$string`.
* *`toLower $string`*: Replace capital letters in `$string` to lowercase.
* *`toUpper $string`*: Replace lowercase letters in `$string` to uppercase.
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise
* *`where $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items having that value.
* *`whereNot $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items **not** having that value.
Expand Down
12 changes: 12 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ func trim(s string) string {
return strings.TrimSpace(s)
}

// toLower return the string in lower case
func toLower(s string) string {
return strings.ToLower(s)
}

// toUpper return the string in upper case
func toUpper(s string) string {
return strings.ToUpper(s)
}

// when returns the trueValue when the condition is true and the falseValue otherwise
func when(condition bool, trueValue, falseValue interface{}) interface{} {
if condition {
Expand Down Expand Up @@ -447,6 +457,8 @@ func newTemplate(name string) *template.Template {
"trimPrefix": trimPrefix,
"trimSuffix": trimSuffix,
"trim": trim,
"toLower": toLower,
"toUpper": toUpper,
"when": when,
"where": where,
"whereNot": whereNot,
Expand Down
18 changes: 18 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,24 @@ func TestTrim(t *testing.T) {
}
}

func TestToLower(t *testing.T) {
const str = ".RaNd0m StrinG_"
const lowered = ".rand0m string_"
got := toLower(str)
if got != lowered {
t.Fatalf("Expected toLower(%s) to be '%s', got '%s'", str, lowered, got)
}
}

func TestToUpper(t *testing.T) {
const str = ".RaNd0m StrinG_"
const uppered = ".RAND0M STRING_"
got := toUpper(str)
if got != uppered {
t.Fatalf("Expected toUpper(%s) to be '%s', got '%s'", str, uppered, got)
}
}

func TestDict(t *testing.T) {
containers := []*RuntimeContainer{
&RuntimeContainer{
Expand Down