Skip to content

Commit

Permalink
name: Adding split on dot (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
adisuissa authored Mar 30, 2023
1 parent c5d8db1 commit 496ad1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions name.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func (n Name) Split() (parts []string) {
}
}

// SplitOnDot breaks apart Name n into its constituent components using dot notation.
func (n Name) SplitOnDot() (parts []string) {
return strings.Split(string(n), ".")
}

// NameTransformer is a function that mutates a string. Many of the methods in
// the standard strings package satisfy this signature.
type NameTransformer func(string) string
Expand Down
27 changes: 27 additions & 0 deletions name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ func TestName_Split(t *testing.T) {
}
}

func TestName_SplitOnDot(t *testing.T) {
t.Parallel()

tests := []struct {
in string
parts []string
}{
{"foo", []string{"foo"}},
{"foo_bar", []string{"foo_bar"}},
{"foo1", []string{"foo1"}},
{"foo.bar", []string{"foo", "bar"}},
{".foo.bar", []string{"", "foo", "bar"}},
{".JSONString.Foo.Bar", []string{"", "JSONString", "Foo", "Bar"}},

// empty
{"", []string{""}},
}

for _, test := range tests {
tc := test
t.Run(tc.in, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.parts, Name(tc.in).SplitOnDot())
})
}
}

func TestName(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 496ad1a

Please sign in to comment.