From 940705059864d41ae5d63e861f6e8b956768d5eb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 5 Apr 2016 04:13:56 -0400 Subject: [PATCH] Add flatten helper to take apart messy nested lists This isn't 100% necessary, but it's a friendly feature to add, and it was a fun function to write. --- misc.go | 22 +++++++++++++++++ misc_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/misc.go b/misc.go index 1f94ec6d..c2432e72 100644 --- a/misc.go +++ b/misc.go @@ -270,6 +270,28 @@ func DirifyFileList(fileList []string, removeDirs bool) []string { return result } +// FlattenListWithSplit flattens a list of input by splitting each element by +// any and all of the strings listed in the split array +func FlattenListWithSplit(input []string, split []string) []string { + if len(split) == 0 { // nothing to split by + return input + } + out := []string{} + for _, x := range input { + s := []string{} + if len(split) == 1 { + s = strings.Split(x, split[0]) // split by only string + } else { + s = []string{x} // initial + for i := range split { + s = FlattenListWithSplit(s, []string{split[i]}) // recurse + } + } + out = append(out, s...) + } + return out +} + // special version of time.After that blocks when given a negative integer // when used in a case statement, the timer restarts on each select call to it func TimeAfterOrBlock(t int) <-chan time.Time { diff --git a/misc_test.go b/misc_test.go index ab484193..eb317345 100644 --- a/misc_test.go +++ b/misc_test.go @@ -742,3 +742,71 @@ func TestMiscT11(t *testing.T) { } } } + +func TestMiscFlattenListWithSplit1(t *testing.T) { + { + in := []string{} // input + ex := []string{} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } + + { + in := []string{"hey"} // input + ex := []string{"hey"} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } + + { + in := []string{"a", "b", "c", "d"} // input + ex := []string{"a", "b", "c", "d"} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } + + { + in := []string{"a,b,c,d"} // input + ex := []string{"a", "b", "c", "d"} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } + + { + in := []string{"a,b;c d"} // input (mixed) + ex := []string{"a", "b", "c", "d"} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } + + { + in := []string{"a,b,c,d;e,f,g,h;i,j,k,l;m,n,o,p q,r,s,t;u,v,w,x y z"} // input (mixed) + ex := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} // expected + out := FlattenListWithSplit(in, []string{",", ";", " "}) + sort.Strings(out) + sort.Strings(ex) + if !reflect.DeepEqual(ex, out) { + t.Errorf("FlattenListWithSplit expected: %v; got: %v.", ex, out) + } + } +}