Skip to content

Commit

Permalink
Add string join and title function (#127)
Browse files Browse the repository at this point in the history
* Add string join and title function
  • Loading branch information
lixingwang authored Aug 5, 2020
1 parent 48faf3d commit 3fe72df
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
34 changes: 34 additions & 0 deletions function/string/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,26 @@
"type": "int"
}
},
{
"name": "join",
"description": "Converts an array to its string representation and concatenates its elements using the specified separator between each element",
"example": "string.join(array.create(\"a\", \"b\"), \"-\") => a-b",
"args": [
{
"name": "items",
"type": "array",
"valueType": "any"
},
{
"name": "separator",
"type": "string"
}
],
"return": {
"type": "array",
"valueType": "any"
}
},
{
"name": "lastIndex",
"description": "LastIndex returns the index of the last instance of substring in inputstring, or -1 if substring is not present in inputstring. string.lastIndex(\"go gopher\", \"go\")",
Expand Down Expand Up @@ -367,6 +387,20 @@
"type": "string"
}
},
{
"name": "toTitleCase",
"description": "returns the string to capitalize the first letter of every word",
"example": "string.toTitleCase(\"hello world\") => Hello World",
"args": [
{
"name": "str",
"type": "string"
},
],
"return": {
"type": "string"
}
},
{
"name": "toLower",
"description": "toLower returns a copy of inputstring with all Unicode letters mapped to their lower case. ",
Expand Down
45 changes: 45 additions & 0 deletions function/string/join.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package string

import (
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
"strings"
)

func init() {
_ = function.Register(&fnJoin{})
}

type fnJoin struct {
}

func (fnJoin) Name() string {
return "join"
}

func (fnJoin) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeArray, data.TypeString}, false
}

func (fnJoin) Eval(params ...interface{}) (interface{}, error) {
a, err := coerce.ToArray(params[0])
if err != nil {
return nil, fmt.Errorf("error converting string.join first argument [%+v] to array: %s", params[0], err.Error())
}
str, err := coerce.ToString(params[1])
if err != nil {
return nil, fmt.Errorf("error converting string.join second argument [%+v] to string: %s", params[0], err.Error())
}
return strings.Join(toStringArray(a), str), nil
}

func toStringArray(array []interface{}) []string {
var tmpArray []string
for _, a := range array {
str, _ := coerce.ToString(a)
tmpArray = append(tmpArray, str)
}
return tmpArray
}
14 changes: 14 additions & 0 deletions function/string/join_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package string

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestFnJoin_Eval(t *testing.T) {
f := fnJoin{}
var a = []string{"abc", "dddd", "cccc"}
v, err := f.Eval(a, "-")
assert.Nil(t, err)
assert.Equal(t, "abc-dddd-cccc", v)
}
32 changes: 32 additions & 0 deletions function/string/title.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package string

import (
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
"strings"
)

func init() {
_ = function.Register(&fnTitle{})
}

type fnTitle struct {
}

func (fnTitle) Name() string {
return "toTitleCase"
}

func (fnTitle) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeString}, false
}

func (fnTitle) Eval(params ...interface{}) (interface{}, error) {
str, err := coerce.ToString(params[0])
if err != nil {
return nil, fmt.Errorf("error converting string.toTitleCase's argument [%+v] to string: %s", params[0], err.Error())
}
return strings.Title(str), nil
}
14 changes: 14 additions & 0 deletions function/string/title_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package string

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestFnTitle_Eval(t *testing.T) {
f := fnTitle{}
str := "hello world"
v, err := f.Eval(str)
assert.Nil(t, err)
assert.Equal(t, "Hello World", v)
}

0 comments on commit 3fe72df

Please sign in to comment.