From 5910e3b8af8a92e4f81d52c84bec1e40d9073be5 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Wed, 26 Oct 2016 05:21:32 -0700 Subject: [PATCH] =?UTF-8?q?Adds=20=E2=80=98tittle=E2=80=99=20built-in=20fu?= =?UTF-8?q?nction.=20(#9087)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tittle function returns a copy of the string with the first characters of all the words capitalized. --- config/interpolate_funcs.go | 14 +++++++++ config/interpolate_funcs_test.go | 30 +++++++++++++++++++ .../docs/configuration/interpolation.html.md | 2 ++ 3 files changed, 46 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index d66073ec6..4645d923f 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -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 + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 0b77af328..dbfa113f6 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -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{ diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index e000e53ad..42e28e173 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -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.