Files
monkey/object/builtin_split.go
Chuck Smith 99cea83f57
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Add tons of builtin helpers and array operations.
2024-03-24 12:11:46 -04:00

37 lines
708 B
Go

package object
import (
"strings"
)
// Split ...
func Split(args ...Object) Object {
if len(args) < 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if obj, ok := args[0].(*String); ok {
var sep string
s := obj.Value
if len(args) == 2 {
if obj, ok := args[1].(*String); ok {
sep = obj.Value
} else {
return newError("expected arg #2 to be `str` got=%T", args[1])
}
}
tokens := strings.Split(s, sep)
elements := make([]Object, len(tokens))
for i, token := range tokens {
elements[i] = &String{Value: token}
}
return &Array{Elements: elements}
} else {
return newError("expected arg #1 to be `str` got got=%T", args[0])
}
}