Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/202205121612.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to run parseListWithCleanup to keep empty lines
36 changes: 30 additions & 6 deletions utils/collection/parseLists.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,50 @@
*/
package collection

import "strings"
import (
"strings"
"unicode"
)

// ParseListWithCleanup splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
// for example,
// ParseListWithCleanup("a, b , c", ",") returns []{"a","b","c"}
func ParseListWithCleanup(input string, sep string) (newS []string) {
func lineIsOnlyWhitespace(line string) bool {
for _, c := range line {
if !unicode.IsSpace(c) {
return false
}
}
return true
}

func parseListWithCleanup(input string, sep string, keepBlankLines bool) (newS []string) {
if len(input) == 0 {
newS = []string{} // initialisation of empty arrays in function returns []string(nil) instead of []string{}
return
}
split := strings.Split(input, sep)
for _, s := range split {
tempString := strings.TrimSpace(s)
if tempString != "" {
if tempString != "" || (keepBlankLines && lineIsOnlyWhitespace(s)) {
newS = append(newS, tempString)
}
}
return
}

// ParseListWithCleanup splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
// for example,
// ParseListWithCleanup("a, b , c", ",") returns []{"a","b","c"}
func ParseListWithCleanup(input string, sep string) (newS []string) {
return parseListWithCleanup(input, sep, false)
}

// ParseListWithCleanupKeepBlankLines splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
// unless the entire item is whitespace in which case it is converted to an empty string. For example,
// ParseListWithCleanupKeepBlankLines("a, b , c", ",") returns []{"a","b","c"}
// ParseListWithCleanupKeepBlankLines("a, b , , c", ",") returns []{"a","b", "", "c"}
func ParseListWithCleanupKeepBlankLines(input string, sep string) (newS []string) {
return parseListWithCleanup(input, sep, true)
}

// ParseCommaSeparatedList returns the list of string separated by a comma
func ParseCommaSeparatedList(input string) []string {
return ParseListWithCleanup(input, ",")
Expand Down
32 changes: 32 additions & 0 deletions utils/collection/parseLists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,35 @@ func TestParseCommaSeparatedListWithSpacesBetweenWords(t *testing.T) {
finalList := ParseCommaSeparatedList(stringList)
require.Equal(t, stringArray, finalList)
}

func TestParseCommaSeparatedListWithSpacesBetweenWordsKeepBlanks(t *testing.T) {
stringList := ""
stringArray := []string{}
// we don't need cryptographically secure random numbers for generating a number of elements in a list
lengthOfList := rand.Intn(10) + 8 //nolint:gosec
for i := 0; i < lengthOfList; i++ {
word := faker.Sentence()
stringList += word
stringArray = append(stringArray, word)
numSpacesToAdd := rand.Intn(5) //nolint:gosec
for j := 0; j < numSpacesToAdd; j++ {
stringList += " "
}
stringList += ","
if i%3 == 2 {
numSpacesToAdd := rand.Intn(5) //nolint:gosec
for j := 0; j < numSpacesToAdd; j++ {
stringList += " "
}
stringArray = append(stringArray, "")
stringList += ","
}
}
stringArray = append(stringArray, "") // account for final ,

finalList1 := ParseCommaSeparatedList(stringList)
require.NotEqual(t, stringArray, finalList1)

finalList2 := ParseListWithCleanupKeepBlankLines(stringList, ",")
require.Equal(t, stringArray, finalList2)
}