Repository: vanus-labs/vanus (go)

Issue:
[Good First Issues]: implement function "capitalize_word" - 8 points 
# Motivation
Vanus Functions are a series of pre-built functions worked within Vanus transformer. By choosing a function, users can specify the way that events will be changed.

It will be a good start if you decide to contribute to Vanus codes.

# Details

This function is used to capitalize all words in a string of an existed json element.

## Description

Function name: **capitalize_word**

Function args: 
- path: the json path of the string which needs to be capitalized.

Function description: **Capitalize all words in a string of an existed json element. The previous value of the json object will be replaced.**

## Example

For example, if users' original event looks like this:

```
{
    "specversion" : "1.0",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "subject": null,
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleothervalue" : 5,
    "datacontenttype" : "application/json",
    "data" : {
        "appinfoA" : "this is jay ding",
        "appinfoB" : 123,
        "appinfoC" : true
    }
}
```

After the function "capitalize_word":

```
"pipeline":[
    {"command":["capitalize_word","$.data.appinfoA"]}
]
```

The transformed event should look like:

```
{
    "specversion" : "1.0",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "subject": null,
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleothervalue" : 5,
    "datacontenttype" : "application/json",
    "data" : {
        "appinfoA" : "This Is Jay Ding",
        "appinfoB" : 123,
        "appinfoC" : true
    }
}
```

# How to implement

1. Fork the vanus repository.
2. Create a branch `function-capitalize-word`
3. Create `capitalize_word.go` under `internal/primitive/transform/action/strings`, and then implement the function.
4. Create `capitalize_word_test.go` under `internal/primitive/transform/action/strings` for unit test.
5. Register your function in `internal/primitive/transform/runtime/init.go`.
6. Submit your PR

# Contribution points

Each issue counts specific points based on its difficulty. Contributors earn the corresponding points if they solve that issue. Contribution points are used to describe the contributions contributors have made.  They can also be used to get rewards when there are community events.

This issue counts **8 points**.

# How to claim to solve the issue

If you want to implement this function, please leave a comment in this issue like:

> I'd like to implement this function, please assign this issue to me.

Vanus community will assign the issue to you on time.

Pull request description:
feat: add Function capitalize_word and unit tests
<!--

Thank you for contributing to Vanus!

PR Title Format: 

feat/fix/refactor/docs/...: what's changed

Note: see https://github.com/linkall-labs/vanus/blob/main/CONTRIBUTING.md to know details about title format
-->

### What problem does this PR solve? 

- implement function "capitalize_word"

<!--

Please create an issue first to describe the problem

There MUST be one line starting with "Issue Number:  ".

-->

Issue Number: close #448

### Problem Summary

- This function is used to capitalize all words in a string of an existed json element.

### What is changed and how does it 

- added function CapitalizeWord to - internal/primitive/transform/function/strings_functions.go
- create file with function - NewCapitalizeWordAction to internal/primitive/transform/action/strings/capitalize_word.go
- create file with test - TestCapitalizeWordAction internal/primitive/transform/action/strings/capitalize_word_test.go
- register function - NewCapitalizeWordAction in vanus/internal/primitive/transform/runtime/init.go

### Check List

<!-- At least one of them must be included. -->

#### Tests

- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No code


Patch:
diff --git a/internal/primitive/transform/action/strings/capitalize_word.go b/internal/primitive/transform/action/strings/capitalize_word.go
new file mode 100644
index 0000000..b8946d7
--- /dev/null
+++ b/internal/primitive/transform/action/strings/capitalize_word.go
@@ -0,0 +1,32 @@
+// Copyright 2023 Linkall Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package strings
+
+import (
+	"github.com/linkall-labs/vanus/internal/primitive/transform/action"
+	"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
+	"github.com/linkall-labs/vanus/internal/primitive/transform/function"
+)
+
+// NewCapitalizeWord ["capitalize_word", "key"].
+func NewCapitalizeWordAction() action.Action {
+	a := &action.SourceTargetSameAction{}
+	a.CommonAction = action.CommonAction{
+		ActionName: "CAPITALIZE_WORD",
+		FixedArgs:  []arg.TypeList{arg.EventList},
+		Fn:         function.CapitalizeWord,
+	}
+	return a
+}
diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go
index da8bd6f..e087bf9 100644
--- a/internal/primitive/transform/function/strings_functions.go
+++ b/internal/primitive/transform/function/strings_functions.go
@@ -17,6 +17,7 @@ package function
 import (
 	"fmt"
 	"strings"
+	"unicode"
 
 	"github.com/linkall-labs/vanus/internal/primitive/transform/common"
 )
@@ -118,3 +119,27 @@ var CapitalizeSentence = function{
 		return strings.ToUpper(string(value[0])) + value[1:], nil
 	},
 }
+
+var CapitalizeWord = function{
+	name:      "CAPITALIZE_WORD",
+	fixedArgs: []common.Type{common.String},
+	fn: func(args []interface{}) (interface{}, error) {
+		value, _ := args[0].(string)
+		if len(value) == 0 {
+			return value, nil
+		}
+		if len(value) == 1 {
+			return strings.ToUpper(string(value[0])), nil
+		}
+		capWords := make([]rune, 0, len([]rune(value)))
+		prev := ' '
+		for _, v := range value {
+			if v != ' ' && prev == ' ' {
+				v = unicode.ToUpper(v)
+			}
+			capWords = append(capWords, v)
+			prev = v
+		}
+		return string(capWords), nil
+	},
+}
diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go
index 6b3747e..ec19d0b 100644
--- a/internal/primitive/transform/runtime/init.go
+++ b/internal/primitive/transform/runtime/init.go
@@ -53,6 +53,7 @@ func init() {
 		strings.NewReplaceBetweenPositionsAction,
 		strings.NewCapitalizeSentenceAction,
 		strings.NewCheckCustomValuesAction,
+		strings.NewCapitalizeWordAction,
 		// condition
 		condition.NewConditionIfAction,
 		// array


Test patch:
diff --git a/internal/primitive/transform/action/strings/capitalize_word_test.go b/internal/primitive/transform/action/strings/capitalize_word_test.go
new file mode 100644
index 0000000..6c4ae36
--- /dev/null
+++ b/internal/primitive/transform/action/strings/capitalize_word_test.go
@@ -0,0 +1,89 @@
+// Copyright 2023 Linkall Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package strings_test
+
+import (
+	"testing"
+
+	cetest "github.com/cloudevents/sdk-go/v2/test"
+	"github.com/linkall-labs/vanus/internal/primitive/transform/action/strings"
+	"github.com/linkall-labs/vanus/internal/primitive/transform/context"
+	"github.com/linkall-labs/vanus/internal/primitive/transform/runtime"
+	. "github.com/smartystreets/goconvey/convey"
+)
+
+func TestCapitalizeWordAction(t *testing.T) {
+	funcName := strings.NewCapitalizeWordAction().Name()
+	Convey("test capitalize word: several words", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", "one two three")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "One Two Three")
+	})
+	Convey("test capitalize word: extra spaces, numbers, other symbols", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", " . one, two,   three q four 111 плюс минус  ")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, " . One, Two,   Three Q Four 111 Плюс Минус  ")
+	})
+	Convey("test capitalize word: empty string", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", "")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "")
+	})
+	Convey("test capitalize word: unicode ♬", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", "♬")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "♬")
+	})
+	Convey("test capitalize word: a", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", "a")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "A")
+	})
+}