Adds ‘tittle’ built-in function. (#9087)

The tittle function returns a copy of the string with the first characters of all the words capitalized.
This commit is contained in:
Gustavo 2016-10-26 05:21:32 -07:00 committed by Paul Stack
parent 4b6e3141c9
commit 5910e3b8af
3 changed files with 46 additions and 0 deletions

View File

@ -81,6 +81,7 @@ func Funcs() map[string]ast.Function {
"signum": interpolationFuncSignum(),
"sort": interpolationFuncSort(),
"split": interpolationFuncSplit(),
"title": interpolationFuncTitle(),
"trimspace": interpolationFuncTrimSpace(),
"upper": interpolationFuncUpper(),
}
@ -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
},
}
}

View File

@ -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{

View File

@ -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.