pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/sliceutil/sliceutil.go (about)

     1  // Package sliceutil provides methods for working with slices
     2  package sliceutil
     3  
     4  // ////////////////////////////////////////////////////////////////////////////////// //
     5  //                                                                                    //
     6  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     7  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     8  //                                                                                    //
     9  // ////////////////////////////////////////////////////////////////////////////////// //
    10  
    11  import (
    12  	"errors"
    13  )
    14  
    15  // ////////////////////////////////////////////////////////////////////////////////// //
    16  
    17  // Copy creates copy of given slice
    18  func Copy(slice []string) []string {
    19  	if len(slice) == 0 {
    20  		return nil
    21  	}
    22  
    23  	s := make([]string, len(slice))
    24  	copy(s, slice)
    25  
    26  	return s
    27  }
    28  
    29  // CopyInts creates copy of given slice
    30  func CopyInts(slice []int) []int {
    31  	if len(slice) == 0 {
    32  		return nil
    33  	}
    34  
    35  	s := make([]int, len(slice))
    36  	copy(s, slice)
    37  
    38  	return s
    39  }
    40  
    41  // CopyFloats creates copy of given slice
    42  func CopyFloats(slice []float64) []float64 {
    43  	if len(slice) == 0 {
    44  		return nil
    45  	}
    46  
    47  	s := make([]float64, len(slice))
    48  	copy(s, slice)
    49  
    50  	return s
    51  }
    52  
    53  // StringToInterface converts slice with strings to slice with interface{}
    54  func StringToInterface(data []string) []interface{} {
    55  	if len(data) == 0 {
    56  		return nil
    57  	}
    58  
    59  	result := make([]interface{}, len(data))
    60  
    61  	for i, r := range data {
    62  		result[i] = r
    63  	}
    64  
    65  	return result
    66  }
    67  
    68  // IntToInterface converts slice with ints to slice with interface{}
    69  func IntToInterface(data []int) []interface{} {
    70  	if len(data) == 0 {
    71  		return nil
    72  	}
    73  
    74  	result := make([]interface{}, len(data))
    75  
    76  	for i, r := range data {
    77  		result[i] = r
    78  	}
    79  
    80  	return result
    81  }
    82  
    83  // StringToError converts slice with strings to slice with errors
    84  func StringToError(data []string) []error {
    85  	if len(data) == 0 {
    86  		return nil
    87  	}
    88  
    89  	result := make([]error, len(data))
    90  
    91  	for i, e := range data {
    92  		result[i] = errors.New(e)
    93  	}
    94  
    95  	return result
    96  }
    97  
    98  // ErrorToString converts slice with errors to slice with strings
    99  func ErrorToString(data []error) []string {
   100  	if len(data) == 0 {
   101  		return nil
   102  	}
   103  
   104  	result := make([]string, len(data))
   105  
   106  	for i, e := range data {
   107  		result[i] = e.Error()
   108  	}
   109  
   110  	return result
   111  }
   112  
   113  // Index returns index of given item in a slice or -1 otherwise
   114  func Index(slice []string, item string) int {
   115  	if len(slice) == 0 {
   116  		return -1
   117  	}
   118  
   119  	for i, v := range slice {
   120  		if v == item {
   121  			return i
   122  		}
   123  	}
   124  
   125  	return -1
   126  }
   127  
   128  // Contains checks if string slice contains some value
   129  func Contains(slice []string, value string) bool {
   130  	return Index(slice, value) != -1
   131  }
   132  
   133  // Exclude removes items from slice
   134  func Exclude(slice []string, items ...string) []string {
   135  	var n int
   136  
   137  	s := Copy(slice)
   138  
   139  	if len(slice) == 0 || len(items) == 0 {
   140  		return s
   141  	}
   142  
   143  LOOP:
   144  	for _, i := range s {
   145  		for _, j := range items {
   146  			if i == j {
   147  				continue LOOP
   148  			}
   149  		}
   150  
   151  		s[n] = i
   152  		n++
   153  	}
   154  
   155  	return s[:n]
   156  }
   157  
   158  // Deduplicate removes duplicates from slice.
   159  // Slice must be sorted before deduplication.
   160  func Deduplicate(slice []string) []string {
   161  	var n int
   162  
   163  	s := Copy(slice)
   164  
   165  	if len(slice) <= 1 {
   166  		return s
   167  	}
   168  
   169  	for i := 1; i < len(s); i++ {
   170  		if s[n] == s[i] {
   171  			continue
   172  		}
   173  
   174  		n++
   175  		s[n] = s[i]
   176  	}
   177  
   178  	return s[:n+1]
   179  }