Skip to content

Commit

Permalink
feat: expose constants in a new agents lib (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito authored Jan 19, 2025
1 parent 8fbd3a9 commit cfaaf41
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 326 deletions.
52 changes: 52 additions & 0 deletions agents/const.go
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)
}
106 changes: 81 additions & 25 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,72 @@ package useragent
import (
"strings"

"github.com/medama-io/go-useragent/agents"
"github.com/medama-io/go-useragent/internal"
)

// GetDevice returns the device type as a string.
func (ua UserAgent) GetDevice() string {
switch ua.device {
case internal.DeviceDesktop:
return internal.Desktop
case internal.DeviceMobile:
return internal.Mobile
case internal.DeviceTablet:
return internal.Tablet
case internal.DeviceTV:
return internal.TV
case internal.DeviceBot:
return internal.Bot
default:
return internal.Unknown
// 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.
Expand Down Expand Up @@ -50,27 +97,36 @@ func (ua UserAgent) IsBot() bool {
}

// 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 ua.browser
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 ua.os
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 string(ua.version[:ua.versionIndex])
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 {
if ua.versionIndex == 0 {
return ""
}

version := string(ua.version[:ua.versionIndex])

return strings.Split(version, ".")[0]
return ua.BrowserVersionMajor()
}
190 changes: 181 additions & 9 deletions internal/const.go
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 ""
}
Loading

0 comments on commit cfaaf41

Please sign in to comment.