-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.go
116 lines (107 loc) · 1.99 KB
/
helper.go
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
package t
import (
"strings"
)
// If 三元判断
func If(b bool, x, y interface{}) Type {
if b {
return New(x)
}
return New(y)
}
// SpliteAndTrimSpace 字符串分割并且去掉两边的空格
func SpliteAndTrimSpace(s, sep string) (res []string) {
split := strings.Split(s, sep)
for _, v := range split {
if v != "" {
res = append(res, strings.TrimSpace(v))
}
}
return
}
// InArray 是否存在给定的数组中
func InArray(argv interface{}, haystack interface{}) bool {
for _, v := range New(haystack).Slice() {
if v.String() == New(argv).String() {
return true
}
}
return false
}
// InArrayString 是否存在给定的字符串数组中
func InArrayString(arg string, args []string) bool {
for _, v := range args {
if v == arg {
return true
}
}
return false
}
// Max 获取最大的数
func Max(args ...interface{}) Type {
if len(args) == 0 {
return New(0)
}
var max = New(args[0]).Float64()
for k, arg := range args {
if k == 0 {
continue
}
tmp := New(arg).Float64()
if tmp > max {
max = tmp
}
}
return New(max)
}
// Min 获取最小的数
func Min(args ...interface{}) Type {
if len(args) == 0 {
return New(0)
}
var min = New(args[0]).Float64()
for k, arg := range args {
if k == 0 {
continue
}
tmp := New(arg).Float64()
if tmp < min {
min = tmp
}
}
return New(min)
}
// ArrayUnique 数组去重
func ArrayUniqueString(arr []string) (newArr []string) {
newArr = make([]string, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return newArr
}
// ArrayUnique 数组去重
func ArrayUnique(array interface{}) (newArr Slice) {
arr := New(array).Slice()
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return newArr
}