aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/util/util.go
blob: 0ed64b87959972fb8af832d5264ce9e31609b63c (plain) (blame)
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
package util

import (
	"iter"
	"time"
)

func ParseDateTime(s string) (time.Time, error) {
	dateFormats := []string{
		"Jan _2, 2006",
		"January 2, 2006",
		"January 2 2006",
		"Jan 2 2006",
		"2006 January 2",
		time.DateOnly,
		time.DateTime,
		time.Layout,
		time.ANSIC,
		time.UnixDate,
		time.RubyDate,
		time.RFC822,
		time.RFC822Z,
		time.RFC850,
		time.RFC1123,
		time.RFC1123Z,
		time.RFC3339,
	}

	var t time.Time
	var err error
	for _, layout := range dateFormats {
		if t, err = time.Parse(layout, s); err == nil {
			return t, nil
		}
	}

	return time.Time{}, err
}

// Create a copy of a slice with all values that satisfy cond
func Fitler[E any](s []E, cond func(e E) bool) []E {
	filtered := make([]E, 0, len(s))
	for _, e := range s {
		if cond(e) {
			filtered = append(filtered, e)
		}
	}

	return filtered
}

// Create an iterator of index and element for all values in a slice which satisfy cond.
func FilterIter[E any](s []E, cond func(e E) bool) iter.Seq2[int, E] {
	return func(yield func(int, E) bool) {
		for i, e := range s {
			if cond(e) {
				if !yield(i, e) {
					return
				}
			}
		}
	}
}

// FilterIter but backwards
func BackwardsFilterIter[E any](s []E, cond func(e E) bool) iter.Seq2[int, E] {
	return func(yield func(int, E) bool) {
		for i := len(s) - 1; i >= 0; i-- {
			if cond(s[i]) {
				if !yield(i, s[i]) {
					return
				}
			}
		}
	}
}