or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

browser-detection.mdcompatibility-testing.mdengine-detection.mdindex.mdos-detection.mdparser-creation.mdplatform-detection.md
tile.json

parser-creation.mddocs/

Parser Creation

Core entry points for creating parser instances and getting parsed results directly from user agent strings.

Capabilities

Get Parser

Creates a Parser instance from a user agent string with optional lazy parsing.

/**
 * Creates a Parser instance
 * @param UA - User agent string
 * @param skipParsing - Will make the Parser postpone parsing until you ask it explicitly
 * @returns Parser instance
 * @throws Error when UA is not a String
 */
static getParser(UA: string, skipParsing?: boolean): Parser;

Usage Examples:

const Bowser = require("bowser");

// Immediate parsing (default)
const parser = Bowser.getParser(window.navigator.userAgent);
console.log(parser.getBrowserName()); // "Chrome"

// Lazy parsing - parse only when needed
const lazyParser = Bowser.getParser(window.navigator.userAgent, true);
console.log(lazyParser.getBrowserName()); // Triggers parsing then returns "Chrome"

// Error handling
try {
  const parser = Bowser.getParser(123); // TypeError
} catch (error) {
  console.error("UserAgent should be a string");
}

Parse

Creates a Parser instance and immediately returns the complete parsed result.

/**
 * Creates a Parser instance and runs Parser.getResult immediately
 * @param UA - User agent string
 * @returns Complete parsed result with browser, OS, platform, and engine information
 */
static parse(UA: string): ParsedResult;

Usage Examples:

const Bowser = require("bowser");

// Get complete parsed result
const result = Bowser.parse(window.navigator.userAgent);
console.log(result);
// {
//   browser: { name: "Chrome", version: "91.0.4472.124" },
//   os: { name: "macOS", version: "10.15.7", versionName: "Catalina" },
//   platform: { type: "desktop" },
//   engine: { name: "Blink", version: "91.0.4472.124" }
// }

// Access specific properties
console.log(result.browser.name); // "Chrome"
console.log(result.os.versionName); // "Catalina"
console.log(result.platform.type); // "desktop"

Constants Access

Static getters providing access to internal mapping constants for browsers, engines, OS, and platforms.

/**
 * Maps browser aliases to full names
 */
static get BROWSER_MAP(): Record<string, string>;

/**
 * Maps rendering engine identifiers to names
 */
static get ENGINE_MAP(): Record<string, string>;

/**
 * Maps OS identifiers to display names
 */
static get OS_MAP(): Record<string, string>;

/**
 * Maps platform type identifiers
 */
static get PLATFORMS_MAP(): Record<string, string>;

Usage Examples:

const Bowser = require("bowser");

// Access browser mappings
console.log(Bowser.BROWSER_MAP.chrome); // "Chrome"
console.log(Bowser.BROWSER_MAP.firefox); // "Firefox"

// Access OS mappings
console.log(Bowser.OS_MAP.MacOS); // "macOS"
console.log(Bowser.OS_MAP.Windows); // "Windows"

// Access engine mappings
console.log(Bowser.ENGINE_MAP.Blink); // "Blink"
console.log(Bowser.ENGINE_MAP.WebKit); // "WebKit"

// Access platform mappings
console.log(Bowser.PLATFORMS_MAP.desktop); // "desktop"
console.log(Bowser.PLATFORMS_MAP.mobile); // "mobile"

Core Utility Methods

Essential utility methods available on Parser instances for accessing raw data and testing patterns.

/**
 * Get UserAgent string of current Parser instance
 * @returns User-Agent String of the current Parser object
 */
getUA(): string;

/**
 * Test a UA string for a regexp
 * @param regex - Regular expression to test against user agent
 * @returns Boolean indicating if the regex matches the UA
 */
test(regex: RegExp): boolean;

/**
 * Get parsed result
 * @returns Complete parsed result with browser, OS, platform, and engine information
 */
getResult(): ParsedResult;

/**
 * Parse full information about the browser
 * @returns Parser instance (chainable)
 */
parse(): Parser;

Usage Examples:

const parser = Bowser.getParser(window.navigator.userAgent);

// Get original user agent string
console.log(parser.getUA()); // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..."

// Test user agent against regex
console.log(parser.test(/Chrome/i)); // true (if Chrome)
console.log(parser.test(/Firefox/i)); // false (if Chrome)

// Get complete parsed result
const result = parser.getResult();
console.log(result);
// {
//   browser: { name: "Chrome", version: "91.0.4472.124" },
//   os: { name: "macOS", version: "10.15.7", versionName: "Catalina" },
//   platform: { type: "desktop" },
//   engine: { name: "Blink", version: "91.0.4472.124" }
// }

// Force parsing (useful with lazy parsing)
const lazyParser = Bowser.getParser(window.navigator.userAgent, true);
lazyParser.parse(); // Force parsing of all components

Types

interface ParsedResult {
  browser: BrowserDetails;
  os: OSDetails;
  platform: PlatformDetails;
  engine: EngineDetails;
}

interface BrowserDetails {
  name?: string;
  version?: string;
}

interface OSDetails {
  name?: string;
  version?: string;
  versionName?: string;
}

interface PlatformDetails {
  type?: string;
  vendor?: string;
  model?: string;
}

interface EngineDetails {
  name?: string;
  version?: string;
}