Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 1e459c6

Browse files
committed
add replace, allow to give additional limit parameter to split
1 parent 1d28f37 commit 1e459c6

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

pkg/builtin/functions.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ var (
7676
"contains?": containsRudiFunction,
7777

7878
"concat": functions.NewBuilder(concatFunction).WithDescription("concatenates items in a vector using a common glue string").Build(),
79-
"split": functions.NewBuilder(splitFunction).WithDescription("splits a string into a vector").Build(),
79+
"split": functions.NewBuilder(splitFunction, splitnFunction).WithDescription("splits a string into a vector").Build(),
8080
"has-prefix?": functions.NewBuilder(hasPrefixFunction).WithDescription("returns true if the given string has the prefix").Build(),
8181
"has-suffix?": functions.NewBuilder(hasSuffixFunction).WithDescription("returns true if the given string has the suffix").Build(),
8282
"trim-prefix": functions.NewBuilder(trimPrefixFunction).WithDescription("removes the prefix from the string, if it exists").Build(),
8383
"trim-suffix": functions.NewBuilder(trimSuffixFunction).WithDescription("removes the suffix from the string, if it exists").Build(),
8484
"to-lower": functions.NewBuilder(toLowerFunction).WithDescription("returns the lowercased version of the given string").Build(),
8585
"to-upper": functions.NewBuilder(toUpperFunction).WithDescription("returns the uppercased version of the given string").Build(),
8686
"trim": functions.NewBuilder(trimFunction).WithDescription("returns the given whitespace with leading/trailing whitespace removed").Build(),
87+
"replace": functions.NewBuilder(replaceAllFunction, replaceLimitFunction).WithDescription("returns a copy of a string with the a substring replaced by another").Build(),
8788
}
8889

8990
ListsFunctions = types.Functions{
@@ -132,6 +133,8 @@ var (
132133
EncodingFunctions = types.Functions{
133134
"to-base64": functions.NewBuilder(toBase64Function).WithDescription("apply base64 encoding to the given string").Build(),
134135
"from-base64": functions.NewBuilder(fromBase64Function).WithDescription("decode a base64 encoded string").Build(),
136+
"to-json": functions.NewBuilder(toJSONFunction).WithDescription("encode the given value using JSON").Build(),
137+
"from-json": functions.NewBuilder(fromJSONFunction).WithDescription("decode a JSON string").Build(),
135138
}
136139

137140
DateTimeFunctions = types.Functions{

pkg/builtin/strings.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ func splitFunction(sep string, source string) (any, error) {
5050
return result, nil
5151
}
5252

53+
func splitnFunction(sep string, source string, limit int64) (any, error) {
54+
parts := strings.SplitN(source, sep, int(limit))
55+
56+
// to []any
57+
result := make([]any, len(parts))
58+
for i, part := range parts {
59+
result[i] = part
60+
}
61+
62+
return result, nil
63+
}
64+
5365
func hasSuffixFunction(source string, suffix string) (any, error) {
5466
return strings.HasSuffix(source, suffix), nil
5567
}
@@ -77,3 +89,11 @@ func toUpperFunction(s string) (any, error) {
7789
func trimFunction(s string) (any, error) {
7890
return strings.TrimSpace(s), nil
7991
}
92+
93+
func replaceAllFunction(s, old, new string) (any, error) {
94+
return strings.ReplaceAll(s, old, new), nil
95+
}
96+
97+
func replaceLimitFunction(s, old, new string, limit int64) (any, error) {
98+
return strings.Replace(s, old, new, int(limit)), nil
99+
}

0 commit comments

Comments
 (0)