2018-09-21 20:36:33 -04:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 20:06:27 +03:00
|
|
|
"strings"
|
|
|
|
|
2018-09-21 20:36:33 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// TRIM returns the string value with whitespace stripped from the start and/or end.
|
|
|
|
// @param {String} str - The string.
|
|
|
|
// @param {String} chars - Overrides the characters that should be removed from the string. It defaults to \r\n \t.
|
|
|
|
// @return {String} - The string without chars on both sides.
|
2018-09-21 20:36:33 -04:00
|
|
|
func Trim(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
2018-09-25 18:06:45 -04:00
|
|
|
return values.NewString(strings.Trim(text, args[1].String())), nil
|
2018-09-21 20:36:33 -04:00
|
|
|
}
|
|
|
|
|
2018-09-25 18:06:45 -04:00
|
|
|
return values.NewString(strings.TrimSpace(text)), nil
|
2018-09-21 20:36:33 -04:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// LTRIM returns the string value with whitespace stripped from the start only.
|
|
|
|
// @param {String} str - The string.
|
|
|
|
// @param {String} chars - Overrides the characters that should be removed from the string. It defaults to \r\n \t.
|
|
|
|
// @return {String} - The string without chars at the left-hand side.
|
2018-09-21 20:36:33 -04:00
|
|
|
func LTrim(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
chars := " "
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
chars = args[1].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(strings.TrimLeft(text, chars)), nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// RTRIM returns the string value with whitespace stripped from the end only.
|
|
|
|
// @param {String} str - The string.
|
|
|
|
// @param {String} chars - Overrides the characters that should be removed from the string. It defaults to \r\n \t.
|
|
|
|
// @return {String} - The string without chars at the right-hand side.
|
2018-09-21 20:36:33 -04:00
|
|
|
func RTrim(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
chars := " "
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
chars = args[1].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(strings.TrimRight(text, chars)), nil
|
|
|
|
}
|