Skip to content

Commit

Permalink
Adds ‘tittle’ built-in function. (hashicorp#9087)
Browse files Browse the repository at this point in the history
The tittle function returns a copy of the string with the first characters of all the words capitalized.
  • Loading branch information
gusmat authored and stack72 committed Oct 26, 2016
1 parent 4b6e314 commit 5910e3b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func Funcs() map[string]ast.Function {
"signum": interpolationFuncSignum(),
"sort": interpolationFuncSort(),
"split": interpolationFuncSplit(),
"title": interpolationFuncTitle(),
"trimspace": interpolationFuncTrimSpace(),
"upper": interpolationFuncUpper(),
}
Expand Down Expand Up @@ -996,3 +997,16 @@ func interpolationFuncUUID() ast.Function {
},
}
}

// interpolationFuncTitle implements the "title" function that returns a copy of the
// string in which first characters of all the words are capitalized.
func interpolationFuncTitle() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
toTitle := args[0].(string)
return strings.Title(toTitle), nil
},
}
}
30 changes: 30 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,36 @@ func TestInterpolateFuncSha256(t *testing.T) {
})
}

func TestInterpolateFuncTitle(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${title("hello")}`,
"Hello",
false,
},

{
`${title("hello world")}`,
"Hello World",
false,
},

{
`${title("")}`,
"",
false,
},

{
`${title()}`,
nil,
true,
},
},
})
}

func TestInterpolateFuncTrimSpace(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ The supported built-in functions are:
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)`

* `title(string)` - Returns a copy of the string with the first characters of all the words capitalized.

* `trimspace(string)` - Returns a copy of the string with all leading and trailing white spaces removed.

* `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case.
Expand Down

0 comments on commit 5910e3b

Please sign in to comment.