-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
69 lines (61 loc) · 1.14 KB
/
util.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
package transformer
import (
"fmt"
"log"
"os"
"strings"
"unicode"
"github.com/fatih/color"
"github.com/mstgnz/transformer/node"
)
// ErrorHandle
// wrap error
func ErrorHandle(err error) {
if err != nil {
log.Printf(err.Error())
}
}
// Contains
// generic func for array contains any type
func Contains[T comparable](s []T, e T) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}
// ConvertToNode
// convert to Node
func ConvertToNode(value any) *node.Node {
if knot, ok := value.(*node.Node); ok {
return knot
}
return nil
}
// ConvertToSlice
// convert to any slice
func ConvertToSlice(value any) []any {
slc, _ := value.([]any)
return slc
}
// StripSpaces
// remove all spaces
func StripSpaces(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
// if the character is a space, drop it
return -1
}
// else keep it in the string
return r
}, str)
}
// PrintErr
// with format
func PrintErr(format string, args ...any) {
_, err := fmt.Fprintf(os.Stderr, color.RedString("error: "+format+"\n"), args...)
if err != nil {
log.Println("error printing failed")
}
}