Skip to content

Commit

Permalink
Add StringTrimPrefix methods to a few types. (avct#46)
Browse files Browse the repository at this point in the history
This allows converting types to their string representation while
removing the type prefix.

For example BrowserUnknown will be "Unkown" rather than "BrowserUnknown"
  • Loading branch information
ryanslade authored Mar 8, 2019
1 parent 6ec1e55 commit 43c6f9a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions uasurfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const (
DeviceTV
)

// StringTrimPrefix is like String() but trims the "Device" prefix
func (d DeviceType) StringTrimPrefix() string {
return strings.TrimPrefix(d.String(), "Device")
}

// BrowserName (int) returns a constant.
type BrowserName int

Expand Down Expand Up @@ -66,6 +71,11 @@ const (
BrowserYahooBot // Bot list ends here
)

// StringTrimPrefix is like String() but trims the "Browser" prefix
func (b BrowserName) StringTrimPrefix() string {
return strings.TrimPrefix(b.String(), "Browser")
}

// OSName (int) returns a constant.
type OSName int

Expand All @@ -91,6 +101,11 @@ const (
OSBot
)

// StringTrimPrefix is like String() but trims the "OS" prefix
func (o OSName) StringTrimPrefix() string {
return strings.TrimPrefix(o.String(), "OS")
}

// Platform (int) returns a constant.
type Platform int

Expand All @@ -114,6 +129,11 @@ const (
PlatformBot
)

// StringTrimPrefix is like String() but trims the "Platform" prefix
func (p Platform) StringTrimPrefix() string {
return strings.TrimPrefix(p.String(), "Platform")
}

type Version struct {
Major int
Minor int
Expand Down
33 changes: 33 additions & 0 deletions uasurfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,36 @@ func BenchmarkParseSafariiPad(b *testing.B) {
Parse("Mozilla/5.0 (iPad; CPU OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4")
}
}

func TestStringTrimPrefix(t *testing.T) {
testCases := []struct {
f func() string
expected string
}{
{
f: DeviceUnknown.StringTrimPrefix,
expected: "Unknown",
},
{
f: BrowserUnknown.StringTrimPrefix,
expected: "Unknown",
},
{
f: OSUnknown.StringTrimPrefix,
expected: "Unknown",
},
{
f: PlatformUnknown.StringTrimPrefix,
expected: "Unknown",
},
}

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
s := tc.f()
if tc.expected != s {
t.Fatalf("Expected %q, got %q", tc.expected, s)
}
})
}
}

0 comments on commit 43c6f9a

Please sign in to comment.