Skip to content

Commit cfaaf41

Browse files
authored
feat: expose constants in a new agents lib (#58)
1 parent 8fbd3a9 commit cfaaf41

File tree

9 files changed

+537
-326
lines changed

9 files changed

+537
-326
lines changed

agents/const.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package agents
2+
3+
type (
4+
// Browser represents a browser name.
5+
Browser string
6+
// OS represents an operating system name.
7+
OS string
8+
// Device represents a device type.
9+
Device string
10+
)
11+
12+
const (
13+
BrowserAndroid Browser = "Android Browser"
14+
BrowserChrome Browser = "Chrome"
15+
BrowserEdge Browser = "Edge"
16+
BrowserFirefox Browser = "Firefox"
17+
BrowserIE Browser = "IE"
18+
BrowserOpera Browser = "pera"
19+
BrowserOperaMini Browser = "Mini"
20+
BrowserSafari Browser = "Safari"
21+
BrowserVivaldi Browser = "Vivaldi"
22+
BrowserSamsung Browser = "Samsung Browser"
23+
BrowserFalkon Browser = "Falkon"
24+
BrowserNintendo Browser = "Nintendo Browser"
25+
BrowserYandex Browser = "Yandex Browser"
26+
27+
OSAndroid OS = "Android"
28+
OSChromeOS OS = "ChromeOS"
29+
OSIOS OS = "iOS"
30+
OSLinux OS = "Linux"
31+
OSOpenBSD OS = "OpenBSD"
32+
OSMacOS OS = "MacOS"
33+
OSWindows OS = "Windows"
34+
35+
DeviceDesktop Device = "Desktop"
36+
DeviceMobile Device = "Mobile"
37+
DeviceTablet Device = "Tablet"
38+
DeviceTV Device = "TV"
39+
DeviceBot Device = "Bot"
40+
)
41+
42+
func (b Browser) String() string {
43+
return string(b)
44+
}
45+
46+
func (o OS) String() string {
47+
return string(o)
48+
}
49+
50+
func (d Device) String() string {
51+
return string(d)
52+
}

helpers.go

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,72 @@ package useragent
33
import (
44
"strings"
55

6+
"github.com/medama-io/go-useragent/agents"
67
"github.com/medama-io/go-useragent/internal"
78
)
89

9-
// GetDevice returns the device type as a string.
10-
func (ua UserAgent) GetDevice() string {
11-
switch ua.device {
12-
case internal.DeviceDesktop:
13-
return internal.Desktop
14-
case internal.DeviceMobile:
15-
return internal.Mobile
16-
case internal.DeviceTablet:
17-
return internal.Tablet
18-
case internal.DeviceTV:
19-
return internal.TV
20-
case internal.DeviceBot:
21-
return internal.Bot
22-
default:
23-
return internal.Unknown
10+
// Browser returns the browser name. If no browser is found, it returns an empty string.
11+
func (ua UserAgent) Browser() agents.Browser {
12+
return ua.browser.GetMatchBrowser()
13+
}
14+
15+
// OS returns the operating system name. If no OS is found, it returns an empty string.
16+
func (ua UserAgent) OS() agents.OS {
17+
return ua.os.GetMatchOS()
18+
}
19+
20+
// Device returns the device type as a string.
21+
func (ua UserAgent) Device() agents.Device {
22+
return ua.device.GetMatchDevice()
23+
}
24+
25+
// BrowserVersion returns the browser version. If no version is found, it returns an empty string.
26+
func (ua UserAgent) BrowserVersion() string {
27+
return string(ua.version[:ua.versionIndex])
28+
}
29+
30+
// BrowserVersionMajor returns the major version of the browser. If no version is found, it returns an empty string.
31+
func (ua UserAgent) BrowserVersionMajor() string {
32+
if ua.versionIndex == 0 {
33+
return ""
2434
}
35+
36+
version := ua.BrowserVersion()
37+
38+
return strings.Split(version, ".")[0]
39+
}
40+
41+
// BrowserVersionMinor returns the minor version of the browser. If no version is found, it returns an empty string.
42+
func (ua UserAgent) BrowserVersionMinor() string {
43+
if ua.versionIndex == 0 {
44+
return ""
45+
}
46+
47+
version := ua.BrowserVersion()
48+
49+
parts := strings.Split(version, ".")
50+
if len(parts) < 2 {
51+
return ""
52+
}
53+
54+
return parts[1]
55+
}
56+
57+
// BrowserVersionPatch returns the patch version of the browser. If no version is found, it returns an empty string.
58+
func (ua UserAgent) BrowserVersionPatch() string {
59+
if ua.versionIndex == 0 {
60+
return ""
61+
}
62+
63+
version := ua.BrowserVersion()
64+
65+
parts := strings.Split(version, ".")
66+
if len(parts) < 3 {
67+
return ""
68+
}
69+
70+
// Sometimes the patch version has a suffix, e.g. "1.2.3b".
71+
return strings.Join(parts[2:], ".")
2572
}
2673

2774
// IsDesktop returns true if the user agent is a desktop browser.
@@ -50,27 +97,36 @@ func (ua UserAgent) IsBot() bool {
5097
}
5198

5299
// GetBrowser returns the browser name. If no browser is found, it returns an empty string.
100+
//
101+
// Deprecated: Use .Browser() instead.
53102
func (ua UserAgent) GetBrowser() string {
54-
return ua.browser
103+
return string(ua.browser.GetMatchBrowser())
55104
}
56105

57106
// GetOS returns the operating system name. If no OS is found, it returns an empty string.
107+
//
108+
// Deprecated: Use .OS() instead.
58109
func (ua UserAgent) GetOS() string {
59-
return ua.os
110+
return string(ua.os.GetMatchOS())
111+
}
112+
113+
// GetDevice returns the device type as a string.
114+
//
115+
// Deprecated: Use .Device() instead.
116+
func (ua UserAgent) GetDevice() string {
117+
return string(ua.device.GetMatchDevice())
60118
}
61119

62120
// GetVersion returns the browser version. If no version is found, it returns an empty string.
121+
//
122+
// Deprecated: Use .BrowserVersion() instead.
63123
func (ua UserAgent) GetVersion() string {
64-
return string(ua.version[:ua.versionIndex])
124+
return ua.BrowserVersion()
65125
}
66126

67127
// GetMajorVersion returns the major version of the browser. If no version is found, it returns an empty string.
128+
//
129+
// Deprecated: Use .BrowserVersionMajor() instead.
68130
func (ua UserAgent) GetMajorVersion() string {
69-
if ua.versionIndex == 0 {
70-
return ""
71-
}
72-
73-
version := string(ua.version[:ua.versionIndex])
74-
75-
return strings.Split(version, ".")[0]
131+
return ua.BrowserVersionMajor()
76132
}

internal/const.go

Lines changed: 181 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,195 @@
11
package internal
22

3+
import "github.com/medama-io/go-useragent/agents"
4+
35
type (
4-
// Match is an enum for the match type.
6+
// Match is an enum for the browser, os or device name.
57
Match uint8
6-
// Device is an enum for the device type.
7-
Device uint8
8+
// MatchType is an enum for the match type.
9+
MatchType uint8
810
)
911

1012
const (
11-
MatchUnknown Match = iota
12-
MatchBrowser
13-
MatchOS
14-
MatchType
15-
MatchVersion
13+
Unknown Match = iota
14+
15+
// There is no match token for Android Browser, but the absence of any browser token paired with Android is a good indicator of this browser.
16+
BrowserAndroid
17+
BrowserChrome
18+
BrowserEdge
19+
BrowserFirefox
20+
BrowserIE
21+
BrowserOpera
22+
BrowserOperaMini
23+
BrowserSafari
24+
BrowserVivaldi
25+
BrowserSamsung
26+
BrowserFalkon
27+
BrowserNintendo
28+
BrowserYandex
29+
30+
OSAndroid
31+
OSChromeOS
32+
OSIOS
33+
OSLinux
34+
OSOpenBSD
35+
OSMacOS
36+
OSWindows
1637

17-
DeviceUnknown Device = iota
1838
DeviceDesktop
1939
DeviceMobile
2040
DeviceTablet
2141
DeviceTV
2242
DeviceBot
43+
44+
TokenVersion
45+
// We need a separate type for mobile devices since some user agents use "Mobile/"
46+
// appended with a device ID. We need to handle these separately to strip those IDs
47+
// out.
48+
TokenMobileDevice
49+
50+
MatchUnknown MatchType = iota
51+
MatchBrowser
52+
MatchOS
53+
MatchDevice
54+
MatchVersion
2355
)
56+
57+
// GetMatchType returns the match type of a match result using the MatchPrecedenceMap.
58+
func (m Match) GetMatchType() MatchType {
59+
switch m {
60+
case BrowserAndroid,
61+
BrowserChrome,
62+
BrowserEdge,
63+
BrowserFirefox,
64+
BrowserIE,
65+
BrowserOpera,
66+
BrowserOperaMini,
67+
BrowserSafari,
68+
BrowserVivaldi,
69+
BrowserSamsung,
70+
BrowserFalkon,
71+
BrowserNintendo,
72+
BrowserYandex:
73+
return MatchBrowser
74+
75+
case OSAndroid,
76+
OSChromeOS,
77+
OSIOS,
78+
OSLinux,
79+
OSOpenBSD,
80+
OSMacOS,
81+
OSWindows:
82+
return MatchOS
83+
84+
case DeviceDesktop,
85+
DeviceMobile,
86+
DeviceTablet,
87+
DeviceTV,
88+
DeviceBot,
89+
TokenMobileDevice:
90+
return MatchDevice
91+
92+
case TokenVersion:
93+
return MatchVersion
94+
}
95+
96+
return MatchUnknown
97+
}
98+
99+
// GetMatchBrowser returns the browser name of a match.
100+
func (m Match) GetMatchBrowser() agents.Browser {
101+
switch m {
102+
case BrowserAndroid:
103+
return agents.BrowserAndroid
104+
case BrowserChrome:
105+
return agents.BrowserChrome
106+
case BrowserEdge:
107+
return agents.BrowserEdge
108+
case BrowserFirefox:
109+
return agents.BrowserFirefox
110+
case BrowserIE:
111+
return agents.BrowserIE
112+
case BrowserOpera:
113+
return agents.BrowserOpera
114+
case BrowserOperaMini:
115+
return agents.BrowserOperaMini
116+
case BrowserSafari:
117+
return agents.BrowserSafari
118+
case BrowserVivaldi:
119+
return agents.BrowserVivaldi
120+
case BrowserSamsung:
121+
return agents.BrowserSamsung
122+
case BrowserFalkon:
123+
return agents.BrowserFalkon
124+
case BrowserNintendo:
125+
return agents.BrowserNintendo
126+
case BrowserYandex:
127+
return agents.BrowserYandex
128+
}
129+
130+
return ""
131+
}
132+
133+
// GetMatchOS returns the OS name of a match.
134+
func (m Match) GetMatchOS() agents.OS {
135+
switch m {
136+
case OSAndroid:
137+
return agents.OSAndroid
138+
case OSChromeOS:
139+
return agents.OSChromeOS
140+
case OSIOS:
141+
return agents.OSIOS
142+
case OSLinux:
143+
return agents.OSLinux
144+
case OSOpenBSD:
145+
return agents.OSOpenBSD
146+
case OSMacOS:
147+
return agents.OSMacOS
148+
case OSWindows:
149+
return agents.OSWindows
150+
}
151+
152+
return ""
153+
}
154+
155+
// GetMatchDevice returns the device name of a match.
156+
func (m Match) GetMatchDevice() agents.Device {
157+
switch m {
158+
case DeviceDesktop:
159+
return agents.DeviceDesktop
160+
case DeviceMobile:
161+
return agents.DeviceMobile
162+
case DeviceTablet:
163+
return agents.DeviceTablet
164+
case DeviceTV:
165+
return agents.DeviceTV
166+
case DeviceBot:
167+
return agents.DeviceBot
168+
}
169+
170+
return ""
171+
}
172+
173+
// GetMatchName returns the name of a match. This is used for debugging in tests.
174+
func (m Match) GetMatchName() string {
175+
if browser := m.GetMatchBrowser(); browser != "" {
176+
return browser.String()
177+
}
178+
179+
if os := m.GetMatchOS(); os != "" {
180+
return os.String()
181+
}
182+
183+
if device := m.GetMatchDevice(); device != "" {
184+
return device.String()
185+
}
186+
187+
switch m {
188+
case TokenVersion:
189+
return "Version"
190+
case TokenMobileDevice:
191+
return "MobileDevice"
192+
}
193+
194+
return ""
195+
}

0 commit comments

Comments
 (0)