-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose constants in a new agents lib (#58)
- Loading branch information
Showing
9 changed files
with
537 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package agents | ||
|
||
type ( | ||
// Browser represents a browser name. | ||
Browser string | ||
// OS represents an operating system name. | ||
OS string | ||
// Device represents a device type. | ||
Device string | ||
) | ||
|
||
const ( | ||
BrowserAndroid Browser = "Android Browser" | ||
BrowserChrome Browser = "Chrome" | ||
BrowserEdge Browser = "Edge" | ||
BrowserFirefox Browser = "Firefox" | ||
BrowserIE Browser = "IE" | ||
BrowserOpera Browser = "pera" | ||
BrowserOperaMini Browser = "Mini" | ||
BrowserSafari Browser = "Safari" | ||
BrowserVivaldi Browser = "Vivaldi" | ||
BrowserSamsung Browser = "Samsung Browser" | ||
BrowserFalkon Browser = "Falkon" | ||
BrowserNintendo Browser = "Nintendo Browser" | ||
BrowserYandex Browser = "Yandex Browser" | ||
|
||
OSAndroid OS = "Android" | ||
OSChromeOS OS = "ChromeOS" | ||
OSIOS OS = "iOS" | ||
OSLinux OS = "Linux" | ||
OSOpenBSD OS = "OpenBSD" | ||
OSMacOS OS = "MacOS" | ||
OSWindows OS = "Windows" | ||
|
||
DeviceDesktop Device = "Desktop" | ||
DeviceMobile Device = "Mobile" | ||
DeviceTablet Device = "Tablet" | ||
DeviceTV Device = "TV" | ||
DeviceBot Device = "Bot" | ||
) | ||
|
||
func (b Browser) String() string { | ||
return string(b) | ||
} | ||
|
||
func (o OS) String() string { | ||
return string(o) | ||
} | ||
|
||
func (d Device) String() string { | ||
return string(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,195 @@ | ||
package internal | ||
|
||
import "github.com/medama-io/go-useragent/agents" | ||
|
||
type ( | ||
// Match is an enum for the match type. | ||
// Match is an enum for the browser, os or device name. | ||
Match uint8 | ||
// Device is an enum for the device type. | ||
Device uint8 | ||
// MatchType is an enum for the match type. | ||
MatchType uint8 | ||
) | ||
|
||
const ( | ||
MatchUnknown Match = iota | ||
MatchBrowser | ||
MatchOS | ||
MatchType | ||
MatchVersion | ||
Unknown Match = iota | ||
|
||
// 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. | ||
BrowserAndroid | ||
BrowserChrome | ||
BrowserEdge | ||
BrowserFirefox | ||
BrowserIE | ||
BrowserOpera | ||
BrowserOperaMini | ||
BrowserSafari | ||
BrowserVivaldi | ||
BrowserSamsung | ||
BrowserFalkon | ||
BrowserNintendo | ||
BrowserYandex | ||
|
||
OSAndroid | ||
OSChromeOS | ||
OSIOS | ||
OSLinux | ||
OSOpenBSD | ||
OSMacOS | ||
OSWindows | ||
|
||
DeviceUnknown Device = iota | ||
DeviceDesktop | ||
DeviceMobile | ||
DeviceTablet | ||
DeviceTV | ||
DeviceBot | ||
|
||
TokenVersion | ||
// We need a separate type for mobile devices since some user agents use "Mobile/" | ||
// appended with a device ID. We need to handle these separately to strip those IDs | ||
// out. | ||
TokenMobileDevice | ||
|
||
MatchUnknown MatchType = iota | ||
MatchBrowser | ||
MatchOS | ||
MatchDevice | ||
MatchVersion | ||
) | ||
|
||
// GetMatchType returns the match type of a match result using the MatchPrecedenceMap. | ||
func (m Match) GetMatchType() MatchType { | ||
switch m { | ||
case BrowserAndroid, | ||
BrowserChrome, | ||
BrowserEdge, | ||
BrowserFirefox, | ||
BrowserIE, | ||
BrowserOpera, | ||
BrowserOperaMini, | ||
BrowserSafari, | ||
BrowserVivaldi, | ||
BrowserSamsung, | ||
BrowserFalkon, | ||
BrowserNintendo, | ||
BrowserYandex: | ||
return MatchBrowser | ||
|
||
case OSAndroid, | ||
OSChromeOS, | ||
OSIOS, | ||
OSLinux, | ||
OSOpenBSD, | ||
OSMacOS, | ||
OSWindows: | ||
return MatchOS | ||
|
||
case DeviceDesktop, | ||
DeviceMobile, | ||
DeviceTablet, | ||
DeviceTV, | ||
DeviceBot, | ||
TokenMobileDevice: | ||
return MatchDevice | ||
|
||
case TokenVersion: | ||
return MatchVersion | ||
} | ||
|
||
return MatchUnknown | ||
} | ||
|
||
// GetMatchBrowser returns the browser name of a match. | ||
func (m Match) GetMatchBrowser() agents.Browser { | ||
switch m { | ||
case BrowserAndroid: | ||
return agents.BrowserAndroid | ||
case BrowserChrome: | ||
return agents.BrowserChrome | ||
case BrowserEdge: | ||
return agents.BrowserEdge | ||
case BrowserFirefox: | ||
return agents.BrowserFirefox | ||
case BrowserIE: | ||
return agents.BrowserIE | ||
case BrowserOpera: | ||
return agents.BrowserOpera | ||
case BrowserOperaMini: | ||
return agents.BrowserOperaMini | ||
case BrowserSafari: | ||
return agents.BrowserSafari | ||
case BrowserVivaldi: | ||
return agents.BrowserVivaldi | ||
case BrowserSamsung: | ||
return agents.BrowserSamsung | ||
case BrowserFalkon: | ||
return agents.BrowserFalkon | ||
case BrowserNintendo: | ||
return agents.BrowserNintendo | ||
case BrowserYandex: | ||
return agents.BrowserYandex | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// GetMatchOS returns the OS name of a match. | ||
func (m Match) GetMatchOS() agents.OS { | ||
switch m { | ||
case OSAndroid: | ||
return agents.OSAndroid | ||
case OSChromeOS: | ||
return agents.OSChromeOS | ||
case OSIOS: | ||
return agents.OSIOS | ||
case OSLinux: | ||
return agents.OSLinux | ||
case OSOpenBSD: | ||
return agents.OSOpenBSD | ||
case OSMacOS: | ||
return agents.OSMacOS | ||
case OSWindows: | ||
return agents.OSWindows | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// GetMatchDevice returns the device name of a match. | ||
func (m Match) GetMatchDevice() agents.Device { | ||
switch m { | ||
case DeviceDesktop: | ||
return agents.DeviceDesktop | ||
case DeviceMobile: | ||
return agents.DeviceMobile | ||
case DeviceTablet: | ||
return agents.DeviceTablet | ||
case DeviceTV: | ||
return agents.DeviceTV | ||
case DeviceBot: | ||
return agents.DeviceBot | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// GetMatchName returns the name of a match. This is used for debugging in tests. | ||
func (m Match) GetMatchName() string { | ||
if browser := m.GetMatchBrowser(); browser != "" { | ||
return browser.String() | ||
} | ||
|
||
if os := m.GetMatchOS(); os != "" { | ||
return os.String() | ||
} | ||
|
||
if device := m.GetMatchDevice(); device != "" { | ||
return device.String() | ||
} | ||
|
||
switch m { | ||
case TokenVersion: | ||
return "Version" | ||
case TokenMobileDevice: | ||
return "MobileDevice" | ||
} | ||
|
||
return "" | ||
} |
Oops, something went wrong.