forked from chasefleming/elem-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (45 loc) · 1.07 KB
/
utils.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
package elem
import (
"sort"
"strings"
)
type Style map[string]string
func (s Style) String() string {
// Extract the keys and sort them for deterministic order
keys := make([]string, 0, len(s))
for key := range s {
keys = append(keys, key)
}
sort.Strings(keys)
var builder strings.Builder
for _, key := range keys {
builder.WriteString(key)
builder.WriteString(": ")
builder.WriteString(s[key])
builder.WriteString("; ")
}
// Trim the last space
var styleStr = builder.String()
if len(styleStr) > 0 {
styleStr = styleStr[:len(styleStr)-1]
}
return styleStr
}
func ApplyStyle(s Style) string {
return s.String()
}
// Show conditionally renders one of the provided elements based on the condition
func If[T any](condition bool, ifTrue, ifFalse T) T {
if condition {
return ifTrue
}
return ifFalse
}
// TransformEach maps a slice of items to a slice of Elements using the provided function
func TransformEach[T any](items []T, fn func(T) Node) []Node {
var nodes []Node
for _, item := range items {
nodes = append(nodes, fn(item))
}
return nodes
}