-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
96 lines (85 loc) · 1.82 KB
/
misc.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
package libwekan
import (
"math/rand"
"time"
)
func newId() string {
return newIdN(17)
}
func newId6() string {
return newIdN(6)
}
func newIdN(n int) string {
chars := "123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz"
l := len(chars)
var digits []byte
for i := 0; i < n; i++ {
digit := rand.Intn(l)
digits = append(digits, chars[digit])
}
return string(digits)
}
func uniq[Element comparable](array []Element) []Element {
m := make(map[Element]struct{})
for _, element := range array {
m[element] = struct{}{}
}
var set []Element
for element := range m {
set = append(set, element)
}
return set
}
func intersect[E comparable](elementsA []E, elementsB []E) (both []E, onlyA []E, onlyB []E) {
for _, elementA := range elementsA {
foundBoth := false
for _, elementB := range elementsB {
if elementA == elementB {
both = append(both, elementA)
foundBoth = true
}
}
if !foundBoth {
onlyA = append(onlyA, elementA)
}
}
for _, elementB := range elementsB {
foundBoth := false
for _, elementA := range elementsA {
if elementA == elementB {
foundBoth = true
}
}
if !foundBoth {
onlyB = append(onlyB, elementB)
}
}
return both, onlyA, onlyB
}
func mapSlice[T any, M any](a []T, f func(T) M) []M {
n := make([]M, len(a))
for i, e := range a {
n[i] = f(e)
}
return n
}
func toMongoTime(t time.Time) time.Time {
return t.In(time.UTC).Truncate(time.Millisecond)
}
func contains[Element comparable](elements []Element, element Element) bool {
for _, actual := range elements {
if element == actual {
return true
}
}
return false
}
func selectSlice[Element any](slice []Element, filter func(Element) bool) []Element {
var accepted []Element
for _, element := range slice {
if filter(element) {
accepted = append(accepted, element)
}
}
return accepted
}