Skip to content

Commit

Permalink
Merge pull request #14 from HotelUrbano/feature/adding-string-slice-t…
Browse files Browse the repository at this point in the history
…o-slice

Feature/adding string slice to slice
  • Loading branch information
bernardolm authored Aug 28, 2018
2 parents 6f3ec6e + 411d725 commit 35b675c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
regexpDatePatternYYYYMMDDTHHMMSS *regexp.Regexp
regexpRFC3339 *regexp.Regexp
regexpRFC3339WithTime *regexp.Regexp
regexpCommaAlphaNum *regexp.Regexp
)

func init() {
Expand All @@ -67,6 +68,7 @@ func init() {
regexpDatePatternYYYYMMDDTHHMMSS, _ = regexp.Compile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`)
regexpRFC3339, _ = regexp.Compile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$`)
regexpRFC3339WithTime, _ = regexp.Compile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+\-]{1}\d{2}:\d{2}$`)
regexpCommaAlphaNum, _ = regexp.Compile(`[^A-Za-z0-9,]`)
}

// ToStringSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION
Expand Down Expand Up @@ -109,6 +111,46 @@ func ToInt64Slice(stringSlice []string) (int64Slice []int64) {
return int64Slice
}

// StringToStringSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION
func StringToStringSlice(s string) []string {
stringSlice := []string{}
if len(s) == 0 {
return []string{}
}

s1 := regexpCommaAlphaNum.ReplaceAllString(s, "")
if len(s1) == 0 {
return []string{}
}

s2 := strings.Split(s1, ",")
if len(s2) == 0 {
return []string{}
}

for _, s3 := range s2 {
if len(s3) > 0 {
stringSlice = append(stringSlice, s3)
}
}

return stringSlice
}

// StringToIntSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION
func StringToIntSlice(s string) []int {
if len(s) == 0 {
return []int{}
}

sl := StringToStringSlice(s)
if len(sl) == 0 {
return []int{}
}

return ToIntSlice(sl)
}

// ParseStringToInt REQUIRE THEM TO DOCUMENT THIS FUNCTION
func ParseStringToInt(s string) (int, error) {
if s == "" {
Expand Down
34 changes: 34 additions & 0 deletions lib/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ func TestToInt64Slice(t *testing.T) {
assert.Equal(t, int64(852369), actual[1])
}

func TestStringToStringSlice(t *testing.T) {
actual := StringToStringSlice("[foo,123,bar,,456,a1b2,,,]")

assert.Len(t, actual, 5)
assert.Equal(t, "foo", actual[0])
assert.Equal(t, "123", actual[1])
assert.Equal(t, "bar", actual[2])
assert.Equal(t, "456", actual[3])
assert.Equal(t, "a1b2", actual[4])

actual = StringToStringSlice("[[foo,123,[bar],,456,,a1b2]")

assert.Len(t, actual, 5)
assert.Equal(t, "foo", actual[0])
assert.Equal(t, "123", actual[1])
assert.Equal(t, "bar", actual[2])
assert.Equal(t, "456", actual[3])
assert.Equal(t, "a1b2", actual[4])
}

func TestStringToIntSlice(t *testing.T) {
actual := StringToIntSlice("[foo,123,bar,,456,a1b2,,,]")

assert.Len(t, actual, 2)
assert.Equal(t, 123, actual[0])
assert.Equal(t, 456, actual[1])

actual = StringToIntSlice("[[foo,123,[bar],,456,,a1b2]")

assert.Len(t, actual, 2)
assert.Equal(t, 123, actual[0])
assert.Equal(t, 456, actual[1])
}

func TestParseInt(t *testing.T) {
i, err := ParseStringToInt("6549")

Expand Down

0 comments on commit 35b675c

Please sign in to comment.