Skip to content

Commit 496ad1a

Browse files
authored
name: Adding split on dot (#119)
1 parent c5d8db1 commit 496ad1a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: name.go

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ func (n Name) Split() (parts []string) {
108108
}
109109
}
110110

111+
// SplitOnDot breaks apart Name n into its constituent components using dot notation.
112+
func (n Name) SplitOnDot() (parts []string) {
113+
return strings.Split(string(n), ".")
114+
}
115+
111116
// NameTransformer is a function that mutates a string. Many of the methods in
112117
// the standard strings package satisfy this signature.
113118
type NameTransformer func(string) string

Diff for: name_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ func TestName_Split(t *testing.T) {
6262
}
6363
}
6464

65+
func TestName_SplitOnDot(t *testing.T) {
66+
t.Parallel()
67+
68+
tests := []struct {
69+
in string
70+
parts []string
71+
}{
72+
{"foo", []string{"foo"}},
73+
{"foo_bar", []string{"foo_bar"}},
74+
{"foo1", []string{"foo1"}},
75+
{"foo.bar", []string{"foo", "bar"}},
76+
{".foo.bar", []string{"", "foo", "bar"}},
77+
{".JSONString.Foo.Bar", []string{"", "JSONString", "Foo", "Bar"}},
78+
79+
// empty
80+
{"", []string{""}},
81+
}
82+
83+
for _, test := range tests {
84+
tc := test
85+
t.Run(tc.in, func(t *testing.T) {
86+
t.Parallel()
87+
assert.Equal(t, tc.parts, Name(tc.in).SplitOnDot())
88+
})
89+
}
90+
}
91+
6592
func TestName(t *testing.T) {
6693
t.Parallel()
6794

0 commit comments

Comments
 (0)