-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
156 lines (147 loc) · 3.7 KB
/
Copy patharray.go
File metadata and controls
156 lines (147 loc) · 3.7 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package php
import (
"reflect"
"sort"
)
// ArrayKeys array_keys returns the keys, numeric and string, from the array.
func ArrayKeys(array interface{}) interface{} {
t, v, l := getCommon(array)
res := make([]interface{}, l)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
res[i] = i
}
case reflect.Map:
for i, k := range v.MapKeys() {
res[i] = k.Interface()
}
default:
panic("expects parameter 1 to be array")
}
return res
}
// ArrayValues array_values returns all the values from the array and indexes the array numerically.
func ArrayValues(array interface{}) interface{} {
t, v, l := getCommon(array)
res := make([]interface{}, l)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
res[i] = v.Index(i).Interface()
}
case reflect.Map:
for i, k := range v.MapKeys() {
res[i] = v.MapIndex(k)
}
default:
panic("expects parameter 1 to be array")
}
return res
}
// ArrayKeyExists array_key_exists — Checks if the given key or index exists in the array
func ArrayKeyExists(key, array interface{}) bool {
t, v, l := getCommon(array)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
if reflect.DeepEqual(key, i) {
return true
}
}
case reflect.Map:
for _, k := range v.MapKeys() {
if reflect.DeepEqual(key, k.Interface()) {
return true
}
}
default:
panic("expects parameter 2 to be array")
}
return false
}
// InArray in_array — Checks if a value exists in an array
func InArray(needle, haystack interface{}) bool {
t, v, l := getCommon(haystack)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
if reflect.DeepEqual(needle, v.Index(i).Interface()) {
return true
}
}
case reflect.Map:
for _, k := range v.MapKeys() {
if reflect.DeepEqual(needle, v.MapIndex(k).Interface()) {
return true
}
}
default:
panic("expects parameter 2 to be array")
}
return false
}
// ArrayFilp array_flip — Exchanges all keys with their associated values in an array
func ArrayFilp(array interface{}) map[interface{}]interface{} {
t, v, l := getCommon(array)
res := make(map[interface{}]interface{}, l)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
res[v.Index(i).Interface()] = i
}
case reflect.Map:
for _, k := range v.MapKeys() {
res[v.MapIndex(k).Interface()] = k.Interface()
}
default:
panic("expects parameter 1 to be array")
}
return res
}
// ArrayUnique array_unique — Removes duplicate values from an array
func ArrayUnique(array interface{}) interface{} {
t, v, l := getCommon(array)
res := make(map[interface{}]int)
switch t.Kind() {
case reflect.Slice:
for i := 0; i < l; i++ {
res[v.Index(i).Interface()] = 1
}
case reflect.Map:
for _, k := range v.MapKeys() {
res[v.MapIndex(k).Interface()] = 1
}
default:
panic("expects parameter 1 to be array")
}
return ArrayKeys(res)
}
// Sort can only sort []int, []string, []float64
func Sort(array interface{}) {
t, v, _ := getCommon(array)
// res := make([]interface{}, l)
if t.Kind() == reflect.Slice {
switch v.Index(0).Kind() {
case reflect.Int:
array := array.([]int)
sort.Ints(array)
case reflect.String:
array := array.([]string)
sort.Strings(array)
case reflect.Float64:
array := array.([]float64)
sort.Float64s(array)
default:
panic("the param can only be int/string/float64 array")
}
} else {
panic("expects parameter 1 to be array")
}
}
func getCommon(array interface{}) (reflect.Type, reflect.Value, int) {
t := reflect.TypeOf(array)
v := reflect.ValueOf(array)
l := v.Len()
return t, v, l
}