github.com/blend/go-sdk@v1.20220411.3/collections/strings.go (about) 1 /* 2 3 Copyright (c) 2022 - 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 type Strings []string 16 17 // Reverse reverses the strings array in place. 18 func (sa Strings) Reverse() (output Strings) { 19 saLen := len(sa) 20 21 switch saLen { 22 case 0: 23 return 24 case 1: 25 output = Strings{sa[0]} 26 return 27 } 28 29 output = make(Strings, len(sa)) 30 saLen2 := saLen >> 1 31 var nx int 32 for x := 0; x < saLen2; x++ { 33 nx = saLen - (x + 1) 34 output[x] = sa[nx] 35 output[nx] = sa[x] 36 } 37 if saLen%2 != 0 { 38 output[saLen2] = sa[saLen2] 39 } 40 return 41 } 42 43 // First returns the first element of the array. 44 func (sa Strings) First() string { 45 if len(sa) == 0 { 46 return "" 47 } 48 return sa[0] 49 } 50 51 // Last returns the last element of the array. 52 func (sa Strings) Last() string { 53 if len(sa) == 0 { 54 return "" 55 } 56 return sa[len(sa)-1] 57 } 58 59 // Contains returns if the given string is in the array. 60 func (sa Strings) Contains(elem string) bool { 61 for _, arrayElem := range sa { 62 if arrayElem == elem { 63 return true 64 } 65 } 66 return false 67 } 68 69 // ContainsLower returns true if the `elem` is in the Strings, false otherwise. 70 func (sa Strings) ContainsLower(elem string) bool { 71 for _, arrayElem := range sa { 72 if strings.ToLower(arrayElem) == elem { 73 return true 74 } 75 } 76 return false 77 } 78 79 // GetByLower returns an element from the array that matches the input. 80 func (sa Strings) GetByLower(elem string) string { 81 for _, arrayElem := range sa { 82 if strings.ToLower(arrayElem) == elem { 83 return arrayElem 84 } 85 } 86 return "" 87 }