-
Notifications
You must be signed in to change notification settings - Fork 160
/
headers.go
112 lines (91 loc) · 2.29 KB
/
headers.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
package table
import (
"fmt"
"strings"
"unicode"
)
const UNKNOWN_HEADER_MAPPING rune = '_'
func NewHeader(title string) Header {
return Header{
Key: KeyifyHeader(title),
Title: title,
Hidden: false,
}
}
func NewHeadersFromStrings(titles []string) (headers []Header) {
for _, t := range titles {
headers = append(headers, NewHeader(t))
}
return
}
func (t *Table) SetColumnVisibility(headers []Header) error {
for tableHeaderIdx, _ := range t.Header {
t.Header[tableHeaderIdx].Hidden = true
}
for _, header := range headers {
foundHeader := false
for tableHeaderIdx, tableHeader := range t.Header {
if tableHeader.Key == header.Key || tableHeader.Title == header.Title {
t.Header[tableHeaderIdx].Hidden = false
foundHeader = true
break
}
}
if !foundHeader {
// key may be empty; if title is present
return fmt.Errorf("Failed to find header: %s", header.Key)
}
}
return nil
}
func (t *Table) SetColumnVisibilityFiltered(headers []Header, filterHeaders []Header) error {
for tableHeaderIdx, _ := range t.Header {
t.Header[tableHeaderIdx].Hidden = true
}
for _, header := range headers {
foundHeader := false
for tableHeaderIdx, tableHeader := range t.Header {
if tableHeader.Key == header.Key || tableHeader.Title == header.Title {
t.Header[tableHeaderIdx].Hidden = false
foundHeader = true
break
}
}
if !foundHeader {
for _, filterHeader := range filterHeaders {
if filterHeader.Key == header.Key || filterHeader.Title == header.Title {
foundHeader = true
break
}
}
}
if !foundHeader {
// key may be empty; if title is present
return fmt.Errorf("Failed to find header: %s", header.Key)
}
}
return nil
}
func KeyifyHeader(header string) string {
splittedStrings := strings.Split(cleanHeader(header), " ")
splittedTrimmedStrings := []string{}
for _, s := range splittedStrings {
if s != "" {
splittedTrimmedStrings = append(splittedTrimmedStrings, s)
}
}
join := strings.Join(splittedTrimmedStrings, "_")
if len(join) == 0 {
return string(UNKNOWN_HEADER_MAPPING)
}
return join
}
func cleanHeader(header string) string {
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return unicode.ToLower(r)
} else {
return ' '
}
}, header)
}