github.com/blend/go-sdk@v1.20240719.1/collections/strings.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"strings"
    12  )
    13  
    14  // Strings is a type alias for []string with some helper methods.
    15  // Deprecated: Use collections/generic version.
    16  type Strings []string
    17  
    18  // Reverse reverses the strings array in place.
    19  // Deprecated: Use collections/generic version.
    20  func (sa Strings) Reverse() (output Strings) {
    21  	saLen := len(sa)
    22  
    23  	switch saLen {
    24  	case 0:
    25  		return
    26  	case 1:
    27  		output = Strings{sa[0]}
    28  		return
    29  	}
    30  
    31  	output = make(Strings, len(sa))
    32  	saLen2 := saLen >> 1
    33  	var nx int
    34  	for x := 0; x < saLen2; x++ {
    35  		nx = saLen - (x + 1)
    36  		output[x] = sa[nx]
    37  		output[nx] = sa[x]
    38  	}
    39  	if saLen%2 != 0 {
    40  		output[saLen2] = sa[saLen2]
    41  	}
    42  	return
    43  }
    44  
    45  // First returns the first element of the array.
    46  // Deprecated: Use collections/generic version.
    47  func (sa Strings) First() string {
    48  	if len(sa) == 0 {
    49  		return ""
    50  	}
    51  	return sa[0]
    52  }
    53  
    54  // Last returns the last element of the array.
    55  // Deprecated: Use collections/generic version.
    56  func (sa Strings) Last() string {
    57  	if len(sa) == 0 {
    58  		return ""
    59  	}
    60  	return sa[len(sa)-1]
    61  }
    62  
    63  // Contains returns if the given string is in the array.
    64  // Deprecated: Use collections/generic version.
    65  func (sa Strings) Contains(elem string) bool {
    66  	for _, arrayElem := range sa {
    67  		if arrayElem == elem {
    68  			return true
    69  		}
    70  	}
    71  	return false
    72  }
    73  
    74  // ContainsLower returns true if the `elem` is in the Strings, false otherwise.
    75  // Deprecated: Use collections/generic version.
    76  func (sa Strings) ContainsLower(elem string) bool {
    77  	for _, arrayElem := range sa {
    78  		if strings.ToLower(arrayElem) == elem {
    79  			return true
    80  		}
    81  	}
    82  	return false
    83  }
    84  
    85  // GetByLower returns an element from the array that matches the input.
    86  // Deprecated: Use collections/generic version.
    87  func (sa Strings) GetByLower(elem string) string {
    88  	for _, arrayElem := range sa {
    89  		if strings.ToLower(arrayElem) == elem {
    90  			return arrayElem
    91  		}
    92  	}
    93  	return ""
    94  }