1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00
ferret/pkg/stdlib/strings/concat.go
Tim Voronov 24d8eedd4c
Feature/doc markup (#543)
* Added release notes

* #509 fixedOCOD typo

* Updated values

* Updated comments

* Changed stdlib docs format

* Changed format of array in docs

* Use 'any' instead of 'value' in docs

* New format for optional params

* Updated docs for testing package

* Added namespace information
2020-08-07 21:49:29 -04:00

91 lines
1.9 KiB
Go

package strings
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// CONCAT concatenates one or more instances of String, or an Array.
// @param {String, repeated | String[]} src - The source string / array.
// @return {String} - A string value.
func Concat(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, core.MaxArgs)
if err != nil {
return values.EmptyString, err
}
argsCount := len(args)
res := values.EmptyString
if argsCount == 1 && args[0].Type() == types.Array {
arr := args[0].(*values.Array)
arr.ForEach(func(value core.Value, _ int) bool {
res = res.Concat(value)
return true
})
return res, nil
}
for _, str := range args {
res = res.Concat(str)
}
return res, nil
}
// CONCAT_SEPARATOR concatenates one or more instances of String, or an Array with a given separator.
// @param {String} separator - The separator string.
// @param {String, repeated | String[]} src - The source string / array.
// @return {String} - Concatenated string.
func ConcatWithSeparator(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, core.MaxArgs)
if err != nil {
return values.EmptyString, err
}
separator := args[0]
if separator.Type() != types.String {
separator = values.NewString(separator.String())
}
res := values.EmptyString
for idx, arg := range args[1:] {
if arg.Type() != types.Array {
if arg.Type() != types.None {
if idx > 0 {
res = res.Concat(separator)
}
res = res.Concat(arg)
}
} else {
arr := arg.(*values.Array)
arr.ForEach(func(value core.Value, idx int) bool {
if value.Type() != types.None {
if idx > 0 {
res = res.Concat(separator)
}
res = res.Concat(value)
}
return true
})
}
}
return res, nil
}