internal

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MatchUnknown Match = iota
	MatchBrowser
	MatchOS
	MatchType
	MatchVersion

	DeviceUnknown Device = iota
	DeviceDesktop
	DeviceMobile
	DeviceTablet
	DeviceTV
	DeviceBot
)
View Source
const (
	// There is no match token for this, but the absence of any browser token paired with Android is a good indicator of this browser.
	AndroidBrowser = "Android Browser"
	Chrome         = "Chrome"
	Edge           = "Edge"
	Firefox        = "Firefox"
	IE             = "IE"
	Opera          = "Opera"
	OperaMini      = "Mini"
	Safari         = "Safari"
	Vivaldi        = "Vivaldi"
	Samsung        = "Samsung Browser"
	Falkon         = "Falkon"
	Nintendo       = "Nintendo Browser"
	YandexBrowser  = "Yandex Browser"

	// Operating Systems.
	Android  = "Android"
	ChromeOS = "ChromeOS"
	IOS      = "iOS"
	Linux    = "Linux"
	OpenBSD  = "OpenBSD"
	MacOS    = "MacOS"
	Windows  = "Windows"

	// Types.
	Desktop = "Desktop"
	Mobile  = "Mobile"
	// 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.
	MobileDevice = "MobileDevice"
	Tablet       = "Tablet"
	TV           = "TV"
	Bot          = "Bot"

	// Version.
	Version = "Version"
	Unknown = "Unknown"
)

Browsers.

Variables

View Source
var MatchPrecedenceMap = map[string]uint8{

	Safari:         1,
	AndroidBrowser: 2,
	Chrome:         3,
	Firefox:        4,
	IE:             5,
	Opera:          6,
	OperaMini:      7,
	Edge:           8,
	Vivaldi:        9,
	Samsung:        10,
	Falkon:         11,
	Nintendo:       12,
	YandexBrowser:  13,

	Linux:    1,
	Android:  2,
	IOS:      3,
	OpenBSD:  4,
	ChromeOS: 5,
	MacOS:    6,
	Windows:  7,

	Desktop:      1,
	Mobile:       2,
	MobileDevice: 3,
	Tablet:       4,
	TV:           5,
	Bot:          6,
}

MatchPrecedenceMap is a map of user agent types to their importance in determining what is the actual browser/device/OS being used.

For example, Chrome user agents also contain the string "Safari" at the end of the user agent. This means that if we only check for "Safari" first, we will incorrectly determine the browser to be Safari instead of Chrome.

By setting a precedence, we can determine which match is more important and use that as the final result.

Functions

func IsDigit

func IsDigit(r rune) bool

IsDigit reports whether the rune is a decimal digit.

This is an optimised version of unicode.IsDigit without the check for r <= unicode.MaxLatin1.

func IsLetter

func IsLetter(r rune) bool

IsLetter reports whether the rune is a letter in ASCII.

This is an optimised version of unicode.IsLetter without the check for r <= unicode.MaxLatin1.

func RemoveAndroidIdentifiers

func RemoveAndroidIdentifiers(ua string) string

RemoveAndroidIdentifiers removes the device identifiers from the user agent string. This specifically removes any strings that follow the Android tokens.

func RemoveMobileIdentifiers

func RemoveMobileIdentifiers(ua string) string

RemoveMobileIdentifiers removes the device identifiers from the user agent string. This specifically removes any strings that follow the Mobile tokens. For example, "Mobile/14F89" should be "Mobile".

func RemoveVersions

func RemoveVersions(ua string) string

RemoveVersions removes the version numbers from the user agent string.

func ReplaceIndexes

func ReplaceIndexes(ua string, indexes []int) string

ReplaceIndexes replaces the runes at the given indexes with empty strings.

Types

type Device added in v1.0.3

type Device uint8

Device is an enum for the device type.

type Match added in v1.0.3

type Match uint8

Match is an enum for the match type.

func GetMatchType

func GetMatchType(match string) Match

GetMatchType returns the match type of a match result using the MatchPrecedenceMap.

type MatchResults

type MatchResults struct {
	Match     string
	EndIndex  int
	MatchType Match
	// Precedence value for each result type to determine which result should be overwritten.
	// Higher values overwrite lower values.
	Precedence uint8
}

MatchResults contains the information from MatchTokenIndexes.

func MatchTokenIndexes

func MatchTokenIndexes(ua string) []MatchResults

MatchTokenIndexes finds the start and end indexes of necessary tokens that match a known browser, device, or OS. This is used to determine when to insert a result value into the trie.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL