github.com/YousefHaggyHeroku/pack@v1.5.5/internal/stringset/stringset.go (about)

     1  package stringset
     2  
     3  // FromSlice converts the given slice to a set in the form of unique keys in a map.
     4  // The value associated with each key should not be relied upon. A value is present
     5  // in the set if its key is present in the map, regardless of the key's value.
     6  func FromSlice(strings []string) map[string]interface{} {
     7  	set := map[string]interface{}{}
     8  	for _, s := range strings {
     9  		set[s] = nil
    10  	}
    11  	return set
    12  }
    13  
    14  // Compare performs a set comparison between two slices. `extra` represents elements present in
    15  // `strings1` but not `strings2`. `missing` represents elements present in `strings2` that are
    16  // missing from `strings1`. `common` represents elements present in both slices. Since the input
    17  // slices are treated as sets, duplicates will be removed in any outputs.
    18  func Compare(strings1, strings2 []string) (extra []string, missing []string, common []string) {
    19  	set1 := FromSlice(strings1)
    20  	set2 := FromSlice(strings2)
    21  
    22  	for s := range set1 {
    23  		if _, ok := set2[s]; !ok {
    24  			extra = append(extra, s)
    25  			continue
    26  		}
    27  		common = append(common, s)
    28  	}
    29  
    30  	for s := range set2 {
    31  		if _, ok := set1[s]; !ok {
    32  			missing = append(missing, s)
    33  		}
    34  	}
    35  
    36  	return extra, missing, common
    37  }