-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
132 lines (105 loc) · 3.53 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package useragent
import (
"strings"
"github.com/medama-io/go-useragent/agents"
"github.com/medama-io/go-useragent/internal"
)
// Browser returns the browser name. If no browser is found, it returns an empty string.
func (ua UserAgent) Browser() agents.Browser {
return ua.browser.GetMatchBrowser()
}
// OS returns the operating system name. If no OS is found, it returns an empty string.
func (ua UserAgent) OS() agents.OS {
return ua.os.GetMatchOS()
}
// Device returns the device type as a string.
func (ua UserAgent) Device() agents.Device {
return ua.device.GetMatchDevice()
}
// BrowserVersion returns the browser version. If no version is found, it returns an empty string.
func (ua UserAgent) BrowserVersion() string {
return string(ua.version[:ua.versionIndex])
}
// BrowserVersionMajor returns the major version of the browser. If no version is found, it returns an empty string.
func (ua UserAgent) BrowserVersionMajor() string {
if ua.versionIndex == 0 {
return ""
}
version := ua.BrowserVersion()
return strings.Split(version, ".")[0]
}
// BrowserVersionMinor returns the minor version of the browser. If no version is found, it returns an empty string.
func (ua UserAgent) BrowserVersionMinor() string {
if ua.versionIndex == 0 {
return ""
}
version := ua.BrowserVersion()
parts := strings.Split(version, ".")
if len(parts) < 2 {
return ""
}
return parts[1]
}
// BrowserVersionPatch returns the patch version of the browser. If no version is found, it returns an empty string.
func (ua UserAgent) BrowserVersionPatch() string {
if ua.versionIndex == 0 {
return ""
}
version := ua.BrowserVersion()
parts := strings.Split(version, ".")
if len(parts) < 3 {
return ""
}
// Sometimes the patch version has a suffix, e.g. "1.2.3b".
return strings.Join(parts[2:], ".")
}
// IsDesktop returns true if the user agent is a desktop browser.
func (ua UserAgent) IsDesktop() bool {
return ua.device == internal.DeviceDesktop
}
// IsMobile returns true if the user agent is a mobile browser.
func (ua UserAgent) IsMobile() bool {
return ua.device == internal.DeviceMobile
}
// IsTablet returns true if the user agent is a tablet browser.
func (ua UserAgent) IsTablet() bool {
return ua.device == internal.DeviceTablet
}
// IsTV returns true if the user agent is a TV browser.
func (ua UserAgent) IsTV() bool {
return ua.device == internal.DeviceTV
}
// IsBot returns true if the user agent is a bot.
func (ua UserAgent) IsBot() bool {
return ua.device == internal.DeviceBot
}
// GetBrowser returns the browser name. If no browser is found, it returns an empty string.
//
// Deprecated: Use .Browser() instead.
func (ua UserAgent) GetBrowser() string {
return string(ua.browser.GetMatchBrowser())
}
// GetOS returns the operating system name. If no OS is found, it returns an empty string.
//
// Deprecated: Use .OS() instead.
func (ua UserAgent) GetOS() string {
return string(ua.os.GetMatchOS())
}
// GetDevice returns the device type as a string.
//
// Deprecated: Use .Device() instead.
func (ua UserAgent) GetDevice() string {
return string(ua.device.GetMatchDevice())
}
// GetVersion returns the browser version. If no version is found, it returns an empty string.
//
// Deprecated: Use .BrowserVersion() instead.
func (ua UserAgent) GetVersion() string {
return ua.BrowserVersion()
}
// GetMajorVersion returns the major version of the browser. If no version is found, it returns an empty string.
//
// Deprecated: Use .BrowserVersionMajor() instead.
func (ua UserAgent) GetMajorVersion() string {
return ua.BrowserVersionMajor()
}