mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Remove splitN() and splitAfterN()
This commit is contained in:
+22
-18
@@ -164,30 +164,34 @@ var Builtins = []*Function{
|
||||
{
|
||||
Name: "split",
|
||||
Func: func(args ...interface{}) (interface{}, error) {
|
||||
return strings.Split(args[0].(string), args[1].(string)), nil
|
||||
if len(args) == 2 {
|
||||
return strings.Split(args[0].(string), args[1].(string)), nil
|
||||
} else if len(args) == 3 {
|
||||
return strings.SplitN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid number of arguments for split (expected 2 or 3, got %d)", len(args))
|
||||
}
|
||||
},
|
||||
Types: types(strings.Split),
|
||||
},
|
||||
{
|
||||
Name: "splitN",
|
||||
Func: func(args ...interface{}) (interface{}, error) {
|
||||
return strings.SplitN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
|
||||
},
|
||||
Types: types(strings.SplitN),
|
||||
Types: types(
|
||||
strings.Split,
|
||||
strings.SplitN,
|
||||
),
|
||||
},
|
||||
{
|
||||
Name: "splitAfter",
|
||||
Func: func(args ...interface{}) (interface{}, error) {
|
||||
return strings.SplitAfter(args[0].(string), args[1].(string)), nil
|
||||
if len(args) == 2 {
|
||||
return strings.SplitAfter(args[0].(string), args[1].(string)), nil
|
||||
} else if len(args) == 3 {
|
||||
return strings.SplitAfterN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid number of arguments for splitAfter (expected 2 or 3, got %d)", len(args))
|
||||
}
|
||||
},
|
||||
Types: types(strings.SplitAfter),
|
||||
},
|
||||
{
|
||||
Name: "splitAfterN",
|
||||
Func: func(args ...interface{}) (interface{}, error) {
|
||||
return strings.SplitAfterN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
|
||||
},
|
||||
Types: types(strings.SplitAfterN),
|
||||
Types: types(
|
||||
strings.SplitAfter,
|
||||
strings.SplitAfterN,
|
||||
),
|
||||
},
|
||||
{
|
||||
Name: "replace",
|
||||
|
||||
@@ -42,9 +42,9 @@ func TestBuiltin(t *testing.T) {
|
||||
{`upper("foo")`, "FOO"},
|
||||
{`lower("FOO")`, "foo"},
|
||||
{`split("foo,bar,baz", ",")`, []string{"foo", "bar", "baz"}},
|
||||
{`splitN("foo,bar,baz", ",", 2)`, []string{"foo", "bar,baz"}},
|
||||
{`split("foo,bar,baz", ",", 2)`, []string{"foo", "bar,baz"}},
|
||||
{`splitAfter("foo,bar,baz", ",")`, []string{"foo,", "bar,", "baz"}},
|
||||
{`splitAfterN("foo,bar,baz", ",", 2)`, []string{"foo,", "bar,baz"}},
|
||||
{`splitAfter("foo,bar,baz", ",", 2)`, []string{"foo,", "bar,baz"}},
|
||||
{`replace("foo,bar,baz", ",", ";")`, "foo;bar;baz"},
|
||||
{`replace("foo,bar,baz,goo", ",", ";", 2)`, "foo;bar;baz,goo"},
|
||||
{`repeat("foo", 3)`, "foofoofoo"},
|
||||
|
||||
@@ -303,36 +303,22 @@ Converts all the characters in string `v` to lowercase.
|
||||
lower("HELLO") == "hello"
|
||||
```
|
||||
|
||||
### split(v, delimiter)
|
||||
### split(v, delimiter[, n])
|
||||
|
||||
Splits the string `v` at each instance of the delimiter and returns an array of substrings.
|
||||
|
||||
```expr
|
||||
split("apple,orange,grape", ",") == ["apple", "orange", "grape"]
|
||||
split("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
|
||||
```
|
||||
|
||||
### splitN(v, delimiter, n)
|
||||
|
||||
Splits the string `v` at each instance of the delimiter but limits the result to `n` substrings.
|
||||
|
||||
```expr
|
||||
splitN("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
|
||||
```
|
||||
|
||||
### splitAfter(v, delimiter)
|
||||
### splitAfter(v, delimiter[, n])
|
||||
|
||||
Splits the string `v` after each instance of the delimiter.
|
||||
|
||||
```expr
|
||||
splitAfter("apple,orange,grape", ",") == ["apple,", "orange,", "grape"]
|
||||
```
|
||||
|
||||
### splitAfterN(v, delimiter, n)
|
||||
|
||||
Splits the string `v` after each instance of the delimiter but limits the result to `n` substrings.
|
||||
|
||||
```expr
|
||||
splitAfterN("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
|
||||
splitAfter("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
|
||||
```
|
||||
|
||||
### replace(v, old, new)
|
||||
@@ -351,12 +337,14 @@ Repeats the string `v` `n` times.
|
||||
repeat("Hi", 3) == "HiHiHi"
|
||||
```
|
||||
|
||||
### join(v, delimiter)
|
||||
### join(v[, delimiter])
|
||||
|
||||
Joins an array of strings `v` into a single string with the given delimiter.
|
||||
If no delimiter is given, an empty string is used.
|
||||
|
||||
```expr
|
||||
join(["apple", "orange", "grape"], ",") == "apple,orange,grape"
|
||||
join(["apple", "orange", "grape"]) == "appleorangegrape"
|
||||
```
|
||||
|
||||
### indexOf(v, substring)
|
||||
@@ -457,7 +445,7 @@ Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||
duration("1h").Seconds() == 3600
|
||||
```
|
||||
|
||||
### date(v[, format, timezone])
|
||||
### date(v[, format[, timezone]])
|
||||
|
||||
Converts the given value `v` into a date representation.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user