...

Source file src/github.com/ory/go-convenience/stringslice/filter.go

Documentation: github.com/ory/go-convenience/stringslice

     1  package stringslice
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  )
     7  
     8  // Filter applies the provided filter function and removes all items from the slice for which the filter function returns true.
     9  // This function uses append and might cause
    10  func Filter(values []string, filter func(string) bool) (ret []string) {
    11  	for _, value := range values {
    12  		if !filter(value) {
    13  			ret = append(ret, value)
    14  		}
    15  	}
    16  
    17  	if ret == nil {
    18  		return []string{}
    19  	}
    20  
    21  	return
    22  }
    23  
    24  // TrimEmptyFilter applies the strings.TrimFunc function and removes all empty strings
    25  func TrimEmptyFilter(values []string, trim func(rune) bool) (ret []string) {
    26  	return Filter(values, func(value string) bool {
    27  		return strings.TrimFunc(value, trim) == ""
    28  	})
    29  }
    30  
    31  // TrimSpaceEmptyFilter applies the strings.TrimSpace function and removes all empty strings
    32  func TrimSpaceEmptyFilter(values []string) []string {
    33  	return TrimEmptyFilter(values, unicode.IsSpace)
    34  }
    35  

View as plain text