github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/collections/lists.go (about)

     1  package collections
     2  
     3  // ListIntersection returns all the items in both list1 and list2. Note that this will dedup the items so that the
     4  // output is more predictable. Otherwise, the end list depends on which list was used as the base.
     5  func ListIntersection(list1 []string, list2 []string) []string {
     6  	out := []string{}
     7  
     8  	// Only need to iterate list1, because we want items in both lists, not union.
     9  	for _, item := range list1 {
    10  		if ListContains(list2, item) && !ListContains(out, item) {
    11  			out = append(out, item)
    12  		}
    13  	}
    14  
    15  	return out
    16  }
    17  
    18  // ListSubtract removes all the items in list2 from list1.
    19  func ListSubtract(list1 []string, list2 []string) []string {
    20  	out := []string{}
    21  
    22  	for _, item := range list1 {
    23  		if !ListContains(list2, item) {
    24  			out = append(out, item)
    25  		}
    26  	}
    27  
    28  	return out
    29  }
    30  
    31  // ListContains returns true if the given list of strings (haystack) contains the given string (needle).
    32  func ListContains(haystack []string, needle string) bool {
    33  	for _, str := range haystack {
    34  		if needle == str {
    35  			return true
    36  		}
    37  	}
    38  
    39  	return false
    40  }