Skip to content

Commit 3fe72df

Browse files
authored
Add string join and title function (#127)
* Add string join and title function
1 parent 48faf3d commit 3fe72df

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

function/string/descriptor.json

+34
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,26 @@
283283
"type": "int"
284284
}
285285
},
286+
{
287+
"name": "join",
288+
"description": "Converts an array to its string representation and concatenates its elements using the specified separator between each element",
289+
"example": "string.join(array.create(\"a\", \"b\"), \"-\") => a-b",
290+
"args": [
291+
{
292+
"name": "items",
293+
"type": "array",
294+
"valueType": "any"
295+
},
296+
{
297+
"name": "separator",
298+
"type": "string"
299+
}
300+
],
301+
"return": {
302+
"type": "array",
303+
"valueType": "any"
304+
}
305+
},
286306
{
287307
"name": "lastIndex",
288308
"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\")",
@@ -367,6 +387,20 @@
367387
"type": "string"
368388
}
369389
},
390+
{
391+
"name": "toTitleCase",
392+
"description": "returns the string to capitalize the first letter of every word",
393+
"example": "string.toTitleCase(\"hello world\") => Hello World",
394+
"args": [
395+
{
396+
"name": "str",
397+
"type": "string"
398+
},
399+
],
400+
"return": {
401+
"type": "string"
402+
}
403+
},
370404
{
371405
"name": "toLower",
372406
"description": "toLower returns a copy of inputstring with all Unicode letters mapped to their lower case. ",

function/string/join.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package string
2+
3+
import (
4+
"fmt"
5+
"github.com/project-flogo/core/data"
6+
"github.com/project-flogo/core/data/coerce"
7+
"github.com/project-flogo/core/data/expression/function"
8+
"strings"
9+
)
10+
11+
func init() {
12+
_ = function.Register(&fnJoin{})
13+
}
14+
15+
type fnJoin struct {
16+
}
17+
18+
func (fnJoin) Name() string {
19+
return "join"
20+
}
21+
22+
func (fnJoin) Sig() (paramTypes []data.Type, isVariadic bool) {
23+
return []data.Type{data.TypeArray, data.TypeString}, false
24+
}
25+
26+
func (fnJoin) Eval(params ...interface{}) (interface{}, error) {
27+
a, err := coerce.ToArray(params[0])
28+
if err != nil {
29+
return nil, fmt.Errorf("error converting string.join first argument [%+v] to array: %s", params[0], err.Error())
30+
}
31+
str, err := coerce.ToString(params[1])
32+
if err != nil {
33+
return nil, fmt.Errorf("error converting string.join second argument [%+v] to string: %s", params[0], err.Error())
34+
}
35+
return strings.Join(toStringArray(a), str), nil
36+
}
37+
38+
func toStringArray(array []interface{}) []string {
39+
var tmpArray []string
40+
for _, a := range array {
41+
str, _ := coerce.ToString(a)
42+
tmpArray = append(tmpArray, str)
43+
}
44+
return tmpArray
45+
}

function/string/join_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package string
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestFnJoin_Eval(t *testing.T) {
9+
f := fnJoin{}
10+
var a = []string{"abc", "dddd", "cccc"}
11+
v, err := f.Eval(a, "-")
12+
assert.Nil(t, err)
13+
assert.Equal(t, "abc-dddd-cccc", v)
14+
}

function/string/title.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package string
2+
3+
import (
4+
"fmt"
5+
"github.com/project-flogo/core/data"
6+
"github.com/project-flogo/core/data/coerce"
7+
"github.com/project-flogo/core/data/expression/function"
8+
"strings"
9+
)
10+
11+
func init() {
12+
_ = function.Register(&fnTitle{})
13+
}
14+
15+
type fnTitle struct {
16+
}
17+
18+
func (fnTitle) Name() string {
19+
return "toTitleCase"
20+
}
21+
22+
func (fnTitle) Sig() (paramTypes []data.Type, isVariadic bool) {
23+
return []data.Type{data.TypeString}, false
24+
}
25+
26+
func (fnTitle) Eval(params ...interface{}) (interface{}, error) {
27+
str, err := coerce.ToString(params[0])
28+
if err != nil {
29+
return nil, fmt.Errorf("error converting string.toTitleCase's argument [%+v] to string: %s", params[0], err.Error())
30+
}
31+
return strings.Title(str), nil
32+
}

function/string/title_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package string
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestFnTitle_Eval(t *testing.T) {
9+
f := fnTitle{}
10+
str := "hello world"
11+
v, err := f.Eval(str)
12+
assert.Nil(t, err)
13+
assert.Equal(t, "Hello World", v)
14+
}

0 commit comments

Comments
 (0)