Repository navigation
Expand file tree
/
Copy pathmethods.go
More file actions
74 lines (71 loc) · 1.75 KB
/
Copy pathmethods.go
File metadata and controls
74 lines (71 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package tender
import (
"fmt"
"strings"
)
// --- Array Methods ---
var arrayMethods = map[string]CallableFunc{
"push": func(args ...Object) (Object, error) {
arr := args[0].(*Array)
arr.Value = append(arr.Value, args[1:]...)
return &Int{Value: int64(len(arr.Value))}, nil
},
"pop": func(args ...Object) (Object, error) {
arr := args[0].(*Array)
if len(arr.Value) == 0 {
return NullValue, nil
}
last := arr.Value[len(arr.Value)-1]
arr.Value = arr.Value[:len(arr.Value)-1]
return last, nil
},
"shift": func(args ...Object) (Object, error) {
arr := args[0].(*Array)
if len(arr.Value) == 0 {
return NullValue, nil
}
first := arr.Value[0]
arr.Value = arr.Value[1:]
return first, nil
},
"unshift": func(args ...Object) (Object, error) {
arr := args[0].(*Array)
arr.Value = append(args[1:], arr.Value...)
return &Int{Value: int64(len(arr.Value))}, nil
},
"join": func(args ...Object) (Object, error) {
arr := args[0].(*Array)
sep := ""
if len(args) > 1 {
s, ok := ToString(args[1])
if !ok {
return nil, fmt.Errorf("join() separator must be a string")
}
sep = s
}
var sParts []string
for _, item := range arr.Value {
sParts = append(sParts, item.String())
}
return &String{Value: strings.Join(sParts, sep)}, nil
},
}
// --- ImmutableArray Methods ---
var immutableArrayMethods = map[string]CallableFunc{
"join": func(args ...Object) (Object, error) {
arr := args[0].(*ImmutableArray)
sep := ""
if len(args) > 1 {
s, ok := ToString(args[1])
if !ok {
return nil, fmt.Errorf("join() separator must be a string")
}
sep = s
}
var sParts []string
for _, item := range arr.Value {
sParts = append(sParts, item.String())
}
return &String{Value: strings.Join(sParts, sep)}, nil
},
}