-
Notifications
You must be signed in to change notification settings - Fork 0
/
directive.go
55 lines (52 loc) · 1.04 KB
/
directive.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
package gox
func Range[T any](s []T, fn func(item T, index int) Node) Node {
children := make([]node, 0)
for i, val := range s {
item := fn(val, i)
n, ok := item.(node)
if !ok {
continue
}
if n.nodeType == nodeFragment {
children = append(children, n.children...)
continue
}
children = append(children, n)
}
return node{
nodeType: nodeFragment,
children: children,
}
}
func MapRange[K comparable, V any](m map[K]V, fn func(key K, value V) Node) Node {
children := make([]node, 0)
for k, v := range m {
item := fn(k, v)
n, ok := item.(node)
if !ok {
continue
}
if n.nodeType == nodeFragment {
children = append(children, n.children...)
continue
}
children = append(children, n)
}
return node{
nodeType: nodeFragment,
children: children,
}
}
func If(condition bool, nodes ...Node) Node {
if !condition {
return node{
nodeType: nodeFragment,
}
}
attributes, children := processNodes(nodes)
return node{
nodeType: nodeFragment,
children: children,
attributes: attributes,
}
}