Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"strconv"
"strings"
)

// ContainsInt64 tells whether a slice contains x.
Expand All @@ -24,6 +25,16 @@ func ContainsString(a []string, x string) bool {
return false
}

// ContainsPrefix tells whether a slice of string contains prefix of in.
func ContainsPrefix(prefixes []string, in string) bool {
for _, pref := range prefixes {
if strings.HasPrefix(in, pref) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case sensitive ga ini kink?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tambahin di unit testnya aja

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case sensitip yak

return true
}
}
return false
}

// SliceAtoi -> convert array of string to array of integer
func SliceAtoi(s []string) ([]int, error) {
var arr []int
Expand Down
40 changes: 40 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,43 @@ func Test_ReverseSlice(t *testing.T) {
assert.Equal(t, dest, ReverseSlice(source))
})
}

func Test_ContainsPrefix(t *testing.T) {
type tc struct {
prefixes []string
input string
result bool
}

testCases := []tc{
{
prefixes: []string{"pre", "start", "begin"},
input: "prefix",
result: true,
},
{
prefixes: []string{"pre", "start", "begin"},
input: "startle",
result: true,
},
{
prefixes: []string{"pre", "start", "begin"},
input: "beginning",
result: true,
},
{
prefixes: []string{"pre", "start", "begin"},
input: "hello",
result: false,
},
{
prefixes: []string{"worldle", "start", "begin"},
input: "worldstartbegin",
result: false,
},
}

for _, tc := range testCases {
assert.Equal(t, tc.result, ContainsPrefix(tc.prefixes, tc.input))
}
}
Loading