podman
87 строк · 2.8 Кб
1package govalidator
2
3// Iterator is the function that accepts element of slice/array and its index
4type Iterator func(interface{}, int)
5
6// ResultIterator is the function that accepts element of slice/array and its index and returns any result
7type ResultIterator func(interface{}, int) interface{}
8
9// ConditionIterator is the function that accepts element of slice/array and its index and returns boolean
10type ConditionIterator func(interface{}, int) bool
11
12// ReduceIterator is the function that accepts two element of slice/array and returns result of merging those values
13type ReduceIterator func(interface{}, interface{}) interface{}
14
15// Some validates that any item of array corresponds to ConditionIterator. Returns boolean.
16func Some(array []interface{}, iterator ConditionIterator) bool {
17res := false
18for index, data := range array {
19res = res || iterator(data, index)
20}
21return res
22}
23
24// Every validates that every item of array corresponds to ConditionIterator. Returns boolean.
25func Every(array []interface{}, iterator ConditionIterator) bool {
26res := true
27for index, data := range array {
28res = res && iterator(data, index)
29}
30return res
31}
32
33// Reduce boils down a list of values into a single value by ReduceIterator
34func Reduce(array []interface{}, iterator ReduceIterator, initialValue interface{}) interface{} {
35for _, data := range array {
36initialValue = iterator(initialValue, data)
37}
38return initialValue
39}
40
41// Each iterates over the slice and apply Iterator to every item
42func Each(array []interface{}, iterator Iterator) {
43for index, data := range array {
44iterator(data, index)
45}
46}
47
48// Map iterates over the slice and apply ResultIterator to every item. Returns new slice as a result.
49func Map(array []interface{}, iterator ResultIterator) []interface{} {
50var result = make([]interface{}, len(array))
51for index, data := range array {
52result[index] = iterator(data, index)
53}
54return result
55}
56
57// Find iterates over the slice and apply ConditionIterator to every item. Returns first item that meet ConditionIterator or nil otherwise.
58func Find(array []interface{}, iterator ConditionIterator) interface{} {
59for index, data := range array {
60if iterator(data, index) {
61return data
62}
63}
64return nil
65}
66
67// Filter iterates over the slice and apply ConditionIterator to every item. Returns new slice.
68func Filter(array []interface{}, iterator ConditionIterator) []interface{} {
69var result = make([]interface{}, 0)
70for index, data := range array {
71if iterator(data, index) {
72result = append(result, data)
73}
74}
75return result
76}
77
78// Count iterates over the slice and apply ConditionIterator to every item. Returns count of items that meets ConditionIterator.
79func Count(array []interface{}, iterator ConditionIterator) int {
80count := 0
81for index, data := range array {
82if iterator(data, index) {
83count = count + 1
84}
85}
86return count
87}
88