-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
120 lines (101 loc) · 2.21 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
117
118
119
120
package spiker
import (
"encoding/json"
"errors"
"strconv"
"unicode"
"unicode/utf8"
)
// ParseNumber parses the number in the string
func ParseNumber(value string) (float64, error) {
var index int
var number []rune
r, size := utf8.DecodeRuneInString(value[index:])
if size > 0 && (r == '-' || unicode.IsDigit(r)) {
number = append(number, r)
index++
r, size = utf8.DecodeRuneInString(value[index:])
for size > 0 && unicode.IsDigit(r) {
number = append(number, r)
index++
r, size = utf8.DecodeRuneInString(value[index:])
}
}
if size > 0 && r == '.' && len(number) > 0 && string(number) != "-" {
number = append(number, '.')
index++
r, size = utf8.DecodeRuneInString(value[index:])
for size > 0 && unicode.IsDigit(r) {
number = append(number, r)
index++
r, size = utf8.DecodeRuneInString(value[index:])
}
}
if len(number) < 1 || string(number) == "-" {
return 0, errors.New("NOT A VALID NUMERIC STRING")
}
num, err := strconv.ParseFloat(string(number), 64)
return num, err
}
// IsNumber is a number string
func IsNumber(value string) bool {
_, err := strconv.ParseFloat(value, 64)
return err == nil
}
// IsTrue a variable is true
func IsTrue(value interface{}) bool {
switch value := value.(type) {
case string:
return len(value) > 0
case int:
return value != 0
case float64:
return value != 0
case bool:
return value
case ValueList:
return len(value) > 0
case ValueMap:
return len(value) > 0
}
return false
}
// Interface2String convert interface{} to string
func Interface2String(inter interface{}) string {
switch inter := inter.(type) {
case string:
return inter
case int:
return strconv.Itoa(inter)
case float64:
return strconv.FormatFloat(inter, 'f', -1, 64)
case bool:
if inter {
return "1"
}
return ""
// just for compare
case ValueList, ValueMap:
js, _ := json.Marshal(inter)
return string(js)
}
return ""
}
// Interface2Float64 convert interface{} to float64
func Interface2Float64(inter interface{}) float64 {
switch inter := inter.(type) {
case string:
num, _ := ParseNumber(inter)
return num
case int:
return float64(inter)
case float64:
return inter
case bool:
if inter {
return 1
}
return 0
}
return 0
}