uaparser

A User Agent parser for Gleam, generated from the ua-parser/uap-core regular expressions. Works on both Erlang and JavaScript targets.

let ua = uaparser.parse_user_agent(
  "Mozilla/5.0 ... Chrome/120.0.0.0 Safari/537.36",
)
ua.family  // "Chrome"
ua.version // Some(Version(major: "120", minor: Some("0"), patch: Some("0")))

Types

The result of parsing a user agent string.

The family field contains the browser name (e.g., "Chrome", "Firefox", "Safari"). If no pattern matches, the family is "Other".

The version field is Some(Version(...)) when version information was extracted, or None when no version could be determined.

pub type UserAgent {
  UserAgent(family: String, version: option.Option(Version))
}

Constructors

A parsed version with major, minor, and patch components.

pub type Version {
  Version(
    major: String,
    minor: option.Option(String),
    patch: option.Option(String),
  )
}

Constructors

Values

pub fn parse_user_agent(ua_string: String) -> UserAgent

Parse a user agent string into a UserAgent result.

Regular expressions are compiled once on first call and cached for subsequent calls. Predictive dispatching reduces the number of patterns tested based on the content of the user agent string.

let ua = uaparser.parse_user_agent(
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
)
ua.family  // "Chrome"
Search Document