Skip to content

Commit 887c98e

Browse files
committed
Remove splitN() and splitAfterN()
1 parent 5597289 commit 887c98e

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

builtin/builtin.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,30 +164,34 @@ var Builtins = []*Function{
164164
{
165165
Name: "split",
166166
Func: func(args ...interface{}) (interface{}, error) {
167-
return strings.Split(args[0].(string), args[1].(string)), nil
168-
},
169-
Types: types(strings.Split),
170-
},
171-
{
172-
Name: "splitN",
173-
Func: func(args ...interface{}) (interface{}, error) {
174-
return strings.SplitN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
167+
if len(args) == 2 {
168+
return strings.Split(args[0].(string), args[1].(string)), nil
169+
} else if len(args) == 3 {
170+
return strings.SplitN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
171+
} else {
172+
return nil, fmt.Errorf("invalid number of arguments for split (expected 2 or 3, got %d)", len(args))
173+
}
175174
},
176-
Types: types(strings.SplitN),
175+
Types: types(
176+
strings.Split,
177+
strings.SplitN,
178+
),
177179
},
178180
{
179181
Name: "splitAfter",
180182
Func: func(args ...interface{}) (interface{}, error) {
181-
return strings.SplitAfter(args[0].(string), args[1].(string)), nil
182-
},
183-
Types: types(strings.SplitAfter),
184-
},
185-
{
186-
Name: "splitAfterN",
187-
Func: func(args ...interface{}) (interface{}, error) {
188-
return strings.SplitAfterN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
183+
if len(args) == 2 {
184+
return strings.SplitAfter(args[0].(string), args[1].(string)), nil
185+
} else if len(args) == 3 {
186+
return strings.SplitAfterN(args[0].(string), args[1].(string), runtime.ToInt(args[2])), nil
187+
} else {
188+
return nil, fmt.Errorf("invalid number of arguments for splitAfter (expected 2 or 3, got %d)", len(args))
189+
}
189190
},
190-
Types: types(strings.SplitAfterN),
191+
Types: types(
192+
strings.SplitAfter,
193+
strings.SplitAfterN,
194+
),
191195
},
192196
{
193197
Name: "replace",

builtin/builtin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func TestBuiltin(t *testing.T) {
4242
{`upper("foo")`, "FOO"},
4343
{`lower("FOO")`, "foo"},
4444
{`split("foo,bar,baz", ",")`, []string{"foo", "bar", "baz"}},
45-
{`splitN("foo,bar,baz", ",", 2)`, []string{"foo", "bar,baz"}},
45+
{`split("foo,bar,baz", ",", 2)`, []string{"foo", "bar,baz"}},
4646
{`splitAfter("foo,bar,baz", ",")`, []string{"foo,", "bar,", "baz"}},
47-
{`splitAfterN("foo,bar,baz", ",", 2)`, []string{"foo,", "bar,baz"}},
47+
{`splitAfter("foo,bar,baz", ",", 2)`, []string{"foo,", "bar,baz"}},
4848
{`replace("foo,bar,baz", ",", ";")`, "foo;bar;baz"},
4949
{`replace("foo,bar,baz,goo", ",", ";", 2)`, "foo;bar;baz,goo"},
5050
{`repeat("foo", 3)`, "foofoofoo"},

docs/Language-Definition.md

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -303,36 +303,22 @@ Converts all the characters in string `v` to lowercase.
303303
lower("HELLO") == "hello"
304304
```
305305

306-
### split(v, delimiter)
306+
### split(v, delimiter[, n])
307307

308308
Splits the string `v` at each instance of the delimiter and returns an array of substrings.
309309

310310
```expr
311311
split("apple,orange,grape", ",") == ["apple", "orange", "grape"]
312+
split("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
312313
```
313314

314-
### splitN(v, delimiter, n)
315-
316-
Splits the string `v` at each instance of the delimiter but limits the result to `n` substrings.
317-
318-
```expr
319-
splitN("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
320-
```
321-
322-
### splitAfter(v, delimiter)
315+
### splitAfter(v, delimiter[, n])
323316

324317
Splits the string `v` after each instance of the delimiter.
325318

326319
```expr
327320
splitAfter("apple,orange,grape", ",") == ["apple,", "orange,", "grape"]
328-
```
329-
330-
### splitAfterN(v, delimiter, n)
331-
332-
Splits the string `v` after each instance of the delimiter but limits the result to `n` substrings.
333-
334-
```expr
335-
splitAfterN("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
321+
splitAfter("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
336322
```
337323

338324
### replace(v, old, new)
@@ -351,12 +337,14 @@ Repeats the string `v` `n` times.
351337
repeat("Hi", 3) == "HiHiHi"
352338
```
353339

354-
### join(v, delimiter)
340+
### join(v[, delimiter])
355341

356342
Joins an array of strings `v` into a single string with the given delimiter.
343+
If no delimiter is given, an empty string is used.
357344

358345
```expr
359346
join(["apple", "orange", "grape"], ",") == "apple,orange,grape"
347+
join(["apple", "orange", "grape"]) == "appleorangegrape"
360348
```
361349

362350
### indexOf(v, substring)
@@ -457,7 +445,7 @@ Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
457445
duration("1h").Seconds() == 3600
458446
```
459447

460-
### date(v[, format, timezone])
448+
### date(v[, format[, timezone]])
461449

462450
Converts the given value `v` into a date representation.
463451

0 commit comments

Comments
 (0)