-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string join and title function (#127)
* Add string join and title function
- Loading branch information
1 parent
48faf3d
commit 3fe72df
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |