git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/arrays.go (about)

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